001: /*--
002:
003: Copyright (C) 2002-2005 Adrian Price.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The names "OBE" and "Open Business Engine" must not be used to
019: endorse or promote products derived from this software without prior
020: written permission. For written permission, please contact
021: adrianprice@sourceforge.net.
022:
023: 4. Products derived from this software may not be called "OBE" or
024: "Open Business Engine", nor may "OBE" or "Open Business Engine"
025: appear in their name, without prior written permission from
026: Adrian Price (adrianprice@users.sourceforge.net).
027:
028: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
029: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
030: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
032: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
034: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
036: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
037: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
038: POSSIBILITY OF SUCH DAMAGE.
039:
040: For more information on OBE, please see
041: <http://obe.sourceforge.net/>.
042:
043: */
044:
045: package org.obe.client.api.local;
046:
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049: import org.obe.client.api.WMClientFactory;
050: import org.obe.client.api.base.WorkflowEngineIntf;
051: import org.obe.client.api.rmi.AbstractJ2EEClient;
052: import org.obe.server.j2ee.J2EEServerConfig;
053: import org.obe.server.j2ee.ejb.EJBHelper;
054: import org.obe.spi.WMLocalClient;
055: import org.obe.spi.event.ApplicationEvent;
056: import org.obe.spi.service.ServiceManager;
057: import org.wfmc.wapi.WMConnectException;
058: import org.wfmc.wapi.WMConnectInfo;
059: import org.wfmc.wapi.WMWorkflowException;
060:
061: import javax.ejb.CreateException;
062: import javax.ejb.EJBLocalObject;
063: import javax.naming.Context;
064: import javax.naming.NamingException;
065: import java.security.PrivilegedExceptionAction;
066:
067: /**
068: * Class used to connect to and communicate with a workflow engine via a local
069: * interface.
070: * <p/>
071: * This interface is based on the WfMC's Interface 2 Client API
072: * specification. Some of the methods have been modified from the original
073: * specification to fit within the normal design of Java applications. For
074: * instance, the WfMC specification functions always return an error object
075: * (even for success) and uses out parameters to return values. This interface
076: * returns the value and throws an exception when an error occurs. If no error
077: * occurs then an exception is not thrown.
078: *
079: * @author Adrian Price
080: */
081: public final class J2EELocalClient extends AbstractJ2EEClient implements
082: WMLocalClient {
083:
084: private static final Log _logger = LogFactory
085: .getLog(J2EELocalClient.class);
086: private static final ServiceManager _svcMgr = J2EEServerConfig.svcMgr;
087:
088: public J2EELocalClient() {
089: }
090:
091: protected Log getLogger() {
092: return _logger;
093: }
094:
095: public String getProtocol() {
096: return WMClientFactory.J2EE_LOCAL;
097: }
098:
099: public synchronized void connect(WMConnectInfo connectInfo)
100: throws WMConnectException {
101:
102: try {
103: final Context ctx = _connect(connectInfo);
104: PrivilegedExceptionAction action = new PrivilegedExceptionAction() {
105: public Object run() throws Exception {
106: try {
107: // Look up J2EEServer's local home.
108: J2EEServerLocalHome home = (J2EEServerLocalHome) EJBHelper
109: .getEJBLocalHome(J2EEServerLocalHome.JNDI_NAME);
110: return home.create();
111: } catch (CreateException e) {
112: throw new WMConnectException(e);
113: } finally {
114: try {
115: ctx.close();
116: } catch (NamingException e) {
117: _logger.error("Error closing JNDI context");
118: }
119: }
120: }
121: };
122: _engine = (WorkflowEngineIntf) doAsSubject(action);
123: } catch (WMConnectException e) {
124: throw e;
125: } catch (WMWorkflowException e) {
126: throw new WMConnectException(e);
127: }
128: }
129:
130: public synchronized void disconnect() throws WMConnectException {
131: try {
132: PrivilegedExceptionAction action = new PrivilegedExceptionAction() {
133: public Object run() throws Exception {
134: try {
135: ((EJBLocalObject) _engine).remove();
136: } catch (Exception e) {
137: getLogger().error("Remove exception", e);
138: } finally {
139: J2EELocalClient.super .disconnect();
140: }
141: return null;
142: }
143: };
144: doAsSubject(action);
145: } catch (WMConnectException e) {
146: throw e;
147: } catch (WMWorkflowException e) {
148: throw new WMConnectException(e);
149: } finally {
150: _engine = null;
151: }
152: }
153:
154: public void raiseEvent(final ApplicationEvent event,
155: final String[] correlationKeys) throws WMWorkflowException {
156:
157: PrivilegedExceptionAction action = new PrivilegedExceptionAction() {
158: public Object run() throws Exception {
159: ((J2EEServerLocal) _engine).raiseEvent(event,
160: correlationKeys);
161: return null;
162: }
163: };
164: doAsSubject(action);
165: }
166:
167: public void refreshWorkItems(final String processInstanceId)
168: throws WMWorkflowException {
169:
170: PrivilegedExceptionAction action = new PrivilegedExceptionAction() {
171: public Object run() throws Exception {
172: ((J2EEServerLocal) _engine)
173: .refreshWorkItems(processInstanceId);
174: return null;
175: }
176: };
177: doAsSubject(action);
178: }
179:
180: public ServiceManager getServiceManager() {
181: return _svcMgr;
182: }
183: }
|