gnuPOA.java 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  1. /* gnuAbstractPOA.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.Poa;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.util.ArrayList;
  34. import java.util.Arrays;
  35. import java.util.HashSet;
  36. import org.omg.CORBA.BAD_INV_ORDER;
  37. import org.omg.CORBA.BAD_PARAM;
  38. import org.omg.CORBA.CompletionStatus;
  39. import org.omg.CORBA.LocalObject;
  40. import org.omg.CORBA.NO_IMPLEMENT;
  41. import org.omg.CORBA.OBJ_ADAPTER;
  42. import org.omg.CORBA.ORB;
  43. import org.omg.CORBA.Object;
  44. import org.omg.CORBA.Policy;
  45. import org.omg.CORBA.SetOverrideType;
  46. import org.omg.CORBA.TRANSIENT;
  47. import org.omg.CORBA.portable.ObjectImpl;
  48. import org.omg.PortableInterceptor.NON_EXISTENT;
  49. import org.omg.PortableInterceptor.ObjectReferenceFactory;
  50. import org.omg.PortableInterceptor.ObjectReferenceTemplate;
  51. import org.omg.PortableInterceptor.ObjectReferenceTemplateHelper;
  52. import org.omg.PortableServer.AdapterActivator;
  53. import org.omg.PortableServer.ForwardRequest;
  54. import org.omg.PortableServer.IdAssignmentPolicy;
  55. import org.omg.PortableServer.IdAssignmentPolicyValue;
  56. import org.omg.PortableServer.IdUniquenessPolicy;
  57. import org.omg.PortableServer.IdUniquenessPolicyValue;
  58. import org.omg.PortableServer.ImplicitActivationPolicy;
  59. import org.omg.PortableServer.ImplicitActivationPolicyValue;
  60. import org.omg.PortableServer.LifespanPolicy;
  61. import org.omg.PortableServer.LifespanPolicyValue;
  62. import org.omg.PortableServer.POA;
  63. import org.omg.PortableServer.POAManager;
  64. import org.omg.PortableServer.RequestProcessingPolicy;
  65. import org.omg.PortableServer.RequestProcessingPolicyValue;
  66. import org.omg.PortableServer.SERVANT_RETENTION_POLICY_ID;
  67. import org.omg.PortableServer.Servant;
  68. import org.omg.PortableServer.ServantActivator;
  69. import org.omg.PortableServer.ServantLocator;
  70. import org.omg.PortableServer.ServantManager;
  71. import org.omg.PortableServer.ServantRetentionPolicy;
  72. import org.omg.PortableServer.ServantRetentionPolicyValue;
  73. import org.omg.PortableServer.ThreadPolicy;
  74. import org.omg.PortableServer.ThreadPolicyValue;
  75. import org.omg.PortableServer.POAManagerPackage.State;
  76. import org.omg.PortableServer.POAPackage.AdapterAlreadyExists;
  77. import org.omg.PortableServer.POAPackage.AdapterNonExistent;
  78. import org.omg.PortableServer.POAPackage.InvalidPolicy;
  79. import org.omg.PortableServer.POAPackage.NoServant;
  80. import org.omg.PortableServer.POAPackage.ObjectAlreadyActive;
  81. import org.omg.PortableServer.POAPackage.ObjectNotActive;
  82. import org.omg.PortableServer.POAPackage.ServantAlreadyActive;
  83. import org.omg.PortableServer.POAPackage.ServantNotActive;
  84. import org.omg.PortableServer.POAPackage.WrongAdapter;
  85. import org.omg.PortableServer.POAPackage.WrongPolicy;
  86. import gnu.CORBA.OrbFunctional;
  87. import gnu.CORBA.CDR.BufferredCdrInput;
  88. import gnu.CORBA.CDR.BufferedCdrOutput;
  89. /**
  90. * Our POA implementation.
  91. *
  92. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  93. */
  94. public class gnuPOA
  95. extends LocalObject
  96. implements POA, ObjectReferenceFactory
  97. {
  98. /**
  99. * The object reference template, associated with this POA.
  100. *
  101. * @since 1.5
  102. */
  103. class RefTemplate implements ObjectReferenceTemplate
  104. {
  105. /**
  106. * Use serialVersionUID for interoperability.
  107. */
  108. private static final long serialVersionUID = 1;
  109. RefTemplate()
  110. {
  111. // The adapter name is computed once.
  112. ArrayList names = new ArrayList();
  113. names.add(the_name());
  114. POA poa = the_parent();
  115. while (poa != null)
  116. {
  117. names.add(poa.the_name());
  118. poa = poa.the_parent();
  119. }
  120. // Fill in the string array in reverse (more natural) order,
  121. // root POA first.
  122. m_adapter_name = new String[names.size()];
  123. for (int i = 0; i < m_adapter_name.length; i++)
  124. m_adapter_name[i] = (String) names.get(m_adapter_name.length - i - 1);
  125. }
  126. /**
  127. * The adapter name
  128. */
  129. final String[] m_adapter_name;
  130. /**
  131. * Get the name of this POA.
  132. */
  133. public String[] adapter_name()
  134. {
  135. return (String[]) m_adapter_name.clone();
  136. }
  137. /**
  138. * Get the ORB id.
  139. */
  140. public String orb_id()
  141. {
  142. return m_orb.orb_id;
  143. }
  144. /**
  145. * Get the server id.
  146. */
  147. public String server_id()
  148. {
  149. return OrbFunctional.server_id;
  150. }
  151. /**
  152. * Create the object.
  153. */
  154. public Object make_object(String repositoryId, byte[] objectId)
  155. {
  156. return create_reference_with_id(objectId, repositoryId);
  157. }
  158. /**
  159. * Get the array of truncatible repository ids.
  160. */
  161. public String[] _truncatable_ids()
  162. {
  163. return ref_template_ids;
  164. }
  165. }
  166. /**
  167. * Use serialVersionUID for interoperability.
  168. */
  169. private static final long serialVersionUID = 1;
  170. /**
  171. * The adapter reference template.
  172. */
  173. ObjectReferenceTemplate refTemplate;
  174. /**
  175. * The reference template repository ids. Defined outside the class as it
  176. * cannot have static members.
  177. */
  178. final static String[] ref_template_ids =
  179. new String[] { ObjectReferenceTemplateHelper.id() };
  180. /**
  181. * The active object map, mapping between object keys, objects and servants.
  182. */
  183. public final AOM aom = new AOM();
  184. /**
  185. * The children of this POA.
  186. */
  187. final ArrayList children = new ArrayList();
  188. /**
  189. * The name of this POA.
  190. */
  191. final String name;
  192. /**
  193. * The parent of this POA (null for the root POA).
  194. */
  195. final POA parent;
  196. /**
  197. * The ior key signature, indicating, that the ior key is encoded using
  198. * internal agreements of this implementation (0x'free').
  199. */
  200. static final int SIGNATURE = 0x66726565;
  201. /**
  202. * The adapter activator for this POA, null if no activator is set.
  203. */
  204. AdapterActivator m_activator;
  205. /**
  206. * The POA manager for this POA.
  207. */
  208. POAManager m_manager;
  209. /**
  210. * The servant manager (servant activator) for this POA.
  211. */
  212. ServantActivator servant_activator;
  213. /**
  214. * The servant manager (servant locator) for this POA.
  215. */
  216. ServantLocator servant_locator;
  217. /**
  218. * The default servant, if on is in use.
  219. */
  220. Servant default_servant;
  221. /**
  222. * The cached poa id value, computed once.
  223. */
  224. private byte[] m_poa_id;
  225. /**
  226. * The all policy values that apply to this POA.
  227. * The used policy values are singletons, unique between policies.
  228. */
  229. private final HashSet m_policies;
  230. /**
  231. * An array of the set policies.
  232. */
  233. Policy[] s_policies;
  234. /**
  235. * The ORB, where the POA is connected.
  236. */
  237. final ORB_1_4 m_orb;
  238. /**
  239. * When true, the POA is being destroyed or is destroyed.
  240. */
  241. boolean m_inDestruction;
  242. /**
  243. * True if the active object map is used by this POA.
  244. * The value is moved into separate boolean value due
  245. * necessity of the frequent checks.
  246. */
  247. public final boolean retain_servant;
  248. /**
  249. * The object reference factory, used to create the new object
  250. * references.
  251. */
  252. ObjectReferenceFactory m_object_factory = this;
  253. /**
  254. * Create a new abstract POA.
  255. *
  256. * @param a_parent the parent of this POA.
  257. * @param a_name a name for this POA.
  258. * @param a_manager a manager for this POA. If null, a new
  259. * {@link gnuPOAManager} will be instantiated.
  260. * @param a_policies an array of policies that apply to this POA.
  261. * @param an_orb an ORB for this POA.
  262. */
  263. public gnuPOA(gnuPOA a_parent, String a_name, POAManager a_manager,
  264. Policy[] a_policies, ORB_1_4 an_orb
  265. )
  266. throws InvalidPolicy
  267. {
  268. // Add default policies.
  269. Policy[] all_policies = StandardPolicies.withDefault(a_policies);
  270. name = a_name;
  271. parent = a_parent;
  272. m_orb = an_orb;
  273. if (a_manager != null)
  274. m_manager = a_manager;
  275. else
  276. m_manager = new gnuPOAManager();
  277. if (m_manager instanceof gnuPOAManager)
  278. {
  279. gnuPOAManager g = (gnuPOAManager) m_manager;
  280. g.addPoa(this);
  281. }
  282. m_policies = new HashSet(all_policies.length);
  283. s_policies = new Policy[ all_policies.length ];
  284. for (int i = 0; i < s_policies.length; i++)
  285. {
  286. s_policies [ i ] = all_policies [ i ].copy();
  287. m_policies.add(((AccessiblePolicy) s_policies [ i ]).getValue());
  288. }
  289. retain_servant = applies(ServantRetentionPolicyValue.RETAIN);
  290. validatePolicies(a_policies);
  291. refTemplate = new RefTemplate();
  292. }
  293. /**
  294. * Wait while at least one of the threads in this POA is actively
  295. * processing one of requests.
  296. */
  297. public void waitWhileRunning()
  298. {
  299. // First pause.
  300. long time = 1;
  301. // Maximal duration between checks.
  302. long max = 500;
  303. boolean runs;
  304. do
  305. {
  306. runs = m_orb.currents.has(this);
  307. if (runs)
  308. {
  309. // Avoid taking CPU resources
  310. // from the thread that is running.
  311. try
  312. {
  313. Thread.sleep(time);
  314. time = time * 2;
  315. if (time > max)
  316. time = max;
  317. }
  318. catch (InterruptedException ex)
  319. {
  320. }
  321. }
  322. }
  323. while (runs);
  324. }
  325. /**
  326. * Etherealize all objects, associated with this POA. Invoked from the
  327. * {@link gnuPOAManager} only if it is known that the servant_activator
  328. * holds non-null value.
  329. */
  330. protected void etherealizeAll()
  331. {
  332. if (servant_activator == null)
  333. return;
  334. ArrayList keys = new ArrayList();
  335. keys.addAll(aom.keySet());
  336. byte[] key;
  337. AOM.Obj obj;
  338. boolean last;
  339. for (int i = 0; i < keys.size(); i++)
  340. {
  341. key = (byte[]) keys.get(i);
  342. obj = aom.get(key);
  343. if (obj.poa == this)
  344. {
  345. aom.remove(key);
  346. if (!obj.isDeactiveted())
  347. {
  348. // Check if the servant still stays under the other key.
  349. last = aom.findServant(obj.servant) == null;
  350. servant_activator.etherealize(obj.key, this, obj.servant, true,
  351. last
  352. );
  353. }
  354. }
  355. }
  356. }
  357. /**
  358. * Create an instance of the POA with the given features.
  359. * This method is not responsible for duplicate checking
  360. * or adding the returned instance to any possible table.
  361. *
  362. * @param child_name the name of the poa being created.
  363. * @param a_manager the poa manager (never null).
  364. * @param policies the array of policies.
  365. * @param an_orb the ORB for this POA.
  366. *
  367. * @return the created POA.
  368. *
  369. * @throws InvalidPolicy for conflicting or otherwise invalid policies.|
  370. */
  371. protected POA createPoaInstance(String child_name, POAManager a_manager,
  372. Policy[] policies, ORB_1_4 an_orb
  373. )
  374. throws InvalidPolicy
  375. {
  376. POAManager some_manager =
  377. a_manager == null ? new gnuPOAManager() : a_manager;
  378. if (some_manager instanceof gnuPOAManager)
  379. {
  380. ((gnuPOAManager) some_manager).addPoa(this);
  381. }
  382. return new gnuPOA(this, child_name, some_manager, policies, an_orb);
  383. }
  384. /**
  385. * Check if the given policy value applies to this POA.
  386. *
  387. * @param policy_value a policy value to check. The policy values are
  388. * singletons and unique between the different policies, so the policy
  389. * type is not passed.
  390. *
  391. * @return true if the policy value applies, false otherwise.
  392. */
  393. public final boolean applies(java.lang.Object policy_value)
  394. {
  395. return m_policies.contains(policy_value);
  396. }
  397. /**
  398. * Check for the presence of the required policy.
  399. *
  400. * @param policy_value a policy value to check.
  401. *
  402. * @throws WrongPolicy if the required policy value is not applicable.
  403. */
  404. public final void required(java.lang.Object policy_value)
  405. throws WrongPolicy
  406. {
  407. if (!applies(policy_value))
  408. throw new WrongPolicy(policy_value + " policy required.");
  409. }
  410. /**
  411. * Check for the absence of the given policy.
  412. *
  413. * @param policy_value a policy value to check.
  414. *
  415. * @throws WrongPolicy if the passed policy value is applicable.
  416. */
  417. public final void excluding(java.lang.Object policy_value)
  418. throws WrongPolicy
  419. {
  420. if (applies(policy_value))
  421. throw new WrongPolicy(policy_value + " policy applies.");
  422. }
  423. /**
  424. * Find and optionally activate the child POA with the given name.
  425. *
  426. * @param poa_name the name of the POA to find.
  427. * @param activate_it if the child with the specified name is not found
  428. * or inactive and this parameter is true, the target POA activator is
  429. * invoked to activate that child. If this succeeds, that child POA
  430. * is returned.
  431. *
  432. * @throws AdapterNonExistent if no active child with the given name
  433. * is found and one of the following is true:
  434. * a) the target POA has no associated
  435. * {@link AdapterActivator}. b) that activator fails to activate the
  436. * child POA. c) <code>activate_id</code> = false.
  437. */
  438. public POA find_POA(String poa_name, boolean activate_it)
  439. throws AdapterNonExistent
  440. {
  441. POA child;
  442. for (int i = 0; i < children.size(); i++)
  443. {
  444. child = (POA) children.get(i);
  445. if (child.the_name().equals(poa_name))
  446. return child;
  447. }
  448. if (activate_it && m_activator != null)
  449. {
  450. boolean activated = m_activator.unknown_adapter(this, poa_name);
  451. if (!activated)
  452. throw new AdapterNonExistent(poa_name + " activation failed.");
  453. // Tha activator should add the child to the childrent table.
  454. for (int i = 0; i < children.size(); i++)
  455. {
  456. child = (POA) children.get(i);
  457. if (child.the_name().equals(poa_name))
  458. return child;
  459. }
  460. throw new AdapterNonExistent(poa_name + " not created. ");
  461. }
  462. else
  463. throw new AdapterNonExistent(poa_name);
  464. }
  465. /**
  466. * Generate the Object Id for the given servant and add the servant to the
  467. * Active Object Map using this Id a a key. If the servant activator is set,
  468. * its incarnate method will be called.
  469. *
  470. * @param a_servant a servant that would serve the object with the returned
  471. * Object Id. If null is passed, under apporoprate policies the servant
  472. * activator is invoked.
  473. *
  474. * @return the generated objert Id for the given servant.
  475. *
  476. * @throws ServantAlreadyActive if this servant is already in the Active
  477. * Object Map and the UNIQUE_ID policy applies.
  478. *
  479. * @throws WrongPolicy if the required policies SYSTEM_ID and RETAIN do not
  480. * apply to this POA.
  481. */
  482. public byte[] activate_object(Servant a_servant)
  483. throws ServantAlreadyActive, WrongPolicy
  484. {
  485. checkDiscarding();
  486. required(ServantRetentionPolicyValue.RETAIN);
  487. required(IdAssignmentPolicyValue.SYSTEM_ID);
  488. AOM.Obj exists = aom.findServant(a_servant);
  489. if (exists != null)
  490. {
  491. if (exists.isDeactiveted())
  492. {
  493. // If exists but deactivated, activate and return
  494. // the existing key.
  495. exists.setDeactivated(false);
  496. incarnate(exists, exists.key, a_servant, false);
  497. return exists.key;
  498. }
  499. else if (applies(IdUniquenessPolicyValue.UNIQUE_ID))
  500. throw new ServantAlreadyActive();
  501. // It multiple ids are allowed, exit block allowing repetetive
  502. // activations.
  503. }
  504. byte[] object_key = AOM.getFreeId();
  505. ServantDelegateImpl delegate = new ServantDelegateImpl(a_servant, this,
  506. object_key);
  507. create_and_connect(object_key,
  508. a_servant._all_interfaces(this, object_key)[0], delegate);
  509. return object_key;
  510. }
  511. /**
  512. * Add the given servant to the Active Object Map as a servant for the object
  513. * with the provided Object Id. If the servant activator is set, its incarnate
  514. * method will be called.
  515. *
  516. * @param an_Object_Id an object id for the given object.
  517. * @param a_servant a servant that will serve the object with the given Object
  518. * Id. If null is passed, under apporoprate policies the servant activator is
  519. * invoked.
  520. *
  521. * @throws ObjectAlreadyActive if the given object id is already in the Active
  522. * Object Map.
  523. * @throws ServantAlreadyActive if the UNIQUE_ID policy applies and this
  524. * servant is already in use.
  525. * @throws WrongPolicy if the required RETAIN policy does not apply to this
  526. * POA.
  527. * @throws BAD_PARAM if the passed object id is invalid due any reason.
  528. */
  529. public void activate_object_with_id(byte[] an_Object_Id, Servant a_servant)
  530. throws ServantAlreadyActive, ObjectAlreadyActive,
  531. WrongPolicy
  532. {
  533. activate_object_with_id(an_Object_Id, a_servant, false);
  534. }
  535. /**
  536. * Same as activate_object_with_id, but permits gnuForwardRequest forwarding
  537. * exception. This is used when the activation is called from the remote
  538. * invocation context and we have possibility to return the forwarding
  539. * message.
  540. */
  541. public void activate_object_with_id(byte[] an_Object_Id, Servant a_servant,
  542. boolean use_forwarding)
  543. throws ServantAlreadyActive, ObjectAlreadyActive, WrongPolicy
  544. {
  545. checkDiscarding();
  546. required(ServantRetentionPolicyValue.RETAIN);
  547. // If the UNIQUE_ID applies, the servant being passed must not be
  548. // already active.
  549. if (applies(IdUniquenessPolicyValue.UNIQUE_ID))
  550. {
  551. AOM.Obj sx = aom.findServant(a_servant, false);
  552. if (sx != null)
  553. throw new ServantAlreadyActive();
  554. }
  555. AOM.Obj exists = aom.get(an_Object_Id);
  556. if (exists != null)
  557. {
  558. if (exists.servant == null)
  559. {
  560. locateServant(an_Object_Id, a_servant, exists, use_forwarding);
  561. exists.setDeactivated(false);
  562. }
  563. else if (exists.isDeactiveted())
  564. {
  565. exists.setDeactivated(false);
  566. incarnate(exists, an_Object_Id, a_servant, use_forwarding);
  567. }
  568. else
  569. throw new ObjectAlreadyActive();
  570. }
  571. else
  572. {
  573. ServantDelegateImpl delegate = new ServantDelegateImpl(a_servant, this,
  574. an_Object_Id);
  575. create_and_connect(an_Object_Id, a_servant._all_interfaces(this,
  576. an_Object_Id)[0], delegate);
  577. }
  578. }
  579. /**
  580. * Locate the servant for this object Id and connect it to ORB.
  581. *
  582. * @param an_Object_Id the object id.
  583. * @param a_servant the servant (may be null).
  584. * @param exists an existing active object map entry.
  585. * @param use_forwarding allow to throw the gnuForwardRequest if the activator
  586. * throws ForwardRequest.
  587. *
  588. * @throws OBJ_ADAPTER minor 4 if the servant cannot be located (the required
  589. * servant manager may be missing).
  590. */
  591. private void locateServant(byte[] an_Object_Id, Servant a_servant,
  592. AOM.Obj exists, boolean use_forwarding
  593. )
  594. throws InternalError
  595. {
  596. // An object was created with create_reference.
  597. gnuServantObject object = (gnuServantObject) exists.object;
  598. if (servant_activator != null)
  599. {
  600. exists.setServant(incarnate(exists, an_Object_Id, a_servant,
  601. use_forwarding
  602. )
  603. );
  604. }
  605. else if (default_servant != null)
  606. {
  607. exists.setServant(default_servant);
  608. }
  609. if (exists.servant == null)
  610. {
  611. exists.setServant(a_servant);
  612. }
  613. if (exists.servant == null)
  614. {
  615. throw new OBJ_ADAPTER("no servant", 4, CompletionStatus.COMPLETED_NO);
  616. }
  617. ServantDelegateImpl delegate =
  618. new ServantDelegateImpl(exists.servant, this, an_Object_Id);
  619. exists.servant._set_delegate(delegate);
  620. object.setServant(exists.servant);
  621. connect_to_orb(an_Object_Id, delegate.object);
  622. }
  623. /**
  624. * Deactivate object with the given id.
  625. *
  626. * The deactivated object will continue to process requests that arrived
  627. * before decativation. If this POA has the associated
  628. * servant manager, a {@link ServantActivatorOperations#etherealize} is
  629. * immediately invoked on the passed id.
  630. *
  631. * @throws WrongPolicy if the required RETAIN policy does not apply to
  632. * this POA.
  633. */
  634. public void deactivate_object(byte[] the_Object_Id)
  635. throws ObjectNotActive, WrongPolicy
  636. {
  637. required(ServantRetentionPolicyValue.RETAIN);
  638. AOM.Obj exists = aom.get(the_Object_Id);
  639. if (exists == null || exists.isDeactiveted())
  640. throw new ObjectNotActive();
  641. exists.setDeactivated(true);
  642. // Check if this servant is serving something else.
  643. aom.remove(the_Object_Id);
  644. AOM.Obj other = aom.findServant(exists.servant, false);
  645. boolean remaining = other != null;
  646. aom.put(exists);
  647. if (servant_activator != null)
  648. servant_activator.etherealize(the_Object_Id, this, exists.servant, false,
  649. remaining
  650. );
  651. }
  652. /**
  653. * Create the object reference, encapsulating the given repository Id and
  654. * the Object Id, generated by this POA. The returned object will not be
  655. * activated by default and may be activated on the first invocation by
  656. * the servant manager (if it is set and if policies are applicable).
  657. *
  658. * @param a_repository_id the repository id for the given object, can
  659. * be null if to be requested from the servant later.
  660. *
  661. * @throws WrongPolicy if the required SYSTEM_ID policy does not apply to
  662. * this POA.
  663. */
  664. public org.omg.CORBA.Object create_reference(String a_repository_id)
  665. throws WrongPolicy
  666. {
  667. required(IdAssignmentPolicyValue.SYSTEM_ID);
  668. return create_reference_with_id(AOM.getFreeId(), a_repository_id);
  669. }
  670. /**
  671. * <p>
  672. * Create the object reference, encapsulating the given repository Id and
  673. * the given Object Id. The returned object will <i>not</i> be
  674. * activated by default and may be activated on the first invocation by
  675. * the servant manager (if the IMPLICIT_ACTIVATION policy applies).
  676. *
  677. * @param an_object_id the object id for the object being created. If this
  678. * POA uses the SYSTEM_ID policy, the portable application should only
  679. * pass the ids, generated by this POA.
  680. *
  681. * @param a_repository_id the repository id for the object being created,
  682. * can be null if this information should be later requested from the
  683. * servant.
  684. */
  685. public org.omg.CORBA.Object create_reference_with_id(byte[] an_object_id,
  686. String a_repository_id
  687. )
  688. {
  689. String[] ids;
  690. if (a_repository_id == null)
  691. ids = null;
  692. else
  693. ids = new String[] { a_repository_id };
  694. // Check maybe such object is already activated.
  695. AOM.Obj e = aom.get(an_object_id);
  696. Servant servant;
  697. if (e == null)
  698. {
  699. servant = null;
  700. }
  701. else
  702. {
  703. servant = e.servant;
  704. e.setDeactivated(false);
  705. }
  706. gnuServantObject object =
  707. new gnuServantObject(ids, an_object_id, this, m_orb);
  708. object._set_delegate(new LocalDelegate(object, this, an_object_id));
  709. aom.add(object.Id, object, servant, this);
  710. connect_to_orb(an_object_id, object);
  711. return object;
  712. }
  713. /**
  714. * Creates a new POA as a child of the target POA.
  715. *
  716. * @param child_name the name of the child POA being created.
  717. * @param manager the manager that will control the new POA. If this parameter
  718. * is null, a new POA manager is created and associated with the new POA.
  719. *
  720. * @param policies the policies, applicable for the parent POA. Policies
  721. * are <i>not</i> inherited from the parent POA.
  722. *
  723. * @return an newly created POA. The POA will be intially in the holding
  724. * state and must be activated to start processing requests.
  725. *
  726. * @throws AdapterAlreadyExists if the child with the given child_name
  727. * already exists for the current POA.
  728. * @throws InvalidPolicy if the policies conflict with each other or are
  729. * otherwise inappropriate.
  730. *
  731. * @see #the_children()
  732. */
  733. public POA create_POA(String child_name, POAManager manager, Policy[] policies)
  734. throws AdapterAlreadyExists, InvalidPolicy
  735. {
  736. POA child;
  737. for (int i = 0; i < children.size(); i++)
  738. {
  739. child = (POA) children.get(i);
  740. if (child.the_name().equals(child_name))
  741. throw new AdapterAlreadyExists(name + "/" + child_name);
  742. }
  743. POA poa = createPoaInstance(child_name, manager, policies, m_orb);
  744. children.add(poa);
  745. return poa;
  746. }
  747. /**
  748. * Returns a default servant for this POA.
  749. *
  750. * @return a servant that will be used for requests for
  751. * which no servant is found in the Active Object Map.
  752. *
  753. * @throws NoServant if there is no default servant associated with this POA.
  754. * @throws WrongPolicy if the USE_DEFAULT_SERVANT policy is not active.
  755. */
  756. public Servant get_servant()
  757. throws NoServant, WrongPolicy
  758. {
  759. required(RequestProcessingPolicyValue.USE_DEFAULT_SERVANT);
  760. if (default_servant == null)
  761. throw new NoServant();
  762. return default_servant;
  763. }
  764. /**
  765. * Sets the default servant for this POA.
  766. *
  767. * @param a_servant a servant that will be used for requests for
  768. * which no servant is found in the Active Object Map.
  769. *
  770. * @throws WrongPolicy if the USE_DEFAULT_SERVANT policy is not active.
  771. */
  772. public void set_servant(Servant a_servant)
  773. throws WrongPolicy
  774. {
  775. required(RequestProcessingPolicyValue.USE_DEFAULT_SERVANT);
  776. default_servant = a_servant;
  777. }
  778. /**
  779. * Set a servant manager for this POA.
  780. *
  781. * @param a servant manager being set. If the RETAIN policy applies, the
  782. * manager must implement a {@link ServantActivator}. If the NON_RETAIN
  783. * policy applies, the manager must implement a {@link ServantLocator}.
  784. *
  785. * @throws WrongPolicy if the required USE_SERVANT_MANAGER policy does not
  786. * apply to this POA.
  787. *
  788. * @throws OBJ_ADAPTER minor code 4 if the passed manager does not
  789. * implement the required interface ({@link ServantActivator},
  790. * {@link ServantLocator}). The POA, that has the RETAIN policy uses
  791. * servant managers that are ServantActivators. When the POA has the
  792. * NON_RETAIN policy it uses servant managers that are ServantLoacators.
  793. *
  794. * @throws BAD_INV_ORDER minor code 6 if the method is called more than once
  795. * on the same POA. The manager can be set only once.
  796. */
  797. public void set_servant_manager(ServantManager a_manager)
  798. throws WrongPolicy
  799. {
  800. required(RequestProcessingPolicyValue.USE_SERVANT_MANAGER);
  801. if (servant_activator != null || servant_locator != null)
  802. throw new BAD_INV_ORDER("Setting manager twice for " + name, 6,
  803. CompletionStatus.COMPLETED_NO
  804. );
  805. if (applies(ServantRetentionPolicyValue.RETAIN))
  806. {
  807. if (a_manager instanceof ServantActivator)
  808. servant_activator = (ServantActivator) a_manager;
  809. else
  810. throw new OBJ_ADAPTER("RETAIN requires ServantActivator", 4,
  811. CompletionStatus.COMPLETED_NO
  812. );
  813. }
  814. else if (applies(ServantRetentionPolicyValue.NON_RETAIN))
  815. {
  816. if (a_manager instanceof ServantLocator)
  817. servant_locator = (ServantLocator) a_manager;
  818. else
  819. throw new OBJ_ADAPTER("NON_RETAIN requires ServantLocator", 4,
  820. CompletionStatus.COMPLETED_NO
  821. );
  822. }
  823. else
  824. throw new WrongPolicy("No servant retention policy is specified.");
  825. }
  826. /**
  827. * Get the servant manager, associated with this POA.
  828. *
  829. * @return the associated servant manager or null if it has
  830. * been previously set.
  831. *
  832. * @throws WrongPolicy if the required USE_SERVANT_MANAGER policy does not
  833. * apply to this POA.
  834. */
  835. public ServantManager get_servant_manager()
  836. throws WrongPolicy
  837. {
  838. required(RequestProcessingPolicyValue.USE_SERVANT_MANAGER);
  839. if (servant_activator != null)
  840. return servant_activator;
  841. else
  842. return servant_locator;
  843. }
  844. /**
  845. * Get the unique Id of the POA in the process in which it is created.
  846. * This Id is needed by portable interceptors. The id is unique
  847. * for the life span of the POA in the process. For persistent
  848. * POAs, if a POA is created in the same path with the same name as
  849. * another POA, these POAs are identical have the same id. All transient
  850. * POAs are assumed unique.
  851. */
  852. public byte[] id()
  853. {
  854. if (m_poa_id != null)
  855. return m_poa_id;
  856. else
  857. {
  858. BufferedCdrOutput buffer = new BufferedCdrOutput();
  859. POA p = this;
  860. while (p != null)
  861. {
  862. buffer.write_string(p.the_name());
  863. p = p.the_parent();
  864. }
  865. m_poa_id = buffer.buffer.toByteArray();
  866. return m_poa_id;
  867. }
  868. }
  869. /**
  870. * Returns the reference to the active object with the given Id.
  871. *
  872. * @param the_Object_Id the object id.
  873. *
  874. * @throws ObjectNotActive if there is no active object with such Id
  875. * in the scope of this POA.
  876. * @throws WrongPolicy if the required RETAIN policy does not apply to
  877. * this POA.
  878. */
  879. public org.omg.CORBA.Object id_to_reference(byte[] the_Object_Id)
  880. throws ObjectNotActive, WrongPolicy
  881. {
  882. required(ServantRetentionPolicyValue.RETAIN);
  883. AOM.Obj ref = aom.get(the_Object_Id);
  884. if (ref == null)
  885. throw new ObjectNotActive();
  886. else
  887. return ref.object;
  888. }
  889. /**
  890. * Returns the servant that serves the active object with the given Id.
  891. *
  892. * @param the_Object_Id the object id.
  893. *
  894. * @throws ObjectNotActive if there is no active object with such Id or
  895. * it is not currently active.
  896. * @throws WrongPolicy. This method requires either RETAIN or
  897. * USE_DEFAULT_SERVANT policies and reaises the WrongPolicy if none of them
  898. * apply to this POA.
  899. */
  900. public Servant id_to_servant(byte[] the_Object_Id)
  901. throws ObjectNotActive, WrongPolicy
  902. {
  903. if (applies(ServantRetentionPolicyValue.RETAIN))
  904. {
  905. AOM.Obj ref = aom.get(the_Object_Id);
  906. if (ref == null || ref.isDeactiveted())
  907. {
  908. if (default_servant != null)
  909. return default_servant;
  910. else
  911. throw new ObjectNotActive();
  912. }
  913. else if (ref.servant != null)
  914. return ref.servant;
  915. else if (default_servant != null)
  916. return default_servant;
  917. else
  918. throw new ObjectNotActive();
  919. }
  920. else if (default_servant != null)
  921. {
  922. return default_servant;
  923. }
  924. else
  925. throw new WrongPolicy("Either RETAIN or USE_DEFAULT_SERVANT required.");
  926. }
  927. /**
  928. * Returns the Object Id, encapsulated in the given object reference.
  929. *
  930. * @param the_Object the object that has been previously created with this
  931. * POA. It need not be active.
  932. *
  933. * @throws WrongAdapter if the passed object is not known for this POA.
  934. * @throws WrongPolicy never (declared for the future extensions only).
  935. */
  936. public byte[] reference_to_id(org.omg.CORBA.Object the_Object)
  937. throws WrongAdapter, WrongPolicy
  938. {
  939. AOM.Obj ref = aom.findObject(the_Object);
  940. if (ref == null)
  941. throw new WrongAdapter();
  942. return ref.key;
  943. }
  944. /**
  945. * Returns the servant that is serving this object.
  946. *
  947. * @return if the RETAIN policy applies and the object is in the Active Object
  948. * Map, the method returns the servant, associated with this object.
  949. * Otherwise, if the USE_DEFAULT_SERVANT policy applies, the method returns
  950. * the default servant (if one was set).
  951. *
  952. * @throws ObjectNotActive if none of the conditions above are satisfied.
  953. * @throws WrongAdapter if the object reference was not created with this POA.
  954. * @throws WrongPolicy. This method requires either RETAIN or
  955. * USE_DEFAULT_SERVANT policies and reaises the WrongPolicy if none of them
  956. * apply to this POA.
  957. */
  958. public Servant reference_to_servant(org.omg.CORBA.Object the_Object)
  959. throws ObjectNotActive, WrongPolicy, WrongAdapter
  960. {
  961. if (applies(ServantRetentionPolicyValue.RETAIN))
  962. {
  963. AOM.Obj ref = aom.findObject(the_Object);
  964. if (ref == null)
  965. {
  966. String object;
  967. if (the_Object == null)
  968. object = "null passed";
  969. else if (the_Object instanceof gnuServantObject)
  970. {
  971. gnuServantObject gs = (gnuServantObject) the_Object;
  972. object = "Wrong owner POA " + gs.poa.the_name();
  973. }
  974. else
  975. object = "Unknown " + the_Object.getClass().getName();
  976. throw new WrongAdapter(object + " for '" + the_name() + "'");
  977. }
  978. else if (ref.isDeactiveted() || ref.servant == null)
  979. {
  980. if (default_servant != null)
  981. return default_servant;
  982. else
  983. throw new ObjectNotActive();
  984. }
  985. else
  986. return ref.servant;
  987. }
  988. else if (default_servant != null)
  989. {
  990. return default_servant;
  991. }
  992. else
  993. throw new WrongPolicy("Either RETAIN or USE_DEFAULT_SERVANT required.");
  994. }
  995. /**
  996. * Returns the id of the object, served by the given servant (assuming that
  997. * the servant serves only one object). The id is found in one of the
  998. * following ways.
  999. * <ul>
  1000. * <li>If the POA has both the RETAIN and the UNIQUE_ID policy and the
  1001. * specified servant is active, the method return the Object Id associated
  1002. * with that servant. </li>
  1003. * <li> If the POA has both the RETAIN and the IMPLICIT_ACTIVATION policy and
  1004. * either the POA has the MULTIPLE_ID policy or the specified servant is
  1005. * inactive, the method activates the servant using a POA-generated Object Id
  1006. * and the Interface Id associated with the servant, and returns that Object
  1007. * Id. </li>
  1008. * <li>If the POA has the USE_DEFAULT_SERVANT policy, the servant specified
  1009. * is the default servant, and the method is being invoked in the context of
  1010. * executing a request on the default servant, the method returns the ObjectId
  1011. * associated with the current invocation. </li>
  1012. * </ul>
  1013. *
  1014. * @throws ServantNotActive in all cases, not listed in the list above.
  1015. * @throws WrongPolicy The method requres USE_DEFAULT_SERVANT policy or a
  1016. * combination of the RETAIN policy and either the UNIQUE_ID or
  1017. * IMPLICIT_ACTIVATION policies and throws the WrongPolicy if these conditions
  1018. * are not satisfied.
  1019. */
  1020. public byte[] servant_to_id(Servant the_Servant)
  1021. throws ServantNotActive, WrongPolicy
  1022. {
  1023. if (applies(RequestProcessingPolicyValue.USE_DEFAULT_SERVANT) ||
  1024. applies(ServantRetentionPolicyValue.RETAIN) &&
  1025. (
  1026. applies(IdUniquenessPolicyValue.UNIQUE_ID) ||
  1027. applies(ImplicitActivationPolicyValue.IMPLICIT_ACTIVATION)
  1028. )
  1029. )
  1030. {
  1031. AOM.Obj ref = null;
  1032. if (!applies(IdUniquenessPolicyValue.MULTIPLE_ID))
  1033. ref = aom.findServant(the_Servant);
  1034. if (ref == null &&
  1035. applies(ImplicitActivationPolicyValue.IMPLICIT_ACTIVATION)
  1036. )
  1037. {
  1038. // Try to activate.
  1039. try
  1040. {
  1041. return activate_object(the_Servant);
  1042. }
  1043. catch (ServantAlreadyActive ex)
  1044. {
  1045. // Either it shuld not be or the policy allows multiple ids.
  1046. throw new InternalError();
  1047. }
  1048. }
  1049. if (ref == null)
  1050. throw new ServantNotActive();
  1051. else
  1052. return ref.key;
  1053. }
  1054. else
  1055. throw new WrongPolicy("(RETAIN and UNIQUE ID) " +
  1056. "or USE_DEFAULT_SERVANT required."
  1057. );
  1058. }
  1059. /**
  1060. * <p>
  1061. * Converts the given servant to the object reference. The servant will serve
  1062. * all methods, invoked on the returned object. The returned object reference
  1063. * can be passed to the remote client, enabling remote invocations.
  1064. * </p>
  1065. * <p>
  1066. * If the specified servant is active, it is returned. Otherwise, if the POA
  1067. * has the IMPLICIT_ACTIVATION policy the method activates the servant. In
  1068. * this case, if the servant activator is set, the
  1069. * {@link ServantActivatorOperations#incarnate} method will be called.
  1070. * </p>
  1071. *
  1072. * @throws ServantNotActive if the servant is inactive and no
  1073. * IMPLICIT_ACTIVATION policy applies.
  1074. * @throws WrongPolicy This method needs the RETAIN policy and either the
  1075. * UNIQUE_ID or IMPLICIT_ACTIVATION policies.
  1076. *
  1077. * @return the object, exposing the given servant in the context of this POA.
  1078. */
  1079. public org.omg.CORBA.Object servant_to_reference(Servant the_Servant)
  1080. throws ServantNotActive, WrongPolicy
  1081. {
  1082. required(ServantRetentionPolicyValue.RETAIN);
  1083. AOM.Obj exists = null;
  1084. if (!applies(IdUniquenessPolicyValue.MULTIPLE_ID))
  1085. exists = aom.findServant(the_Servant);
  1086. if (exists != null)
  1087. {
  1088. if (exists.isDeactiveted())
  1089. {
  1090. if (applies(ImplicitActivationPolicyValue.IMPLICIT_ACTIVATION))
  1091. {
  1092. checkDiscarding();
  1093. exists.setDeactivated(false);
  1094. incarnate(exists, exists.key, the_Servant, false);
  1095. }
  1096. else
  1097. throw new ServantNotActive();
  1098. }
  1099. else
  1100. return exists.object;
  1101. }
  1102. if (exists == null
  1103. && applies(ImplicitActivationPolicyValue.IMPLICIT_ACTIVATION))
  1104. {
  1105. checkDiscarding();
  1106. byte[] object_key = AOM.getFreeId();
  1107. ServantDelegateImpl delegate = new ServantDelegateImpl(the_Servant,
  1108. this, object_key);
  1109. create_and_connect(object_key, the_Servant._all_interfaces(this,
  1110. object_key)[0], delegate);
  1111. return delegate.object;
  1112. }
  1113. else
  1114. throw new ServantNotActive();
  1115. }
  1116. /**
  1117. * Incarnate in cases when request forwarding is not expected because the
  1118. * servant must be provided by the servant activator.
  1119. *
  1120. * @param x the aom entry, where the object is replaced by value, returned by
  1121. * servant activator (if not null).
  1122. *
  1123. * @param object_key the object key.
  1124. *
  1125. * @param a_servant the servant that was passed as a parameter in the
  1126. * activation method.
  1127. *
  1128. * @param use_forwarding if true, the gnuForwardRequest is throw under the
  1129. * forwarding exception (for remote client). Otherwise, the request is
  1130. * internally redirected (for local invocation).
  1131. */
  1132. private Servant incarnate(AOM.Obj x, byte[] object_key,
  1133. Servant a_servant, boolean use_forwarding
  1134. )
  1135. {
  1136. if (servant_activator != null)
  1137. {
  1138. Servant servant;
  1139. try
  1140. {
  1141. servant = servant_activator.incarnate(object_key, this);
  1142. }
  1143. catch (ForwardRequest ex)
  1144. {
  1145. if (use_forwarding)
  1146. throw new gnuForwardRequest(ex.forward_reference);
  1147. else
  1148. servant =
  1149. ForwardedServant.create((ObjectImpl) ex.forward_reference);
  1150. }
  1151. if (servant != null && x != null)
  1152. x.setServant(servant);
  1153. if (servant == null && x != null)
  1154. servant = x.servant;
  1155. return servant;
  1156. }
  1157. else if (a_servant != null)
  1158. {
  1159. x.setServant(a_servant);
  1160. return a_servant;
  1161. }
  1162. else if (x.servant != null)
  1163. {
  1164. return x.servant;
  1165. }
  1166. else if (default_servant != null)
  1167. {
  1168. x.setServant(default_servant);
  1169. return x.servant;
  1170. }
  1171. else
  1172. throw new BAD_INV_ORDER("No servant given and the servant activator not set");
  1173. }
  1174. /**
  1175. * Return the POA manager, associated with this POA.
  1176. *
  1177. * @return the associated POA manager (always available).
  1178. */
  1179. public POAManager the_POAManager()
  1180. {
  1181. return m_manager;
  1182. }
  1183. /**
  1184. * Returns the adapter activator, associated with this POA.
  1185. * The newly created POA has no activator (null would be
  1186. * returned). The ORB root POA also initially has no activator.
  1187. *
  1188. * @return tha adapter activator or null if this POA has no
  1189. * associated adapter activator.
  1190. */
  1191. public AdapterActivator the_activator()
  1192. {
  1193. return m_activator;
  1194. }
  1195. /**
  1196. * Set the adapter activator for this POA.
  1197. *
  1198. * @param an_activator the activator being set.
  1199. */
  1200. public void the_activator(AdapterActivator an_activator)
  1201. {
  1202. m_activator = an_activator;
  1203. }
  1204. /**
  1205. * The children of this POA.
  1206. *
  1207. * @return the array of all childs for this POA.
  1208. */
  1209. public POA[] the_children()
  1210. {
  1211. POA[] poas = new POA[ children.size() ];
  1212. for (int i = 0; i < poas.length; i++)
  1213. {
  1214. poas [ i ] = (POA) children.get(i);
  1215. }
  1216. return poas;
  1217. }
  1218. /**
  1219. * Return the name of this POA.
  1220. *
  1221. * @return the name of POA, relative to its parent.
  1222. */
  1223. public String the_name()
  1224. {
  1225. return name;
  1226. }
  1227. /**
  1228. * Return the parent of this POA.
  1229. *
  1230. * @return the parent POA or <code>null</code> if this is a root POA.
  1231. */
  1232. public POA the_parent()
  1233. {
  1234. return parent;
  1235. }
  1236. /** {@inheritDoc} */
  1237. public IdAssignmentPolicy create_id_assignment_policy(IdAssignmentPolicyValue a_value)
  1238. {
  1239. return new gnuIdAssignmentPolicy(a_value);
  1240. }
  1241. /** {@inheritDoc} */
  1242. public IdUniquenessPolicy create_id_uniqueness_policy(IdUniquenessPolicyValue a_value)
  1243. {
  1244. return new gnuIdUniquenessPolicy(a_value);
  1245. }
  1246. /** {@inheritDoc} */
  1247. public ImplicitActivationPolicy create_implicit_activation_policy(ImplicitActivationPolicyValue a_value)
  1248. {
  1249. return new gnuImplicitActivationPolicy(a_value);
  1250. }
  1251. /** {@inheritDoc} */
  1252. public LifespanPolicy create_lifespan_policy(LifespanPolicyValue a_value)
  1253. {
  1254. return new gnuLifespanPolicy(a_value);
  1255. }
  1256. /** {@inheritDoc} */
  1257. public RequestProcessingPolicy create_request_processing_policy(RequestProcessingPolicyValue a_value)
  1258. {
  1259. return new gnuRequestProcessingPolicy(a_value);
  1260. }
  1261. /** {@inheritDoc} */
  1262. public ServantRetentionPolicy create_servant_retention_policy(ServantRetentionPolicyValue a_value)
  1263. {
  1264. return new gnuServantRetentionPolicy(a_value);
  1265. }
  1266. /** {@inheritDoc} */
  1267. public ThreadPolicy create_thread_policy(ThreadPolicyValue a_value)
  1268. {
  1269. return new gnuThreadPolicy(a_value);
  1270. }
  1271. /**
  1272. * <p>
  1273. * Destroy this POA and all descendant POAs. The destroyed POAs can be later
  1274. * re-created via {@link AdapterActivator} or by invoking {@link #create_POA}.
  1275. * This differs from {@link PoaManagerOperations#deactivate} that does not
  1276. * allow recreation of the deactivated POAs. After deactivation, recreation is
  1277. * only possible if the POAs were later destroyed.
  1278. * </p>
  1279. * <p>
  1280. * The remote invocation on the target, belonging to the POA that is currently
  1281. * destroyed return the remote exception ({@link TRANSIENT}, minor code 4).
  1282. * </p>
  1283. *
  1284. * @param etherealize_objects if true, and POA has RETAIN policy, and the
  1285. * servant manager is available, the servant manager method
  1286. * {@link ServantActivatorOperations#etherealize} is called for each <i>active</i>
  1287. * object in the Active Object Map. This method should not try to access POA
  1288. * being destroyed. If <code>destroy</code> is called multiple times before
  1289. * the destruction completes, the etherialization should be invoked only once.
  1290. *
  1291. * @param wait_for_completion if true, the method waits till the POA being
  1292. * destroyed completes all current requests and etherialization. If false, the
  1293. * method returns immediately.
  1294. */
  1295. public void destroy(boolean etherealize_objects, boolean wait_for_completion)
  1296. {
  1297. // Notify the IOR interceptors about that the POA is destroyed.
  1298. if (m_orb.iIor != null)
  1299. m_orb.iIor.adapter_state_changed(
  1300. new ObjectReferenceTemplate[] { getReferenceTemplate() },
  1301. NON_EXISTENT.value);
  1302. if (wait_for_completion)
  1303. waitWhileRunning();
  1304. // Nofify the IOR interceptors that the POA is destroyed.
  1305. if (m_manager instanceof gnuPOAManager)
  1306. {
  1307. ((gnuPOAManager) m_manager).poaDestroyed(this);
  1308. }
  1309. // Put the brake instead of manager, preventing the subsequent
  1310. // requests.
  1311. gnuPOAManager g = new gnuPOAManager();
  1312. g.state = State.INACTIVE;
  1313. m_manager = g;
  1314. // Disconnect from parent.
  1315. if (parent instanceof gnuPOA)
  1316. {
  1317. ((gnuPOA) parent).children.remove(this);
  1318. }
  1319. unregisterFromManager();
  1320. // Disconnect from the ORB all objects, registered with this POA.
  1321. ArrayList keys = new ArrayList();
  1322. keys.addAll(aom.keySet());
  1323. byte[] key;
  1324. AOM.Obj obj;
  1325. for (int i = 0; i < keys.size(); i++)
  1326. {
  1327. key = (byte[]) keys.get(i);
  1328. obj = aom.get(key);
  1329. if (obj.poa == this)
  1330. m_orb.disconnect(obj.object);
  1331. }
  1332. m_orb.identityDestroyed(this);
  1333. if (etherealize_objects && servant_activator != null && !m_inDestruction)
  1334. {
  1335. etherealizeAll();
  1336. }
  1337. m_inDestruction = true;
  1338. POA[] ch = the_children();
  1339. for (int i = 0; i < ch.length; i++)
  1340. {
  1341. ch[i].destroy(etherealize_objects, wait_for_completion);
  1342. }
  1343. }
  1344. /**
  1345. * Destroy this POA if it has not been destroyed, destroys it.
  1346. */
  1347. protected void finalize()
  1348. throws java.lang.Throwable
  1349. {
  1350. if (!m_inDestruction)
  1351. destroy(false, false);
  1352. }
  1353. /**
  1354. * Remove self from the manager list.
  1355. */
  1356. private void unregisterFromManager()
  1357. {
  1358. if (m_manager instanceof gnuPOAManager)
  1359. {
  1360. gnuPOAManager p = (gnuPOAManager) m_manager;
  1361. p.removePOA(this);
  1362. }
  1363. }
  1364. /**
  1365. * Get the policy of the given type, associated with this POA.
  1366. *
  1367. * @param a_policy_type a type of the requested policy.
  1368. * @return a policy of the given type, applyting to this POA.
  1369. *
  1370. * @throws org.omg.CORBA.BAD_PARAM if the policy of this type has not
  1371. * been specified for this POA.
  1372. */
  1373. public Policy _get_policy(int a_policy_type)
  1374. throws org.omg.CORBA.BAD_PARAM
  1375. {
  1376. for (int i = 0; i < s_policies.length; i++)
  1377. {
  1378. if (s_policies [ i ].policy_type() == a_policy_type)
  1379. return s_policies [ i ].copy();
  1380. }
  1381. throw new BAD_PARAM("No policy type " + a_policy_type);
  1382. }
  1383. /**
  1384. * Get the copy of the policy array.
  1385. */
  1386. public Policy[] getPolicyArray()
  1387. {
  1388. Policy[] r = new Policy[ s_policies.length ];
  1389. for (int i = 0; i < s_policies.length; i++)
  1390. {
  1391. r [ i ] = s_policies [ i ].copy();
  1392. }
  1393. return r;
  1394. }
  1395. /**
  1396. * The POAs cannot be created by this method.
  1397. *
  1398. * @specnote this is also not possible in Suns jdk at least till 1.4.
  1399. *
  1400. * @throws NO_IMPLEMENT always.
  1401. */
  1402. public org.omg.CORBA.Object _set_policy_override(Policy[] policies,
  1403. SetOverrideType how
  1404. )
  1405. {
  1406. throw new NO_IMPLEMENT("Use createPOA instead.");
  1407. }
  1408. /**
  1409. * Get the ORB, where this POA is connected.
  1410. */
  1411. public ORB orb()
  1412. {
  1413. return m_orb;
  1414. }
  1415. /**
  1416. * Connect the given delegate under the given key, also calling incarnate.
  1417. */
  1418. private void create_and_connect(byte[] object_key, String repository_id,
  1419. ServantDelegateImpl delegate)
  1420. {
  1421. aom.add(delegate);
  1422. connect_to_orb(object_key, getReferenceFactory().make_object(repository_id,
  1423. object_key));
  1424. if (servant_activator != null)
  1425. incarnate(null, object_key, delegate.servant, false);
  1426. }
  1427. /**
  1428. * Check if the POA is not in a discarding mode. The activation
  1429. * operations are forbidded in discarding mode.
  1430. *
  1431. * @throws TRANSIENT if the POA is in discarding mode.
  1432. */
  1433. void checkDiscarding()
  1434. throws TRANSIENT
  1435. {
  1436. if (m_manager.get_state() == State.DISCARDING)
  1437. throw new TRANSIENT("Discarding mode", 1, CompletionStatus.COMPLETED_MAYBE);
  1438. }
  1439. /**
  1440. * Connect the given delegate object to orb.
  1441. */
  1442. protected void connect_to_orb(byte[] an_Object_Id, org.omg.CORBA.Object object)
  1443. {
  1444. if (applies(ThreadPolicyValue.SINGLE_THREAD_MODEL))
  1445. m_orb.connect_1_thread(object, toIORKey(an_Object_Id), this);
  1446. else
  1447. m_orb.connect(object, toIORKey(an_Object_Id));
  1448. }
  1449. /**
  1450. * Returns the representation of this POA tree.
  1451. */
  1452. public String toString()
  1453. {
  1454. CPStringBuilder b = new CPStringBuilder(name);
  1455. if (children.size() != 0)
  1456. {
  1457. b.append(" (");
  1458. for (int i = 0; i < children.size(); i++)
  1459. {
  1460. b.append(children.get(i));
  1461. if (i < children.size() - 2)
  1462. b.append(", ");
  1463. }
  1464. b.append(")");
  1465. }
  1466. return b.toString();
  1467. }
  1468. /**
  1469. * Check if the policy set is valid.
  1470. */
  1471. protected boolean validatePolicies(Policy[] a)
  1472. throws InvalidPolicy
  1473. {
  1474. if (applies(ServantRetentionPolicyValue.NON_RETAIN))
  1475. {
  1476. if (!applies(RequestProcessingPolicyValue.USE_DEFAULT_SERVANT) &&
  1477. !applies(RequestProcessingPolicyValue.USE_SERVANT_MANAGER)
  1478. )
  1479. {
  1480. short p = 0;
  1481. for (short i = 0; i < a.length; i++)
  1482. {
  1483. if (a [ i ].policy_type() == SERVANT_RETENTION_POLICY_ID.value)
  1484. p = i;
  1485. }
  1486. throw new InvalidPolicy("NON_RETAIN requires either " +
  1487. "USE_DEFAULT_SERVANT or USE_SERVANT_MANAGER",
  1488. p
  1489. );
  1490. }
  1491. }
  1492. return true;
  1493. }
  1494. /**
  1495. * Recursively searches for the given object in the POA tree.
  1496. */
  1497. public AOM.Obj findObject(org.omg.CORBA.Object object)
  1498. {
  1499. AOM.Obj h = aom.findObject(object);
  1500. if (h != null)
  1501. return h;
  1502. else
  1503. {
  1504. for (int i = 0; i < children.size(); i++)
  1505. {
  1506. h = ((gnuPOA) children.get(i)).findObject(object);
  1507. if (h != null)
  1508. return h;
  1509. }
  1510. }
  1511. return h;
  1512. }
  1513. /**
  1514. * Recursively searches for the given key in the POA tree.
  1515. * @param ior_key the key, ecapsulating both object
  1516. * and poa ids.
  1517. * @return
  1518. */
  1519. public AOM.Obj findKey(byte[] object_id, byte[] poa_id)
  1520. {
  1521. AOM.Obj h = null;
  1522. if (Arrays.equals(poa_id, id()))
  1523. h = aom.get(object_id);
  1524. if (h != null)
  1525. return h;
  1526. else
  1527. {
  1528. for (int i = 0; i < children.size(); i++)
  1529. {
  1530. h = ((gnuPOA) children.get(i)).findKey(object_id, poa_id);
  1531. if (h != null)
  1532. return h;
  1533. }
  1534. }
  1535. return h;
  1536. }
  1537. /**
  1538. * Parses the given key, extracts poa and object id and searches
  1539. * for such reference.
  1540. */
  1541. public AOM.Obj findIorKey(byte[] ior_key)
  1542. {
  1543. BufferredCdrInput in = new BufferredCdrInput(ior_key);
  1544. int signature = in.read_long();
  1545. if (signature != SIGNATURE)
  1546. return null;
  1547. byte[] id = in.read_sequence();
  1548. byte[] poa = in.read_sequence();
  1549. return findKey(id, poa);
  1550. }
  1551. /**
  1552. * Converts the object Id into the IOR key. IOR key must be
  1553. * unique in the scope of the ORB, and Ids only in the scope of POA.
  1554. * Hence the IOR key includes the POA identifiers.
  1555. */
  1556. public byte[] toIORKey(byte[] object_id)
  1557. {
  1558. BufferedCdrOutput buffer = new BufferedCdrOutput();
  1559. buffer.write_long(SIGNATURE);
  1560. buffer.write_sequence(object_id);
  1561. buffer.write_sequence(id());
  1562. return buffer.buffer.toByteArray();
  1563. }
  1564. /**
  1565. * Extracts the object id from the ior key.
  1566. *
  1567. * @param ior_key
  1568. *
  1569. * @return the encapsulated object ior key or null if
  1570. * this ior key either refers a different POA or encoding signature
  1571. * mismatch.
  1572. */
  1573. public byte[] idFormIor(byte[] ior_key)
  1574. {
  1575. BufferredCdrInput in = new BufferredCdrInput(ior_key);
  1576. int signature = in.read_long();
  1577. if (signature != SIGNATURE)
  1578. return null;
  1579. byte[] object_id = in.read_sequence();
  1580. byte[] poa_id = in.read_sequence();
  1581. if (Arrays.equals(poa_id, id()))
  1582. return object_id;
  1583. else
  1584. return null;
  1585. }
  1586. /**
  1587. * Recursively searches for the given servant in the POA tree.
  1588. */
  1589. public AOM.Obj findServant(Servant servant)
  1590. {
  1591. AOM.Obj h = aom.findServant(servant);
  1592. if (h != null)
  1593. return h;
  1594. else
  1595. {
  1596. for (int i = 0; i < children.size(); i++)
  1597. {
  1598. h = ((gnuPOA) children.get(i)).findServant(servant);
  1599. if (h != null)
  1600. return h;
  1601. }
  1602. }
  1603. return h;
  1604. }
  1605. /**
  1606. * Get the object reference template of this POA.
  1607. * Instantiate a singleton instance, if required.
  1608. */
  1609. public ObjectReferenceTemplate getReferenceTemplate()
  1610. {
  1611. if (refTemplate == null)
  1612. refTemplate = new RefTemplate();
  1613. return refTemplate;
  1614. }
  1615. public ObjectReferenceFactory getReferenceFactory()
  1616. {
  1617. return m_object_factory;
  1618. }
  1619. public void setReferenceFactory(ObjectReferenceFactory factory)
  1620. {
  1621. m_object_factory = factory;
  1622. }
  1623. /**
  1624. * Create the object (needed by the factory interface).
  1625. */
  1626. public Object make_object(String a_repository_id, byte[] an_object_id)
  1627. {
  1628. AOM.Obj existing = aom.get(an_object_id);
  1629. // The object may already exist. In this case, it is just returned.
  1630. if (existing != null && existing.object != null)
  1631. return existing.object;
  1632. else
  1633. {
  1634. return new gnuServantObject(new String[] { a_repository_id },
  1635. an_object_id, this, m_orb);
  1636. }
  1637. }
  1638. /**
  1639. * Required by object reference factory ops.
  1640. */
  1641. public String[] _truncatable_ids()
  1642. {
  1643. return ref_template_ids;
  1644. }
  1645. }