duke@1: /* ohair@158: * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@158: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@158: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@158: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@158: * or visit www.oracle.com if you need additional information or have any ohair@158: * questions. duke@1: */ duke@1: package org.omg.PortableServer; duke@1: duke@1: import org.omg.CORBA.ORB; duke@1: import org.omg.PortableServer.portable.Delegate; duke@1: duke@1: /** duke@1: * Defines the native Servant type. In Java, the duke@1: * Servant type is mapped to the Java duke@1: * org.omg.PortableServer.Servant class. duke@1: * It serves as the base class for all POA servant duke@1: * implementations and provides a number of methods that may duke@1: * be invoked by the application programmer, as well as methods duke@1: * which are invoked by the POA itself and may be overridden by duke@1: * the user to control aspects of servant behavior. duke@1: * Based on IDL to Java spec. (CORBA V2.3.1) ptc/00-01-08.pdf. duke@1: */ duke@1: duke@1: abstract public class Servant { duke@1: duke@1: private transient Delegate _delegate = null; duke@1: /** duke@1: * Gets the ORB vendor-specific implementation of duke@1: * PortableServer::Servant. duke@1: * @return _delegate the ORB vendor-specific duke@1: * implementation of PortableServer::Servant. duke@1: */ duke@1: final public Delegate _get_delegate() { duke@1: if (_delegate == null) { duke@1: throw duke@1: new duke@1: org.omg.CORBA.BAD_INV_ORDER duke@1: ("The Servant has not been associated with an ORB instance"); duke@1: } duke@1: return _delegate; duke@1: } duke@1: duke@1: /** duke@1: * Supports the Java ORB portability duke@1: * interfaces by providing a method for classes that support duke@1: * ORB portability through delegation to set their delegate. duke@1: * @param delegate ORB vendor-specific implementation of duke@1: * the PortableServer::Servant. duke@1: */ duke@1: final public void _set_delegate(Delegate delegate) { duke@1: _delegate = delegate; duke@1: } duke@1: duke@1: /** duke@1: * Allows the servant to obtain the object reference for duke@1: * the target CORBA object it is incarnating for that request. duke@1: * @return this_object Object reference duke@1: * associated with the request. duke@1: */ duke@1: final public org.omg.CORBA.Object _this_object() { duke@1: return _get_delegate().this_object(this); duke@1: } duke@1: duke@1: /** duke@1: * Allows the servant to obtain the object reference for duke@1: * the target CORBA Object it is incarnating for that request. duke@1: * @param orb ORB with which the servant is associated. duke@1: * @return _this_object reference associated with the request. duke@1: */ duke@1: final public org.omg.CORBA.Object _this_object(ORB orb) { duke@1: try { duke@1: ((org.omg.CORBA_2_3.ORB)orb).set_delegate(this); duke@1: } duke@1: catch(ClassCastException e) { duke@1: throw duke@1: new duke@1: org.omg.CORBA.BAD_PARAM duke@1: ("POA Servant requires an instance of org.omg.CORBA_2_3.ORB"); duke@1: } duke@1: return _this_object(); duke@1: } duke@1: duke@1: /** duke@1: * Returns the instance of the ORB duke@1: * currently associated with the Servant (convenience method). duke@1: * @return orb the instance of the ORB currently duke@1: * associated with the Servant. duke@1: */ duke@1: final public ORB _orb() { duke@1: return _get_delegate().orb(this); duke@1: } duke@1: duke@1: /** duke@1: * Allows easy execution of common methods, equivalent to duke@1: * PortableServer::Current:get_POA. duke@1: * @return poa POA associated with the servant. duke@1: */ duke@1: final public POA _poa() { duke@1: return _get_delegate().poa(this); duke@1: } duke@1: duke@1: /** duke@1: * Allows easy execution of duke@1: * common methods, equivalent duke@1: * to calling PortableServer::Current::get_object_id. duke@1: * @return object_id the Object ID associated duke@1: * with this servant. duke@1: */ duke@1: final public byte[] _object_id() { duke@1: return _get_delegate().object_id(this); duke@1: } duke@1: duke@1: /** duke@1: * Returns the duke@1: * root POA from the ORB instance associated with the servant. duke@1: * Subclasses may override this method to return a different POA. duke@1: * @return default_POA the POA associated with the duke@1: * Servant. duke@1: */ duke@1: public POA _default_POA() { duke@1: return _get_delegate().default_POA(this); duke@1: } duke@1: duke@1: /** duke@1: * Checks to see if the specified repository_id is present duke@1: * on the list returned by _all_interfaces() or is the duke@1: * repository_id for the generic CORBA Object. duke@1: * @param repository_id the repository_id duke@1: * to be checked in the repository list or against the id duke@1: * of generic CORBA objects. duke@1: * @return is_a boolean indicating whether the specified duke@1: * repository_id is duke@1: * in the repository list or is same as a generic CORBA duke@1: * object. duke@1: */ duke@1: public boolean _is_a(String repository_id) { duke@1: return _get_delegate().is_a(this, repository_id); duke@1: } duke@1: duke@1: /** duke@1: * Checks for the existence of an duke@1: * Object. duke@1: * The Servant provides a default implementation of duke@1: * _non_existent() that can be overridden by derived servants. duke@1: * @return non_existent true if that object does duke@1: * not exist, false otherwise. duke@1: */ duke@1: public boolean _non_existent() { duke@1: return _get_delegate().non_existent(this); duke@1: } duke@1: duke@1: // Ken and Simon will ask about editorial changes duke@1: // needed in IDL to Java mapping to the following duke@1: // signature. duke@1: /** duke@1: * Returns an object in the Interface Repository duke@1: * which provides type information that may be useful to a program. duke@1: * Servant provides a default implementation of duke@1: * _get_interface() duke@1: * that can be overridden by derived servants if the default duke@1: * behavior is not adequate. duke@1: * @return get_interface type information that corresponds to this servant. duke@1: */ duke@1: /* duke@1: public org.omg.CORBA.Object _get_interface() { duke@1: return _get_delegate().get_interface(this); duke@1: } duke@1: */ duke@1: duke@1: // _get_interface_def() replaces the _get_interface() method duke@1: duke@1: /** duke@1: * Returns an InterfaceDef object as a duke@1: * CORBA::Object that defines the runtime type of the duke@1: * CORBA::Object implemented by the Servant. duke@1: * The invoker of _get_interface_def duke@1: * must narrow the result to an InterfaceDef in order duke@1: * to use it. duke@1: *

This default implementation of _get_interface_def() duke@1: * can be overridden duke@1: * by derived servants if the default behavior is not adequate. duke@1: * As defined in the CORBA 2.3.1 specification, section 11.3.1, the duke@1: * default behavior of _get_interface_def() is to use duke@1: * the most derived duke@1: * interface of a static servant or the most derived interface retrieved duke@1: * from a dynamic servant to obtain the InterfaceDef. duke@1: * This behavior must duke@1: * be supported by the Delegate that implements the duke@1: * Servant. duke@1: * @return get_interface_def an InterfaceDef duke@1: * object as a duke@1: * CORBA::Object that defines the runtime type of the duke@1: * CORBA::Object implemented by the Servant. duke@1: */ duke@1: public org.omg.CORBA.Object _get_interface_def() duke@1: { duke@1: // First try to call the delegate implementation class's duke@1: // "Object get_interface_def(..)" method (will work for ORBs duke@1: // whose delegates implement this method). duke@1: // Else call the delegate implementation class's duke@1: // "InterfaceDef get_interface(..)" method using reflection duke@1: // (will work for ORBs that were built using an older version duke@1: // of the Delegate interface with a get_interface method duke@1: // but not a get_interface_def method). duke@1: duke@1: org.omg.PortableServer.portable.Delegate delegate = _get_delegate(); duke@1: try { duke@1: // If the ORB's delegate class does not implement duke@1: // "Object get_interface_def(..)", this will throw duke@1: // an AbstractMethodError. duke@1: return delegate.get_interface_def(this); duke@1: } catch( AbstractMethodError aex ) { duke@1: // Call "InterfaceDef get_interface(..)" method using reflection. duke@1: try { duke@1: Class[] argc = { org.omg.PortableServer.Servant.class }; duke@1: java.lang.reflect.Method meth = duke@1: delegate.getClass().getMethod("get_interface", argc); duke@1: Object[] argx = { this }; duke@1: return (org.omg.CORBA.Object)meth.invoke(delegate, argx); duke@1: } catch( java.lang.reflect.InvocationTargetException exs ) { duke@1: Throwable t = exs.getTargetException(); duke@1: if (t instanceof Error) { duke@1: throw (Error) t; duke@1: } else if (t instanceof RuntimeException) { duke@1: throw (RuntimeException) t; duke@1: } else { duke@1: throw new org.omg.CORBA.NO_IMPLEMENT(); duke@1: } duke@1: } catch( RuntimeException rex ) { duke@1: throw rex; duke@1: } catch( Exception exr ) { duke@1: throw new org.omg.CORBA.NO_IMPLEMENT(); duke@1: } duke@1: } duke@1: } duke@1: duke@1: // methods for which the user must provide an duke@1: // implementation duke@1: /** duke@1: * Used by the ORB to obtain complete type duke@1: * information from the servant. duke@1: * @param poa POA with which the servant is associated. duke@1: * @param objectId is the id corresponding to the object duke@1: * associated with this servant. duke@1: * @return list of type information for the object. duke@1: */ duke@1: abstract public String[] _all_interfaces( POA poa, byte[] objectId); duke@1: }