001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1999-2004 Gerald Brose
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: *
020: */
021: package org.jacorb.orb.portableInterceptor;
022:
023: import org.omg.IOP.*;
024: import org.omg.CORBA.*;
025: import org.omg.PortableInterceptor.*;
026: import org.omg.Dynamic.Parameter;
027: import org.omg.ETF.*;
028:
029: import org.apache.avalon.framework.logger.*;
030:
031: import java.util.*;
032:
033: import org.jacorb.orb.etf.ProfileBase;
034: import org.jacorb.util.ObjectUtil;
035:
036: /**
037: * This class represents the type of info object,
038: * that will be passed to the ClientRequestInterceptors. <br>
039: * See PI Spec p.5-46ff
040: *
041: * @author Nicolas Noffke
042: * @version $Id: ClientRequestInfoImpl.java,v 1.32 2006/07/07 10:55:57 alphonse.bendt Exp $
043: */
044:
045: public class ClientRequestInfoImpl extends RequestInfoImpl implements
046: ClientRequestInfo {
047: private final Logger logger;
048:
049: //from ClientRequestInfo
050: public org.omg.CORBA.Object target = null;
051: public final org.omg.CORBA.Object effective_target;
052: public TaggedProfile effective_profile = null;
053: public final Any received_exception;
054: public String received_exception_id = null;
055: public final TaggedComponent[] effective_components;
056: public final org.jacorb.orb.Delegate delegate;
057: public final org.jacorb.orb.ORB orb;
058:
059: public final org.jacorb.orb.giop.RequestOutputStream request_os;
060: public org.jacorb.orb.giop.ReplyInputStream reply_is = null;
061:
062: public final org.jacorb.orb.giop.ClientConnection connection;
063:
064: public ClientRequestInfoImpl(org.jacorb.orb.ORB orb,
065: ClientRequestInfoImpl original,
066: org.jacorb.orb.giop.RequestOutputStream ros,
067: org.omg.CORBA.Object self,
068: org.jacorb.orb.Delegate delegate,
069: org.jacorb.orb.ParsedIOR piorOriginal,
070: org.jacorb.orb.giop.ClientConnection connection) {
071: super ();
072:
073: this .orb = orb;
074: logger = orb.getConfiguration().getNamedLogger(
075: "jacorb.orb.interceptors");
076:
077: this .operation = ros.operation();
078: this .response_expected = ros.response_expected();
079: this .received_exception = orb.create_any();
080:
081: if (ros.getRequest() != null) {
082: this .setRequest(ros.getRequest());
083: }
084:
085: this .effective_target = self;
086:
087: org.jacorb.orb.ParsedIOR pior = delegate.getParsedIOR();
088:
089: if (piorOriginal == null) {
090: this .target = self;
091: } else {
092: this .target = orb._getObject(pior);
093: }
094:
095: Profile profile = pior.getEffectiveProfile();
096:
097: // If this ParsedIOR is using a profile that extends ProfileBase e.g. IIOPProfile
098: // and WIOP (within the regression suite) then grab the effective profile and the
099: // possibly null effective_components.
100: if (profile instanceof ProfileBase) {
101: this .effective_profile = ((ProfileBase) profile)
102: .asTaggedProfile();
103: this .effective_components = (((ProfileBase) profile)
104: .getComponents() == null ? new org.omg.IOP.TaggedComponent[0]
105: : ((ProfileBase) profile).getComponents().asArray());
106: } else {
107: this .effective_components = new org.omg.IOP.TaggedComponent[0];
108: }
109:
110: this .delegate = delegate;
111:
112: this .request_id = ros.requestId();
113: InterceptorManager manager = orb.getInterceptorManager();
114:
115: this .current = manager.getCurrent();
116:
117: //allow interceptors access to request output stream
118: this .request_os = ros;
119:
120: //allow (BiDir) interceptor to inspect the connection
121: this .connection = connection;
122:
123: // If the original ClientRequestInfo is not null and the forward_reference
124: // is not null then copy it over.
125: if (original != null && original.forward_reference != null) {
126: forward_reference = original.forward_reference;
127: }
128: }
129:
130: public final void setRequest(org.jacorb.orb.dii.Request request) {
131: arguments = new org.omg.Dynamic.Parameter[request.arguments
132: .count()];
133: for (int i = 0; i < arguments.length; i++) {
134: try {
135: NamedValue value = request.arguments.item(i);
136:
137: ParameterMode mode = null;
138: if (value.flags() == ARG_IN.value) {
139: mode = ParameterMode.PARAM_IN;
140: } else if (value.flags() == ARG_OUT.value) {
141: mode = ParameterMode.PARAM_OUT;
142: } else if (value.flags() == ARG_INOUT.value) {
143: mode = ParameterMode.PARAM_INOUT;
144: }
145:
146: arguments[i] = new org.omg.Dynamic.Parameter(value
147: .value(), mode);
148: } catch (Exception e) {
149: logger.debug("unexpected exception", e);
150: }
151: }
152: //exceptions will be set when available
153: }
154:
155: /**
156: * This method builds an array of ServiceContexts.
157: * The last ServiceContext is a dummy object for
158: * data aligning purposes.
159: */
160:
161: public Enumeration getRequestServiceContexts() {
162: return request_ctx.elements();
163: }
164:
165: // implementation of
166: // org.omg.PortableInterceptor.RequestInfoOperations interface
167:
168: public Parameter[] arguments() {
169: if (!(caller_op == ClientInterceptorIterator.SEND_REQUEST)
170: && !(caller_op == ClientInterceptorIterator.RECEIVE_REPLY)) {
171: throw new BAD_INV_ORDER(
172: "The attribute \"arguments\" is currently invalid!",
173: 10, CompletionStatus.COMPLETED_MAYBE);
174: }
175:
176: if (arguments == null) {
177: throw new NO_RESOURCES(
178: "Stream-based skeletons/stubs do not support this op",
179: 1, CompletionStatus.COMPLETED_MAYBE);
180: }
181:
182: return arguments;
183: }
184:
185: public TypeCode[] exceptions() {
186: if (caller_op == ClientInterceptorIterator.SEND_POLL) {
187: throw new BAD_INV_ORDER(
188: "The attribute \"exceptions\" is currently invalid!",
189: 10, CompletionStatus.COMPLETED_MAYBE);
190: }
191:
192: if (exceptions == null) {
193: throw new NO_RESOURCES(
194: "Stream-based skeletons/stubs do not support this op",
195: 1, CompletionStatus.COMPLETED_MAYBE);
196: }
197:
198: return exceptions;
199: }
200:
201: public Any result() {
202: if (caller_op != ClientInterceptorIterator.RECEIVE_REPLY) {
203: throw new BAD_INV_ORDER(
204: "The attribute \"result\" is currently invalid!",
205: 10, CompletionStatus.COMPLETED_MAYBE);
206: }
207:
208: if (result == null) {
209: throw new NO_RESOURCES(
210: "Stream-based skeletons/stubs do not support this op",
211: 1, CompletionStatus.COMPLETED_MAYBE);
212: }
213:
214: return result;
215: }
216:
217: public short sync_scope() {
218: if (caller_op == ClientInterceptorIterator.SEND_POLL) {
219: throw new BAD_INV_ORDER(
220: "The attribute \"sync_scope\" is currently invalid!",
221: 10, CompletionStatus.COMPLETED_MAYBE);
222: }
223:
224: return org.omg.Messaging.SYNC_WITH_TRANSPORT.value;
225: }
226:
227: public short reply_status() {
228: if ((caller_op == ClientInterceptorIterator.SEND_REQUEST)
229: || (caller_op == ClientInterceptorIterator.SEND_POLL)) {
230: throw new BAD_INV_ORDER(
231: "The attribute \"reply_status\" is currently invalid!",
232: 10, CompletionStatus.COMPLETED_MAYBE);
233: }
234:
235: return reply_status;
236: }
237:
238: /**
239: * <code>forward_reference</code> returns the forward reference for the client request. Note
240: * that the current version of the specification does not permit this to be accessed by
241: * SendRequest; this modification is a PrismTech enhancement complying one of the suggested
242: * portable solutions within http://www.omg.org/issues/issue5266.txt.
243: *
244: * @return an <code>org.omg.CORBA.Object</code> value
245: */
246: public org.omg.CORBA.Object forward_reference() {
247: if (caller_op != ClientInterceptorIterator.SEND_REQUEST
248: && (caller_op != ClientInterceptorIterator.RECEIVE_OTHER && (reply_status != LOCATION_FORWARD.value))) {
249: throw new BAD_INV_ORDER(
250: "The attribute \"forward_reference\" is currently "
251: + "invalid!", 10,
252: CompletionStatus.COMPLETED_MAYBE);
253: }
254: return forward_reference;
255: }
256:
257: public ServiceContext get_request_service_context(int id) {
258: if (caller_op == ClientInterceptorIterator.SEND_POLL) {
259: throw new BAD_INV_ORDER(
260: "The attribute \"operation_context\" is currently "
261: + "invalid!", 10,
262: CompletionStatus.COMPLETED_MAYBE);
263: }
264:
265: return super .get_request_service_context(id);
266: }
267:
268: public ServiceContext get_reply_service_context(int id) {
269: if ((caller_op == ClientInterceptorIterator.SEND_REQUEST)
270: || (caller_op == ClientInterceptorIterator.SEND_POLL)) {
271: throw new BAD_INV_ORDER(
272: "The attribute \"reply_status\" is currently invalid!",
273: 10, CompletionStatus.COMPLETED_MAYBE);
274: }
275:
276: return super .get_reply_service_context(id);
277: }
278:
279: // implementation of ClientRequestInfoOperations interface
280: public org.omg.CORBA.Object target() {
281: return target;
282: }
283:
284: public org.omg.CORBA.Object effective_target() {
285: return effective_target;
286: }
287:
288: public TaggedProfile effective_profile() {
289: return effective_profile;
290: }
291:
292: public Any received_exception() {
293: if (caller_op != ClientInterceptorIterator.RECEIVE_EXCEPTION) {
294: throw new BAD_INV_ORDER(
295: "The attribute \"received_exception\" is currently "
296: + "invalid!", 10,
297: CompletionStatus.COMPLETED_MAYBE);
298: }
299:
300: return received_exception;
301: }
302:
303: public String received_exception_id() {
304: if (caller_op != ClientInterceptorIterator.RECEIVE_EXCEPTION) {
305: throw new BAD_INV_ORDER(
306: "The attribute \"received_exception_id\" is "
307: + "currently invalid!", 10,
308: CompletionStatus.COMPLETED_MAYBE);
309: }
310:
311: return received_exception_id;
312: }
313:
314: public TaggedComponent get_effective_component(int id) {
315: if (caller_op == ClientInterceptorIterator.SEND_POLL) {
316: throw new BAD_INV_ORDER(
317: "The operation \"get_effective_component\" is "
318: + "currently invalid!", 10,
319: CompletionStatus.COMPLETED_MAYBE);
320: }
321:
322: for (int _i = 0; _i < effective_components.length; _i++) {
323: if (effective_components[_i].tag == id) {
324: return effective_components[_i];
325: }
326: }
327:
328: throw new BAD_PARAM("No TaggedComponent with id " + id
329: + " found", 25, CompletionStatus.COMPLETED_MAYBE);
330: }
331:
332: public TaggedComponent[] get_effective_components(int id) {
333: if (caller_op == ClientInterceptorIterator.SEND_POLL) {
334: throw new BAD_INV_ORDER(
335: "The operation \"get_effective_components\" is "
336: + "currently invalid!", 10,
337: CompletionStatus.COMPLETED_MAYBE);
338: }
339:
340: List _store = new ArrayList();
341: for (int _i = 0; _i < effective_components.length; _i++) {
342: if (effective_components[_i].tag == id) {
343: _store.add(effective_components[_i]);
344: }
345: }
346:
347: if (_store.size() == 0) {
348: throw new BAD_PARAM("No TaggedComponents with id " + id
349: + " found", 25, CompletionStatus.COMPLETED_MAYBE);
350: }
351:
352: TaggedComponent[] _result = new TaggedComponent[_store.size()];
353: for (int _i = 0; _i < _result.length; _i++) {
354: _result[_i] = (TaggedComponent) _store.get(_i);
355: }
356:
357: return _result;
358: }
359:
360: /**
361: * WARNING: This method relies on the DomainService to be available.
362: * Make shure that the DS is running, if you want to call this method.
363: */
364:
365: public Policy get_request_policy(int type) {
366: if (caller_op == ClientInterceptorIterator.SEND_POLL) {
367: throw new BAD_INV_ORDER(
368: "The operation \"get_request_policy\" is currently "
369: + "invalid!", 10,
370: CompletionStatus.COMPLETED_MAYBE);
371: }
372:
373: if (!orb.hasPolicyFactoryForType(type)) {
374: throw new INV_POLICY("No PolicyFactory for type " + type
375: + " has been registered!", 1,
376: CompletionStatus.COMPLETED_MAYBE);
377: }
378:
379: try {
380: return delegate.get_policy(target, type);
381: } catch (INV_POLICY e) {
382: e.minor = 1;
383: throw e;
384: }
385: }
386:
387: public void add_request_service_context(
388: ServiceContext service_context, boolean replace) {
389:
390: if (caller_op != ClientInterceptorIterator.SEND_REQUEST) {
391: throw new BAD_INV_ORDER(
392: "The operation \"add_request_service_context\" is "
393: + "currently invalid!", 10,
394: CompletionStatus.COMPLETED_MAYBE);
395: }
396:
397: Integer _id = ObjectUtil.newInteger(service_context.context_id);
398:
399: if (!replace && request_ctx.containsKey(_id)) {
400: throw new BAD_INV_ORDER("The ServiceContext with id "
401: + _id.toString() + " has already been set!", 11,
402: CompletionStatus.COMPLETED_MAYBE);
403: }
404:
405: request_ctx.put(_id, service_context);
406: }
407:
408: } // ClientRequestInfoImpl
|