Merge with trank @ 137446
[official-gcc.git] / libjava / classpath / gnu / CORBA / IorDelegate.java
bloba2c5a0469be44a560d92ffecd4c58e77467a14d0
1 /* gnuDelegate.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
39 package gnu.CORBA;
41 import gnu.CORBA.CDR.BufferredCdrInput;
42 import gnu.CORBA.GIOP.ReplyHeader;
44 import org.omg.CORBA.CompletionStatus;
45 import org.omg.CORBA.Context;
46 import org.omg.CORBA.ContextList;
47 import org.omg.CORBA.ExceptionList;
48 import org.omg.CORBA.MARSHAL;
49 import org.omg.CORBA.NVList;
50 import org.omg.CORBA.NamedValue;
51 import org.omg.CORBA.ORB;
52 import org.omg.CORBA.Request;
53 import org.omg.CORBA.portable.ApplicationException;
54 import org.omg.CORBA.portable.InputStream;
55 import org.omg.CORBA.portable.OutputStream;
56 import org.omg.CORBA.portable.RemarshalException;
57 import org.omg.PortableInterceptor.ForwardRequest;
59 import java.io.IOException;
61 /**
62 * The Classpath implementation of the {@link Delegate} functionality in the
63 * case, when the object was constructed from an IOR object. The IOR can be
64 * constructed from the stringified object reference.
66 * There is an different instance of this delegate for each CORBA object.
68 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
70 public class IorDelegate extends SimpleDelegate
72 /**
73 * Contructs an instance of object using the given IOR.
75 public IorDelegate(ORB an_orb, IOR an_ior)
77 super(an_orb, an_ior);
80 /**
81 * Creates the request to invoke the method on this object.
83 * @param target the object, for that the operation must be invoked.
84 * @param context context (null allowed)
85 * @param operation the method name
86 * @param parameters the method parameters
87 * @param returns the return value holder
89 * @return the created request.
91 public Request create_request(org.omg.CORBA.Object target, Context context,
92 String operation, NVList parameters, NamedValue returns
95 gnuRequest request = getRequestInstance(target);
97 request.setIor(getIor());
98 request.set_target(target);
100 request.setOperation(operation);
101 request.set_args(parameters);
102 request.m_context = context;
103 request.set_result(returns);
104 request.setORB(orb);
106 return request;
110 * Creates the request to invoke the method on this object.
112 * @param target the object, for that the operation must be invoked.
113 * @param context context (null allowed)
114 * @param operation the method name
115 * @param parameters the method parameters
116 * @param returns the return value holder
118 * @return the created request.
120 public Request create_request(org.omg.CORBA.Object target, Context context,
121 String operation, NVList parameters, NamedValue returns,
122 ExceptionList exceptions, ContextList ctx_list
125 gnuRequest request = getRequestInstance(target);
127 request.setIor(ior);
128 request.set_target(target);
130 request.setOperation(operation);
131 request.set_args(parameters);
132 request.m_context = context;
133 request.set_result(returns);
134 request.set_exceptions(exceptions);
135 request.set_context_list(ctx_list);
136 request.setORB(orb);
138 return request;
142 * Get the instance of request.
144 protected gnuRequest getRequestInstance(org.omg.CORBA.Object target)
146 return new gnuRequest();
150 * Invoke operation on the given object, als handling temproray and permanent
151 * redirections. The ReplyHeader.LOCATION_FORWARD will cause to resend the
152 * request to the new direction. The ReplyHeader.LOCATION_FORWARD_PERM will
153 * cause additionally to remember the new location by this delegate, so
154 * subsequent calls will be immediately delivered to the new target.
156 * @param target the target object.
157 * @param output the output stream, previously returned by
158 * {@link #request(org.omg.CORBA.Object, String, boolean)}.
160 * @return the input stream, to read the response from or null for a one-way
161 * request.
163 * @throws SystemException if the SystemException has been thrown on the
164 * remote side (the exact type and the minor code matches the data of the
165 * remote exception that has been thrown).
167 * @throws org.omg.CORBA.portable.ApplicationException as specified.
168 * @throws org.omg.CORBA.portable.RemarshalException as specified.
170 public InputStream invoke(org.omg.CORBA.Object target, OutputStream output)
171 throws ApplicationException, RemarshalException
173 StreamBasedRequest request = (StreamBasedRequest) output;
174 while (true)
178 if (request.response_expected)
180 RawReply response = request.request.submit();
182 // Read reply header.
183 ReplyHeader rh = response.header.create_reply_header();
184 BufferredCdrInput input = response.getStream();
185 input.setOrb(orb);
186 rh.read(input);
187 request.request.m_rph = rh;
189 boolean moved_permanently = false;
191 switch (rh.reply_status)
193 case ReplyHeader.NO_EXCEPTION:
194 if (request.request.m_interceptor != null)
195 request.request.m_interceptor.receive_reply(request.request.m_info);
196 if (response.header.version.since_inclusive(1, 2))
197 input.align(8);
198 return input;
200 case ReplyHeader.SYSTEM_EXCEPTION:
201 if (response.header.version.since_inclusive(1, 2))
202 input.align(8);
203 showException(request, input);
205 throw ObjectCreator.readSystemException(input,
206 rh.service_context);
208 case ReplyHeader.USER_EXCEPTION:
209 if (response.header.version.since_inclusive(1, 2))
210 input.align(8);
211 showException(request, input);
213 throw new ApplicationException(
214 request.request.m_exception_id, input);
216 case ReplyHeader.LOCATION_FORWARD_PERM:
217 moved_permanently = true;
219 case ReplyHeader.LOCATION_FORWARD:
220 if (response.header.version.since_inclusive(1, 2))
221 input.align(8);
223 IOR forwarded = new IOR();
226 forwarded._read_no_endian(input);
228 catch (IOException ex)
230 MARSHAL t = new MARSHAL("Cant read forwarding info",
231 5102, CompletionStatus.COMPLETED_NO);
232 t.initCause(ex);
233 throw t;
236 gnuRequest prev = request.request;
237 gnuRequest r = getRequestInstance(target);
239 r.m_interceptor = prev.m_interceptor;
240 r.m_slots = prev.m_slots;
242 r.m_args = prev.m_args;
243 r.m_context = prev.m_context;
244 r.m_context_list = prev.m_context_list;
245 r.m_environment = prev.m_environment;
246 r.m_exceptions = prev.m_exceptions;
247 r.m_operation = prev.m_operation;
248 r.m_parameter_buffer = prev.m_parameter_buffer;
249 r.m_parameter_buffer.request = r;
250 r.m_result = prev.m_result;
251 r.m_target = prev.m_target;
252 r.oneWay = prev.oneWay;
253 r.m_forward_ior = forwarded;
255 if (r.m_interceptor != null)
256 r.m_interceptor.receive_other(r.m_info);
258 r.setIor(forwarded);
260 IorObject it = new IorObject(orb,
261 forwarded);
263 r.m_target = it;
265 request.request = r;
267 IOR prev_ior = getIor();
269 setIor(forwarded);
273 return invoke(it, request);
275 finally
277 if (!moved_permanently)
278 setIor(prev_ior);
281 default:
282 throw new MARSHAL("Unknow reply status: "
283 + rh.reply_status, 8000 + rh.reply_status,
284 CompletionStatus.COMPLETED_NO);
287 else
289 request.request.send_oneway();
290 return null;
293 catch (ForwardRequest forwarded)
295 ForwardRequest fw = forwarded;
296 Forwarding2: while (true)
300 gnuRequest prev = request.request;
301 gnuRequest r = getRequestInstance(target);
303 r.m_interceptor = prev.m_interceptor;
304 r.m_args = prev.m_args;
305 r.m_context = prev.m_context;
306 r.m_context_list = prev.m_context_list;
307 r.m_environment = prev.m_environment;
308 r.m_exceptions = prev.m_exceptions;
309 r.m_operation = prev.m_operation;
310 r.m_parameter_buffer = prev.m_parameter_buffer;
311 r.m_parameter_buffer.request = r;
312 r.m_result = prev.m_result;
313 r.m_target = prev.m_target;
314 r.oneWay = prev.oneWay;
316 r.m_forwarding_target = fw.forward;
318 if (r.m_interceptor != null)
319 r.m_interceptor.receive_other(r.m_info);
321 r.m_target = fw.forward;
322 request.request = r;
323 break Forwarding2;
325 catch (ForwardRequest e)
327 forwarded = e;
335 * Show exception to interceptor.
337 void showException(StreamBasedRequest request, BufferredCdrInput input)
338 throws ForwardRequest
340 input.mark(2048);
341 request.request.m_exception_id = input.read_string();
342 input.reset();
344 if (request.request.m_interceptor != null)
345 request.request.m_interceptor.receive_exception(request.request.m_info);
349 * Create a request to invoke the method of this CORBA object.
351 * @param target the CORBA object, to that this operation must be applied.
352 * @param operation the name of the method to invoke.
354 * @return the request.
356 public Request request(org.omg.CORBA.Object target, String operation)
358 gnuRequest request = getRequestInstance(target);
360 request.setIor(ior);
361 request.set_target(target);
363 request.setOperation(operation);
364 request.setORB(orb);
366 return request;
370 * Create a request to invoke the method of this CORBA object.
372 * @param target the CORBA object, to that this operation must be applied.
373 * @param operation the name of the method to invoke.
374 * @param response_expected specifies if this is one way message or the
375 * response to the message is expected.
377 * @return the stream where the method arguments should be written.
379 public OutputStream request(org.omg.CORBA.Object target, String operation,
380 boolean response_expected
383 gnuRequest request = getRequestInstance(target);
385 request.setIor(ior);
386 request.set_target(target);
387 request.setOperation(operation);
389 StreamBasedRequest out = request.getParameterStream();
390 out.response_expected = response_expected;
391 request.setORB(orb);
392 out.setOrb(orb);
394 return out;
398 * If there is an opened cache socket to access this object, close that
399 * socket.
401 * @param target The target is not used, this delegate requires a single
402 * instance per object.
404 public void release(org.omg.CORBA.Object target)
406 // Do nothing here.
410 * Reset the remote_ior flag, forcing to check if the object is local on the
411 * next getRequestInstance call.
413 public void setIor(IOR an_ior)
415 super.setIor(an_ior);
419 * Checks if the ior is local so far it is easy.
421 public boolean is_local(org.omg.CORBA.Object self)
423 return false;