KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > ServantDelegate


1 package org.jacorb.orb;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import org.omg.CORBA.ORB JavaDoc;
24 import org.omg.CORBA.ORBPackage.InvalidName JavaDoc;
25 import org.omg.CORBA.*;
26
27 import org.omg.PortableServer.*;
28 import org.omg.PortableServer.POAPackage.*;
29 import org.omg.PortableServer.CurrentPackage.NoContext JavaDoc;
30
31
32 /**
33  * JacORB-specific implementation of PortableServer.Servant
34  *
35  * $Id: ServantDelegate.java,v 1.25 2005/06/01 11:26:00 andre.spiegel Exp $
36  */

37
38 public class ServantDelegate
39     implements org.omg.PortableServer.portable.Delegate JavaDoc
40 {
41     private transient ORB orb = null;
42     private transient Repository ir = null;
43     private transient org.omg.PortableServer.Current JavaDoc _current = null;
44     private transient POA poa = null;
45
46     ServantDelegate( org.jacorb.orb.ORB orb )
47     {
48         this.orb = orb;
49     }
50
51     /**
52      * Must be checked for every invocation (cf. Lang. Mapping p. 1-89)
53      */

54
55     private final void check()
56     {
57         if (orb == null)
58         {
59             throw new org.omg.CORBA.BAD_INV_ORDER JavaDoc ("The Servant has not been associated with an ORB instance");
60         }
61     }
62
63     final public org.omg.CORBA.Object JavaDoc this_object (org.omg.PortableServer.Servant JavaDoc self)
64     {
65         check();
66         try
67         {
68             poa = poa(self);
69         }
70         catch (org.omg.CORBA.OBJ_ADAPTER JavaDoc e)
71         {
72             // Use servants default POA. Operation may be re-implemented
73
// by servant implementation.
74

75             poa = self._default_POA ();
76         }
77
78         if (poa == null)
79         {
80             throw new org.omg.CORBA.OBJ_ADAPTER JavaDoc("null value returned by _default_POA() on Servant " + self);
81         }
82
83         try
84         {
85             return poa.servant_to_reference(self);
86         }
87         catch(ServantNotActive e)
88         {
89             throw new org.omg.CORBA.OBJ_ADAPTER JavaDoc(e.toString());
90         }
91         catch(WrongPolicy e)
92         {
93             throw new org.omg.CORBA.OBJ_ADAPTER JavaDoc(e.toString());
94         }
95     }
96
97
98     final public ORB orb(org.omg.PortableServer.Servant JavaDoc self)
99     {
100         check();
101         return orb;
102     }
103
104     final public POA poa(org.omg.PortableServer.Servant JavaDoc self)
105     {
106         check();
107         if (_current == null)
108         {
109             _getPOACurrent();
110         }
111         try
112         {
113             // CORBA 2.4 added the get_servant() operation to the
114
// PortableServer::Current interface. As of JDK 1.4.2,
115
// however, the class org.omg.PortableServant.Current
116
// in Sun's JDK does not have the method get_servant().
117
// Instead of simply saying _current.get_servant(), below
118
// we say ((org.jacorb.poa.Current)_current).get_servant().
119
// The cast allows JacORB to run with the obsolete Sun class.
120
if( ((org.jacorb.poa.Current)_current).get_servant() != self )
121             {
122                 throw new org.omg.CORBA.OBJ_ADAPTER JavaDoc();
123             }
124
125             return _current.get_POA();
126         }
127         catch(NoContext JavaDoc e)
128         {
129             throw new org.omg.CORBA.OBJ_ADAPTER JavaDoc(e.toString());
130         }
131     }
132
133     final public byte[] object_id(org.omg.PortableServer.Servant JavaDoc self)
134     {
135         check();
136         if (_current == null)
137         {
138             _getPOACurrent();
139         }
140         try
141         {
142             return _current.get_object_id();
143         }
144         catch(NoContext JavaDoc e)
145         {
146             throw new org.omg.CORBA.OBJ_ADAPTER JavaDoc(e.toString());
147         }
148     }
149
150     private synchronized void _getPOACurrent()
151     {
152         if (_current == null)
153         {
154             try
155             {
156                 _current = org.omg.PortableServer.CurrentHelper.narrow(orb.resolve_initial_references("POACurrent"));
157             }
158             catch (Exception JavaDoc e)
159             {
160                 throw new org.omg.CORBA.INITIALIZE JavaDoc(e.toString());
161             }
162         }
163     }
164
165     public POA default_POA(org.omg.PortableServer.Servant JavaDoc self)
166     {
167         check();
168         try
169         {
170             return POAHelper.narrow(orb(self).resolve_initial_references("RootPOA"));
171         }
172         catch(InvalidName JavaDoc e)
173         {
174             throw new org.omg.CORBA.INITIALIZE JavaDoc(e.toString());
175         }
176     }
177
178     public boolean non_existent(org.omg.PortableServer.Servant JavaDoc self)
179     {
180         check();
181         return false;
182     }
183
184     public org.omg.CORBA.Object JavaDoc get_interface_def( org.omg.PortableServer.Servant JavaDoc self)
185     {
186         check();
187         if ( ir == null)
188         {
189             try
190             {
191                 ir = RepositoryHelper.narrow(orb.resolve_initial_references("InterfaceRepository"));
192             }
193             catch (Exception JavaDoc e)
194             {
195                 throw new org.omg.CORBA.INITIALIZE JavaDoc(e.toString());
196             }
197         }
198         return ir.lookup_id( ((org.omg.CORBA.portable.ObjectImpl JavaDoc)self._this_object())._ids()[0] );
199     }
200
201     public org.omg.CORBA.InterfaceDef JavaDoc get_interface(org.omg.PortableServer.Servant JavaDoc self)
202     {
203         return org.omg.CORBA.InterfaceDefHelper.narrow( get_interface_def( self ));
204     }
205
206     public boolean is_a(org.omg.PortableServer.Servant JavaDoc self, String JavaDoc repid)
207     {
208         String JavaDoc [] intf = self._all_interfaces(poa(self), object_id(self));
209
210         for( int i = 0; i < intf.length; i++)
211         {
212             if( intf[i].equals(repid))
213             {
214                 return true;
215             }
216         }
217         return repid.equals("IDL:omg.org/CORBA/Object:1.0");
218     }
219
220
221     /**
222      * _get_policy
223      */

224
225     public org.omg.CORBA.Policy JavaDoc _get_policy(org.omg.CORBA.Object JavaDoc self,
226                                             int policy_type)
227     {
228
229         return poa != null ? ((org.jacorb.poa.POA)poa).getPolicy(policy_type) : null;
230     }
231
232     /**
233      * _get_domain_managers
234      */

235
236     public org.omg.CORBA.DomainManager JavaDoc[] _get_domain_managers
237         (org.omg.CORBA.Object JavaDoc self)
238     {
239         return null;
240     }
241
242     /**
243      * Similar to invoke in InvokeHandler, which is ultimately implement by
244      * skeletons. This method is used by the POA to handle operations that
245      * are "special", i.e. not implemented by skeletons
246      */

247
248     public org.omg.CORBA.portable.OutputStream JavaDoc _invoke(org.omg.PortableServer.Servant JavaDoc self,
249                                                        String JavaDoc method,
250                                                        org.omg.CORBA.portable.InputStream JavaDoc _input,
251                                                        org.omg.CORBA.portable.ResponseHandler JavaDoc handler)
252         throws org.omg.CORBA.SystemException JavaDoc
253     {
254         org.omg.CORBA.portable.OutputStream JavaDoc _out = null;
255
256         if( method.equals("_get_policy"))
257         {
258             _out = handler.createReply();
259             _out.write_Object(_get_policy(_input.read_Object() , _input.read_long() ) );
260         }
261         else if( method.equals("_is_a"))
262         {
263             _out = handler.createReply();
264             _out.write_boolean(self._is_a(_input.read_string() ));
265         }
266         else if( method.equals("_interface"))
267         {
268             _out = handler.createReply();
269             _out.write_Object(self._get_interface_def() );
270         }
271         else if( method.equals("_non_existent"))
272         {
273             _out = handler.createReply();
274             _out.write_boolean(self._non_existent() );
275         }
276         else
277             throw new BAD_PARAM("Unknown operation: " + method );
278
279         return _out;
280     }
281
282
283 }
284
Popular Tags