1 package com.tirsen.nanning.samples.prevayler; 2 3 import java.io.Serializable ; 4 import java.security.PrivilegedExceptionAction ; 5 6 import javax.security.auth.Subject ; 7 8 import com.tirsen.nanning.Invocation; 9 10 public class MarshallingCall extends AuthenticatedCall { 11 static final long serialVersionUID = -8607314000649422353L; 12 13 private transient Marshaller marshaller; 14 15 public MarshallingCall(Invocation invocation, Marshaller marshaller) throws Exception { 16 setMarshaller(marshaller); 17 setInvocation(invocation); 18 } 19 20 public void setMarshaller(Marshaller marshaller) { 21 this.marshaller = marshaller; 22 } 23 24 public Marshaller getMarshaller() { 25 if (marshaller == null) { 26 marshaller = createMarshaller(); 27 } 28 return marshaller; 29 } 30 31 protected Marshaller createMarshaller() { 32 return null; 33 } 34 35 protected void setInvocation(Invocation invocation) throws Exception { 36 super.setInvocation(invocation); 37 target = marshal(target); 38 args = marshalArguments(invocation.getArgs()); 39 } 40 41 protected Object [] marshalArguments(Object [] args) throws Exception { 42 if (args == null) { 43 return null; 44 } 45 46 Object [] marshalled = new Object [args.length]; 47 for (int i = 0; i < args.length; i++) { 48 Object arg = args[i]; 49 marshalled[i] = marshal(arg); 50 } 51 return marshalled; 52 } 53 54 protected Object marshal(Object o) { 55 return getMarshaller().marshal(o); 56 } 57 58 private Object [] unmarshalArguments(Object [] args) { 59 if (args == null) { 60 return null; 61 } 62 63 Object [] unmarshalled = new Object [args.length]; 64 for (int i = 0; i < args.length; i++) { 65 Object o = args[i]; 66 unmarshalled[i] = unmarshal(o); 67 } 68 return unmarshalled; 69 } 70 71 protected Object unmarshal(Object o) { 72 return getMarshaller().unmarshal(o); 73 } 74 75 public Object [] getArgs() { 76 return unmarshalArguments(args); 77 } 78 79 public Object getTarget() { 80 assert target != null; 81 return unmarshal(target); 82 } 83 84 public void setTarget(Object target) { 85 this.target = marshaller.marshal(target); 86 } 87 88 public Object invoke() throws Exception { 89 Subject subject = getSubject(); 90 91 Object result; 92 93 if (subject != null) { 94 try { 95 result = Subject.doAs(subject, new PrivilegedExceptionAction () { 96 public Object run() throws Exception { 97 return MarshallingCall.super.invoke(); 98 } 99 }); 100 } catch (java.security.PrivilegedActionException e) { 101 throw e.getException(); 102 } 103 } else { 104 result = super.invoke(); 105 } 106 107 return result; 108 } 109 } 110 | Popular Tags |