Coverage Report - org.apache.camel.component.quartz.QuartzComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
QuartzComponent
85% 
100% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.component.quartz;
 19  
 
 20  
 import org.apache.camel.CamelContext;
 21  
 import org.apache.camel.impl.DefaultComponent;
 22  
 import org.apache.camel.util.IntrospectionSupport;
 23  
 import org.apache.camel.util.ObjectHelper;
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 import org.quartz.JobDetail;
 27  
 import org.quartz.Scheduler;
 28  
 import org.quartz.SchedulerException;
 29  
 import org.quartz.SchedulerFactory;
 30  
 import org.quartz.Trigger;
 31  
 import org.quartz.CronTrigger;
 32  
 import org.quartz.impl.StdSchedulerFactory;
 33  
 
 34  
 import java.util.Map;
 35  
 import java.net.URI;
 36  
 
 37  
 import com.sun.jndi.toolkit.url.Uri;
 38  
 
 39  
 /**
 40  
  * A <a href="http://activemq.apache.org/camel/quartz.html">Quartz Component</a>
 41  
  *
 42  
  * @version $Revision:520964 $
 43  
  */
 44  4
 public class QuartzComponent extends DefaultComponent<QuartzExchange> {
 45  1
     private static final transient Log log = LogFactory.getLog(QuartzComponent.class);
 46  
     private SchedulerFactory factory;
 47  
     private Scheduler scheduler;
 48  
     private Map<Trigger, JobDetail> triggers;
 49  
 
 50  4
     public QuartzComponent() {
 51  4
     }
 52  
 
 53  
     public QuartzComponent(CamelContext context) {
 54  0
         super(context);
 55  0
     }
 56  
 
 57  
     @Override
 58  
     protected QuartzEndpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
 59  4
         QuartzEndpoint answer = new QuartzEndpoint(uri, this, getScheduler());
 60  
 
 61  
         // lets split the remaining into a group/name
 62  4
         URI u = new URI(uri);
 63  
         String name;
 64  4
         String group = "Camel";
 65  4
         String path = u.getPath();
 66  4
         CronTrigger cronTrigger = null;
 67  4
         if (path != null && path.length() > 1) {
 68  3
             if (path.startsWith("/")) {
 69  3
                 path = path.substring(1);
 70  
             }
 71  3
             int idx = path.indexOf('/');
 72  3
             if (idx > 0) {
 73  1
                 cronTrigger = new CronTrigger();
 74  1
                 name = path.substring(0, idx);
 75  1
                 String cronExpression = path.substring(idx + 1);
 76  
                 // lets allow / instead of spaces and allow $ instead of ?
 77  1
                 cronExpression = cronExpression.replace('/', ' ');
 78  1
                 cronExpression = cronExpression.replace('$', '?');
 79  1
                 log.debug("Creating cron trigger: " + cronExpression);
 80  1
                 cronTrigger.setCronExpression(cronExpression);
 81  1
                 answer.setTrigger(cronTrigger);
 82  1
             }
 83  
             else {
 84  2
                 name = path;
 85  
             }
 86  3
             group = u.getHost();
 87  3
         }
 88  
         else {
 89  1
             name = u.getHost();
 90  
         }
 91  
 /*
 92  
         String[] names = ObjectHelper.splitOnCharacter(remaining, "/", 2);
 93  
         if (names[1] != null) {
 94  
             group = names[0];
 95  
             name = names[1];
 96  
         }
 97  
         else {
 98  
             name = names[0];
 99  
         }
 100  
 */
 101  4
         Trigger trigger = cronTrigger;
 102  4
         if (trigger == null) {
 103  3
             trigger = answer.getTrigger();
 104  
         }
 105  4
         trigger.setName(name);
 106  4
         trigger.setGroup(group);
 107  
 
 108  4
         Map triggerParameters = IntrospectionSupport.extractProperties(parameters, "trigger.");
 109  4
         Map jobParameters = IntrospectionSupport.extractProperties(parameters, "job.");
 110  
 
 111  4
         IntrospectionSupport.setProperties(trigger, triggerParameters);
 112  4
         IntrospectionSupport.setProperties(answer.getJobDetail(), jobParameters);
 113  
 
 114  4
         return answer;
 115  
     }
 116  
 
 117  
     @Override
 118  
     protected void doStart() throws Exception {
 119  4
         super.doStart();
 120  4
         getScheduler().start();
 121  4
     }
 122  
 
 123  
     @Override
 124  
     protected void doStop() throws Exception {
 125  4
         if (scheduler != null) {
 126  4
             scheduler.shutdown();
 127  
         }
 128  4
         super.doStop();
 129  4
     }
 130  
 
 131  
     // Properties
 132  
     //-------------------------------------------------------------------------
 133  
     public SchedulerFactory getFactory() {
 134  4
         if (factory == null) {
 135  4
             factory = createSchedulerFactory();
 136  
         }
 137  4
         return factory;
 138  
     }
 139  
 
 140  
     public void setFactory(SchedulerFactory factory) {
 141  0
         this.factory = factory;
 142  0
     }
 143  
 
 144  
     public Scheduler getScheduler() throws SchedulerException {
 145  8
         if (scheduler == null) {
 146  4
             scheduler = createScheduler();
 147  
         }
 148  8
         return scheduler;
 149  
     }
 150  
 
 151  
     public void setScheduler(Scheduler scheduler) {
 152  0
         this.scheduler = scheduler;
 153  0
     }
 154  
 
 155  
     public Map getTriggers() {
 156  0
         return triggers;
 157  
     }
 158  
 
 159  
     public void setTriggers(Map triggers) {
 160  0
         this.triggers = triggers;
 161  0
     }
 162  
 
 163  
     // Implementation methods
 164  
     //-------------------------------------------------------------------------
 165  
     protected SchedulerFactory createSchedulerFactory() {
 166  4
         return new StdSchedulerFactory();
 167  
     }
 168  
 
 169  
     protected Scheduler createScheduler() throws SchedulerException {
 170  4
         return getFactory().getScheduler();
 171  
     }
 172  
 }