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