1 package com.tirsen.nanning.samples.rmi; 2 3 import java.io.*; 4 import java.security.AccessController ; 5 import java.util.HashMap ; 6 import java.util.Map ; 7 8 import javax.security.auth.Subject ; 9 10 import com.tirsen.nanning.AspectFactory; 11 import com.tirsen.nanning.Aspects; 12 import com.tirsen.nanning.samples.prevayler.MarshallingCall; 13 import com.tirsen.nanning.samples.prevayler.Call; 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 17 public class RemoteCallServer { 18 private static final Log logger = LogFactory.getLog(RemoteCallServer.class); 19 20 private AspectFactory aspectFactory; 21 private Map naming = new HashMap (); 22 private RemoteMarshaller marshaller; 23 24 public RemoteCallServer(ServerConnectionManager connectionManager) { 25 marshaller = RemoteMarshaller.createServerSideMarshaller(connectionManager); 26 } 27 28 public void setAspectFactory(AspectFactory aspectRepository) { 29 this.aspectFactory = aspectRepository; 30 } 31 32 public void processCall(InputStream commandStream, OutputStream resultStream) { 33 Aspects.setContextAspectFactory(aspectFactory); 34 Subject subject = Subject.getSubject(AccessController.getContext()); 35 if (subject != null) { 36 subject.getPrincipals().clear(); 37 subject.getPrivateCredentials().clear(); 38 subject.getPublicCredentials().clear(); 39 } 40 41 try { 42 MarshallingInputStream input = new MarshallingInputStream(commandStream, marshaller); 43 Object command = input.readObject(); 44 45 Object result = processCall(command); 46 47 MarshallingOutputStream output = new MarshallingOutputStream(resultStream, marshaller); 48 try { 49 output.writeObject(result); 50 } catch (NotSerializableException e) { 51 String message = "Could not serialize object with class " + result.getClass(); 52 assert false : message; 53 logger.fatal(message); 54 } 55 output.flush(); 56 57 } catch (IOException e) { 58 logger.error("error communicating with client", e); 59 } catch (ClassNotFoundException e) { 60 logger.error("error communicating with client", e); 61 } 62 } 63 64 65 private Object processCall(Object command) { 66 Object result; 67 if (command instanceof Call) { 68 Call call = (Call) command; 69 70 result = processRemoteCall(call); 71 72 } else if (command instanceof NamingLookup) { 73 NamingLookup namingLookup = (NamingLookup) command; 74 75 result = processNamingLookup(namingLookup); 76 } else { 77 result = new ExceptionThrown(new RuntimeException ("No such command exception.")); 78 } 79 80 return result; 81 } 82 83 private Object processNamingLookup(NamingLookup namingLookup) { 84 Object result; 85 String name = namingLookup.getName(); 86 result = naming.get(name); 87 return result; 88 } 89 90 private Object processRemoteCall(Call call) { 91 Object result; 92 try { 93 result = call.invoke(); 94 } catch (Throwable e) { 95 logger.error("error executing call", e); 96 result = new ExceptionThrown(e); 97 } 98 return result; 99 } 100 101 public void bind(String name, Object o) { 102 naming.put(name, o); 103 } 104 105 public void reset() { 106 marshaller.reset(); 107 } 108 } 109 | Popular Tags |