001: /**
002: * EasyBeans
003: * Copyright (C) 2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: EasyBeansTimerHandle.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component.quartz;
025:
026: import java.util.List;
027:
028: import javax.ejb.EJBException;
029: import javax.ejb.NoSuchObjectLocalException;
030: import javax.ejb.Timer;
031: import javax.ejb.TimerHandle;
032:
033: import org.ow2.easybeans.api.EZBServer;
034: import org.ow2.easybeans.api.EmbeddedManager;
035: import org.ow2.easybeans.api.components.EZBComponentRegistry;
036: import org.ow2.easybeans.component.itf.TimerComponent;
037: import org.quartz.JobDetail;
038: import org.quartz.Scheduler;
039: import org.quartz.SchedulerException;
040: import org.quartz.Trigger;
041:
042: /**
043: * Implementation of the Timer handle interface.
044: * @author Florent Benoit
045: */
046: public class EasyBeansTimerHandle implements TimerHandle {
047:
048: /**
049: * Serial version UID for serializable classes.
050: */
051: private static final long serialVersionUID = 5391559452078385341L;
052:
053: /**
054: * JobDetail used to get parameters.
055: */
056: private EasyBeansJobDetail easyBeansJobDetail;
057:
058: /**
059: * Constructor. Build an handle for this timer.
060: * @param easyBeansJobDetail the job detail.
061: */
062: public EasyBeansTimerHandle(
063: final EasyBeansJobDetail easyBeansJobDetail) {
064: this .easyBeansJobDetail = easyBeansJobDetail;
065: }
066:
067: /**
068: * Obtain a reference to the timer represented by this handle.
069: * @return a reference to the timer represented by this handle.
070: * @throws IllegalStateException If this method is invoked while the
071: * instance is in a state that does not allow access to this method.
072: * @throws NoSuchObjectLocalException If invoked on a handle whose
073: * associated timer has expired or has been cancelled.
074: * @throws EJBException If this method could not complete due to a
075: * system-level failure.
076: */
077: public Timer getTimer() throws IllegalStateException,
078: NoSuchObjectLocalException, EJBException {
079: Timer timer = null;
080:
081: // Get data from the Job Detail
082: String jobName = easyBeansJobDetail.getName();
083: String groupName = easyBeansJobDetail.getGroup();
084:
085: // Get data from the jobDetail
086: EasyBeansJobDetailData easyBeansJobDetailData = easyBeansJobDetail
087: .getJobDetailData();
088: Integer easyBeansServerID = easyBeansJobDetailData
089: .getEasyBeansServerID();
090:
091: // Get the EasyBeans embedded instance
092: EZBServer embedded = EmbeddedManager
093: .getEmbedded(easyBeansServerID);
094:
095: // Get the components registry
096: EZBComponentRegistry registry = embedded.getComponentManager()
097: .getComponentRegistry();
098:
099: // Get the timer components
100: List<TimerComponent> timerComponents = registry
101: .getComponents(TimerComponent.class);
102:
103: // Find the quartz component in this list
104: if (timerComponents == null || timerComponents.size() == 0) {
105: throw new EJBException(
106: "Cannot get the timer object as no timer component have been found on the EasyBeans server");
107: }
108: // Check the first one
109: TimerComponent timerComponent = timerComponents.get(0);
110: QuartzComponent quartzComponent = null;
111: if (timerComponent instanceof QuartzComponent) {
112: quartzComponent = (QuartzComponent) timerComponent;
113: } else {
114: throw new EJBException(
115: "The timer component found is not a Quartz Timer Component ('"
116: + timerComponent + "').");
117: }
118:
119: // Get Scheduler on the quartz component
120: Scheduler scheduler = quartzComponent.getScheduler();
121:
122: // Get detail
123: JobDetail jobDetail = null;
124: try {
125: jobDetail = scheduler.getJobDetail(jobName, groupName);
126: } catch (SchedulerException e) {
127: throw new EJBException(
128: "Cannot get the jobDetail for the jobName '"
129: + jobName + "'.", e);
130: }
131:
132: // Cast to correct object
133: EasyBeansJobDetail easyBeansJobDetail = null;
134: if (jobDetail instanceof EasyBeansJobDetail) {
135: easyBeansJobDetail = (EasyBeansJobDetail) jobDetail;
136: } else {
137: throw new EJBException(
138: "JobDetail found for the job named '" + jobName
139: + "' is not an EasyBeansJobDetail object");
140: }
141:
142: // Get triggers
143: Trigger[] triggers = null;
144: try {
145: triggers = scheduler.getTriggersOfJob(jobName, groupName);
146: } catch (SchedulerException e) {
147: throw new EJBException(
148: "Cannot get triggers for the job named '" + jobName
149: + "'.", e);
150: }
151:
152: // Should be only once trigger per job
153: if (triggers == null || triggers.length > 1) {
154: throw new EJBException(
155: "Invalid numbers of triggers found for the job named '"
156: + jobName + "'.");
157: }
158:
159: // Build a timer object and return it
160: timer = new EasyBeansTimer(easyBeansJobDetail, triggers[0],
161: scheduler);
162:
163: // Return the timer instance
164: return timer;
165: }
166:
167: }
|