1 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 ; 38 import java.net.InetAddress ; 39 import java.util.logging.Level ; 40 import java.util.logging.Logger ; 41 42 48 public class IiopRequest implements ServerRequest { 49 private static final Logger log = 50 Logger.getLogger(IiopRequest.class.getName()); 51 52 Connection _conn; 53 IiopProtocol _server; 54 IiopReader _reader; 55 56 ClassLoader _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 _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 96 public void init() 97 { 98 } 99 100 public boolean isWaitForRead() 101 { 102 return true; 103 } 104 105 113 public boolean handleRequest() throws IOException 114 { 115 Thread thread = Thread.currentThread(); 116 ClassLoader 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 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 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 str = _reader.readString(); 182 183 writer.startReplyOk(_reader.getRequestId()); 184 185 if (str.equals("NameService")) { 186 String 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 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 ("bad oid: " + oid); 211 } 212 213 log.fine("IIOP[" + _conn.getId() + "] complete request"); 214 } 215 } catch (org.omg.CORBA.SystemException 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 e) { 225 log.log(Level.WARNING, e.toString(), e); 226 227 231 writer.startReplyUserException(_reader.getRequestId()); 232 String 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 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 |