1 23 24 package com.sun.enterprise.admin.comm; 25 26 import java.io.BufferedInputStream ; 28 import java.io.BufferedOutputStream ; 29 import java.io.ObjectInputStream ; 30 import java.io.ObjectOutputStream ; 31 import java.io.IOException ; 32 import java.io.Serializable ; 33 import java.net.URLConnection ; 34 import java.net.HttpURLConnection ; 35 import java.net.URL ; 36 37 import com.sun.enterprise.admin.common.*; 39 import com.sun.enterprise.admin.common.constant.AdminConstants; 40 import com.sun.enterprise.admin.util.*; 41 42 50 class ServletConnection implements IConnection 51 { 52 static final String UNKNOWN_HOST = "Unknown host : "; 53 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."; 54 static final String UNAUTHORIZED_ACCESS = 55 "Invalid user or password"; 56 57 private URLConnection mConnection = null; 58 private ObjectOutputStream mObjectOutStream = null; 59 private ObjectInputStream mObjectInStream = null; 60 61 62 ServletConnection(HttpConnectorAddress a) throws IOException { 63 try{ 64 mConnection = a.openConnection("/"+AdminConstants.kAdminServletURI); 65 } 66 catch (IOException ioe){ 67 handleException(ioe); 68 } 69 } 70 71 72 75 public Object receive( ) throws IOException , ClassNotFoundException 76 { 77 Object value = null; 78 try 79 { 80 mObjectInStream = new ObjectInputStream ( 81 new BufferedInputStream (mConnection.getInputStream())); 82 value = mObjectInStream.readObject(); 83 } 84 catch (IOException ioe) 85 { 86 handleException(ioe); 87 } 88 return value; 89 } 90 91 94 public void send( Serializable object ) throws IOException 95 { 96 try 97 { 98 mObjectOutStream = new ObjectOutputStream ( 99 new BufferedOutputStream ( 100 mConnection.getOutputStream())); 101 mObjectOutStream.writeObject(object); 102 mObjectOutStream.flush(); 103 mObjectOutStream.close(); 104 } 105 catch (IOException ioe) 106 { 107 handleException(ioe); 108 } 109 } 110 111 public void close() 112 { 113 try 114 { 115 mObjectInStream.close(); 116 mObjectOutStream.close(); 117 } 118 catch(Exception e) 119 { 120 Debug.printStackTrace(e); 121 } 122 } 123 124 125 private void handleException(IOException e) throws IOException 126 { 127 IOException exception = null; 128 if (e instanceof java.net.UnknownHostException ) 129 { 130 exception = new java.net.UnknownHostException (UNKNOWN_HOST + 131 e.getMessage()); 132 } 133 else if (e instanceof java.net.ConnectException ) 134 { 135 exception = new java.net.ConnectException (INVALID_HOST_PORT); 136 } 137 else 138 { 139 int responseCode = 140 ((HttpURLConnection )mConnection).getResponseCode(); 141 if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) 142 { 143 exception = new IOException (UNAUTHORIZED_ACCESS); 144 } 145 else 146 { 147 exception = e; 148 } 149 } 150 throw exception; 151 } 152 } 153 | Popular Tags |