KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > iiop > IiopRequest


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.iiop;
31
32 import com.caucho.server.connection.Connection;
33 import com.caucho.server.port.ServerRequest;
34 import com.caucho.vfs.ReadStream;
35 import com.caucho.vfs.WriteStream;
36
37 import java.io.IOException JavaDoc;
38 import java.net.InetAddress JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * Protocol specific information for each request. ServerRequest
44  * is reused to reduce memory allocations.
45  *
46  * <p>ServerRequests are created by Server.createRequest()
47  */

48 public class IiopRequest implements ServerRequest {
49   private static final Logger JavaDoc log =
50     Logger.getLogger(IiopRequest.class.getName());
51
52   Connection _conn;
53   IiopProtocol _server;
54   IiopReader _reader;
55
56   ClassLoader JavaDoc _loader;
57
58   StreamMessageWriter _messageWriter;
59   IiopWriter _writer10;
60   IiopWriter _writer11;
61   IiopWriter _writer12;
62
63   ReadStream _readStream;
64   WriteStream _writeStream;
65
66   CosServer _cos;
67   IiopSkeleton _cosSkel;
68
69   String JavaDoc _hostName;
70   int _port;
71   
72   IiopRequest(IiopProtocol server, Connection conn)
73   {
74     _server = server;
75     _conn = conn;
76
77     _reader = new IiopReader();
78
79     _messageWriter = new StreamMessageWriter();
80     
81     _writer10 = new Iiop10Writer();
82     _writer10.init(_messageWriter);
83     _writer11 = new Iiop11Writer();
84     _writer11.init(_messageWriter);
85     _writer12 = new Iiop12Writer();
86     _writer12.init(_messageWriter);
87     
88     _cos = _server.getCos();
89
90     _loader = Thread.currentThread().getContextClassLoader();
91   }
92   /**
93    * Initialize the connection. At this point, the current thread is the
94    * connection thread.
95    */

96   public void init()
97   {
98   }
99
100   public boolean isWaitForRead()
101   {
102     return true;
103   }
104   
105   /**
106    * Handles a new connection. The controlling TcpServer may call
107    * handleConnection again after the connection completes, so
108    * the implementation must initialize any variables for each connection.
109    *
110    * @param conn Information about the connection, including buffered
111    * read and write streams.
112    */

113   public boolean handleRequest() throws IOException JavaDoc
114   {
115     Thread JavaDoc thread = Thread.currentThread();
116     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
117
118     try {
119       thread.setContextClassLoader(_loader);
120     
121       log.finer("IIOP[" + _conn.getId() + "]: handle request");
122     
123       _readStream = _conn.getReadStream();
124       _writeStream = _conn.getWriteStream();
125
126       if (_cosSkel != null) {
127       }
128       else {
129     InetAddress JavaDoc local = _conn.getLocalAddress();
130     _hostName = local.getHostName();
131     _port = _conn.getLocalPort();
132
133     _cos.setHost(_hostName);
134     _cos.setPort(_port);
135
136     _cosSkel = new IiopSkeleton(_cos, _cos.getClass(), _loader,
137                     _hostName, _port, "/NameService");
138       }
139
140       int ch = _readStream.read();
141
142       if (ch < 0) {
143     log.finer("IIOP[" + _conn.getId() + "]: end of stream");
144     return false;
145       }
146
147       _readStream.unread();
148
149       _reader.init(_readStream);
150
151       _messageWriter.init(_writeStream);
152       IiopWriter writer = _writer10;
153
154       _reader.readRequest();
155
156       switch (_reader.getMinorVersion()) {
157       case 0:
158     writer = _writer10;
159     break;
160       case 1:
161     writer = _writer11;
162     break;
163       case 2:
164     writer = _writer12;
165     break;
166       default:
167     writer = _writer10;
168     break;
169       }
170
171       writer.setHost(_hostName);
172       writer.setPort(_port);
173
174       String JavaDoc oid = _reader.getObjectKey().toString();
175
176       if (log.isLoggable(Level.FINER))
177     log.finer("IIOP[" + _conn.getId() + "] OID: " + oid);
178
179       try {
180     if (oid.equals("INIT")) {
181       String JavaDoc str = _reader.readString();
182
183       writer.startReplyOk(_reader.getRequestId());
184
185       if (str.equals("NameService")) {
186         String JavaDoc nameService = "IDL:omg.org/CosNaming/NamingContext:1.0";
187
188         IOR ior = new IOR(nameService, _hostName, _port, "/NameService");
189         byte []bytes = ior.getByteArray();
190         writer.write(bytes, 0, bytes.length);
191       }
192       else
193         writer.writeNullIOR();
194     }
195     else if (oid.equals("/NameService")) {
196       /*
197         cos.service(reader, writer);
198       */

199       _cosSkel.service(_cosSkel.getObject(), _reader, writer);
200     }
201     else {
202       IiopSkeleton skel = _server.getService(_hostName, _port, oid);
203
204       if (skel != null) {
205         skel.service(skel.getObject(), _reader, writer);
206       }
207       else {
208         log.fine("IIOP[" + _conn.getId() + "] can't find service: " + oid);
209
210         throw new IOException JavaDoc("bad oid: " + oid);
211       }
212
213       log.fine("IIOP[" + _conn.getId() + "] complete request");
214     }
215       } catch (org.omg.CORBA.SystemException JavaDoc e) {
216     e.printStackTrace();
217     
218     log.log(Level.WARNING, e.toString(), e);
219       
220     writer.startReplySystemException(_reader.getRequestId(),
221                      e.toString(),
222                      e.minor,
223                      e.completed.value());
224       } catch (Throwable JavaDoc e) {
225     log.log(Level.WARNING, e.toString(), e);
226
227     // ejb/1110 vs ejb/114p
228
//writer.startReplyUserException(_reader.getRequestId(), e.toString());
229
// ejb/1110
230

231     writer.startReplyUserException(_reader.getRequestId());
232     String JavaDoc exName = e.getClass().getName().replace('.', '/');
233     if (exName.length() > 20)
234       exName = exName.substring(0, 20);
235     writer.write_string("IDL:" + exName + ":1.0");
236     writer.write_value(e);
237       }
238       
239       _messageWriter.close();
240
241       _reader.completeRead();
242
243       if (log.isLoggable(Level.FINER))
244     log.finer("IIOP[" + _conn.getId() + "]: recycle");
245
246       return true;
247     } catch (Throwable JavaDoc e) {
248       e.printStackTrace();
249       log.log(Level.WARNING, "IIOP[" + _conn.getId() + "] " + e.toString(), e);
250       return false;
251     } finally {
252       thread.setContextClassLoader(oldLoader);
253     }
254   }
255
256   public void protocolCloseEvent()
257   {
258   }
259
260 }
261
Popular Tags