1 package com.tirsen.nanning.samples.rmi; 2 3 import java.io.IOException ; 4 5 import com.tirsen.nanning.AspectInstance; 6 import com.tirsen.nanning.Invocation; 7 import com.tirsen.nanning.MethodInterceptor; 8 import com.tirsen.nanning.MixinInstance; 9 import com.tirsen.nanning.config.Aspect; 10 import com.tirsen.nanning.samples.prevayler.Call; 11 import com.tirsen.nanning.samples.prevayler.Marshaller; 12 import com.tirsen.nanning.samples.prevayler.AuthenticatedCall; 13 14 public class RemoteAspect implements Aspect, MethodInterceptor { 15 private Marshaller marshaller; 16 17 public Object invoke(Invocation invocation) throws Throwable { 18 assert invocation.getTarget() instanceof RemoteIdentity : 19 "target is not remote-reference: " + invocation.getTarget(); 20 21 RemoteIdentity remoteIdentity = (RemoteIdentity) invocation.getTarget(); 22 23 ServerConnectionManager connectionManager = remoteIdentity.getConnectionManager(); 24 ServerConnection connection = null; 25 try { 26 connection = connectionManager.openConnection(); 27 } catch (IOException e) { 28 throw new CouldNotConnectException("could not connect with " + connectionManager, e); 29 } 30 31 try { 32 Call call = new AuthenticatedCall(invocation); 33 34 MarshallingOutputStream output = null; 35 try { 36 output = new MarshallingOutputStream(connection.getOutputStream(), marshaller); 37 } catch (IOException e) { 38 throw new CouldNotConnectException(e); 39 } 40 output.writeObject(call); 41 MarshallingInputStream input = new MarshallingInputStream(connection.getInputStream(), marshaller); 42 Object result = marshaller.unmarshal(input.readObject()); 43 if (result instanceof ExceptionThrown) { 44 ExceptionThrown exceptionThrown = (ExceptionThrown) result; 45 Throwable throwable = exceptionThrown.getThrowable(); 46 if (throwable == null) { 47 throwable = new RuntimeException ("Remote error but exception was null"); 48 } 49 throw throwable.fillInStackTrace(); 50 } 51 return result; 52 } finally { 53 connection.close(); 54 } 55 } 56 57 void setMarshaller(Marshaller marshaller) { 58 this.marshaller = marshaller; 59 } 60 61 public void introduce(AspectInstance aspectInstance) { 62 } 63 64 public void advise(AspectInstance aspectInstance) { 65 aspectInstance.addInterceptor(this); 66 } 67 } 68 | Popular Tags |