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: * Condition to check a remote EJB that implements {@link ConditionRemote}.
27: * A required argument for this condition 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 RemoteEJBCondition implements Condition {
42: //~ Methods ////////////////////////////////////////////////////////////////
43:
44: public boolean passesCondition(Map transientVars, Map args,
45: PropertySet ps) throws WorkflowException {
46: String ejbLocation = (String) args
47: .get(AbstractWorkflow.EJB_LOCATION);
48:
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: ConditionRemote sessionBean;
65:
66: try {
67: EJBHome home = (EJBHome) PortableRemoteObject.narrow(
68: new InitialContext().lookup(ejbLocation),
69: EJBHome.class);
70: Method create = home.getClass().getMethod("create",
71: new Class[0]);
72: sessionBean = (ConditionRemote) create.invoke(home,
73: new Object[0]);
74: } catch (Exception e) {
75: String message = "Could not get handle to Remote Condition: "
76: + ejbLocation;
77: throw new WorkflowException(message, e);
78: }
79:
80: try {
81: return sessionBean.passesCondition(transientVars, args, ps);
82: } catch (RemoteException e) {
83: String message = "Remote exception encountered while executing Remote Condition: "
84: + ejbLocation;
85: throw new WorkflowException(message, e);
86: }
87: }
88: }
|