summaryrefslogtreecommitdiff
path: root/java/src/IceInternal/Incoming.java
blob: 75075840fc52c6f83f8adc36f01d929d30193a5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package IceInternal;

final public class Incoming extends IncomingBase
{
    public
    Incoming(Instance instance, Ice.ConnectionI connection, Ice.ObjectAdapter adapter, boolean response, byte compress,
             int requestId)
    {
        super(instance, connection, adapter, response, compress, requestId);

        _is = new BasicStream(instance);
    }

    //
    // These functions allow this object to be reused, rather than reallocated.
    //
    public void
    reset(Instance instance, Ice.ConnectionI connection, Ice.ObjectAdapter adapter, boolean response, byte compress,
          int requestId)
    {
        if(_is == null)
        {
            _is = new BasicStream(instance);
        }

        super.reset(instance, connection, adapter, response, compress, requestId);
    }

    public void
    reclaim()
    {
        if(_is != null)
        {
            _is.reset();
        }

        super.reclaim();
    }

    public void
    invoke(ServantManager servantManager)
    {
        //
        // Read the current.
        //
        _current.id.__read(_is);

        //
        // For compatibility with the old FacetPath.
        //
        String[] facetPath = _is.readStringSeq();
        if(facetPath.length > 0)
        {
            if(facetPath.length > 1)
            {
                throw new Ice.MarshalException();
            }
            _current.facet = facetPath[0];
        }
        else
        {
            _current.facet = "";
        }

        _current.operation = _is.readString();
        _current.mode = Ice.OperationMode.convert(_is.readByte());
        int sz = _is.readSize();
        while(sz-- > 0)
        {
            String first = _is.readString();
            String second = _is.readString();
            if(_current.ctx == null)
            {
                _current.ctx = new java.util.HashMap();
            }
            _current.ctx.put(first, second);
        }

        _is.startReadEncaps();

        if(_response)
        {
            assert(_os.size() == Protocol.headerSize + 4); // Dispatch status position.
            _os.writeByte((byte)0);
            _os.startWriteEncaps();
        }

        // Initialize status to some value, to keep the compiler happy.
        DispatchStatus status = DispatchStatus.DispatchOK;
        
        //
        // Don't put the code above into the try block below. Exceptions
        // in the code above are considered fatal, and must propagate to
        // the caller of this operation.
        //

        try
        {
            try
            {
                if(servantManager != null)
                {
                    _servant = servantManager.findServant(_current.id, _current.facet);
                    if(_servant == null)
                    {
                        _locator = servantManager.findServantLocator(_current.id.category);
                        if(_locator == null && _current.id.category.length() > 0)
                        {
                            _locator = servantManager.findServantLocator("");
                        }
                        if(_locator != null)
                        {
                            _servant = _locator.locate(_current, _cookie);
                        }
                    }
                    if(_servant == null)
                    {
                        _locator = servantManager.findServantLocator("");
                        if(_locator != null)
                        {
                            _servant = _locator.locate(_current, _cookie);
                        }
                    }
                }
                if(_servant == null)
                {
                    if(servantManager != null && servantManager.hasServant(_current.id))
                    {
                        status = DispatchStatus.DispatchFacetNotExist;
                    }
                    else
                    {
                        status = DispatchStatus.DispatchObjectNotExist;
                    }
                }
                else
                {
                    status = _servant.__dispatch(this, _current);
                }
            }
            finally
            {
                if(_locator != null && _servant != null && status != DispatchStatus.DispatchAsync)
                {
                    _locator.finished(_current, _servant, _cookie.value);
                }
            }
        }
        /* Not possible in Java - UserExceptions are checked exceptions
        catch(Ice.UserException ex)
        {
        // ...
        }
        */
        catch(java.lang.Exception ex)
        {
            _is.endReadEncaps();
            __handleException(ex);
            return;
        }
        
        //
        // Don't put the code below into the try block above. Exceptions
        // in the code below are considered fatal, and must propagate to
        // the caller of this operation.
        //

        _is.endReadEncaps();

        //
        // DispatchAsync is "pseudo dispatch status", used internally
        // only to indicate async dispatch.
        //
        if(status == DispatchStatus.DispatchAsync)
        {
            //
            // If this was an asynchronous dispatch, we're done here.
            //
            return;
        }

        if(_response)
        {
            _os.endWriteEncaps();
            
            if(status != DispatchStatus.DispatchOK && status != DispatchStatus.DispatchUserException)
            {
                assert(status == DispatchStatus.DispatchObjectNotExist ||
                       status == DispatchStatus.DispatchFacetNotExist ||
                       status == DispatchStatus.DispatchOperationNotExist);
                
                _os.resize(Protocol.headerSize + 4, false); // Dispatch status position.
                _os.writeByte((byte)status.value());
                
                _current.id.__write(_os);

                //
                // For compatibility with the old FacetPath.
                //
                if(_current.facet == null || _current.facet.length() == 0)
                {
                    _os.writeStringSeq(null);
                }
                else
                {
                    String[] facetPath2 = { _current.facet };
                    _os.writeStringSeq(facetPath2);
                }

                _os.writeString(_current.operation);
            }
            else
            {
                int save = _os.pos();
                _os.pos(Protocol.headerSize + 4); // Dispatch status position.
                _os.writeByte((byte)status.value());
                _os.pos(save);
            }

            _connection.sendResponse(_os, _compress);
        }
        else
        {
            _connection.sendNoResponse();
        }
    }

    public BasicStream
    is()
    {
        return _is;
    }

    public BasicStream
    os()
    {
        return _os;
    }

    public Incoming next; // For use by ConnectionI.

    private BasicStream _is;
}