KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > corba > ServerRequestImpl


1 /*
2  * @(#)ServerRequestImpl.java 1.73 04/04/07
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Licensed Materials - Property of IBM
9  * RMI-IIOP v1.0
10  * Copyright IBM Corp. 1998 1999 All Rights Reserved
11  *
12  * US Government Users Restricted Rights - Use, duplication or
13  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
14  */

15
16 package com.sun.corba.se.impl.corba;
17
18 import org.omg.CORBA.Any JavaDoc;
19 import org.omg.CORBA.Context JavaDoc;
20 import org.omg.CORBA.NamedValue JavaDoc;
21 import org.omg.CORBA.NVList JavaDoc;
22 import org.omg.CORBA.TypeCode JavaDoc;
23 import org.omg.CORBA.TCKind JavaDoc;
24 import org.omg.CORBA.ServerRequest JavaDoc;
25 import org.omg.CORBA.Bounds JavaDoc;
26 import org.omg.CORBA.portable.InputStream JavaDoc;
27 import org.omg.CORBA.portable.OutputStream JavaDoc;
28 import org.omg.CORBA.CompletionStatus JavaDoc;
29
30 import com.sun.corba.se.spi.orb.ORB ;
31 import com.sun.corba.se.spi.protocol.CorbaMessageMediator;
32
33 import com.sun.corba.se.spi.logging.CORBALogDomains ;
34 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
35
36 public class ServerRequestImpl extends ServerRequest JavaDoc {
37
38     ///////////////////////////////////////////////////////////////////////////
39
// data members
40

41     private ORB _orb = null;
42     private ORBUtilSystemException _wrapper = null;
43     private String JavaDoc _opName = null;
44     private NVList JavaDoc _arguments = null;
45     private Context JavaDoc _ctx = null;
46     private InputStream JavaDoc _ins = null;
47         
48     // booleans to check for various operation invocation restrictions
49
private boolean _paramsCalled = false;
50     private boolean _resultSet = false;
51     private boolean _exceptionSet = false;
52     private Any JavaDoc _resultAny = null;
53     private Any JavaDoc _exception = null;
54
55
56     public ServerRequestImpl (CorbaMessageMediator req, ORB orb) {
57         _opName = req.getOperationName();
58         _ins = (InputStream JavaDoc)req.getInputObject();
59         _ctx = null; // if we support contexts, this would
60
// presumably also be available on
61
// the server invocation
62
_orb = orb;
63     _wrapper = ORBUtilSystemException.get( orb,
64         CORBALogDomains.OA_INVOCATION ) ;
65     }
66
67     public String JavaDoc operation() {
68         return _opName;
69     }
70
71     public void arguments(NVList JavaDoc args)
72     {
73         if (_paramsCalled)
74         throw _wrapper.argumentsCalledMultiple() ;
75
76     if (_exceptionSet)
77         throw _wrapper.argumentsCalledAfterException() ;
78
79     if (args == null )
80         throw _wrapper.argumentsCalledNullArgs() ;
81
82         _paramsCalled = true;
83
84         NamedValue JavaDoc arg = null;
85         for (int i=0; i < args.count() ; i++) {
86             try {
87             arg = args.item(i);
88             } catch (Bounds JavaDoc e) {
89         throw _wrapper.boundsCannotOccur(e) ;
90             }
91
92         try {
93                 if ((arg.flags() == org.omg.CORBA.ARG_IN.value) ||
94                 (arg.flags() == org.omg.CORBA.ARG_INOUT.value)) {
95                 // unmarshal the value into the Any
96
arg.value().read_value(_ins, arg.value().type());
97                 }
98         } catch ( Exception JavaDoc ex ) {
99         throw _wrapper.badArgumentsNvlist( ex ) ;
100         }
101         }
102
103         // hang on to the NVList for marshaling the result
104
_arguments = args;
105
106     _orb.getPIHandler().setServerPIInfo( _arguments );
107     _orb.getPIHandler().invokeServerPIIntermediatePoint();
108     }
109     
110     public void set_result(Any JavaDoc res) {
111         // check for invocation restrictions
112
if (!_paramsCalled)
113         throw _wrapper.argumentsNotCalled() ;
114     if (_resultSet)
115         throw _wrapper.setResultCalledMultiple() ;
116     if (_exceptionSet)
117         throw _wrapper.setResultAfterException() ;
118     if ( res == null )
119         throw _wrapper.setResultCalledNullArgs() ;
120
121         _resultAny = res;
122         _resultSet = true;
123
124     // Notify portable interceptors of the result so that
125
// ServerRequestInfo.result() functions as desired.
126
_orb.getPIHandler().setServerPIInfo( _resultAny );
127
128         // actual marshaling of the reply msg header and params takes place
129
// after the DSI returns control to the ORB.
130
}
131
132     public void set_exception(Any JavaDoc exc)
133     {
134     // except can be called by the DIR at any time (CORBA 2.2 section 6.3).
135

136     if ( exc == null )
137         throw _wrapper.setExceptionCalledNullArgs() ;
138
139     // Ensure that the Any contains a SystemException or a
140
// UserException. If the UserException is not a declared exception,
141
// the client will get an UNKNOWN exception.
142
TCKind JavaDoc kind = exc.type().kind();
143     if ( kind != TCKind.tk_except )
144         throw _wrapper.setExceptionCalledBadType() ;
145
146         _exception = exc;
147
148     // Inform Portable interceptors of the exception that was set
149
// so sending_exception can return the right value.
150
_orb.getPIHandler().setServerPIExceptionInfo( _exception );
151
152     // The user can only call arguments once and not at all after
153
// set_exception. (internal flags ensure this). However, the user
154
// can call set_exception multiple times. Therefore, we only
155
// invoke receive_request the first time set_exception is
156
// called (if they haven't already called arguments).
157
if( !_exceptionSet && !_paramsCalled ) {
158         // We need to invoke intermediate points here.
159
_orb.getPIHandler().invokeServerPIIntermediatePoint();
160     }
161
162         _exceptionSet = true;
163
164         // actual marshaling of the reply msg header and exception takes place
165
// after the DSI returns control to the ORB.
166
}
167     
168
169     /** This is called from the ORB after the DynamicImplementation.invoke
170      * returns. Here we set the result if result() has not already been called.
171      * @return the exception if there is one (then ORB will not call
172      * marshalReplyParams()) otherwise return null.
173      */

174     public Any JavaDoc checkResultCalled()
175     {
176     // Two things to be checked (CORBA 2.2 spec, section 6.3):
177
// 1. Unless it calls set_exception(), the DIR must call arguments()
178
// exactly once, even if the operation signature contains
179
// no parameters.
180
// 2. Unless set_exception() is called, if the invoked operation has a
181
// non-void result type, set_result() must be called exactly once
182
// before the DIR returns.
183

184     if ( _paramsCalled && _resultSet ) // normal invocation return
185
return null;
186         else if ( _paramsCalled && !_resultSet && !_exceptionSet ) {
187             try {
188         // Neither a result nor an exception has been set.
189
// Assume that the return type is void. If this is not so,
190
// the client will throw a MARSHAL exception while
191
// unmarshaling the return value.
192
TypeCode JavaDoc result_tc = _orb.get_primitive_tc(
193             org.omg.CORBA.TCKind.tk_void);
194             _resultAny = _orb.create_any();
195                 _resultAny.type(result_tc);
196             _resultSet = true;
197
198             return null;
199             } catch ( Exception JavaDoc ex ) {
200         throw _wrapper.dsiResultException(
201             CompletionStatus.COMPLETED_MAYBE, ex ) ;
202             }
203         } else if ( _exceptionSet )
204             return _exception;
205         else {
206         throw _wrapper.dsimethodNotcalled(
207         CompletionStatus.COMPLETED_MAYBE ) ;
208         }
209     }
210
211     /** This is called from the ORB after the DynamicImplementation.invoke
212      * returns. Here we marshal the return value and inout/out params.
213      */

214     public void marshalReplyParams(OutputStream JavaDoc os)
215     {
216         // marshal the operation return value
217
_resultAny.write_value(os);
218
219         // marshal the inouts/outs
220
NamedValue JavaDoc arg = null;
221
222         for (int i=0; i < _arguments.count() ; i++) {
223             try {
224                 arg = _arguments.item(i);
225             } catch (Bounds JavaDoc e) {}
226
227             if ((arg.flags() == org.omg.CORBA.ARG_OUT.value) ||
228                 (arg.flags() == org.omg.CORBA.ARG_INOUT.value)) {
229                 arg.value().write_value(os);
230             }
231         }
232     }
233
234     public Context JavaDoc ctx()
235     {
236         if ( !_paramsCalled || _resultSet || _exceptionSet )
237         throw _wrapper.contextCalledOutOfOrder() ;
238
239     throw _wrapper.contextNotImplemented() ;
240     }
241 }
242
Popular Tags