01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.workflow.util.ejb.remote;
06:
07: import com.opensymphony.module.propertyset.PropertySet;
08:
09: import com.opensymphony.workflow.*;
10:
11: import java.lang.reflect.Method;
12:
13: import java.rmi.RemoteException;
14:
15: import java.util.Hashtable;
16: import java.util.Iterator;
17: import java.util.Map;
18:
19: import javax.ejb.EJBHome;
20:
21: import javax.naming.InitialContext;
22:
23: import javax.rmi.PortableRemoteObject;
24:
25: /**
26: * Function to execute a remote EJB that implements {@link FunctionProviderRemote}.
27: * A required argument for this function is <code>ejb.location</code>, which specifies
28: * the JNDI location to look up the remote EJB at.
29: * <p>
30: * Note that by default, the EJB will be looked up from a default InitialContext. It is
31: * however possible to specify an environment for the initial context by specifying the
32: * standard JNDI keys as arguments.
33: * <p>
34: * For example, specifying an argument with a name of <code>java.naming.security.principal</code>
35: * and a value of <code>testuser</code> will result in an InitialContext being created with
36: * the user specified as 'testuser'.
37: *
38: * @author $Author: hani $
39: * @version $Revision: 1.3 $
40: */
41: public class RemoteEJBFunctionProvider implements FunctionProvider {
42: //~ Methods ////////////////////////////////////////////////////////////////
43:
44: public void execute(Map transientVars, Map args, PropertySet ps)
45: throws WorkflowException {
46: FunctionProviderRemote sessionBean;
47: String ejbLocation = (String) args
48: .get(AbstractWorkflow.EJB_LOCATION);
49: Hashtable env = null;
50: Iterator iter = args.entrySet().iterator();
51:
52: while (iter.hasNext()) {
53: Map.Entry entry = (Map.Entry) iter.next();
54:
55: if (env == null) {
56: env = new Hashtable();
57: }
58:
59: if (((String) entry.getKey()).startsWith("java.naming.")) {
60: env.put(entry.getKey(), entry.getValue());
61: }
62: }
63:
64: try {
65: EJBHome home = (EJBHome) PortableRemoteObject.narrow(
66: new InitialContext(env).lookup(ejbLocation),
67: EJBHome.class);
68: Method create = home.getClass().getMethod("create",
69: new Class[0]);
70: sessionBean = (FunctionProviderRemote) create.invoke(home,
71: new Object[0]);
72: } catch (Exception e) {
73: String message = "Could not get handle to remote EJB at: "
74: + ejbLocation;
75: throw new WorkflowException(message, e);
76: }
77:
78: try {
79: sessionBean.execute(transientVars, args, ps);
80: } catch (RemoteException e) {
81: String message = "Remote exception from EJB at: "
82: + ejbLocation;
83: throw new WorkflowException(message, e);
84: }
85: }
86: }
|