001/* Delegate.java --
002Copyright (C) 2005, 2006 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package org.omg.CORBA.portable;
040
041import gnu.java.lang.CPStringBuilder;
042
043import org.omg.CORBA.BAD_PARAM;
044import org.omg.CORBA.Context;
045import org.omg.CORBA.ContextList;
046import org.omg.CORBA.DomainManager;
047import org.omg.CORBA.ExceptionList;
048import org.omg.CORBA.NO_IMPLEMENT;
049import org.omg.CORBA.NVList;
050import org.omg.CORBA.NamedValue;
051import org.omg.CORBA.ORB;
052import org.omg.CORBA.Policy;
053import org.omg.CORBA.Request;
054import org.omg.CORBA.SetOverrideType;
055
056/**
057 * Specifies a vendor specific implementation of the
058 * {@link org.omg.CORBA.Object} methods. The calls to these
059 * methods are forwarded to the object delegate that can be
060 * replaced, if needed. The first parameter is the actual
061 * CORBA object to that the operation must be applied.
062 *
063 * Some methods in this class are not abstract, but no implemented,
064 * thowing the {@link NO_IMPLEMENT}. This, however, does not mean that
065 * they are not implemented in the derived classes as well.
066 *
067 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
068 */
069public abstract class Delegate
070{
071  /**
072   * Explains the reason of throwing the NO_IMPLEMENT.
073   */
074  private static final String WHY =
075    "Following 1.4 API, this Delegate method must not be implemented. Override.";
076
077  /**
078   * Create a request to invoke the method of this object.
079   *
080   * @param target the CORBA object, to that this operation must be applied.
081   * @param context a list of additional properties.
082   * @param operation the name of method to be invoked.
083   * @param parameters the method parameters.
084   * @param returns the container for tge method returned value.
085   *
086   * @return the created reaquest.
087   */
088  public abstract Request create_request(org.omg.CORBA.Object target,
089                                         Context context, String operation,
090                                         NVList parameters, NamedValue returns
091                                        );
092
093  /**
094   * Create a request to invoke the method of this object, specifying
095   * context list and the list of the expected exception.
096   *
097   * @param target the CORBA object, to that this operation must be applied.
098   * @param context a list of additional properties.
099   * @param operation the name of method to be invoked.
100   * @param parameters the method parameters.
101   * @param returns the container for tge method returned value.
102   * @param exceptions the list of the possible exceptions that the method
103   * can throw.
104   * @param ctx_list the list of the context strings that need to be
105   * resolved and send as a context instance.
106   *
107   * @return the created reaquest.
108   */
109  public abstract Request create_request(org.omg.CORBA.Object target,
110                                         Context context, String operation,
111                                         NVList parameters, NamedValue returns,
112                                         ExceptionList exceptions,
113                                         ContextList ctx_list
114                                        );
115
116  /**
117   * Duplicate the object reference. This does not make much sense for
118   * java platform and is just included for the sake of compliance with
119   * CORBA APIs.
120   *
121   * @param target the CORBA object, to that this operation must be applied.
122   *
123   * The method may return the object reference itself.
124   *
125   * @return as a rule, <code>this</code>.
126   */
127  public abstract org.omg.CORBA.Object duplicate(org.omg.CORBA.Object target);
128
129  /**
130   * Retrieve the domain managers for this object.
131   *
132   * @param target the CORBA object, to that this operation must be applied.
133   *
134   * @return the domain managers.
135   *
136   * @throws NO_IMPLEMENT, always (following the 1.4 specification).
137   */
138  public DomainManager[] get_domain_managers(org.omg.CORBA.Object target)
139  {
140    throw new NO_IMPLEMENT(WHY);
141  }
142
143  /**
144   *
145   * @param target the CORBA object, to that this operation must be applied.
146   *
147   * Get the <code>InterfaceDef</code> for this Object.
148   */
149  public abstract org.omg.CORBA.Object get_interface_def(org.omg.CORBA.Object target);
150
151  /**
152   * Returns the {@link Policy}, applying to this object.
153   *
154   * @param target the CORBA object, to that this operation must be applied.
155   * @param a_policy_type a type of policy to be obtained.
156   * @return a corresponding Policy object.
157   *
158   * @throws NO_IMPLEMENT, always (following the 1.4 specification).
159   */
160  public Policy get_policy(org.omg.CORBA.Object target, int a_policy_type)
161                    throws BAD_PARAM
162  {
163    throw new NO_IMPLEMENT(WHY);
164  }
165
166  /**
167   * Get the hashcode this object reference. The same hashcode still
168   * does not means that the references are the same. From the other
169   * side, two different references may still refer to the same CORBA
170   * object. The returned value must not change during the object
171   * lifetime.
172   *
173   * @param target the CORBA object, to that this operation must be applied.
174   * @param maximum the maximal value to return.
175   *
176   * @return the hashcode.
177   */
178  public abstract int hash(org.omg.CORBA.Object target, int maximum);
179
180  /**
181   * Check if this object can be referenced by the given repository id.
182   *
183   * @param target the CORBA object, to that this operation must be applied.
184   * @param repositoryIdentifer the repository id.
185   *
186   * @return true if the passed parameter is a repository id of this
187   * CORBA object.
188   */
189  public abstract boolean is_a(org.omg.CORBA.Object target,
190                               String repositoryIdentifer
191                              );
192
193  /**
194   * Return true if the other object references are equivalent, so far as
195   * it is possible to determine this easily.
196   *
197   * @param target the CORBA object, to that this operation must be applied.
198   * @param other the other object reference.
199   *
200   * @return true if both references refer the same object, false
201   * if they probably can refer different objects.
202   *
203   */
204  public abstract boolean is_equivalent(org.omg.CORBA.Object target,
205                                        org.omg.CORBA.Object other
206                                       );
207
208  /**
209   * Returns true if the object is local.
210   *
211   * @param self the object to check.
212   *
213   * @return false, always (following 1.4 specs). Override to get
214   * functionality.
215   */
216  public boolean is_local(org.omg.CORBA.Object self)
217  {
218    return false;
219  }
220
221  /**
222   * Determines if the server object for this reference has already
223   * been destroyed.
224   *
225   * @param target the CORBA object, to that this operation must be applied.
226   *
227   * @return true if the object has been destroyed, false otherwise.
228   */
229  public abstract boolean non_existent(org.omg.CORBA.Object target);
230
231  /**
232   * Compares two objects for equality. The default implementations
233   * delegated call to {@link java.lang.Object#equals(java.lang.Object)}.
234   *
235   * @param self this CORBA object.
236   * @param other the other CORBA object.
237   *
238   * @return true if the objects are equal.
239   */
240  public boolean equals(org.omg.CORBA.Object self, java.lang.Object other)
241  {
242    return self==other;
243  }
244
245  /**
246   * Return the hashcode for this CORBA object. The default implementation
247   * delegates call to {@link #hash(org.omg.CORBA.Object, int)}, passing Integer.MAX_VALUE as an
248   * argument.
249   *
250   * @param target the object, for that the hash code must be computed.
251   *
252   * @return the hashcode.
253   */
254  public int hashCode(org.omg.CORBA.Object target)
255  {
256    return hash(target, Integer.MAX_VALUE);
257  }
258
259  /**
260   * Invoke the operation.
261   *
262   * @param target the invocation target.
263   * @param output the stream, containing the written arguments.
264   *
265   * @return the stream, from where the input parameters could be read.
266   *
267   * @throws ApplicationException if the application throws an exception,
268   * defined as a part of its remote method definition.
269   *
270   * @throws RemarshalException if reading(remarshalling) fails.
271   *
272   * @throws NO_IMPLEMENT, always (following the 1.4 specification).
273   */
274  public InputStream invoke(org.omg.CORBA.Object target,
275                            org.omg.CORBA.portable.OutputStream output
276                           )
277                     throws ApplicationException, RemarshalException
278  {
279    throw new NO_IMPLEMENT(WHY);
280  }
281
282  /**
283   * Provides the reference to ORB.
284   *
285   * @param target the object reference.
286   *
287   * @return the associated ORB.
288   *
289   * @throws NO_IMPLEMENT, always (following the 1.4 specification).
290   */
291  public ORB orb(org.omg.CORBA.Object target)
292  {
293    throw new NO_IMPLEMENT(WHY);
294  }
295
296  /**
297   * Free resoureces, occupied by this reference. The object implementation
298   * is not notified, and the other references to the same object are not
299   * affected.
300   *
301   * @param target the CORBA object, to that this operation must be applied.
302   */
303  public abstract void release(org.omg.CORBA.Object target);
304
305  /**
306   * Release the reply stream back to ORB after finishing reading the data
307   * from it.
308   *
309   * @param target the CORBA object, to that this operation must be applied.
310   * @param input the stream, normally returned by {@link #invoke} or
311   * {@link ApplicationException#getInputStream()}, can be null.
312   *
313   * The default method returns without action.
314   */
315  public void releaseReply(org.omg.CORBA.Object target,
316                           org.omg.CORBA.portable.InputStream input
317                          )
318  {
319  }
320
321  /**
322   * Create a request to invoke the method of this CORBA object.
323   *
324   * @param target the CORBA object, to that this operation must be applied.
325   * @param operation the name of the method to invoke.
326   *
327   * @return the request.
328   */
329  public abstract Request request(org.omg.CORBA.Object target, String operation);
330
331  /**
332   * Create a request to invoke the method of this CORBA object.
333   *
334   * @param target the CORBA object, to that this operation must be applied.
335   * @param operation the name of the method to invoke.
336   * @param response_expected specifies if this is one way message or the
337   * response to the message is expected.
338   *
339   * @return the stream where the method arguments should be written.
340   */
341  public org.omg.CORBA.portable.OutputStream request(org.omg.CORBA.Object target,
342                                                     String operation,
343                                                     boolean response_expected
344                                                    )
345  {
346    throw new NO_IMPLEMENT(WHY);
347  }
348
349  /**
350   * This method is always called after invoking the operation on the
351   * local servant.
352   *
353   * The default method returns without action.
354   *
355   * @param self the object.
356   * @param servant the servant.
357   */
358  public void servant_postinvoke(org.omg.CORBA.Object self,
359                                 ServantObject servant
360                                )
361  {
362  }
363
364  /**
365   * Returns a servant that should be used for this request.
366   * The servant can also be casted to the expected type, calling the
367   * required method directly.
368   *
369   * @param self the object
370   * @param operation the operation
371   * @param expectedType the expected type of the servant.
372   *
373   * This implementation always returns null; override for different
374   * behavior.
375   *
376   * @return the servant or null if the servant is not an expected type
377   * of the method is not supported, for example, due security reasons.
378   */
379  public ServantObject servant_preinvoke(org.omg.CORBA.Object self,
380                                         String operation, Class expectedType
381                                        )
382  {
383    return null;
384  }
385
386  /**
387   * Returns a new object with the new policies either replacing or
388   * extending the current policies, depending on the second parameter.
389   *
390   * @param target the CORBA object, to that this operation must be applied.
391   * @param policies the policy additions or replacements.
392   * @param how either {@link SetOverrideType#SET_OVERRIDE} to override the
393   * current policies of {@link SetOverrideType#ADD_OVERRIDE} to replace
394   * them.
395   *
396   * @throws NO_IMPLEMENT, always (following the 1.4 specification).
397   *
398   * @return the new reference with the changed policies.
399   */
400  public org.omg.CORBA.Object set_policy_override(org.omg.CORBA.Object target,
401                                                  Policy[] policies,
402                                                  SetOverrideType how
403                                                 )
404  {
405    throw new NO_IMPLEMENT(WHY);
406  }
407
408  /**
409   * Return a string representation of the passed object.
410   *
411   * @param self the CORBA object, to that the string representation must be
412   * returned. By default, the call is delegated to
413   * {@link java.lang.Object#toString()}.
414   *
415   * @return the string representation.
416   */
417  public String toString(org.omg.CORBA.Object self)
418  {
419    if (self instanceof ObjectImpl)
420      {
421        ObjectImpl x = (ObjectImpl) self;
422        CPStringBuilder b = new CPStringBuilder(x.getClass().getName());
423        b.append(": [");
424        for (int i = 0; i < x._ids().length; i++)
425          {
426            b.append(x._ids() [ i ]);
427            b.append(" ");
428          }
429        b.append("]");
430        return b.toString();
431      }
432    else
433      return self.getClass().getName();
434  }
435}