Source for org.omg.PortableServer.Servant

   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:    */
 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:    */
 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:   */
 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 repository_id a repository ID, representing an IDL type for that the
 132:   * servant must be checked.
 133:   *
 134:   * @return true if the servant is an instance of the given type, false
 135:   * otherwise.
 136:   */
 137:   public boolean _is_a(String repository_id)
 138:   {
 139:     return delegate.is_a(this, repository_id);
 140:   }
 141: 
 142:   /**
 143:    * Determines if the server object for this reference has already
 144:    * been destroyed. By default, forwards request to the delegate.
 145:    *
 146:    * @return true if the object has been destroyed, false otherwise.
 147:    */
 148:   public boolean _non_existent()
 149:   {
 150:     return delegate.non_existent(this);
 151:   }
 152: 
 153:   /**
 154:   * Returns the ORB that is directly associated with the given servant.
 155:   * In this implementation, the method is overridden to return
 156:   */
 157:   public final ORB _orb()
 158:   {
 159:     return delegate.orb(this);
 160:   }
 161: 
 162:   /**
 163:    * Returns the root POA of the ORB instance, associated with this servant.
 164:    * It is the same POA that would be returned by resolving the initial
 165:    * reference "RootPOA" for that orb. By default, forwards request to the
 166:    * delegate.
 167:    *
 168:    * @see ORB#resolve_initial_references
 169:    */
 170:   public POA _default_POA()
 171:   {
 172:     return delegate == null ? null : delegate.default_POA(this);
 173:   }
 174: 
 175:   /**
 176:   * Return the invocation target object identifier as a byte array.
 177:   * This is typically used when the same servant serves multiple objects,
 178:   * and the object id can encapsulated the whole description of the
 179:   * object.
 180:   *
 181:   * This method returns correct values even when the same
 182:   * servant serves several objects in parallel threads. The ORB maintains the
 183:   * thread to invocation data map for all calls that are currently being
 184:   * processed.
 185:   */
 186:   public final byte[] _object_id()
 187:   {
 188:     if (delegate != null)
 189:       return delegate.object_id(this);
 190:     else
 191:       throw new OBJECT_NOT_EXIST();
 192:   }
 193: 
 194:   /**
 195:   * Get POA that is directly associated with the given servant.
 196:   * By default, forwards request to the delegate.
 197:   */
 198:   public final POA _poa()
 199:   {
 200:     return delegate.poa(this);
 201:   }
 202: 
 203:   /**
 204:   * Set the delegate for this servant.
 205:   */
 206:   public final void _set_delegate(Delegate a_delegate)
 207:   {
 208:     delegate = a_delegate;
 209:   }
 210: 
 211:   /**
 212:   * Obtains the CORBA object reference that is a current invocation target for
 213:   * the given servant. This is important when the same servant serves
 214:   * multiple objects. If the servant is not yet connected to the passed
 215:   * orb, the method will try to connect it to that orb on POA, returned
 216:   * by the method {@link #_default_POA}. That method can be overridden to
 217:   * get poa where the object must be automatically connected when
 218:   * calling this method.
 219:   *
 220:   * @param an_orb the ORB with relate to that the object is requested.
 221:   */
 222:   public final org.omg.CORBA.Object _this_object(ORB an_orb)
 223:   {
 224:     if (delegate != null)
 225:       return delegate.this_object(this);
 226:     else
 227:       {
 228:         if (an_orb instanceof ORB_1_4)
 229:           {
 230:             ORB_1_4 m_orb = (ORB_1_4) an_orb;
 231: 
 232:             gnuPOA dp = (gnuPOA) _default_POA();
 233:             if (dp == null)
 234:               dp = m_orb.rootPOA;
 235: 
 236:             try
 237:               {
 238:                 return dp.servant_to_reference(this);
 239:               }
 240:             catch (WrongPolicy unexp)
 241:               {
 242:                 BAD_OPERATION bad = new BAD_OPERATION();
 243:                 bad.minor = Minor.Policy;
 244:                 bad.initCause(unexp);
 245:                 throw bad;
 246:               }
 247:             catch (ServantNotActive ex)
 248:               {
 249:                 try
 250:                   {
 251:                     return dp.id_to_reference(dp.activate_object(this));
 252:                   }
 253:                 catch (Exception unexp)
 254:                   {
 255:                     unexp.initCause(ex);
 256: 
 257:                     BAD_OPERATION bad = new BAD_OPERATION();
 258:                     bad.minor = Minor.Activation;
 259:                     bad.initCause(unexp);
 260:                     throw bad;
 261:                   }
 262:               }
 263:           }
 264:       }
 265:     throw new OBJECT_NOT_EXIST();
 266:   }
 267: 
 268:   /**
 269:   * Obtains the CORBA object reference that is a current invocation target for
 270:   * the given servant. This is important when the same servant serves
 271:   * multiple objects. This method required the servant to be connected
 272:   * to a single orb, and a delegate set.
 273:   *
 274:   * This method returns correct values even when the same
 275:   * servant serves several objects in parallel threads. The ORB maintains the
 276:   * thread to invocation data map for all calls that are currently being
 277:   * processed.
 278:   */
 279:   public final org.omg.CORBA.Object _this_object()
 280:   {
 281:     if (delegate != null)
 282:       return _this_object(_orb());
 283:     else
 284:       {
 285:         POA def = _default_POA();
 286:         if (def instanceof gnuPOA)
 287:           return _this_object(((gnuPOA) def).orb());
 288:       }
 289:     throw new OBJECT_NOT_EXIST();
 290:   }