1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 package IceInternal;
6 
7 import Ice.Instrumentation.CommunicatorObserver;
8 
9 final public class Incoming extends IncomingBase implements Ice.Request
10 {
11     public
Incoming(Instance instance, ResponseHandler responseHandler, Ice.ConnectionI connection, Ice.ObjectAdapter adapter, boolean response, byte compress, int requestId)12     Incoming(Instance instance, ResponseHandler responseHandler, Ice.ConnectionI connection, Ice.ObjectAdapter adapter,
13              boolean response, byte compress, int requestId)
14     {
15         super(instance, responseHandler, connection, adapter, response, compress, requestId);
16     }
17 
18     @Override
19     public Ice.Current
getCurrent()20     getCurrent()
21     {
22         return _current;
23     }
24 
25     @Override
26     public void
reclaim()27     reclaim()
28     {
29         super.reclaim();
30         _inAsync = null;
31         _inParamPos = -1;
32     }
33 
34     public final void
push(Ice.DispatchInterceptorAsyncCallback cb)35     push(Ice.DispatchInterceptorAsyncCallback cb)
36     {
37         if(_interceptorCBs == null)
38         {
39             _interceptorCBs = new java.util.LinkedList<Ice.DispatchInterceptorAsyncCallback>();
40         }
41 
42         _interceptorCBs.addFirst(cb);
43     }
44 
45     public final void
pop()46     pop()
47     {
48         assert _interceptorCBs != null;
49         _interceptorCBs.removeFirst();
50     }
51 
52     public final void
startOver()53     startOver()
54     {
55         if(_inParamPos == -1)
56         {
57             //
58             // That's the first startOver, so almost nothing to do
59             //
60             _inParamPos = _is.pos();
61         }
62         else
63         {
64             // Reset input stream's position and clear response
65             if(_inAsync != null)
66             {
67                 _inAsync.kill(this);
68                 _inAsync = null;
69             }
70             _os = null;
71 
72             _is.pos(_inParamPos);
73         }
74     }
75 
76     public void
setAsync(IncomingAsync inAsync)77     setAsync(IncomingAsync inAsync)
78     {
79         assert(_inAsync == null);
80         _inAsync = inAsync;
81     }
82 
83     public void
setFormat(Ice.FormatType format)84     setFormat(Ice.FormatType format)
85     {
86         _format = format;
87     }
88 
89     public void
invoke(ServantManager servantManager, Ice.InputStream stream)90     invoke(ServantManager servantManager, Ice.InputStream stream)
91     {
92         _is = stream;
93 
94         int start = _is.pos();
95 
96         //
97         // Read the current.
98         //
99         _current.id.ice_readMembers(_is);
100 
101         //
102         // For compatibility with the old FacetPath.
103         //
104         String[] facetPath = _is.readStringSeq();
105         if(facetPath.length > 0)
106         {
107             if(facetPath.length > 1)
108             {
109                 throw new Ice.MarshalException();
110             }
111             _current.facet = facetPath[0];
112         }
113         else
114         {
115             _current.facet = "";
116         }
117 
118         _current.operation = _is.readString();
119         _current.mode = Ice.OperationMode.values()[_is.readByte()];
120         _current.ctx = new java.util.HashMap<String, String>();
121         int sz = _is.readSize();
122         while(sz-- > 0)
123         {
124             String first = _is.readString();
125             String second = _is.readString();
126             _current.ctx.put(first, second);
127         }
128 
129         CommunicatorObserver obsv = _instance.initializationData().observer;
130         if(obsv != null)
131         {
132             // Read the parameter encapsulation size.
133             int size = _is.readInt();
134             _is.pos(_is.pos() - 4);
135 
136             _observer = obsv.getDispatchObserver(_current, _is.pos() - start + size);
137             if(_observer != null)
138             {
139                 _observer.attach();
140             }
141         }
142 
143         //
144         // Don't put the code above into the try block below. Exceptions
145         // in the code above are considered fatal, and must propagate to
146         // the caller of this operation.
147         //
148 
149         if(servantManager != null)
150         {
151             _servant = servantManager.findServant(_current.id, _current.facet);
152             if(_servant == null)
153             {
154                 _locator = servantManager.findServantLocator(_current.id.category);
155                 if(_locator == null && _current.id.category.length() > 0)
156                 {
157                     _locator = servantManager.findServantLocator("");
158                 }
159 
160                 if(_locator != null)
161                 {
162                     try
163                     {
164                         if(_cookie == null)
165                         {
166                             _cookie = new Ice.LocalObjectHolder();
167                         }
168                         assert(_cookie.value == null);
169                         _servant = _locator.locate(_current, _cookie);
170                     }
171                     catch(java.lang.Throwable ex)
172                     {
173                         skipReadParams(); // Required for batch requests.
174                         handleException(ex, false);
175                         return;
176                     }
177                 }
178             }
179         }
180 
181         if(_servant == null)
182         {
183             try
184             {
185                 if(servantManager != null && servantManager.hasServant(_current.id))
186                 {
187                     throw new Ice.FacetNotExistException(_current.id, _current.facet, _current.operation);
188                 }
189                 else
190                 {
191                     throw new Ice.ObjectNotExistException(_current.id, _current.facet, _current.operation);
192                 }
193             }
194             catch(java.lang.Exception ex)
195             {
196                 skipReadParams(); // Required for batch requests.
197                 handleException(ex, false);
198                 return;
199             }
200         }
201 
202         try
203         {
204             if(_instance.useApplicationClassLoader())
205             {
206                 Thread.currentThread().setContextClassLoader(_servant.getClass().getClassLoader());
207             }
208 
209             _servant._iceDispatch(this, _current);
210 
211             //
212             // If the request was not dispatched asynchronously, send the response.
213             //
214             if(_inAsync == null)
215             {
216                 response(false);
217             }
218         }
219         catch(IceInternal.ServantError ex)
220         {
221             throw ex;
222         }
223         catch(java.lang.Throwable ex)
224         {
225             if(_inAsync != null)
226             {
227                 try
228                 {
229                     _inAsync.kill(this);
230                     _inAsync = null;
231                 }
232                 catch(Ice.ResponseSentException exc)
233                 {
234                     if(ex instanceof java.lang.Error)
235                     {
236                         throw new ServantError((java.lang.Error)ex);
237                     }
238 
239                     if(_instance.initializationData().properties.getPropertyAsIntWithDefault("Ice.Warn.Dispatch", 1) > 1)
240                     {
241                         warning(ex);
242                     }
243                     return;
244                 }
245             }
246             exception(ex, false);
247         }
248         finally
249         {
250             if(_instance.useApplicationClassLoader())
251             {
252                 Thread.currentThread().setContextClassLoader(null);
253             }
254         }
255     }
256 
257     public final Ice.InputStream
startReadParams()258     startReadParams()
259     {
260         //
261         // Remember the encoding used by the input parameters, we'll
262         // encode the response parameters with the same encoding.
263         //
264         _current.encoding = _is.startEncapsulation();
265         return _is;
266     }
267 
268     public final void
endReadParams()269     endReadParams()
270     {
271         _is.endEncapsulation();
272     }
273 
274     public final void
readEmptyParams()275     readEmptyParams()
276     {
277         _current.encoding = _is.skipEmptyEncapsulation();
278     }
279 
280     public final void
skipReadParams()281     skipReadParams()
282     {
283         _current.encoding = _is.skipEncapsulation();
284     }
285 
286     public final byte[]
readParamEncaps()287     readParamEncaps()
288     {
289         _current.encoding = new Ice.EncodingVersion();
290         return _is.readEncapsulation(_current.encoding);
291     }
292 
293     public Incoming next; // For use by ConnectionI.
294 
295     private Ice.InputStream _is;
296     private IncomingAsync _inAsync;
297     private int _inParamPos = -1;
298 }
299