001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.ejb.impl;
023:
024: import javax.ejb.CreateException;
025: import javax.ejb.SessionBean;
026: import javax.ejb.SessionContext;
027: import javax.naming.Context;
028: import javax.naming.InitialContext;
029: import javax.naming.NamingException;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.jbpm.JbpmConfiguration;
034: import org.jbpm.JbpmContext;
035: import org.jbpm.JbpmException;
036: import org.jbpm.command.Command;
037: import org.jbpm.command.CommandService;
038:
039: /**
040: * A stateless session bean implementation of the {@link org.jbpm.command.CommandService}.
041: *
042: * @author Jim Rigsbee, Tom Baeyens
043: */
044: public class CommandServiceBean implements SessionBean, CommandService {
045:
046: private static final long serialVersionUID = 1L;
047:
048: JbpmConfiguration jbpmConfiguration = null;
049: SessionContext sessionContext = null;
050:
051: /**
052: * creates a command service that will be used to execute the commands that
053: * are passed in the execute method. The command service will be build by
054: * creating a jbpm configuration. In case the environment key JbpmCfgResource
055: * is specified for this bean, that value will be used to resolve the jbpm
056: * configuration file as a resource. If that key is not configured, the default
057: * jbpm configuration file will be used (jbpm.cfg.xml).
058: */
059: public void ejbCreate() throws CreateException {
060: String jbpmCfgResource = null;
061: try {
062: log
063: .debug("getting jbpm configuration resource from the environment properties");
064: Context initial = new InitialContext();
065: Context environment = (Context) initial
066: .lookup("java:comp/env");
067: jbpmCfgResource = (String) environment
068: .lookup("JbpmCfgResource");
069:
070: } catch (NamingException e) {
071: log
072: .debug("couldn't find configuration property JbpmCfgResource through JNDI");
073: }
074:
075: if (log.isDebugEnabled()) {
076: if (jbpmCfgResource == null) {
077: log
078: .debug("getting default jbpm configuration resource (jbpm.cfg.xml)");
079: } else {
080: log.debug("getting jbpm configuration from resource "
081: + jbpmCfgResource);
082: }
083: }
084:
085: jbpmConfiguration = JbpmConfiguration
086: .getInstance(jbpmCfgResource);
087: }
088:
089: public Object execute(Command command) {
090: log
091: .debug("handing over the command execution to the command service");
092:
093: Object result = null;
094: JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
095: try {
096: log.debug("executing " + command);
097: result = command.execute(jbpmContext);
098: } catch (Exception e) {
099: throw new JbpmException("couldn't execute " + command, e);
100: } finally {
101: jbpmContext.close();
102: }
103: return result;
104: }
105:
106: public void setSessionContext(SessionContext sessionContext) {
107: this .sessionContext = sessionContext;
108: }
109:
110: public void ejbRemove() {
111: this .sessionContext = null;
112: }
113:
114: public void ejbActivate() {
115: }
116:
117: public void ejbPassivate() {
118: }
119:
120: private static final Log log = LogFactory
121: .getLog(CommandServiceBean.class);
122: }
|