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