1 /* ServantDelegateImpl.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 gnu.CORBA.Poa;
40 
41 import gnu.CORBA.Unexpected;
42 
43 import org.omg.CORBA.NO_IMPLEMENT;
44 import org.omg.CORBA.ORB;
45 import org.omg.CORBA.ORBPackage.InvalidName;
46 import org.omg.CORBA.Object;
47 import org.omg.PortableServer.CurrentPackage.NoContext;
48 import org.omg.PortableServer.POA;
49 import org.omg.PortableServer.POAHelper;
50 import org.omg.PortableServer.Servant;
51 import org.omg.PortableServer.portable.Delegate;
52 
53 /**
54  * The implementation of the servant delegate for the locally existing
55  * servant.The associated servant that must also implement the
56  * {@link InvokeHandler} interface. Each servant requires a separate
57  * instance of this delegate and can serve a single object only.
58  * Hence the fields are final, but the delegate is typically reused
59  * unless the same servant is connected to different objects.
60  *
61  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
62  */
63 public class ServantDelegateImpl
64   implements Delegate
65 {
66   /**
67    * The servant, associated with this object.
68    */
69   final Servant servant;
70 
71   /**
72    * The servant (not object) id.
73    */
74   final byte[] servant_id;
75 
76   /**
77    * The POA, where the servant is connected.
78    */
79   final gnuPOA poa;
80 
81   /**
82    * The object, exposed as an object, served by this servant.
83    */
84   final gnuServantObject object;
85 
86   /**
87    * Create the delegat for the servant that will be connected to the
88    * given poa. The method is normally called from inside of gnuPOA.
89    * The constructor sets the newly created delegate as the delegate to this
90    * servant by calling Servant._set_delegate.
91    *
92    * @param a_poa the poa.
93    * @param a_servant the servant.
94    * @param a_servant_id the servant id.
95    */
ServantDelegateImpl(Servant a_servant, gnuPOA a_poa, byte[] a_servant_id)96   public ServantDelegateImpl(Servant a_servant, gnuPOA a_poa, byte[] a_servant_id)
97   {
98     poa = a_poa;
99     servant = a_servant;
100     servant_id = a_servant_id;
101     servant._set_delegate(this);
102     object =
103       new gnuServantObject(servant, servant_id, (ORB_1_4) servant._orb(), a_poa);
104     object._set_delegate(new LocalDelegate(object, poa, a_servant_id));
105   }
106 
107   /**
108    * Check if this object could be named by the given repository id.
109    * @param idl_id the repository id to check.
110    *
111    * @return true if it is one of the possible repository ids of this
112    * object.
113    */
is_a(Servant a_servant, String idl_id)114   public boolean is_a(Servant a_servant, String idl_id)
115   {
116     same(a_servant);
117 
118     String[] maybe = object.repository_ids;
119     if (maybe == null)
120       maybe = servant._all_interfaces(poa, object.Id);
121     for (int i = 0; i < maybe.length; i++)
122       {
123         if (maybe [ i ].equals(idl_id))
124           return true;
125       }
126     return false;
127   }
128 
129   /**
130    * Return the ORB's default POA.
131    */
default_POA(Servant a_servant)132   public POA default_POA(Servant a_servant)
133   {
134     same(a_servant);
135     try
136       {
137         return POAHelper.narrow(orb(a_servant).resolve_initial_references("RootPOA"));
138       }
139     catch (InvalidName ex)
140       {
141         throw new Unexpected(ex);
142       }
143   }
144 
145   /**
146    * Get ORB.
147    */
orb(Servant a_servant)148   public ORB orb(Servant a_servant)
149   {
150     same(a_servant);
151     return poa.orb();
152   }
153 
154   /**
155    * Get the object, exposing the servant.
156    */
this_object(Servant a_servant)157   public Object this_object(Servant a_servant)
158   {
159     same(a_servant);
160     try
161       {
162         return poa.aom.get(poa.m_orb.currents.get_object_id()).object;
163       }
164     catch (NoContext ex)
165       {
166         return object;
167       }
168   }
169 
170   /**
171    * Not supported.
172    *
173    * @specnote Same as for Sun up till 1.5 inclusive.
174    */
get_interface_def(Servant a_servant)175   public Object get_interface_def(Servant a_servant)
176   {
177     same(a_servant);
178     throw new NO_IMPLEMENT();
179   }
180 
181   /**
182    * Get the Id of the object being currently served.
183    */
object_id(Servant a_servant)184   public byte[] object_id(Servant a_servant)
185   {
186     same(a_servant);
187     try
188       {
189         byte[] id = poa.m_orb.currents.get_object_id();
190         return id;
191       }
192     catch (NoContext ex)
193       {
194         return object.Id;
195       }
196   }
197 
198   /**
199    * Always returns false;
200    */
non_existent(Servant a_servant)201   public boolean non_existent(Servant a_servant)
202   {
203     same(a_servant);
204     return false;
205   }
206 
207   /**
208    * Return the associated POA.
209    */
poa(Servant a_servant)210   public POA poa(Servant a_servant)
211   {
212     same(a_servant);
213     try
214       {
215         return poa.m_orb.currents.get_POA();
216       }
217     catch (NoContext ex)
218       {
219         return poa;
220       }
221   }
222 
223   /**
224    * Checks if the passed servant is the same as the servant, associated with
225    * this delegate. This class requires a single servant per delegate.
226    */
same(Servant some_servant)227   void same(Servant some_servant)
228   {
229     if (servant != some_servant)
230       throw new InternalError("Only one servant per delegate is supported.");
231   }
232 }
233