1 22 package org.jboss.invocation.unified.interfaces; 23 24 import java.io.Externalizable ; 25 import java.io.IOException ; 26 import java.io.ObjectInput ; 27 import java.io.ObjectOutput ; 28 import java.io.StreamCorruptedException ; 29 import java.rmi.MarshalledObject ; 30 import java.rmi.RemoteException ; 31 import java.rmi.ServerException ; 32 import org.jboss.invocation.Invocation; 33 import org.jboss.invocation.Invoker; 34 import org.jboss.logging.Logger; 35 import org.jboss.remoting.Client; 36 import org.jboss.remoting.InvokerLocator; 37 38 44 public class UnifiedInvokerProxy implements Invoker, Externalizable 45 { 46 static final long serialVersionUID = -1108158470271861548L; 47 48 private transient Client client; 49 50 private InvokerLocator locator; 51 52 private boolean strictRMIException = false; 53 54 private String subsystem = "invoker"; 55 56 protected final Logger log = Logger.getLogger(getClass()); 57 58 static final int VERSION_5_0 = 500; 59 static final int CURRENT_VERSION = VERSION_5_0; 60 61 62 public UnifiedInvokerProxy() 63 { 64 super(); 65 } 66 67 public UnifiedInvokerProxy(InvokerLocator locator) 68 { 69 init(locator); 70 } 71 72 public UnifiedInvokerProxy(InvokerLocator locator, boolean isStrictRMIException) 73 { 74 this.strictRMIException = isStrictRMIException; 75 init(locator); 76 } 77 78 protected void init(InvokerLocator locator) 79 { 80 this.locator = locator; 81 82 try 83 { 84 client = new Client(locator, getSubSystem()); 85 client.connect(); 86 } 87 catch(Exception e) 88 { 89 log.fatal("Could not initialize UnifiedInvokerProxy.", e); 90 } 91 92 } 93 94 public String getSubSystem() 95 { 96 return subsystem; 97 } 98 99 public void setSubSystem(String subsystem) 100 { 101 this.subsystem = subsystem; 102 } 103 104 105 public boolean isStrictRMIException() 106 { 107 return strictRMIException; 108 } 109 110 protected Client getClient() 111 { 112 return client; 113 } 114 115 protected InvokerLocator getLocator() 116 { 117 return locator; 118 } 119 120 protected void setLocator(InvokerLocator locator) 121 { 122 this.locator = locator; 123 } 124 125 protected void setStrictRMIException(boolean strictRMIException) 126 { 127 this.strictRMIException = strictRMIException; 128 } 129 130 131 138 public String getServerHostName() throws Exception 139 { 140 if(locator != null) 141 { 142 return locator.getHost(); 143 } 144 else 145 { 146 return null; 147 } 148 } 149 150 155 public Object invoke(Invocation invocation) throws Exception 156 { 157 Object response = null; 158 159 try 160 { 161 response = client.invoke(invocation, null); 162 163 if(response instanceof Exception ) 164 { 165 throw ((Exception ) response); 166 } 167 if(response instanceof MarshalledObject ) 168 { 169 return ((MarshalledObject ) response).get(); 170 } 171 return response; 172 173 } 174 catch(RemoteException aex) 175 { 176 if(strictRMIException) 178 { 179 throw new ServerException (aex.getMessage(), aex); 180 } 181 else 182 { 183 throw aex; 184 } 185 } 186 catch(Throwable throwable) 187 { 188 if(throwable instanceof Exception ) 193 { 194 throw (Exception ) throwable; 195 } 196 throw new Exception (throwable); 197 } 198 } 199 200 203 public void writeExternal(final ObjectOutput out) 204 throws IOException 205 { 206 out.writeInt(CURRENT_VERSION); 207 208 out.writeUTF(locator.getOriginalURI()); 209 out.writeBoolean(strictRMIException); 210 } 211 212 215 public void readExternal(final ObjectInput in) 216 throws IOException , ClassNotFoundException 217 { 218 int version = in.readInt(); 219 switch(version) 221 { 222 case VERSION_5_0: 223 locator = new InvokerLocator(in.readUTF()); 224 strictRMIException = in.readBoolean(); 225 init(locator); 226 break; 227 default: 228 throw new StreamCorruptedException ("Unknown version seen: " + version); 229 } 230 } 231 232 } 233 | Popular Tags |