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: QuartzComponent.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component.quartz;
025:
026: import java.util.List;
027: import java.util.Properties;
028:
029: import javax.ejb.TimerService;
030:
031: import org.ow2.easybeans.api.Factory;
032: import org.ow2.easybeans.component.api.EZBComponentException;
033: import org.ow2.easybeans.component.itf.TimerComponent;
034: import org.ow2.easybeans.component.util.Property;
035: import org.quartz.Scheduler;
036: import org.quartz.SchedulerException;
037: import org.quartz.SchedulerFactory;
038: import org.quartz.impl.StdSchedulerFactory;
039:
040: /**
041: * This component starts the Quartz framework and configure it.
042: * It is also providing the Scheduler that EJB timer will use for their use.
043: * @author Florent Benoit
044: *
045: */
046: public class QuartzComponent implements TimerComponent {
047:
048: /**
049: * Quartz scheduler shared by all Timer services.
050: */
051: private Scheduler scheduler = null;
052:
053: /**
054: * Properties for the scheduler. These properties should have been set before the init of the scheduler.
055: */
056: private List<Property> quartzProperties = null;
057:
058: /**
059: * Quartz scheduler Factory.
060: */
061: private SchedulerFactory schedulerFactory = null;
062:
063: /**
064: * Init method.<br/>
065: * This method is called before the start method.
066: * @throws EZBComponentException if the initialization has failed.
067: */
068: public void init() throws EZBComponentException {
069:
070: // Get properties
071: Properties schedulerProperties = new Properties();
072: if (quartzProperties != null) {
073: for (Property property : quartzProperties) {
074: schedulerProperties.put(property.getName(), property
075: .getValue());
076: }
077: }
078:
079: // Initialize the Quartz scheduler Factory
080: schedulerFactory = null;
081: try {
082: schedulerFactory = new StdSchedulerFactory(
083: schedulerProperties);
084: } catch (SchedulerException e) {
085: throw new EZBComponentException(
086: "Cannot initialize the Scheduler factory", e);
087: }
088: }
089:
090: /**
091: * Start method.<br/>
092: * This method is called after the init method.
093: * @throws EZBComponentException if the start has failed.
094: */
095: public void start() throws EZBComponentException {
096:
097: // Build a Scheduler
098: try {
099: this .scheduler = schedulerFactory.getScheduler();
100: } catch (SchedulerException e) {
101: throw new EZBComponentException(
102: "Cannot get a scheduler from the factory", e);
103: }
104:
105: // Start the scheduler
106: try {
107: scheduler.start();
108: } catch (SchedulerException e) {
109: throw new EZBComponentException(
110: "Cannot start the scheduler", e);
111: }
112: }
113:
114: /**
115: * Stop method.<br/>
116: * This method is called when component needs to be stopped.
117: * @throws EZBComponentException if the stop is failing.
118: */
119: public void stop() throws EZBComponentException {
120: // Stop the scheduler
121: try {
122: scheduler.shutdown();
123: } catch (SchedulerException e) {
124: throw new EZBComponentException(
125: "Cannot stop the scheduler", e);
126: }
127: }
128:
129: /**
130: * Gets an EJB timer service through this component.
131: * @param factory an EasyBeans factory providing timeout notification.
132: * @return an EJB timer service
133: */
134: public TimerService getTimerService(final Factory factory) {
135: return new QuartzTimerService(factory, scheduler);
136: }
137:
138: /**
139: * Gets the list of properties.
140: * @return the list of properties.
141: */
142: public List<Property> getProperties() {
143: return this .quartzProperties;
144: }
145:
146: /**
147: * Set the list of properties.
148: * @param quartzProperties the list of properties.
149: */
150: public void setProperties(final List<Property> quartzProperties) {
151: this .quartzProperties = quartzProperties;
152: }
153:
154: /**
155: * Gets the Quartz scheduler.
156: * @return the Quartz scheduler.
157: */
158: public Scheduler getScheduler() {
159: return scheduler;
160: }
161:
162: }
|