001: package org.jacorb.orb;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import org.omg.CORBA.BAD_PARAM;
024: import org.omg.CORBA.ORB;
025: import org.omg.CORBA.Repository;
026: import org.omg.CORBA.RepositoryHelper;
027: import org.omg.CORBA.ORBPackage.InvalidName;
028: import org.omg.PortableServer.POA;
029: import org.omg.PortableServer.POAHelper;
030: import org.omg.PortableServer.CurrentPackage.NoContext;
031: import org.omg.PortableServer.POAPackage.ServantNotActive;
032: import org.omg.PortableServer.POAPackage.WrongPolicy;
033:
034: /**
035: * JacORB-specific implementation of PortableServer.Servant
036: *
037: * $Id: ServantDelegate.java,v 1.28 2007/02/15 12:56:06 andre.spiegel Exp $
038: */
039: public class ServantDelegate implements
040: org.omg.PortableServer.portable.Delegate {
041: private final ORB orb;
042: private Repository ir = null;
043: private org.omg.PortableServer.Current _current = null;
044: private POA poa = null;
045:
046: ServantDelegate(org.jacorb.orb.ORB orb) {
047: this .orb = orb;
048: }
049:
050: /**
051: * Must be checked for every invocation (cf. Lang. Mapping p. 1-89)
052: */
053:
054: private final void check() {
055: if (orb == null) {
056: throw new org.omg.CORBA.BAD_INV_ORDER(
057: "The Servant has not been associated with an ORB instance");
058: }
059: }
060:
061: final public org.omg.CORBA.Object this _object(
062: org.omg.PortableServer.Servant self) {
063: check();
064: try {
065: poa = poa(self);
066: } catch (org.omg.CORBA.OBJ_ADAPTER e) {
067: // Use servants default POA. Operation may be re-implemented
068: // by servant implementation.
069:
070: poa = self._default_POA();
071: }
072:
073: if (poa == null) {
074: throw new org.omg.CORBA.OBJ_ADAPTER(
075: "null value returned by _default_POA() on Servant "
076: + self);
077: }
078:
079: try {
080: return poa.servant_to_reference(self);
081: } catch (ServantNotActive e) {
082: throw new org.omg.CORBA.OBJ_ADAPTER(e.toString());
083: } catch (WrongPolicy e) {
084: throw new org.omg.CORBA.OBJ_ADAPTER(e.toString());
085: }
086: }
087:
088: final public ORB orb(org.omg.PortableServer.Servant self) {
089: check();
090: return orb;
091: }
092:
093: final public POA poa(org.omg.PortableServer.Servant self) {
094: check();
095:
096: _getPOACurrent();
097:
098: try {
099: // CORBA 2.4 added the get_servant() operation to the
100: // PortableServer::Current interface. As of JDK 1.4.2,
101: // however, the class org.omg.PortableServant.Current
102: // in Sun's JDK does not have the method get_servant().
103: // Instead of simply saying _current.get_servant(), below
104: // we say ((org.jacorb.poa.Current)_current).get_servant().
105: // The cast allows JacORB to run with the obsolete Sun class.
106: if (((org.jacorb.poa.Current) _current).get_servant() != self) {
107: throw new org.omg.CORBA.OBJ_ADAPTER();
108: }
109:
110: return _current.get_POA();
111: } catch (NoContext e) {
112: throw new org.omg.CORBA.OBJ_ADAPTER(e.toString());
113: }
114: }
115:
116: final public byte[] object_id(org.omg.PortableServer.Servant self) {
117: check();
118:
119: _getPOACurrent();
120:
121: try {
122: return _current.get_object_id();
123: } catch (NoContext e) {
124: throw new org.omg.CORBA.OBJ_ADAPTER(e.toString());
125: }
126: }
127:
128: private synchronized void _getPOACurrent() {
129: if (_current == null) {
130: try {
131: _current = org.omg.PortableServer.CurrentHelper
132: .narrow(orb
133: .resolve_initial_references("POACurrent"));
134: } catch (Exception e) {
135: throw new org.omg.CORBA.INITIALIZE(e.toString());
136: }
137: }
138: }
139:
140: public POA default_POA(org.omg.PortableServer.Servant self) {
141: check();
142: try {
143: return POAHelper.narrow(orb(self)
144: .resolve_initial_references("RootPOA"));
145: } catch (InvalidName e) {
146: throw new org.omg.CORBA.INITIALIZE(e.toString());
147: }
148: }
149:
150: public boolean non_existent(org.omg.PortableServer.Servant self) {
151: check();
152: return false;
153: }
154:
155: public org.omg.CORBA.Object get_component(
156: org.omg.PortableServer.Servant self) {
157: check();
158: return null;
159: }
160:
161: public org.omg.CORBA.Object get_interface_def(
162: org.omg.PortableServer.Servant self) {
163: check();
164: if (ir == null) {
165: try {
166: ir = RepositoryHelper
167: .narrow(orb
168: .resolve_initial_references("InterfaceRepository"));
169: } catch (Exception e) {
170: throw new org.omg.CORBA.INITIALIZE(e.toString());
171: }
172: }
173: return ir.lookup_id(((org.omg.CORBA.portable.ObjectImpl) self
174: ._this _object())._ids()[0]);
175: }
176:
177: public org.omg.CORBA.InterfaceDef get_interface(
178: org.omg.PortableServer.Servant self) {
179: return org.omg.CORBA.InterfaceDefHelper
180: .narrow(get_interface_def(self));
181: }
182:
183: public boolean is_a(org.omg.PortableServer.Servant self,
184: String repid) {
185: String[] intf = self
186: ._all_interfaces(poa(self), object_id(self));
187:
188: for (int i = 0; i < intf.length; i++) {
189: if (intf[i].equals(repid)) {
190: return true;
191: }
192: }
193: return "IDL:omg.org/CORBA/Object:1.0".equals(repid);
194: }
195:
196: /**
197: * _get_policy
198: */
199:
200: public org.omg.CORBA.Policy _get_policy(org.omg.CORBA.Object self,
201: int policy_type) {
202: return poa != null ? ((org.jacorb.poa.POA) poa)
203: .getPolicy(policy_type) : null;
204: }
205:
206: /**
207: * _get_domain_managers
208: */
209:
210: public org.omg.CORBA.DomainManager[] _get_domain_managers(
211: org.omg.CORBA.Object self) {
212: return null;
213: }
214:
215: /**
216: * Similar to invoke in InvokeHandler, which is ultimately implement by
217: * skeletons. This method is used by the POA to handle operations that
218: * are "special", i.e. not implemented by skeletons
219: */
220:
221: public org.omg.CORBA.portable.OutputStream _invoke(
222: org.omg.PortableServer.Servant self, String method,
223: org.omg.CORBA.portable.InputStream _input,
224: org.omg.CORBA.portable.ResponseHandler handler)
225: throws org.omg.CORBA.SystemException {
226: org.omg.CORBA.portable.OutputStream _out = null;
227:
228: if ("_get_policy".equals(method)) {
229: _out = handler.createReply();
230: _out.write_Object(_get_policy(_input.read_Object(), _input
231: .read_long()));
232: } else if ("_is_a".equals(method)) {
233: _out = handler.createReply();
234: _out.write_boolean(self._is_a(_input.read_string()));
235: } else if ("_interface".equals(method)) {
236: _out = handler.createReply();
237: _out.write_Object(self._get_interface_def());
238: } else if ("_non_existent".equals(method)) {
239: _out = handler.createReply();
240: _out.write_boolean(self._non_existent());
241: } else if ("_get_component".equals(method)) {
242: _out = handler.createReply();
243: _out.write_Object(self._get_component());
244: } else {
245: throw new BAD_PARAM("Unknown operation: " + method);
246: }
247:
248: return _out;
249: }
250: }
|