KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > util > EJBInvoker


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.util;
6
7 import com.opensymphony.module.propertyset.PropertySet;
8
9 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 JavaDoc;
16
17 import java.util.Hashtable JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import javax.naming.InitialContext JavaDoc;
21
22 import javax.rmi.PortableRemoteObject JavaDoc;
23
24
25 /**
26  * Generic EJB Invoker function.
27  * This function is used to invoke an EJB listener in a step. The EJB
28  * must implement WorkflowListener if it's a remote session bean, or
29  * WorkflowLocalListener if it's a local session bean.<br>
30  * It accepts a number of arguments, these are:
31  *
32  * <ul>
33  * <li>ejb-home - The fully qualified class name of the EJB remote home interface</li>
34  * <li>ejb-local-home - The fully qualified class name of the local home interface</li>
35  * <li>ejb-jndi-location - The JNDI location of the ejb to invoke</li>
36  * </ul>
37  * <p>
38  * Note that only one of ejb-home or ejb-local-home can be specified.
39  *
40  * Also, please note that the entire set of properties will be passed through to the
41  * constructor for InitialContext, meaning that if you need to use an
42  * InintialContextFactory other than the default one, you are free to include arguments
43  * that will do so.
44  *
45  * @author Hani Suleiman
46  * @version $Revision: 1.2 $
47  * Date: Apr 6, 2002
48  * Time: 11:48:14 PM
49  */

50 public class EJBInvoker implements FunctionProvider {
51     //~ Static fields/initializers /////////////////////////////////////////////
52

53     private static final Log log = LogFactory.getLog(EJBInvoker.class);
54
55     //~ Methods ////////////////////////////////////////////////////////////////
56

57     public void execute(Map JavaDoc transientVars, Map JavaDoc args, PropertySet ps) {
58         Class JavaDoc homeClass = null;
59         Object JavaDoc home = null;
60         WorkflowEntry entry = (WorkflowEntry) transientVars.get("entry");
61         Hashtable JavaDoc env = new Hashtable JavaDoc(args);
62
63         try {
64             InitialContext JavaDoc ic = new InitialContext JavaDoc(env);
65
66             if (log.isDebugEnabled()) {
67                 log.debug("executing with properties=" + args);
68             }
69
70             if (args.containsKey("ejb-home")) {
71                 homeClass = Class.forName((String JavaDoc) args.get("ejb-home"));
72             } else if (args.containsKey("ejb-local-home")) {
73                 homeClass = Class.forName((String JavaDoc) args.get("ejb-local-home"));
74             }
75
76             home = PortableRemoteObject.narrow(ic.lookup((String JavaDoc) args.get("ejb-jndi-location")), homeClass);
77
78             Method JavaDoc method = homeClass.getMethod("create", new Class JavaDoc[0]);
79
80             if (java.rmi.Remote JavaDoc.class.isAssignableFrom(homeClass)) {
81                 WorkflowListener listener = (WorkflowListener) method.invoke(home, new Object JavaDoc[0]);
82                 listener.stateChanged(entry);
83             } else {
84                 WorkflowLocalListener listener = (WorkflowLocalListener) method.invoke(home, new Object JavaDoc[0]);
85                 listener.stateChanged(entry);
86             }
87         } catch (Exception JavaDoc e) {
88             log.error("Error invoking EJB homeClass=" + homeClass, e);
89         }
90     }
91 }
92
Popular Tags