1 23 24 29 30 package com.sun.enterprise.admin.jmx.remote.comm; 31 32 import java.io.BufferedInputStream ; 34 import java.io.BufferedOutputStream ; 35 import java.io.ByteArrayOutputStream ; 36 import java.io.ObjectInputStream ; 37 import java.io.ObjectOutputStream ; 38 import java.io.IOException ; 39 import java.io.Serializable ; 40 import java.net.URLConnection ; 41 import java.net.HttpURLConnection ; 42 import java.net.URL ; 43 44 import java.io.InputStream ; 45 import java.io.OutputStream ; 46 import java.util.logging.Logger ; 47 48 49 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration; 50 51 import com.sun.enterprise.admin.jmx.remote.streams.*; 52 53 54 63 64 class ServletConnection implements IConnection, Runnable { 65 static final String UNKNOWN_HOST = "Unknown host : "; 66 static final String INVALID_HOST_PORT = "Unable to connect to admin-server. Please check if the server is up and running and that the host and port provided are correct."; 67 static final String UNAUTHORIZED_ACCESS = 68 "Invalid user or password"; 69 70 private URLConnection mConnection = null; 71 private ObjectOutputStream mObjectOutStream = null; 72 private ObjectInputStream mObjectInStream = null; 73 74 75 private static final Logger logger = Logger.getLogger( 76 DefaultConfiguration.JMXCONNECTOR_LOGGER); 78 79 private boolean response_received =false; 80 private int streamSize = 0; 81 private int reqSize = 0; 82 83 84 ServletConnection(HttpConnectorAddress a) throws IOException { 85 try{ 86 87 String uri = a.getPath(); 89 if (uri == null || uri.trim().length() == 0) 90 uri = DefaultConfiguration.DEFAULT_SERVLET_CONTEXT_ROOT; 91 mConnection = a.openConnection(uri); 92 93 } 94 catch (IOException ioe){ 95 handleException(ioe); 96 } 97 } 98 99 100 103 public Object receive( ) throws IOException , ClassNotFoundException { 104 Object value = null; 105 try { 106 InputStream in = mConnection.getInputStream(); 107 mObjectInStream = new ObjectInputStream (in); 108 value = mObjectInStream.readObject(); 109 response_received = true; 110 JMXInbandStream.setOutputStream(null, 0); 111 StreamMBeanServerResponseMessage res = 112 (StreamMBeanServerResponseMessage) value; 113 if (res.isStreamAvailable()) { 114 JMXInbandStream.setIncomingStream( 115 new JMXChunkedInputStream(in)); 116 } 117 } 118 catch (IOException ioe) { 119 handleException(ioe); 120 } 121 return value; 122 } 123 124 127 public void send(Serializable req) 128 throws IOException { 129 InputStream in = null; 130 if (req instanceof StreamMBeanServerRequestMessage) { 131 StreamMBeanServerRequestMessage reqm = 132 (StreamMBeanServerRequestMessage) req; 133 JMXInbandStream.setIncomingStream(null); 134 in = JMXInbandStream.getOutgoingStream(); 135 if (in != null) { 136 reqm.setStreamAvailable(true); 137 int len = (int) JMXInbandStream.getOutgoingStreamLength(); 138 139 ByteArrayOutputStream byO = new ByteArrayOutputStream (); 140 ObjectOutputStream oO = new ObjectOutputStream (byO); 141 oO.writeObject(reqm); 142 reqSize = byO.size(); 143 byO.reset(); 144 145 int chunks = (len/8192) + 2; 146 streamSize = reqSize + len + (chunks * 4); 147 ((HttpURLConnection )mConnection).setFixedLengthStreamingMode(streamSize); 148 mConnection.setRequestProperty("Connection", "Close"); 149 } 150 } 151 sendReq(req); 152 if (in != null) 153 sendStream(); 154 } 155 156 private void sendStream() { 157 160 run(); 161 } 162 163 public void run() { 164 OutputStream out = null; 165 try { 166 out = new JMXChunkedOutputStream(mConnection.getOutputStream()); 167 InputStream in = JMXInbandStream.getOutgoingStream(); 168 byte[] bytes = new byte[8192]; 169 int len = 0; 170 int prLen = 0; 171 int flBytes = 0; 172 while ((len = in.read(bytes)) != -1) { 173 out.write(bytes, 0, len); 174 } 175 JMXInbandStream.setOutputStream(null, 0); 176 out.flush(); 177 ((JMXChunkedOutputStream)out).writeEOF(reqSize); 178 } catch (Exception ex) { 179 ex.printStackTrace(); 180 } 181 } 182 183 private void sendReq( Serializable object ) throws IOException { 184 response_received = false; 185 try { 186 mObjectOutStream = new ObjectOutputStream ( 187 new BufferedOutputStream (mConnection.getOutputStream())); 188 mObjectOutStream.writeObject(object); 189 mObjectOutStream.flush(); 190 } 192 catch (IOException ioe) { 193 handleException(ioe); 194 } 195 } 196 197 public void close() { 198 try { 199 mObjectInStream.close(); 200 mObjectOutStream.close(); 201 } 202 catch(Exception e) { 203 throw new RuntimeException (e); 204 } 205 } 206 207 208 private void handleException(IOException e) throws IOException { 209 IOException exception = null; 210 if (e instanceof java.net.UnknownHostException ) { 211 exception = new java.net.UnknownHostException (UNKNOWN_HOST + 212 e.getMessage()); 213 } 214 else if (e instanceof java.net.ConnectException ) { 215 exception = new java.net.ConnectException (INVALID_HOST_PORT); 216 } 217 else { 218 int responseCode = 219 ((HttpURLConnection )mConnection).getResponseCode(); 220 if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) { 221 exception = new IOException (UNAUTHORIZED_ACCESS); 222 } 223 else { 224 exception = e; 225 } 226 } 227 throw exception; 228 } 229 } 230 | Popular Tags |