1 /* Local_delegate.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;
40 
41 import org.omg.CORBA.Context;
42 import org.omg.CORBA.ContextList;
43 import org.omg.CORBA.ExceptionList;
44 import org.omg.CORBA.NO_IMPLEMENT;
45 import org.omg.CORBA.NVList;
46 import org.omg.CORBA.NamedValue;
47 import org.omg.CORBA.ORB;
48 import org.omg.CORBA.Request;
49 import org.omg.CORBA.portable.Delegate;
50 import org.omg.CORBA.portable.ObjectImpl;
51 
52 /**
53  * The delegate, implementing the basic functionality only. This delegate
54  * is set in {@link ORG.connect(org.omg.CORBA.Object)} if ORB
55  * determines that the object is an instance of the
56  * {@link org.omg.CORBA.portable.ObjectImpl} and no other delegate is set.
57  *
58  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
59  */
60 public class SimpleDelegate
61   extends Delegate
62   implements IorProvider
63 {
64   /**
65    * The orb.
66    */
67   protected final ORB orb;
68 
69   /**
70    * The ior.
71    */
72   protected IOR ior;
73 
SimpleDelegate(ORB an_orb, IOR an_ior)74   public SimpleDelegate(ORB an_orb, IOR an_ior)
75   {
76     orb = an_orb;
77     ior = an_ior;
78   }
79 
80   /**
81    * Set the IOR of this object. The IOR must be newly set if
82    * the server reports that the object has permanently moved to a new
83    * location.
84    *
85    * @param an_ior the new IOR.
86    */
setIor(IOR an_ior)87   public void setIor(IOR an_ior)
88   {
89     this.ior = an_ior;
90   }
91 
92   /**
93    * Get the IOR of this object.
94    */
getIor()95   public IOR getIor()
96   {
97     return ior;
98   }
99 
100   /**
101    * Create the request for the local call
102    */
create_request(org.omg.CORBA.Object target, Context context, String operation, NVList parameters, NamedValue returns )103   public Request create_request(org.omg.CORBA.Object target, Context context,
104                                 String operation, NVList parameters,
105                                 NamedValue returns
106                                )
107   {
108     if (orb instanceof OrbFunctional)
109       {
110         ((OrbFunctional) orb).ensureRunning();
111       }
112     gnuRequest g = new gnuRequest();
113     g.setORB(orb);
114     g.setOperation(operation);
115     g.setIor(ior);
116     g.m_target = target;
117     g.ctx(context);
118     g.set_args(parameters);
119     if (returns != null)
120       g.set_result(returns);
121     return g;
122   }
123 
124   /**
125    * Create the request for the local call.
126    */
create_request(org.omg.CORBA.Object target, Context context, String operation, NVList parameters, NamedValue returns, ExceptionList exceptions, ContextList ctx_list )127   public Request create_request(org.omg.CORBA.Object target, Context context,
128                                 String operation, NVList parameters,
129                                 NamedValue returns, ExceptionList exceptions,
130                                 ContextList ctx_list
131                                )
132   {
133     if (orb instanceof OrbFunctional)
134       {
135         ((OrbFunctional) orb).ensureRunning();
136       }
137     gnuRequest g = new gnuRequest();
138     g.setORB(orb);
139     g.setOperation(operation);
140     g.setIor(ior);
141     g.m_target = target;
142     g.ctx(context);
143     g.set_args(parameters);
144     g.set_exceptions(exceptions);
145     g.set_context_list(ctx_list);
146     if (returns != null)
147       g.set_result(returns);
148     return g;
149   }
150 
151   /**
152    * Not implemented.
153    *
154    * @throws NO_IMPLEMENT, always.
155    */
duplicate(org.omg.CORBA.Object target)156   public org.omg.CORBA.Object duplicate(org.omg.CORBA.Object target)
157   {
158     throw new NO_IMPLEMENT();
159   }
160 
161   /**
162    * Performs direct comparison ('==').
163    */
equals(org.omg.CORBA.Object self, org.omg.CORBA.Object other)164   public boolean equals(org.omg.CORBA.Object self, org.omg.CORBA.Object other)
165   {
166     return self == other;
167   }
168 
169   /**
170    * Not implemented.
171    *
172    * @throws NO_IMPLEMENT, always.
173    */
get_interface_def(org.omg.CORBA.Object target)174   public org.omg.CORBA.Object get_interface_def(org.omg.CORBA.Object target)
175   {
176     throw new NO_IMPLEMENT();
177   }
178 
179   /**
180    * Return the hashcode (0 <= hashcode < maximum).
181    */
hash(org.omg.CORBA.Object target, int maximum)182   public int hash(org.omg.CORBA.Object target, int maximum)
183   {
184     return target == null ? 0 : target.hashCode() % maximum;
185   }
186 
187   /**
188    * Delegates functionality to java.lang.Object.hashCode();
189    */
hashCode(org.omg.CORBA.Object target)190   public int hashCode(org.omg.CORBA.Object target)
191   {
192     return target == null ? 0 : target.hashCode();
193   }
194 
195   /**
196    * Check if this object can be referenced by the given repository id.
197    *
198    * @param target the CORBA object, must be an instance of
199    * {@link org.omg.CORBA.portable.ObjectImpl}.
200    *
201    * @param repositoryIdentifer the repository id.
202    *
203    * @return true if the passed parameter is a repository id of this
204    * CORBA object.
205    */
is_a(org.omg.CORBA.Object target, String repositoryIdentifer)206   public boolean is_a(org.omg.CORBA.Object target, String repositoryIdentifer)
207   {
208     if (!(target instanceof ObjectImpl))
209       throw new NO_IMPLEMENT("Supported only for org.omg.CORBA.portable.ObjectImpl");
210 
211     ObjectImpl imp = (ObjectImpl) target;
212     String[] ids = imp._ids();
213 
214     for (int i = 0; i < ids.length; i++)
215       {
216         if (ids [ i ].equals(repositoryIdentifer))
217           return true;
218       }
219     return false;
220   }
221 
222   /**
223    * Returns true if the objects are the same or have the same delegate set. All
224    * objects in this implementation have a separate delegate.
225    */
is_equivalent(org.omg.CORBA.Object target, org.omg.CORBA.Object other)226   public boolean is_equivalent(org.omg.CORBA.Object target,
227     org.omg.CORBA.Object other)
228   {
229     if (target == other)
230       return true;
231     if ((target instanceof ObjectImpl) && other instanceof ObjectImpl)
232       {
233         try
234           {
235             org.omg.CORBA.portable.Delegate a = ((ObjectImpl) target)._get_delegate();
236             org.omg.CORBA.portable.Delegate b = ((ObjectImpl) other)._get_delegate();
237             if (a == b)
238               {
239                 return true;
240               }
241             else
242               {
243                 // We compere the IOR's in this case.
244                 if (a instanceof IorProvider && b instanceof IorProvider)
245                   {
246                     IOR ia = ((IorProvider) a).getIor();
247                     IOR ib = ((IorProvider) b).getIor();
248 
249                     if (ia != null && ib != null)
250                       return (ia.equals(ib));
251                     else
252                       return ia == ib;
253                   }
254               }
255             if (a != null && b != null)
256               {
257                 return a.equals(b);
258               }
259           }
260         catch (Exception ex)
261           {
262             // Unable to get one of the delegates.
263             return false;
264           }
265       }
266     return false;
267   }
268 
269   /**
270    * Returns true by default.
271    */
is_local(org.omg.CORBA.Object self)272   public boolean is_local(org.omg.CORBA.Object self)
273   {
274     return true;
275   }
276 
277   /**
278    * Returns true if the target is null.
279    */
non_existent(org.omg.CORBA.Object target)280   public boolean non_existent(org.omg.CORBA.Object target)
281   {
282     return target == null;
283   }
284 
285   /**
286    * Returns the ORB, passed in constructor,
287    * regardless of the argument. This class requires a single instance
288    * per each object.
289    */
orb(org.omg.CORBA.Object target)290   public ORB orb(org.omg.CORBA.Object target)
291   {
292     return orb;
293   }
294 
295   /**
296    * Returns without action.
297    */
release(org.omg.CORBA.Object target)298   public void release(org.omg.CORBA.Object target)
299   {
300   }
301 
302   /**
303    * This method assumes that the target is local and connected to the ORB.
304    */
request(org.omg.CORBA.Object target, String operation)305   public Request request(org.omg.CORBA.Object target, String operation)
306   {
307     if (orb instanceof OrbFunctional)
308       {
309         ((OrbFunctional) orb).ensureRunning();
310       }
311     gnuRequest g = new gnuRequest();
312     g.setORB(orb);
313     g.setOperation(operation);
314     g.setIor(ior);
315     g.m_target = target;
316     return g;
317   }
318 }
319