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 java.rmi.RemoteException;
025: import java.util.Date;
026:
027: import javax.ejb.CreateException;
028: import javax.ejb.EJBException;
029: import javax.ejb.EJBLocalHome;
030: import javax.ejb.EJBLocalObject;
031: import javax.ejb.EntityBean;
032: import javax.ejb.EntityContext;
033: import javax.ejb.RemoveException;
034: import javax.ejb.TimedObject;
035: import javax.ejb.Timer;
036: import javax.ejb.TimerService;
037: import javax.naming.Context;
038: import javax.naming.InitialContext;
039: import javax.naming.NamingException;
040:
041: import org.apache.commons.logging.Log;
042: import org.apache.commons.logging.LogFactory;
043: import org.jbpm.JbpmConfiguration;
044: import org.jbpm.JbpmException;
045: import org.jbpm.calendar.BusinessCalendar;
046: import org.jbpm.calendar.Duration;
047: import org.jbpm.ejb.LocalCommandService;
048: import org.jbpm.ejb.LocalCommandServiceHome;
049: import org.jbpm.ejb.LocalTimerEntity;
050: import org.jbpm.scheduler.ejbtimer.ExecuteTimerCommand;
051:
052: /**
053: * A stateless session bean implementation of the {@link org.jbpm.command.CommandService}.
054: *
055: * @author Jim Rigsbee, Tom Baeyens
056: */
057: public class TimerEntityBean implements EntityBean, LocalTimerEntity,
058: TimedObject {
059:
060: private static final long serialVersionUID = 1L;
061:
062: Long id;
063: String dueDate;
064: String repeat;
065:
066: JbpmConfiguration jbpmConfiguration = null;
067: EntityContext entityContext = null;
068:
069: /**
070: * creates a command service that will be used to execute the commands that
071: * are passed in the execute method. The command service will be build by
072: * creating a jbpm configuration. In case the environment key JbpmCfgResource
073: * is specified for this bean, that value will be used to resolve the jbpm
074: * configuration file as a resource. If that key is not configured, the default
075: * jbpm configuration file will be used (jbpm.cfg.xml).
076: */
077: public void ejbCreate() throws CreateException {
078: String jbpmCfgResource = null;
079: try {
080: log
081: .debug("getting jbpm configuration resource from the environment properties");
082: Context initial = new InitialContext();
083: Context environment = (Context) initial
084: .lookup("java:comp/env");
085: jbpmCfgResource = (String) environment
086: .lookup("JbpmCfgResource");
087:
088: } catch (NamingException e) {
089: log
090: .debug("couldn't find configuration property JbpmCfgResource through JNDI");
091: }
092:
093: if (log.isDebugEnabled()) {
094: if (jbpmCfgResource == null) {
095: log
096: .debug("getting default jbpm configuration resource (jbpm.cfg.xml)");
097: } else {
098: log.debug("getting jbpm configuration from resource "
099: + jbpmCfgResource);
100: }
101: }
102:
103: jbpmConfiguration = JbpmConfiguration
104: .getInstance(jbpmCfgResource);
105: }
106:
107: public void schedule() {
108: log.debug("creating timer " + id + " in the ejb timer service");
109: createTimer(dueDate);
110: }
111:
112: public void createTimer(String duration) {
113: TimerService timerService = entityContext.getTimerService();
114: BusinessCalendar businessCalendar = new BusinessCalendar();
115: Date dueDateTime = businessCalendar.add(new Date(),
116: new Duration(duration));
117: timerService.createTimer(dueDateTime, null);
118: }
119:
120: public void cancel() {
121: }
122:
123: public void ejbTimeout(Timer timer) {
124: log.debug("ejb timer " + timer + " fires");
125: String localCommandServiceJndiName = "java:comp/env/ejb/LocalCommandServiceBean";
126: try {
127: Context initial = new InitialContext();
128: LocalCommandServiceHome localCommandServiceHome = (LocalCommandServiceHome) initial
129: .lookup(localCommandServiceJndiName);
130: LocalCommandService localCommandService = localCommandServiceHome
131: .create();
132: localCommandService.execute(new ExecuteTimerCommand(id
133: .longValue()));
134: // if the timer has repeat
135: if (repeat != null) {
136: // create a new timer
137: log.debug("repeating timer " + id);
138: createTimer(repeat);
139: }
140: } catch (Exception e) {
141: JbpmException jbpmException = new JbpmException(
142: "couldn't execute timer", e);
143: log.error(jbpmException);
144: throw jbpmException;
145: }
146: }
147:
148: public EJBLocalHome getEJBLocalHome() throws EJBException {
149: return null;
150: }
151:
152: public Object getPrimaryKey() throws EJBException {
153: return id;
154: }
155:
156: public boolean isIdentical(EJBLocalObject elo) throws EJBException {
157: return false;
158: }
159:
160: public void setEntityContext(EntityContext entityContext) {
161: this .entityContext = entityContext;
162: }
163:
164: public void unsetEntityContext() throws EJBException,
165: RemoteException {
166: this .entityContext = null;
167: }
168:
169: public void remove() throws RemoveException, EJBException {
170: }
171:
172: public void ejbStore() throws EJBException, RemoteException {
173: }
174:
175: public void ejbLoad() throws EJBException, RemoteException {
176: }
177:
178: public void ejbActivate() throws EJBException, RemoteException {
179: }
180:
181: public void ejbPassivate() throws EJBException, RemoteException {
182: }
183:
184: public void ejbRemove() {
185: }
186:
187: private static final Log log = LogFactory
188: .getLog(TimerEntityBean.class);
189: }
|