1 package com.tirsen.nanning.samples.prevayler; 2 3 import java.lang.reflect.InvocationTargetException ; 4 import java.lang.reflect.Method ; 5 import java.util.Arrays ; 6 import java.util.Collections ; 7 import java.util.List ; 8 9 import javax.security.auth.Subject ; 10 11 import com.tirsen.nanning.Invocation; 12 import org.prevayler.util.clock.ClockedSystem; 13 import org.prevayler.util.clock.ClockedTransaction; 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 17 public class InvokeCommand extends ClockedTransaction { 18 private static final Log logger = LogFactory.getLog(InvokeCommand.class); 19 static final long serialVersionUID = 320681517664792343L; 20 21 private AuthenticatedCall call; 22 23 public InvokeCommand(Invocation invocation, boolean resolveEntities) throws Exception { 24 if (resolveEntities) { 25 call = new IdentifyingCall(invocation); 26 27 } else { 28 call = new AuthenticatedCall(invocation); 29 } 30 } 31 32 protected Object executeClocked(ClockedSystem system) throws Exception { 33 Object prev = null; 34 if (CurrentPrevayler.isInitialized()) { 35 prev = CurrentPrevayler.getSystem(); 36 } 37 if (!CurrentPrevayler.hasSystem() || CurrentPrevayler.getSystem() != system) { 38 CurrentPrevayler.setSystem(system); 39 } 40 CurrentPrevayler.enterTransaction(); 41 try { 42 if (logger.isDebugEnabled()) { 43 Object target = call.getTarget(); 44 Object [] args = call.getArgs(); 45 Method method = call.getMethod(); 46 Subject subject = call.getSubject(); 47 48 List argsList = Collections.EMPTY_LIST; 49 if (args != null) { 50 argsList = Arrays.asList(args); 51 } 52 logger.debug("invoking method " + method + " on " + target); 53 logger.debug("args " + argsList); 54 logger.debug("user " + subject); 55 } 56 57 Object result = call.invoke(); 58 logger.debug("success!"); 59 return result; 60 } catch (Exception e) { 61 62 63 if (e instanceof InvocationTargetException ) { 64 InvocationTargetException invocationTargetException = (InvocationTargetException ) e; 65 if (invocationTargetException.getCause() instanceof Exception ) { 66 e = (Exception ) e.getCause(); 67 } 68 } 69 logger.error("Failed to execute command.", e); 70 71 throw e; 72 } finally { 73 CurrentPrevayler.exitTransaction(); 74 CurrentPrevayler.setSystem(prev); 75 } 76 } 77 78 public Call getCall() { 79 return call; 80 } 81 } 82 | Popular Tags |