KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > samples > prevayler > InvokeCommand


1 package com.tirsen.nanning.samples.prevayler;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.List JavaDoc;
8
9 import javax.security.auth.Subject JavaDoc;
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 JavaDoc {
24         if (resolveEntities) {
25             call = new IdentifyingCall(invocation);
26             
27         } else {
28             call = new AuthenticatedCall(invocation);
29         }
30     }
31
32     protected Object JavaDoc executeClocked(ClockedSystem system) throws Exception JavaDoc {
33         Object JavaDoc 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 JavaDoc target = call.getTarget();
44                 Object JavaDoc[] args = call.getArgs();
45                 Method JavaDoc method = call.getMethod();
46                 Subject JavaDoc subject = call.getSubject();
47
48                 List JavaDoc 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 JavaDoc result = call.invoke();
58             logger.debug("success!");
59             return result;
60         } catch (Exception JavaDoc e) {
61
62             /** Unwrap the invocation target exceptions */
63             if (e instanceof InvocationTargetException JavaDoc) {
64                 InvocationTargetException JavaDoc invocationTargetException = (InvocationTargetException JavaDoc) e;
65                 if (invocationTargetException.getCause() instanceof Exception JavaDoc) {
66                     e = (Exception JavaDoc) 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