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