1 9 package org.jboss.remoting; 10 11 import java.io.IOException ; 12 import java.util.Map ; 13 import org.jboss.remoting.loading.ClassByteClassLoader; 14 import org.jboss.remoting.marshal.InvalidMarshallingResource; 15 import org.jboss.remoting.marshal.MarshalFactory; 16 import org.jboss.remoting.marshal.Marshaller; 17 import org.jboss.remoting.marshal.UnMarshaller; 18 import org.jboss.remoting.transport.ClientInvoker; 19 20 33 public abstract class RemoteClientInvoker extends AbstractInvoker implements ClientInvoker 34 { 35 protected boolean connected = false; 36 private Marshaller marshaller; 37 private UnMarshaller unmarshaller; 38 private String dataType; 39 40 41 public RemoteClientInvoker(InvokerLocator locator) 42 { 43 super(locator); 44 } 45 46 53 public Object invoke(InvocationRequest invocationReq) 54 throws Throwable 55 { 56 Object returnValue = null; 57 int invokeCount = 0; 58 59 if(log.isTraceEnabled()) 60 { 61 log.trace((++invokeCount) + ") invoking =>" + invocationReq + " with parameter: " + invocationReq.getParameter()); 62 } 63 64 Marshaller marshaller = getMarshaller(); 65 UnMarshaller unmarshaller = getUnMarshaller(); 66 67 if(marshaller == null) 68 { 69 marshaller = MarshalFactory.getMarshaller(getLocator(), getClassLoader()); 71 if(marshaller == null) 72 { 73 marshaller = MarshalFactory.getMarshaller(getDataType()); 75 if(marshaller == null) 76 { 77 throw new InvalidMarshallingResource("Can not find a valid marshaller for data type: " + getDataType()); 79 } 80 } 81 } 82 83 if(unmarshaller == null) 84 { 85 unmarshaller = MarshalFactory.getUnMarshaller(getLocator(), getClassLoader()); 87 if(unmarshaller == null) 88 { 89 unmarshaller = MarshalFactory.getUnMarshaller(getDataType()); 90 if(unmarshaller == null) 91 { 92 throw new InvalidMarshallingResource("Can not find a valid unmarshaller for data type: " + getDataType()); 94 } 95 } 96 } 97 98 try 99 { 100 Object payload = null; 102 Map metadata = invocationReq.getRequestPayload(); 103 if(metadata != null && metadata.get(Client.RAW) != null) 104 { 105 payload = invocationReq.getParameter(); 106 } 107 else 108 { 109 payload = invocationReq; 110 } 111 112 returnValue = transport(invocationReq.getSessionId(), payload, metadata, marshaller, unmarshaller); 113 114 if(log.isTraceEnabled()) 115 { 116 log.trace("received result=>" + returnValue); 117 } 118 119 if(returnValue instanceof InvocationResponse) 121 { 122 InvocationResponse response = (InvocationResponse) returnValue; 123 returnValue = response.getResult(); 124 if(log.isTraceEnabled()) 125 { 126 log.trace("received result was an InvocationResponse so going to return response's return value of " + returnValue); 127 log.trace("response is exception = " + response.isException()); 128 } 129 if(response.isException()) 131 { 132 throw (Throwable ) returnValue; 133 } 134 } 135 } 136 catch(ClassNotFoundException cnf) 137 { 138 throw cnf; 140 } 141 return returnValue; 142 } 143 144 153 protected void preProcess(String sessionId, Object param, Map sendPayload, Map receivedPayload) 154 { 155 } 156 157 166 protected void postProcess(String sessionId, Object param, Map sendPayload, 167 Map receivedPayload) 168 { 169 170 } 171 172 180 protected abstract Object transport(String sessionId, Object invocation, Map metadata, Marshaller marshaller, UnMarshaller unmarshaller) 181 throws IOException , ConnectionFailedException, ClassNotFoundException ; 182 183 191 public boolean isConnected() 192 { 193 return connected; 194 } 195 196 197 202 public synchronized void connect() 203 throws ConnectionFailedException 204 { 205 if(!connected) 206 { 207 log.debug("connect called for: " + this); 208 209 handleConnect(); 210 connected = true; 211 } 212 } 213 214 222 protected abstract void handleConnect() 223 throws ConnectionFailedException; 224 225 231 protected abstract void handleDisconnect(); 232 233 236 public synchronized void disconnect() 237 { 238 if(connected) 239 { 240 log.debug("disconnect called for: " + this); 241 242 connected = false; 243 handleDisconnect(); 244 ClassLoader classLoader = getClassLoader(); 245 if(classLoader != null && classLoader instanceof ClassByteClassLoader) 246 { 247 ((ClassByteClassLoader) classbyteloader).destroy(); 248 } 249 } 250 255 InvokerRegistry.destroyClientInvoker(getLocator()); 256 } 257 258 public void setMarshaller(Marshaller marshaller) 259 { 260 this.marshaller = marshaller; 261 } 262 263 public Marshaller getMarshaller() 264 { 265 return this.marshaller; 266 } 267 268 public void setUnMarshaller(UnMarshaller unmarshaller) 269 { 270 this.unmarshaller = unmarshaller; 271 } 272 273 public UnMarshaller getUnMarshaller() 274 { 275 return this.unmarshaller; 276 } 277 278 286 private String getDataType() 287 { 288 if(dataType == null) 289 { 290 dataType = getDataType(getLocator()); 291 if(dataType == null) 292 { 293 dataType = getDefaultDataType(); 294 } 295 } 296 return dataType; 297 } 298 299 private String getDataType(InvokerLocator locator) 300 { 301 String type = null; 302 303 if(locator != null) 304 { 305 Map params = locator.getParameters(); 306 if(params != null) 307 { 308 type = (String ) params.get(InvokerLocator.DATATYPE); 309 if(type == null) 310 { 311 type = (String ) params.get(InvokerLocator.DATATYPE_CASED); 312 } 313 } 314 } 315 return type; 316 } 317 318 325 protected abstract String getDefaultDataType(); 326 327 328 375 protected void finalize() throws Throwable 376 { 377 disconnect(); 378 super.finalize(); 379 } 380 } 381 | Popular Tags |