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.servicemix.quartz.support;
018    
019    import org.apache.servicemix.quartz.QuartzComponent;
020    import org.apache.servicemix.quartz.QuartzEndpoint;
021    import org.quartz.Job;
022    import org.quartz.JobExecutionContext;
023    import org.quartz.JobExecutionException;
024    import org.quartz.SchedulerException;
025    
026    /**
027     * A Quartz Job which dispatches a message to a component.
028     *
029     * @version $Revision: 806 $
030     */
031    public class ServiceMixJob implements Job {
032        
033        public static final String COMPONENT_NAME = "org.apache.servicemix.quartz.ComponentName";
034        public static final String ENDPOINT_NAME = "org.apache.servicemix.quartz.EndpointName";
035        
036        public void execute(JobExecutionContext context) throws JobExecutionException {
037            try {
038                String componentName = (String) context.getJobDetail().getJobDataMap().get(COMPONENT_NAME);
039                if (componentName == null) {
040                    throw new JobExecutionException("No property '" + COMPONENT_NAME + "' defined. Bad job data map");
041                }
042                QuartzComponent component = (QuartzComponent) context.getScheduler().getContext().get(componentName);
043                if (component == null) {
044                    throw new JobExecutionException("No quartz JBI component available for key: " + componentName + "."
045                            + " Bad job data map");
046                }
047                String endpointName = (String) context.getJobDetail().getJobDataMap().get(ENDPOINT_NAME);
048                if (endpointName == null) {
049                    throw new JobExecutionException("No property '" + ENDPOINT_NAME + "' defined. Bad job data map");
050                }
051                QuartzEndpoint endpoint = (QuartzEndpoint) component.getRegistry().getEndpoint(endpointName);
052                if (endpoint == null) {
053                    throw new JobExecutionException("No quartz JBI endpoint available for key: " + endpointName + "."
054                            + " Bad job data map");
055                }
056                endpoint.onJobExecute(context);
057            } catch (SchedulerException e) {
058                throw new JobExecutionException(e);
059            }
060        }
061        
062    }