KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > server > MessageHandler


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.rmi.iiop.server;
19
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import java.net.Socket JavaDoc;
24
25 import org.apache.geronimo.interop.SystemException;
26 import org.apache.geronimo.interop.adapter.Adapter;
27 import org.apache.geronimo.interop.adapter.AdapterManager;
28 import org.apache.geronimo.interop.naming.NameService;
29 import org.apache.geronimo.interop.rmi.iiop.BadMagicException;
30 import org.apache.geronimo.interop.rmi.iiop.CdrInputStream;
31 import org.apache.geronimo.interop.rmi.iiop.CdrOutputStream;
32 import org.apache.geronimo.interop.rmi.iiop.GiopMessage;
33 import org.apache.geronimo.interop.rmi.iiop.ObjectInputStream;
34 import org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream;
35 import org.apache.geronimo.interop.rmi.iiop.UnsupportedProtocolVersionException;
36 import org.apache.geronimo.interop.util.UTF8;
37 import org.omg.GIOP.KeyAddr;
38 import org.omg.GIOP.LocateReplyHeader_1_2;
39 import org.omg.GIOP.LocateRequestHeader_1_2;
40 import org.omg.GIOP.LocateStatusType_1_2;
41 import org.omg.GIOP.MsgType_1_1;
42 import org.omg.GIOP.ProfileAddr;
43 import org.omg.GIOP.ReferenceAddr;
44 import org.omg.GIOP.ReplyHeader_1_2;
45 import org.omg.GIOP.ReplyStatusType_1_2;
46 import org.omg.GIOP.RequestHeader_1_2;
47 import org.omg.GIOP.TargetAddress;
48 import org.omg.IOP.ServiceContext JavaDoc;
49
50 public class MessageHandler {
51
52     private AdapterManager adapterManager;
53     private boolean simpleIDL;
54     private boolean writeSystemExceptionStackTrace;
55     private NameService nameService = NameService.getInstance();
56
57     public MessageHandler( AdapterManager adapterManager, boolean simpleIDL,
58                            boolean writeSystemExceptionStackTrace )
59     {
60         this.adapterManager = adapterManager;
61         this.simpleIDL = simpleIDL;
62         this.writeSystemExceptionStackTrace = writeSystemExceptionStackTrace;
63     }
64
65     public void service(Socket JavaDoc socket) throws Exception JavaDoc {
66
67         InputStream JavaDoc in;
68         OutputStream JavaDoc out;
69
70         String JavaDoc clientHostName;
71         String JavaDoc clientHostAddress;
72         String JavaDoc clientInfo;
73
74         in = socket.getInputStream();
75         out = socket.getOutputStream();
76
77         InetAddress JavaDoc addr = socket.getInetAddress();
78         clientHostName = addr.getHostName();
79         clientHostAddress = addr.getHostAddress();
80         clientInfo = clientHostName;
81
82         if (!clientHostAddress.equals(clientHostName)) {
83             clientInfo += " (" + clientHostAddress + ")";
84         }
85
86         boolean firstMessage = true;
87         CdrInputStream input = CdrInputStream.getInstance();
88         CdrOutputStream output = CdrOutputStream.getInstance();
89         CdrOutputStream results = CdrOutputStream.getInstance();
90
91         for (; ;) {
92             boolean sendResponse = true;
93             GiopMessage inputMessage;
94
95             try {
96                 inputMessage = input.receive_message( in, clientInfo );
97                 firstMessage = false;
98             } catch (BadMagicException ex) {
99                 if (firstMessage) {
100                     warnBadMagic(clientInfo, ex);
101                 } else {
102                     warnBadMagicBadSize(clientInfo, ex);
103                 }
104                 closeStreams( in, out );
105                 return;
106             } catch (UnsupportedProtocolVersionException ex) {
107                 warnGiopVersion( clientInfo, ex);
108                 closeStreams( in, out );
109                 return;
110             } catch (Exception JavaDoc ex) {
111                 if (input.getOffset() > 0) {
112                     ex.printStackTrace();
113                     warnReceiveFailed( clientInfo, ex);
114                 }
115                 // Otherwise client shutdown was not in the middle of a
116
// request, i.e. probably 'normal' and unworthy of a
117
// log message.
118
closeStreams( in, out );
119                 return;
120             }
121
122             output.setGiopVersion(input.getGiopVersion());
123
124             switch (inputMessage.type) {
125                 case MsgType_1_1._Request:
126                     processRequest(input, output, results, inputMessage.request, clientInfo);
127                     if ((inputMessage.request.response_flags & 1) == 0) {
128                         sendResponse = false; // oneway request
129
}
130                     break;
131                 case MsgType_1_1._LocateRequest:
132                     processLocateRequest(output, inputMessage.locateRequest);
133                     break;
134                 default:
135                     throw new SystemException("TODO: message type = " + inputMessage.type);
136             }
137
138             if (sendResponse) {
139                 try {
140                     if(inputMessage.httpTunneling)
141                     {
142                         output.send_http_response( out, clientInfo );
143                     }
144                     else
145                     {
146                         output.send_message( out, clientInfo );
147                     }
148                 } catch (Exception JavaDoc ex) {
149                     warnSendFailed(clientInfo, ex);
150                     closeStreams( in, out );
151                     return;
152                 }
153             }
154
155             input.reset();
156             output.reset();
157             results.reset();
158         }
159     }
160
161     protected void closeStreams( InputStream JavaDoc in, OutputStream JavaDoc out ) {
162         try {
163             if (in != null) {
164                 in.close();
165             }
166         } catch (Exception JavaDoc ignore) {
167         }
168
169         try {
170             if (out != null) {
171                 out.close();
172             }
173         } catch (Exception JavaDoc ignore) {
174         }
175     }
176
177     protected byte[] getObjectKey(TargetAddress target) {
178         switch (target.discriminator()) {
179             case KeyAddr.value:
180                 return target.object_key();
181             case ProfileAddr.value:
182             case ReferenceAddr.value:
183                 throw new SystemException("TODO");
184             default:
185                 throw new IllegalArgumentException JavaDoc("target discriminator = " + target.discriminator());
186         }
187     }
188
189     protected void processRequest(CdrInputStream parameters,
190                                   CdrOutputStream output,
191                                   CdrOutputStream results,
192                                   RequestHeader_1_2 request,
193                                   String JavaDoc clientInfo
194                                   ) {
195         byte[] objectKey = getObjectKey(request.target);
196         int keyLength = objectKey.length;
197         int keyType = keyLength == 0 ? 0 : objectKey[0];
198
199         ReplyHeader_1_2 reply = new ReplyHeader_1_2();
200         reply.request_id = request.request_id;
201
202         ObjectInputStream objectIn;
203         ObjectOutputStream objectOut;
204
205         if (simpleIDL || keyType == 'N' || keyType == 'J') {
206             // Name Service and JMS use simple IDL interoperability.
207
objectIn = org.apache.geronimo.interop.rmi.iiop.SimpleObjectInputStream.getInstance(parameters);
208             objectOut = org.apache.geronimo.interop.rmi.iiop.SimpleObjectOutputStream.getInstance(results);
209         } else {
210             // Otherwise use RMI-IIOP interoperability.
211
objectIn = org.apache.geronimo.interop.rmi.iiop.ObjectInputStream.getInstance(parameters);
212             objectOut = org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream.getInstance(results);
213         }
214
215         try {
216             String JavaDoc objectName = null;
217             for (int colonPos = 0; colonPos < keyLength; colonPos++) {
218                 if (objectKey[colonPos] == ':') {
219                     objectName = UTF8.toString(objectKey, 0, colonPos);
220                     int newKeyLength = keyLength - colonPos - 1;
221                     byte[] newObjectKey = new byte[newKeyLength];
222                     System.arraycopy(objectKey, colonPos + 1, newObjectKey, 0, newKeyLength);
223                     objectKey = newObjectKey;
224                     break;
225                 }
226             }
227
228             if (objectName == null) {
229                 objectName = UTF8.toString(objectKey);
230             }
231
232             processServiceContext(request);
233
234             Object JavaDoc object;
235             try
236             {
237                 object = nameService.lookup(objectName);
238             }
239             catch (javax.naming.NameNotFoundException JavaDoc notFound)
240             {
241                 warnLookupFailed(clientInfo, notFound);
242                 throw new org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc(objectName);
243             }
244
245             Adapter adapter = (Adapter)object;
246             if (adapter != null)
247             {
248                 adapter.invoke(request.operation, objectKey, objectIn, objectOut);
249
250                 if (objectOut.hasException()) {
251                     reply.reply_status = ReplyStatusType_1_2.USER_EXCEPTION;
252                 } else {
253                     reply.reply_status = ReplyStatusType_1_2.NO_EXCEPTION;
254                 }
255
256                 output.write_reply(reply, results);
257             } else {
258                 throw new org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc(objectName);
259             }
260         } catch (Exception JavaDoc ex) {
261             warnSystemException(clientInfo, ex);
262             results = CdrOutputStream.getInstance(); // in case we already wrote to it
263
results.write_SystemException(ex, writeSystemExceptionStackTrace);
264             reply.reply_status = ReplyStatusType_1_2.SYSTEM_EXCEPTION;
265             output.write_reply(reply, results);
266         }
267     }
268
269     protected void processLocateRequest(CdrOutputStream output, LocateRequestHeader_1_2 request) {
270         // Fake LocateReply, pretend we host any object.
271
// Since we never move objects, this is sufficient.
272
LocateReplyHeader_1_2 reply = new LocateReplyHeader_1_2();
273         reply.request_id = request.request_id;
274         reply.locate_status = LocateStatusType_1_2.OBJECT_HERE;
275         output.write_reply(reply);
276     }
277
278     protected void processServiceContext(RequestHeader_1_2 request) {
279         ServiceContext JavaDoc[] contextList = request.service_context;
280         int n = contextList.length;
281         String JavaDoc username = null;
282         String JavaDoc password = null;
283
284         for (int i = 0; i < n; i++) {
285             ServiceContext JavaDoc context = contextList[i];
286             int tag = context.context_id;
287
288             /*
289             if (tag == SecurityInfo.TAG_USERNAME)
290             {
291                 username = SecurityInfo.decode(context.context_data);
292             }
293             else if (tag == SecurityInfo.TAG_PASSWORD)
294             {
295                 password = SecurityInfo.decode(context.context_data);
296             }
297             */

298
299             // TODO: Is the ServiceContext a CSIv2 Security Context?
300
}
301     }
302
303     // log methods
304

305     protected void warnBadMagic(String JavaDoc clientHost, Exception JavaDoc ex) {
306         System.out.println("MH.warnBadMagic: clientHost: " + clientHost + ", ex = " + ex);
307     }
308
309     protected void warnBadMagicBadSize(String JavaDoc clientHost, Exception JavaDoc ex) {
310         System.out.println("MH.warnBadMagicBadSize: clientHost: " + clientHost + ", ex = " + ex);
311     }
312
313     protected void warnGiopVersion(String JavaDoc clientHost, Exception JavaDoc ex) {
314         System.out.println("MH.warnGiopVersion: clientHost: " + clientHost + ", ex = " + ex);
315     }
316
317     protected void warnInvokeFailedNoRemoteInterface(String JavaDoc clientHost, Object JavaDoc object, Class JavaDoc type) {
318         System.out.println("MH.warnInvokeFailedNoRemoteInterface: clientHost: " + clientHost + ", object = " + object + ", type: " + type);
319     }
320
321     protected void warnLookupFailed(String JavaDoc clientHost, Exception JavaDoc ex) {
322         System.out.println("MH.warnLookupFailed: clientHost: " + clientHost + ", ex = " + ex);
323     }
324
325     protected void warnReceiveFailed(String JavaDoc clientHost, Exception JavaDoc ex) {
326         System.out.println("MH.warnReceiveFailed: clientHost: " + clientHost + ", ex = " + ex);
327     }
328
329     protected void warnSendFailed(String JavaDoc clientHost, Exception JavaDoc ex) {
330         System.out.println("MH.warnSendFailed: clientHost: " + clientHost + ", ex = " + ex);
331     }
332
333     protected void warnSystemException(String JavaDoc clientHost, Exception JavaDoc ex) {
334         System.out.println("MH.warnSystemException: clientHost: " + clientHost + ", ex = " + ex);
335     }
336
337 }
338
Popular Tags