001/* Servant.java --
002   Copyright (C) 2005 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package org.omg.PortableServer;
040
041import org.omg.CORBA.BAD_OPERATION;
042import org.omg.CORBA.BAD_INV_ORDER;
043import org.omg.CORBA.NO_IMPLEMENT;
044import org.omg.CORBA.OBJECT_NOT_EXIST;
045import org.omg.CORBA.ORB;
046import org.omg.PortableServer.POAPackage.ServantNotActive;
047import org.omg.PortableServer.POAPackage.WrongPolicy;
048import org.omg.PortableServer.portable.Delegate;
049
050import gnu.CORBA.Minor;
051import gnu.CORBA.Poa.ORB_1_4;
052import gnu.CORBA.Poa.gnuPOA;
053
054/**
055 * <p>
056 * The servant is responsible for handling the method invocation on the
057 * target object. It can be one servant per object, or the same servant can
058 * support several (possibly all) objects, associated with the given POA.
059 * </p> <p>
060 * Till JDK 1.3 inclusive, a typical IDL to java compiler generates an
061 * implementation base (name pattern _*ImplBase.java) that is derived from the
062 * {@link org.omg.CORBA.portable.ObjectImpl}. Since JDK 1.4 the implementation
063 * base is derived from the Servant, also having a different name pattern
064 * (*POA.java). This suffix may be confusing, as the servant itself is
065 * <i>not</i> POA nor it is derived from it.
066 * </p><p>
067 * In both cases, the implementation base also inherits an interface, containing
068 * definitions of the application specific methods. The application programmer
069 * writes a child of the implementation base, implementing these methods
070 * for the application-specific functionality. The ObjectImpl is connected
071 * directly to the ORB. The Servant is connected to POA that can be obtained
072 * from the ORB.
073 * </p><p>
074 * If the servant is connected to more than one object, the exact object
075 * being currently served can be identified with {@link #_object_id}.
076 * </p><p>
077 * The derivativ of Servant, being directly connected to serve requests,
078 * must inherit either from {@link org.omg.CORBA.portable.InvokeHandler}
079 * or from {@link org.omg.PortableServer.DynamicImplementation}).
080 * </p><p>
081 * The Servant type is a CORBA <code>native</code> type.
082 * </p>
083 *
084 * @see POA#servant_to_reference(Servant)
085 *
086 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
087 */
088public abstract class Servant
089{
090  /**
091   * The delegate, where calls to some Servant methods are forwarded.
092   */
093  private Delegate delegate;
094
095  /**
096   * Get the repository ids of all interfaces, supported by the
097   * CORBA object, identified by the passed Id. In the typical code the
098   * passed parameters are ignored, returning an array of repository ids,
099   * supported by the servant implementation.
100   *
101   * @param poa a POA of the given object.
102   * @param object_ID the object Id of the given object.
103   *
104   * @return an array, containing the repository ids.
105   */
106  public abstract String[] _all_interfaces(POA poa, byte[] object_ID);
107
108  /**
109   * Get the delegate, where calls to some Servant methods are forwarded.
110   */
111  public final Delegate _get_delegate()
112  {
113    if (delegate == null) {
114      throw new BAD_INV_ORDER
115        ("The Servant has not been associated with an ORBinstance");
116    }
117    return delegate;
118  }
119
120  /**
121  * Get the interface repository definition <code>InterfaceDef</code> for this
122  * Object. By default, forwards request to the delegate.
123  *
124  * @specnote The interface repository is officially not implemented up till
125  * JDK 1.5 inclusive. The delegate throws NO_IMPLEMENT, always.
126  */
127  public org.omg.CORBA.Object _get_interface_def()
128  {
129    throw new NO_IMPLEMENT();
130  }
131
132  /**
133  * Checks if the passed servant is an instance of the given CORBA IDL type.
134  * By default, forwards the requet to the delegate.
135  *
136  * @param repository_id a repository ID, representing an IDL type for that the
137  * servant must be checked.
138  *
139  * @return true if the servant is an instance of the given type, false
140  * otherwise.
141  */
142  public boolean _is_a(String repository_id)
143  {
144    return delegate.is_a(this, repository_id);
145  }
146
147  /**
148   * Determines if the server object for this reference has already
149   * been destroyed. By default, forwards request to the delegate.
150   *
151   * @return true if the object has been destroyed, false otherwise.
152   */
153  public boolean _non_existent()
154  {
155    return delegate.non_existent(this);
156  }
157
158  /**
159  * Returns the ORB that is directly associated with the given servant.
160  * In this implementation, the method is overridden to return
161  */
162  public final ORB _orb()
163  {
164    return delegate.orb(this);
165  }
166
167  /**
168   * Returns the root POA of the ORB instance, associated with this servant.
169   * It is the same POA that would be returned by resolving the initial
170   * reference "RootPOA" for that orb. By default, forwards request to the
171   * delegate.
172   *
173   * @see ORB#resolve_initial_references
174   */
175  public POA _default_POA()
176  {
177    return delegate == null ? null : delegate.default_POA(this);
178  }
179
180  /**
181  * Return the invocation target object identifier as a byte array.
182  * This is typically used when the same servant serves multiple objects,
183  * and the object id can encapsulated the whole description of the
184  * object.
185  *
186  * This method returns correct values even when the same
187  * servant serves several objects in parallel threads. The ORB maintains the
188  * thread to invocation data map for all calls that are currently being
189  * processed.
190  */
191  public final byte[] _object_id()
192  {
193    if (delegate != null)
194      return delegate.object_id(this);
195    else
196      throw new OBJECT_NOT_EXIST();
197  }
198
199  /**
200  * Get POA that is directly associated with the given servant.
201  * By default, forwards request to the delegate.
202  */
203  public final POA _poa()
204  {
205    return delegate.poa(this);
206  }
207
208  /**
209  * Set the delegate for this servant.
210  */
211  public final void _set_delegate(Delegate a_delegate)
212  {
213    delegate = a_delegate;
214  }
215
216  /**
217  * Obtains the CORBA object reference that is a current invocation target for
218  * the given servant. This is important when the same servant serves
219  * multiple objects. If the servant is not yet connected to the passed
220  * orb, the method will try to connect it to that orb on POA, returned
221  * by the method {@link #_default_POA}. That method can be overridden to
222  * get poa where the object must be automatically connected when
223  * calling this method.
224  *
225  * @param an_orb the ORB with relate to that the object is requested.
226  */
227  public final org.omg.CORBA.Object _this_object(ORB an_orb)
228  {
229    if (delegate != null)
230      return delegate.this_object(this);
231    else
232      {
233        if (an_orb instanceof ORB_1_4)
234          {
235            ORB_1_4 m_orb = (ORB_1_4) an_orb;
236
237            gnuPOA dp = (gnuPOA) _default_POA();
238            if (dp == null)
239              dp = m_orb.rootPOA;
240
241            try
242              {
243                return dp.servant_to_reference(this);
244              }
245            catch (WrongPolicy unexp)
246              {
247                BAD_OPERATION bad = new BAD_OPERATION();
248                bad.minor = Minor.Policy;
249                bad.initCause(unexp);
250                throw bad;
251              }
252            catch (ServantNotActive ex)
253              {
254                try
255                  {
256                    return dp.id_to_reference(dp.activate_object(this));
257                  }
258                catch (Exception unexp)
259                  {
260                    unexp.initCause(ex);
261
262                    BAD_OPERATION bad = new BAD_OPERATION();
263                    bad.minor = Minor.Activation;
264                    bad.initCause(unexp);
265                    throw bad;
266                  }
267              }
268          }
269      }
270    throw new OBJECT_NOT_EXIST();
271  }
272
273  /**
274  * Obtains the CORBA object reference that is a current invocation target for
275  * the given servant. This is important when the same servant serves
276  * multiple objects. This method required the servant to be connected
277  * to a single orb, and a delegate set.
278  *
279  * This method returns correct values even when the same
280  * servant serves several objects in parallel threads. The ORB maintains the
281  * thread to invocation data map for all calls that are currently being
282  * processed.
283  */
284  public final org.omg.CORBA.Object _this_object()
285  {
286    if (delegate != null)
287      return _this_object(_orb());
288    else
289      {
290        POA def = _default_POA();
291        if (def instanceof gnuPOA)
292          return _this_object(((gnuPOA) def).orb());
293      }
294    throw new OBJECT_NOT_EXIST();
295  }
296}