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.util.Map;
021    
022    import org.apache.camel.CamelContext;
023    import org.apache.camel.impl.DefaultComponent;
024    import org.apache.camel.util.IntrospectionSupport;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.quartz.CronTrigger;
028    import org.quartz.JobDetail;
029    import org.quartz.Scheduler;
030    import org.quartz.SchedulerException;
031    import org.quartz.SchedulerFactory;
032    import org.quartz.Trigger;
033    import org.quartz.impl.StdSchedulerFactory;
034    
035    /**
036     * A <a href="http://activemq.apache.org/camel/quartz.html">Quartz Component</a>
037     *
038     * @version $Revision:520964 $
039     */
040    public class QuartzComponent extends DefaultComponent<QuartzExchange> {
041        private static final transient Log LOG = LogFactory.getLog(QuartzComponent.class);
042        private SchedulerFactory factory;
043        private Scheduler scheduler;
044        private Map<Trigger, JobDetail> triggers;
045    
046        public QuartzComponent() {
047        }
048    
049        public QuartzComponent(final CamelContext context) {
050            super(context);
051        }
052    
053        @Override
054        protected QuartzEndpoint createEndpoint(final String uri, final String remaining, final Map parameters) throws Exception {
055            QuartzEndpoint answer = new QuartzEndpoint(uri, this, getScheduler());
056    
057            // lets split the remaining into a group/name
058            URI u = new URI(uri);
059            String name;
060            String group = "Camel";
061            String path = u.getPath();
062            CronTrigger cronTrigger = null;
063            if (path != null && path.length() > 1) {
064                if (path.startsWith("/")) {
065                    path = path.substring(1);
066                }
067                int idx = path.indexOf('/');
068                if (idx > 0) {
069                    cronTrigger = new CronTrigger();
070                    name = path.substring(0, idx);
071                    String cronExpression = path.substring(idx + 1);
072                    // lets allow / instead of spaces and allow $ instead of ?
073                    cronExpression = cronExpression.replace('/', ' ');
074                    cronExpression = cronExpression.replace('$', '?');
075                    LOG.debug("Creating cron trigger: " + cronExpression);
076                    cronTrigger.setCronExpression(cronExpression);
077                    answer.setTrigger(cronTrigger);
078                } else {
079                    name = path;
080                }
081                group = u.getHost();
082            } else {
083                name = u.getHost();
084            }
085            /*
086             * String[] names = ObjectHelper.splitOnCharacter(remaining, "/", 2); if
087             * (names[1] != null) { group = names[0]; name = names[1]; } else { name =
088             * names[0]; }
089             */
090            Trigger trigger = cronTrigger;
091            if (trigger == null) {
092                trigger = answer.getTrigger();
093            }
094            trigger.setName(name);
095            trigger.setGroup(group);
096    
097            Map triggerParameters = IntrospectionSupport.extractProperties(parameters, "trigger.");
098            Map jobParameters = IntrospectionSupport.extractProperties(parameters, "job.");
099    
100            setProperties(trigger, triggerParameters);
101            setProperties(answer.getJobDetail(), jobParameters);
102    
103            return answer;
104        }
105    
106        @Override
107        protected void doStart() throws Exception {
108            super.doStart();
109            getScheduler().start();
110        }
111    
112        @Override
113        protected void doStop() throws Exception {
114            if (scheduler != null) {
115                scheduler.shutdown();
116            }
117            super.doStop();
118        }
119    
120        // Properties
121        // -------------------------------------------------------------------------
122        public SchedulerFactory getFactory() {
123            if (factory == null) {
124                factory = createSchedulerFactory();
125            }
126            return factory;
127        }
128    
129        public void setFactory(final SchedulerFactory factory) {
130            this.factory = factory;
131        }
132    
133        public Scheduler getScheduler() throws SchedulerException {
134            if (scheduler == null) {
135                scheduler = createScheduler();
136            }
137            return scheduler;
138        }
139    
140        public void setScheduler(final Scheduler scheduler) {
141            this.scheduler = scheduler;
142        }
143    
144        public Map getTriggers() {
145            return triggers;
146        }
147    
148        public void setTriggers(final Map triggers) {
149            this.triggers = triggers;
150        }
151    
152        // Implementation methods
153        // -------------------------------------------------------------------------
154        protected SchedulerFactory createSchedulerFactory() {
155            return new StdSchedulerFactory();
156        }
157    
158        protected Scheduler createScheduler() throws SchedulerException {
159            Scheduler scheduler = getFactory().getScheduler();
160            scheduler.getContext().put(QuartzEndpoint.CONTEXT_KEY, getCamelContext());
161            return scheduler;
162        }
163    }