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.Map;
022
023 import org.apache.camel.CamelContext;
024 import org.apache.camel.impl.DefaultComponent;
025 import org.apache.camel.util.IntrospectionSupport;
026 import org.apache.camel.util.ObjectHelper;
027 import org.apache.commons.logging.Log;
028 import org.apache.commons.logging.LogFactory;
029 import org.quartz.CronTrigger;
030 import org.quartz.Scheduler;
031 import org.quartz.SchedulerException;
032 import org.quartz.SchedulerFactory;
033 import org.quartz.SimpleTrigger;
034 import org.quartz.Trigger;
035 import org.quartz.impl.StdSchedulerFactory;
036
037 /**
038 * A <a href="http://camel.apache.org/quartz.html">Quartz Component</a>
039 * <p/>
040 * For a bried tutorial on setting cron expression see
041 * <a href="http://www.opensymphony.com/quartz/wikidocs/CronTriggers%20Tutorial.html">Quartz cron tutorial</a>.
042 *
043 * @version $Revision:520964 $
044 */
045 public class QuartzComponent extends DefaultComponent {
046 private static final transient Log LOG = LogFactory.getLog(QuartzComponent.class);
047 private SchedulerFactory factory;
048 private Scheduler scheduler;
049
050 public QuartzComponent() {
051 }
052
053 public QuartzComponent(final CamelContext context) {
054 super(context);
055 }
056
057 @Override
058 protected QuartzEndpoint createEndpoint(final String uri, final String remaining, final Map<String, Object> parameters) throws Exception {
059 QuartzEndpoint answer = new QuartzEndpoint(uri, this, getScheduler());
060
061 // lets split the remaining into a group/name
062 URI u = new URI(uri);
063 String path = ObjectHelper.after(u.getPath(), "/");
064 String host = u.getHost();
065 String cron = getAndRemoveParameter(parameters, "cron", String.class);
066
067 // group can be optional, if so set it to Camel
068 String name;
069 String group;
070 if (ObjectHelper.isNotEmpty(path) && ObjectHelper.isNotEmpty(host)) {
071 group = host;
072 name = path;
073 } else {
074 group = "Camel";
075 name = host;
076 }
077
078 // create the trigger either cron or simple
079 Trigger trigger;
080 if (ObjectHelper.isNotEmpty(cron)) {
081 trigger = createCronTrigger(cron);
082 } else {
083 trigger = new SimpleTrigger();
084 }
085 answer.setTrigger(trigger);
086
087 trigger.setName(name);
088 trigger.setGroup(group);
089
090 Map<String, Object> triggerParameters = IntrospectionSupport.extractProperties(parameters, "trigger.");
091 Map<String, Object> jobParameters = IntrospectionSupport.extractProperties(parameters, "job.");
092
093 setProperties(trigger, triggerParameters);
094 setProperties(answer.getJobDetail(), jobParameters);
095
096 return answer;
097 }
098
099 protected CronTrigger createCronTrigger(String path) throws ParseException {
100 // replace + back to space so its a cron expression
101 path = path.replaceAll("\\+", " ");
102 CronTrigger cron = new CronTrigger();
103 cron.setCronExpression(path);
104 return cron;
105 }
106
107 @Override
108 protected void doStart() throws Exception {
109 super.doStart();
110 if (scheduler == null) {
111 scheduler = getScheduler();
112 }
113 if (LOG.isDebugEnabled()) {
114 LOG.debug("Starting Quartz scheduler: " + scheduler.getSchedulerName());
115 }
116 scheduler.start();
117 }
118
119 @Override
120 protected void doStop() throws Exception {
121 if (scheduler != null) {
122 if (LOG.isDebugEnabled()) {
123 LOG.debug("Shutting down Quartz scheduler: " + scheduler.getSchedulerName());
124 }
125 scheduler.shutdown();
126 }
127 super.doStop();
128 }
129
130 // Properties
131 // -------------------------------------------------------------------------
132 public SchedulerFactory getFactory() {
133 if (factory == null) {
134 factory = createSchedulerFactory();
135 }
136 return factory;
137 }
138
139 public void setFactory(final SchedulerFactory factory) {
140 this.factory = factory;
141 }
142
143 public Scheduler getScheduler() throws SchedulerException {
144 if (scheduler == null) {
145 scheduler = createScheduler();
146 }
147 return scheduler;
148 }
149
150 public void setScheduler(final Scheduler scheduler) {
151 this.scheduler = scheduler;
152 }
153
154 // Implementation methods
155 // -------------------------------------------------------------------------
156 protected SchedulerFactory createSchedulerFactory() {
157 return new StdSchedulerFactory();
158 }
159
160 protected Scheduler createScheduler() throws SchedulerException {
161 Scheduler scheduler = getFactory().getScheduler();
162 scheduler.getContext().put(QuartzConstants.QUARTZ_CAMEL_CONTEXT, getCamelContext());
163 return scheduler;
164 }
165 }