OrbRestricted.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /* RestrictedORB.java --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.CORBA;
  32. import gnu.CORBA.CDR.BufferedCdrOutput;
  33. import gnu.CORBA.typecodes.AliasTypeCode;
  34. import gnu.CORBA.typecodes.ArrayTypeCode;
  35. import gnu.CORBA.typecodes.PrimitiveTypeCode;
  36. import gnu.CORBA.typecodes.RecordTypeCode;
  37. import gnu.CORBA.typecodes.StringTypeCode;
  38. import org.omg.CORBA.Any;
  39. import org.omg.CORBA.BAD_PARAM;
  40. import org.omg.CORBA.Context;
  41. import org.omg.CORBA.ContextList;
  42. import org.omg.CORBA.Environment;
  43. import org.omg.CORBA.ExceptionList;
  44. import org.omg.CORBA.NO_IMPLEMENT;
  45. import org.omg.CORBA.NVList;
  46. import org.omg.CORBA.NamedValue;
  47. import org.omg.CORBA.ORB;
  48. import org.omg.CORBA.ORBPackage.InvalidName;
  49. import org.omg.CORBA.Request;
  50. import org.omg.CORBA.StructMember;
  51. import org.omg.CORBA.TCKind;
  52. import org.omg.CORBA.TypeCode;
  53. import org.omg.CORBA.TypeCodePackage.BadKind;
  54. import org.omg.CORBA.UnionMember;
  55. import org.omg.CORBA.portable.OutputStream;
  56. import org.omg.CORBA.portable.ValueFactory;
  57. import org.omg.PortableInterceptor.ClientRequestInterceptorOperations;
  58. import org.omg.PortableInterceptor.IORInterceptor_3_0Operations;
  59. import org.omg.PortableInterceptor.ServerRequestInterceptorOperations;
  60. import java.applet.Applet;
  61. import java.util.Hashtable;
  62. import java.util.Properties;
  63. /**
  64. * This class implements so-called Singleton ORB, a highly restricted version
  65. * that cannot communicate over network. This ORB is provided for the
  66. * potentially malicious applets with heavy security restrictions. It, however,
  67. * supports some basic features that might be needed even when the network
  68. * access is not granted.
  69. *
  70. * This ORB can only create typecodes, {@link Any}, {@link ContextList},
  71. * {@link NVList} and {@link org.omg.CORBA.portable.OutputStream} that writes to
  72. * an internal buffer.
  73. *
  74. * All other methods throw the {@link NO_IMPLEMENT} exception.
  75. *
  76. * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  77. */
  78. public class OrbRestricted extends org.omg.CORBA_2_3.ORB
  79. {
  80. /**
  81. * The singleton instance of this ORB.
  82. */
  83. public static final ORB Singleton = new OrbRestricted();
  84. /**
  85. * The cumulated listener for all IOR interceptors. Interceptors are used by
  86. * {@link gnu.CORBA.Poa.ORB_1_4}.
  87. */
  88. public IORInterceptor_3_0Operations iIor;
  89. /**
  90. * The cumulated listener for all server request interceptors. Interceptors
  91. * are used by {@link gnu.CORBA.Poa.ORB_1_4}.
  92. */
  93. public ServerRequestInterceptorOperations iServer;
  94. /**
  95. * The cumulated listener for all client request interceptros. Interceptors
  96. * are used by {@link gnu.CORBA.Poa.ORB_1_4}.
  97. */
  98. public ClientRequestInterceptorOperations iClient;
  99. /**
  100. * The required size of the interceptor slot array.
  101. */
  102. public int icSlotSize = 0;
  103. /**
  104. * The value factories.
  105. */
  106. protected Hashtable factories = new Hashtable();
  107. /**
  108. * The policy factories.
  109. */
  110. protected Hashtable policyFactories = new Hashtable();
  111. /**
  112. * Create a new instance of the RestrictedORB. This is used in derived classes
  113. * only.
  114. */
  115. protected OrbRestricted()
  116. {
  117. }
  118. /** {@inheritDoc} */
  119. public TypeCode create_alias_tc(String id, String name, TypeCode typecode)
  120. {
  121. return new AliasTypeCode(typecode, id, name);
  122. }
  123. /** {@inheritDoc} */
  124. public Any create_any()
  125. {
  126. gnuAny any = new gnuAny();
  127. any.setOrb(this);
  128. return any;
  129. }
  130. /** {@inheritDoc} */
  131. public TypeCode create_array_tc(int length, TypeCode element_type)
  132. {
  133. ArrayTypeCode p =
  134. new ArrayTypeCode(TCKind.tk_array, element_type);
  135. p.setLength(length);
  136. return p;
  137. }
  138. /** {@inheritDoc} */
  139. public ContextList create_context_list()
  140. {
  141. return new gnuContextList();
  142. }
  143. /** {@inheritDoc} */
  144. public TypeCode create_enum_tc(String id, String name, String[] values)
  145. {
  146. RecordTypeCode r = new RecordTypeCode(TCKind.tk_enum);
  147. for (int i = 0; i < values.length; i++)
  148. {
  149. r.field().name = values [ i ];
  150. }
  151. r.setId(id);
  152. r.setName(name);
  153. return r;
  154. }
  155. /** {@inheritDoc} */
  156. public Environment create_environment()
  157. {
  158. return new gnuEnvironment();
  159. }
  160. /** {@inheritDoc} */
  161. public ExceptionList create_exception_list()
  162. {
  163. return new gnuExceptionList();
  164. }
  165. /** {@inheritDoc} */
  166. public TypeCode create_exception_tc(String id, String name,
  167. StructMember[] members
  168. )
  169. {
  170. RecordTypeCode r = new RecordTypeCode(TCKind.tk_except);
  171. r.setId(id);
  172. r.setName(name);
  173. for (int i = 0; i < members.length; i++)
  174. {
  175. r.add(members [ i ]);
  176. }
  177. return r;
  178. }
  179. /**
  180. * This method is not allowed for a RestrictedORB.
  181. *
  182. * @throws NO_IMPLEMENT, always.
  183. */
  184. public TypeCode create_interface_tc(String id, String name)
  185. {
  186. no();
  187. return null;
  188. }
  189. /** {@inheritDoc} */
  190. public NVList create_list(int count)
  191. {
  192. return new gnuNVList(count);
  193. }
  194. /** {@inheritDoc} */
  195. public NamedValue create_named_value(String s, Any any, int flags)
  196. {
  197. return new gnuNamedValue();
  198. }
  199. /** {@inheritDoc} */
  200. public OutputStream create_output_stream()
  201. {
  202. BufferedCdrOutput stream = new BufferedCdrOutput();
  203. stream.setOrb(this);
  204. return stream;
  205. }
  206. /** {@inheritDoc} */
  207. public TypeCode create_sequence_tc(int bound, TypeCode element_type)
  208. {
  209. ArrayTypeCode p =
  210. new ArrayTypeCode(TCKind.tk_sequence, element_type);
  211. p.setLength(bound);
  212. return p;
  213. }
  214. /** {@inheritDoc} */
  215. public TypeCode create_string_tc(int bound)
  216. {
  217. StringTypeCode p = new StringTypeCode(TCKind.tk_string);
  218. p.setLength(bound);
  219. return p;
  220. }
  221. /** {@inheritDoc} */
  222. public TypeCode create_struct_tc(String id, String name,
  223. StructMember[] members
  224. )
  225. {
  226. RecordTypeCode r = new RecordTypeCode(TCKind.tk_struct);
  227. r.setId(id);
  228. r.setName(name);
  229. for (int i = 0; i < members.length; i++)
  230. {
  231. r.add(members [ i ]);
  232. }
  233. return r;
  234. }
  235. /** {@inheritDoc} */
  236. public TypeCode create_union_tc(String id, String name,
  237. TypeCode discriminator_type, UnionMember[] members
  238. )
  239. {
  240. RecordTypeCode r = new RecordTypeCode(TCKind.tk_union);
  241. r.setId(id);
  242. r.setName(name);
  243. r.setDiscriminator_type(discriminator_type);
  244. r.setDefaultIndex(0);
  245. for (int i = 0; i < members.length; i++)
  246. {
  247. r.add(members [ i ]);
  248. }
  249. return r;
  250. }
  251. /** {@inheritDoc} */
  252. public TypeCode create_wstring_tc(int bound)
  253. {
  254. StringTypeCode p = new StringTypeCode(TCKind.tk_wstring);
  255. p.setLength(bound);
  256. return p;
  257. }
  258. /** {@inheritDoc} */
  259. public TypeCode get_primitive_tc(TCKind tcKind)
  260. {
  261. try
  262. {
  263. return TypeKindNamer.getPrimitveTC(tcKind);
  264. }
  265. catch (BadKind ex)
  266. {
  267. throw new BAD_PARAM("This is not a primitive type code: " +
  268. tcKind.value()
  269. );
  270. }
  271. }
  272. /**
  273. * This method is not allowed for a RestrictedORB.
  274. *
  275. * @throws NO_IMPLEMENT, always.
  276. */
  277. public String[] list_initial_services()
  278. {
  279. no();
  280. throw new InternalError();
  281. }
  282. /**
  283. * This method is not allowed for a RestrictedORB.
  284. *
  285. * @throws NO_IMPLEMENT, always.
  286. */
  287. public String object_to_string(org.omg.CORBA.Object forObject)
  288. {
  289. no();
  290. throw new InternalError();
  291. }
  292. /**
  293. * This method is not allowed for a RestrictedORB.
  294. *
  295. * @throws InvalidName never in this class, but it is thrown in the derived
  296. * classes.
  297. *
  298. * @throws NO_IMPLEMENT, always.
  299. */
  300. public org.omg.CORBA.Object resolve_initial_references(String name)
  301. throws InvalidName
  302. {
  303. no();
  304. throw new InternalError();
  305. }
  306. /**
  307. * Shutdown the ORB server.
  308. *
  309. * For RestrictedORB, returns witout action.
  310. */
  311. public void run()
  312. {
  313. }
  314. /**
  315. * Shutdown the ORB server.
  316. *
  317. * For RestrictedORB, returns witout action.
  318. */
  319. public void shutdown(boolean wait_for_completion)
  320. {
  321. }
  322. /**
  323. * This method is not allowed for a RestrictedORB.
  324. *
  325. * @throws NO_IMPLEMENT, always.
  326. */
  327. public org.omg.CORBA.Object string_to_object(String IOR)
  328. {
  329. no();
  330. throw new InternalError();
  331. }
  332. /**
  333. * This method is not allowed for a RestrictedORB.
  334. *
  335. * @throws NO_IMPLEMENT, always.
  336. */
  337. protected void set_parameters(Applet app, Properties props)
  338. {
  339. no();
  340. }
  341. /**
  342. * This method is not allowed for a RestrictedORB.
  343. *
  344. * @throws NO_IMPLEMENT, always.
  345. */
  346. protected void set_parameters(String[] args, Properties props)
  347. {
  348. no();
  349. }
  350. /**
  351. * Throws an exception, stating that the given method is not supported by the
  352. * Restricted ORB.
  353. */
  354. private final void no()
  355. {
  356. // Apart the programming errors, this can only happen if the
  357. // malicious code is trying to do that it is not allowed.
  358. throw new NO_IMPLEMENT("Use init(args, props) for the functional version.");
  359. }
  360. /**
  361. * This method is not allowed for a RestrictedORB.
  362. *
  363. * @throws NO_IMPLEMENT, always.
  364. */
  365. public Request get_next_response() throws org.omg.CORBA.WrongTransaction
  366. {
  367. no();
  368. throw new InternalError();
  369. }
  370. /**
  371. * This method is not allowed for a RestrictedORB.
  372. *
  373. * @throws NO_IMPLEMENT, always.
  374. */
  375. public boolean poll_next_response()
  376. {
  377. no();
  378. throw new InternalError();
  379. }
  380. /**
  381. * This method is not allowed for a RestrictedORB.
  382. *
  383. * @throws NO_IMPLEMENT, always.
  384. */
  385. public void send_multiple_requests_deferred(Request[] requests)
  386. {
  387. no();
  388. }
  389. /**
  390. * This method is not allowed for a RestrictedORB.
  391. *
  392. * @throws NO_IMPLEMENT, always.
  393. */
  394. public void send_multiple_requests_oneway(Request[] requests)
  395. {
  396. no();
  397. }
  398. /**
  399. * Register the value factory under the given repository id.
  400. */
  401. public ValueFactory register_value_factory(String repository_id,
  402. ValueFactory factory
  403. )
  404. {
  405. factories.put(repository_id, factory);
  406. return factory;
  407. }
  408. /**
  409. * Unregister the value factroy.
  410. */
  411. public void unregister_value_factory(String id)
  412. {
  413. factories.remove(id);
  414. }
  415. /**
  416. * Look for the value factory for the value, having the given repository id.
  417. * The implementation checks for the registered value factories first. If none
  418. * found, it tries to load and instantiate the class, mathing the given naming
  419. * convention. If this faild, null is returned.
  420. *
  421. * @param repository_id a repository id.
  422. *
  423. * @return a found value factory, null if none.
  424. */
  425. public ValueFactory lookup_value_factory(String repository_id)
  426. {
  427. ValueFactory f = (ValueFactory) factories.get(repository_id);
  428. if (f != null)
  429. {
  430. return f;
  431. }
  432. f = (ValueFactory) ObjectCreator.createObject(repository_id,
  433. "DefaultFactory"
  434. );
  435. if (f != null)
  436. {
  437. factories.put(repository_id, f);
  438. }
  439. return f;
  440. }
  441. /**
  442. * Destroy the interceptors, if they are present.
  443. */
  444. public void destroy()
  445. {
  446. if (iIor != null)
  447. {
  448. iIor.destroy();
  449. iIor = null;
  450. }
  451. if (iServer != null)
  452. {
  453. iServer.destroy();
  454. iServer = null;
  455. }
  456. if (iClient != null)
  457. {
  458. iClient.destroy();
  459. iClient = null;
  460. }
  461. super.destroy();
  462. }
  463. /**
  464. * Create a typecode, representing a tree-like structure.
  465. * This structure contains a member that is a sequence of the same type,
  466. * as the structure itself. You can imagine as if the folder definition
  467. * contains a variable-length array of the enclosed (nested) folder
  468. * definitions. In this way, it is possible to have a tree like
  469. * structure that can be transferred via CORBA CDR stream.
  470. *
  471. * @deprecated It is easier and clearler to use a combination of
  472. * create_recursive_tc and create_sequence_tc instead.
  473. *
  474. * @param bound the maximal expected number of the nested components
  475. * on each node; 0 if not limited.
  476. *
  477. * @param offset the position of the field in the returned structure
  478. * that contains the sequence of the structures of the same field.
  479. * The members before this field are intialised using parameterless
  480. * StructMember constructor.
  481. *
  482. * @return a typecode, defining a stucture, where a member at the
  483. * <code>offset</code> position defines an array of the identical
  484. * structures.
  485. *
  486. * @see #create_recursive_tc(String)
  487. * @see #create_sequence_tc(int, TypeCode)
  488. */
  489. public TypeCode create_recursive_sequence_tc(int bound, int offset)
  490. {
  491. RecordTypeCode r = new RecordTypeCode(TCKind.tk_struct);
  492. for (int i = 0; i < offset; i++)
  493. r.add(new StructMember());
  494. TypeCode recurs = new PrimitiveTypeCode(TCKind.tk_sequence);
  495. r.add(new StructMember("", recurs, null));
  496. return r;
  497. }
  498. /**
  499. * Get the default context of this ORB. This is an initial root of all
  500. * contexts.
  501. *
  502. * The default method returns a new context with the empty name and
  503. * no parent context.
  504. *
  505. * @return the default context of this ORB.
  506. */
  507. public Context get_default_context()
  508. {
  509. return new gnuContext("", null);
  510. }
  511. }