01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.workflow.util;
06:
07: import com.opensymphony.module.propertyset.PropertySet;
08:
09: import com.opensymphony.workflow.FunctionProvider;
10: import com.opensymphony.workflow.spi.WorkflowEntry;
11:
12: import org.apache.commons.logging.Log;
13: import org.apache.commons.logging.LogFactory;
14:
15: import java.lang.reflect.Method;
16:
17: import java.util.Hashtable;
18: import java.util.Map;
19:
20: import javax.naming.InitialContext;
21:
22: import javax.rmi.PortableRemoteObject;
23:
24: /**
25: * Generic EJB Invoker function.
26: * This function is used to invoke an EJB listener in a step. The EJB
27: * must implement WorkflowListener if it's a remote session bean, or
28: * WorkflowLocalListener if it's a local session bean.<br>
29: * It accepts a number of arguments, these are:
30: *
31: * <ul>
32: * <li>ejb-home - The fully qualified class name of the EJB remote home interface</li>
33: * <li>ejb-local-home - The fully qualified class name of the local home interface</li>
34: * <li>ejb-jndi-location - The JNDI location of the ejb to invoke</li>
35: * </ul>
36: * <p>
37: * Note that only one of ejb-home or ejb-local-home can be specified.
38: *
39: * Also, please note that the entire set of properties will be passed through to the
40: * constructor for InitialContext, meaning that if you need to use an
41: * InintialContextFactory other than the default one, you are free to include arguments
42: * that will do so.
43: *
44: * @author Hani Suleiman
45: * @version $Revision: 1.2 $
46: * Date: Apr 6, 2002
47: * Time: 11:48:14 PM
48: */
49: public class EJBInvoker implements FunctionProvider {
50: //~ Static fields/initializers /////////////////////////////////////////////
51:
52: private static final Log log = LogFactory.getLog(EJBInvoker.class);
53:
54: //~ Methods ////////////////////////////////////////////////////////////////
55:
56: public void execute(Map transientVars, Map args, PropertySet ps) {
57: Class homeClass = null;
58: Object home = null;
59: WorkflowEntry entry = (WorkflowEntry) transientVars
60: .get("entry");
61: Hashtable env = new Hashtable(args);
62:
63: try {
64: InitialContext ic = new InitialContext(env);
65:
66: if (log.isDebugEnabled()) {
67: log.debug("executing with properties=" + args);
68: }
69:
70: if (args.containsKey("ejb-home")) {
71: homeClass = Class
72: .forName((String) args.get("ejb-home"));
73: } else if (args.containsKey("ejb-local-home")) {
74: homeClass = Class.forName((String) args
75: .get("ejb-local-home"));
76: }
77:
78: home = PortableRemoteObject.narrow(ic.lookup((String) args
79: .get("ejb-jndi-location")), homeClass);
80:
81: Method method = homeClass.getMethod("create", new Class[0]);
82:
83: if (java.rmi.Remote.class.isAssignableFrom(homeClass)) {
84: WorkflowListener listener = (WorkflowListener) method
85: .invoke(home, new Object[0]);
86: listener.stateChanged(entry);
87: } else {
88: WorkflowLocalListener listener = (WorkflowLocalListener) method
89: .invoke(home, new Object[0]);
90: listener.stateChanged(entry);
91: }
92: } catch (Exception e) {
93: log.error("Error invoking EJB homeClass=" + homeClass, e);
94: }
95: }
96: }
|