1 /* Servant.java --
2    Copyright (C) 2005 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 
39 package org.omg.PortableServer;
40 
41 import org.omg.CORBA.BAD_OPERATION;
42 import org.omg.CORBA.NO_IMPLEMENT;
43 import org.omg.CORBA.OBJECT_NOT_EXIST;
44 import org.omg.CORBA.ORB;
45 import org.omg.PortableServer.POAPackage.ServantNotActive;
46 import org.omg.PortableServer.POAPackage.WrongPolicy;
47 import org.omg.PortableServer.portable.Delegate;
48 
49 import gnu.CORBA.Minor;
50 import gnu.CORBA.Poa.ORB_1_4;
51 import gnu.CORBA.Poa.gnuPOA;
52 
53 /**
54  * <p>
55  * The servant is responsible for handling the method invocation on the
56  * target object. It can be one servant per object, or the same servant can
57  * support several (possibly all) objects, associated with the given POA.
58  * </p> <p>
59  * Till JDK 1.3 inclusive, a typical IDL to java compiler generates an
60  * implementation base (name pattern _*ImplBase.java) that is derived from the
61  * {@link org.omg.CORBA.portable.ObjectImpl}. Since JDK 1.4 the implementation
62  * base is derived from the Servant, also having a different name pattern
63  * (*POA.java). This suffix may be confusing, as the servant itself is
64  * <i>not</i> POA nor it is derived from it.
65  * </p><p>
66  * In both cases, the implementation base also inherits an interface, containing
67  * definitions of the application specific methods. The application programmer
68  * writes a child of the implementation base, implementing these methods
69  * for the application-specific functionality. The ObjectImpl is connected
70  * directly to the ORB. The Servant is connected to POA that can be obtained
71  * from the ORB.
72  * </p><p>
73  * If the servant is connected to more than one object, the exact object
74  * being currently served can be identified with {@link #_object_id}.
75  * </p><p>
76  * The derivativ of Servant, being directly connected to serve requests,
77  * must inherit either from {@link org.omg.CORBA.portable.InvokeHandler}
78  * or from {@link org.omg.PortableServer.DynamicImplementation}).
79  * </p><p>
80  * The Servant type is a CORBA <code>native</code> type.
81  * </p>
82  *
83  * @see POA.servant_to_reference(Servant)
84  *
85  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
86  */
87 public abstract class Servant
88 {
89   /**
90    * The delegate, where calls to some Servant methods are forwarded.
91    */
92   private Delegate delegate;
93 
94   /**
95    * Get the repository ids of all interfaces, supported by the
96    * CORBA object, identified by the passed Id. In the typical code the
97    * passed parameters are ignored, returning an array of repository ids,
98    * supported by the servant implementation.
99    *
100    * @param poa a POA of the given object.
101    * @param object_ID the object Id of the given object.
102    *
103    * @return an array, containing the repository ids.
104    */
_all_interfaces(POA poa, byte[] object_ID)105   public abstract String[] _all_interfaces(POA poa, byte[] object_ID);
106 
107   /**
108    * Get the delegate, where calls to some Servant methods are forwarded.
109    */
_get_delegate()110   public final Delegate _get_delegate()
111   {
112     return delegate;
113   }
114 
115   /**
116   * Get the interface repository definition <code>InterfaceDef</code> for this
117   * Object. By default, forwards request to the delegate.
118   *
119   * @specnote The interface repository is officially not implemented up till
120   * JDK 1.5 inclusive. The delegate throws NO_IMPLEMENT, always.
121   */
_get_interface_def()122   public org.omg.CORBA.Object _get_interface_def()
123   {
124     throw new NO_IMPLEMENT();
125   }
126 
127   /**
128   * Checks if the passed servant is an instance of the given CORBA IDL type.
129   * By default, forwards the requet to the delegate.
130   *
131   * @param a_servant a servant to check.
132   * @param an_id a repository ID, representing an IDL type for that the
133   * servant must be checked.
134   *
135   * @return true if the servant is an instance of the given type, false
136   * otherwise.
137   */
_is_a(String repository_id)138   public boolean _is_a(String repository_id)
139   {
140     return delegate.is_a(this, repository_id);
141   }
142 
143   /**
144    * Determines if the server object for this reference has already
145    * been destroyed. By default, forwards request to the delegate.
146    *
147    * @return true if the object has been destroyed, false otherwise.
148    */
_non_existent()149   public boolean _non_existent()
150   {
151     return delegate.non_existent(this);
152   }
153 
154   /**
155   * Returns the ORB that is directly associated with the given servant.
156   * In this implementation, the method is overridden to return
157   */
_orb()158   public final ORB _orb()
159   {
160     return delegate.orb(this);
161   }
162 
163   /**
164    * Returns the root POA of the ORB instance, associated with this servant.
165    * It is the same POA that would be returned by resolving the initial
166    * reference "RootPOA" for that orb. By default, forwards request to the
167    * delegate.
168    *
169    * @see ORB.resolve_initial_references
170    */
_default_POA()171   public POA _default_POA()
172   {
173     return delegate == null ? null : delegate.default_POA(this);
174   }
175 
176   /**
177   * Return the invocation target object identifier as a byte array.
178   * This is typically used when the same servant serves multiple objects,
179   * and the object id can encapsulated the whole description of the
180   * object.
181   *
182   * This method returns correct values even when the same
183   * servant serves several objects in parallel threads. The ORB maintains the
184   * thread to invocation data map for all calls that are currently being
185   * processed.
186   */
_object_id()187   public final byte[] _object_id()
188   {
189     if (delegate != null)
190       return delegate.object_id(this);
191     else
192       throw new OBJECT_NOT_EXIST();
193   }
194 
195   /**
196   * Get POA that is directly associated with the given servant.
197   * By default, forwards request to the delegate.
198   */
_poa()199   public final POA _poa()
200   {
201     return delegate.poa(this);
202   }
203 
204   /**
205   * Set the delegate for this servant.
206   */
_set_delegate(Delegate a_delegate)207   public final void _set_delegate(Delegate a_delegate)
208   {
209     delegate = a_delegate;
210   }
211 
212   /**
213   * Obtains the CORBA object reference that is a current invocation target for
214   * the given servant. This is important when the same servant serves
215   * multiple objects. If the servant is not yet connected to the passed
216   * orb, the method will try to connect it to that orb on POA, returned
217   * by the method {@link _default_POA}. That method can be overridden to
218   * get poa where the object must be automatically connected when
219   * calling this method.
220   *
221   * @param an_orb the ORB with relate to that the object is requested.
222   */
_this_object(ORB an_orb)223   public final org.omg.CORBA.Object _this_object(ORB an_orb)
224   {
225     if (delegate != null)
226       return delegate.this_object(this);
227     else
228       {
229         if (an_orb instanceof ORB_1_4)
230           {
231             ORB_1_4 m_orb = (ORB_1_4) an_orb;
232 
233             gnuPOA dp = (gnuPOA) _default_POA();
234             if (dp == null)
235               dp = m_orb.rootPOA;
236 
237             try
238               {
239                 return dp.servant_to_reference(this);
240               }
241             catch (WrongPolicy unexp)
242               {
243                 BAD_OPERATION bad = new BAD_OPERATION();
244                 bad.minor = Minor.Policy;
245                 bad.initCause(unexp);
246                 throw bad;
247               }
248             catch (ServantNotActive ex)
249               {
250                 try
251                   {
252                     return dp.id_to_reference(dp.activate_object(this));
253                   }
254                 catch (Exception unexp)
255                   {
256                     unexp.initCause(ex);
257 
258                     BAD_OPERATION bad = new BAD_OPERATION();
259                     bad.minor = Minor.Activation;
260                     bad.initCause(unexp);
261                     throw bad;
262                   }
263               }
264           }
265       }
266     throw new OBJECT_NOT_EXIST();
267   }
268 
269   /**
270   * Obtains the CORBA object reference that is a current invocation target for
271   * the given servant. This is important when the same servant serves
272   * multiple objects. This method required the servant to be connected
273   * to a single orb, and a delegate set.
274   *
275   * This method returns correct values even when the same
276   * servant serves several objects in parallel threads. The ORB maintains the
277   * thread to invocation data map for all calls that are currently being
278   * processed.
279   */
_this_object()280   public final org.omg.CORBA.Object _this_object()
281   {
282     if (delegate != null)
283       return _this_object(_orb());
284     else
285       {
286         POA def = _default_POA();
287         if (def instanceof gnuPOA)
288           return _this_object(((gnuPOA) def).orb());
289       }
290     throw new OBJECT_NOT_EXIST();
291   }
292 }