001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.quartz;
018    
019    import java.net.URI;
020    import java.text.ParseException;
021    import java.util.Date;
022    import java.util.Map;
023    
024    import org.apache.camel.CamelContext;
025    import org.apache.camel.impl.DefaultComponent;
026    import org.apache.camel.util.IntrospectionSupport;
027    import org.apache.camel.util.ObjectHelper;
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    import org.quartz.CronTrigger;
031    import org.quartz.Scheduler;
032    import org.quartz.SchedulerException;
033    import org.quartz.SchedulerFactory;
034    import org.quartz.SimpleTrigger;
035    import org.quartz.Trigger;
036    import org.quartz.impl.StdSchedulerFactory;
037    
038    /**
039     * A <a href="http://camel.apache.org/quartz.html">Quartz Component</a>
040     * <p/>
041     * For a bried tutorial on setting cron expression see
042     * <a href="http://www.opensymphony.com/quartz/wikidocs/CronTriggers%20Tutorial.html">Quartz cron tutorial</a>.
043     * 
044     * @version $Revision:520964 $
045     */
046    public class QuartzComponent extends DefaultComponent {
047        private static final transient Log LOG = LogFactory.getLog(QuartzComponent.class);
048        private SchedulerFactory factory;
049        private Scheduler scheduler;
050    
051        public QuartzComponent() {
052        }
053    
054        public QuartzComponent(final CamelContext context) {
055            super(context);
056        }
057    
058        @Override
059        protected QuartzEndpoint createEndpoint(final String uri, final String remaining, final Map<String, Object> parameters) throws Exception {
060            QuartzEndpoint answer = new QuartzEndpoint(uri, this, getScheduler());
061    
062            // lets split the remaining into a group/name
063            URI u = new URI(uri);
064            String path = ObjectHelper.after(u.getPath(), "/");
065            String host = u.getHost();
066            String cron = getAndRemoveParameter(parameters, "cron", String.class);
067            Boolean fireNow = getAndRemoveParameter(parameters, "fireNow", Boolean.class, Boolean.FALSE);
068    
069            // group can be optional, if so set it to Camel
070            String name;
071            String group;
072            if (ObjectHelper.isNotEmpty(path) && ObjectHelper.isNotEmpty(host)) {
073                group = host;
074                name = path;
075            } else {
076                group = "Camel";
077                name = host;
078            }
079    
080            Map<String, Object> triggerParameters = IntrospectionSupport.extractProperties(parameters, "trigger.");
081            Map<String, Object> jobParameters = IntrospectionSupport.extractProperties(parameters, "job.");
082            
083            // create the trigger either cron or simple
084            Trigger trigger;
085            if (ObjectHelper.isNotEmpty(cron)) {
086                trigger = createCronTrigger(cron);
087            } else {
088                trigger = new SimpleTrigger();
089                if (fireNow) {
090                    String intervalString = (String) triggerParameters.get("repeatInterval");
091                    if (intervalString != null) {
092                        long interval = Long.valueOf(intervalString);
093                        trigger.setStartTime(new Date(System.currentTimeMillis() - interval));
094                    }
095                }
096            }
097            answer.setTrigger(trigger);
098    
099            trigger.setName(name);
100            trigger.setGroup(group);
101            
102            setProperties(trigger, triggerParameters);
103            setProperties(answer.getJobDetail(), jobParameters);
104    
105            return answer;
106        }
107    
108        protected CronTrigger createCronTrigger(String path) throws ParseException {
109            // replace + back to space so its a cron expression
110            path = path.replaceAll("\\+", " ");
111            CronTrigger cron = new CronTrigger();
112            cron.setCronExpression(path);
113            return cron;
114        }
115    
116        @Override
117        protected void doStart() throws Exception {
118            super.doStart();
119            if (scheduler == null) {
120                scheduler = getScheduler();
121            }
122            if (LOG.isDebugEnabled()) {
123                LOG.debug("Starting Quartz scheduler: " + scheduler.getSchedulerName());
124            }
125            scheduler.start();
126        }
127    
128        @Override
129        protected void doStop() throws Exception {
130            if (scheduler != null) {
131                if (LOG.isDebugEnabled()) {
132                    LOG.debug("Shutting down Quartz scheduler: " + scheduler.getSchedulerName());
133                }
134                scheduler.shutdown();
135            }
136            super.doStop();
137        }
138    
139        // Properties
140        // -------------------------------------------------------------------------
141        public SchedulerFactory getFactory() {
142            if (factory == null) {
143                factory = createSchedulerFactory();
144            }
145            return factory;
146        }
147    
148        public void setFactory(final SchedulerFactory factory) {
149            this.factory = factory;
150        }
151    
152        public Scheduler getScheduler() throws SchedulerException {
153            if (scheduler == null) {
154                scheduler = createScheduler();
155            }
156            return scheduler;
157        }
158    
159        public void setScheduler(final Scheduler scheduler) {
160            this.scheduler = scheduler;
161        }
162    
163        // Implementation methods
164        // -------------------------------------------------------------------------
165        protected SchedulerFactory createSchedulerFactory() {
166            return new StdSchedulerFactory();
167        }
168    
169        protected Scheduler createScheduler() throws SchedulerException {
170            Scheduler scheduler = getFactory().getScheduler();
171            scheduler.getContext().put(QuartzConstants.QUARTZ_CAMEL_CONTEXT, getCamelContext());
172            return scheduler;
173        }
174    }