001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.camel.component.quartz; 019 020 import org.apache.camel.CamelContext; 021 import org.apache.camel.impl.DefaultComponent; 022 import org.apache.camel.util.IntrospectionSupport; 023 import org.apache.camel.util.ObjectHelper; 024 import org.apache.commons.logging.Log; 025 import org.apache.commons.logging.LogFactory; 026 import org.quartz.JobDetail; 027 import org.quartz.Scheduler; 028 import org.quartz.SchedulerException; 029 import org.quartz.SchedulerFactory; 030 import org.quartz.Trigger; 031 import org.quartz.CronTrigger; 032 import org.quartz.impl.StdSchedulerFactory; 033 034 import java.util.Map; 035 import java.net.URI; 036 037 import com.sun.jndi.toolkit.url.Uri; 038 039 /** 040 * A <a href="http://activemq.apache.org/camel/quartz.html">Quartz Component</a> 041 * 042 * @version $Revision:520964 $ 043 */ 044 public class QuartzComponent extends DefaultComponent<QuartzExchange> { 045 private static final transient Log log = LogFactory.getLog(QuartzComponent.class); 046 private SchedulerFactory factory; 047 private Scheduler scheduler; 048 private Map<Trigger, JobDetail> triggers; 049 050 public QuartzComponent() { 051 } 052 053 public QuartzComponent(CamelContext context) { 054 super(context); 055 } 056 057 @Override 058 protected QuartzEndpoint createEndpoint(String uri, String remaining, Map 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 name; 064 String group = "Camel"; 065 String path = u.getPath(); 066 CronTrigger cronTrigger = null; 067 if (path != null && path.length() > 1) { 068 if (path.startsWith("/")) { 069 path = path.substring(1); 070 } 071 int idx = path.indexOf('/'); 072 if (idx > 0) { 073 cronTrigger = new CronTrigger(); 074 name = path.substring(0, idx); 075 String cronExpression = path.substring(idx + 1); 076 // lets allow / instead of spaces and allow $ instead of ? 077 cronExpression = cronExpression.replace('/', ' '); 078 cronExpression = cronExpression.replace('$', '?'); 079 log.debug("Creating cron trigger: " + cronExpression); 080 cronTrigger.setCronExpression(cronExpression); 081 answer.setTrigger(cronTrigger); 082 } 083 else { 084 name = path; 085 } 086 group = u.getHost(); 087 } 088 else { 089 name = u.getHost(); 090 } 091 /* 092 String[] names = ObjectHelper.splitOnCharacter(remaining, "/", 2); 093 if (names[1] != null) { 094 group = names[0]; 095 name = names[1]; 096 } 097 else { 098 name = names[0]; 099 } 100 */ 101 Trigger trigger = cronTrigger; 102 if (trigger == null) { 103 trigger = answer.getTrigger(); 104 } 105 trigger.setName(name); 106 trigger.setGroup(group); 107 108 Map triggerParameters = IntrospectionSupport.extractProperties(parameters, "trigger."); 109 Map jobParameters = IntrospectionSupport.extractProperties(parameters, "job."); 110 111 IntrospectionSupport.setProperties(trigger, triggerParameters); 112 IntrospectionSupport.setProperties(answer.getJobDetail(), jobParameters); 113 114 return answer; 115 } 116 117 @Override 118 protected void doStart() throws Exception { 119 super.doStart(); 120 getScheduler().start(); 121 } 122 123 @Override 124 protected void doStop() throws Exception { 125 if (scheduler != null) { 126 scheduler.shutdown(); 127 } 128 super.doStop(); 129 } 130 131 // Properties 132 //------------------------------------------------------------------------- 133 public SchedulerFactory getFactory() { 134 if (factory == null) { 135 factory = createSchedulerFactory(); 136 } 137 return factory; 138 } 139 140 public void setFactory(SchedulerFactory factory) { 141 this.factory = factory; 142 } 143 144 public Scheduler getScheduler() throws SchedulerException { 145 if (scheduler == null) { 146 scheduler = createScheduler(); 147 } 148 return scheduler; 149 } 150 151 public void setScheduler(Scheduler scheduler) { 152 this.scheduler = scheduler; 153 } 154 155 public Map getTriggers() { 156 return triggers; 157 } 158 159 public void setTriggers(Map triggers) { 160 this.triggers = triggers; 161 } 162 163 // Implementation methods 164 //------------------------------------------------------------------------- 165 protected SchedulerFactory createSchedulerFactory() { 166 return new StdSchedulerFactory(); 167 } 168 169 protected Scheduler createScheduler() throws SchedulerException { 170 return getFactory().getScheduler(); 171 } 172 }