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 */
017package org.apache.activemq.broker.scheduler;
018
019import java.util.Collections;
020import java.util.List;
021
022import org.apache.activemq.util.ByteSequence;
023
024/**
025 * A wrapper for instances of the JobScheduler interface that ensures that methods
026 * provides safe and sane return values and can deal with null values being passed
027 * in etc.  Provides a measure of safety when using unknown implementations of the
028 * JobSchedulerStore which might not always do the right thing.
029 */
030public class JobSchedulerFacade implements JobScheduler {
031
032    private final SchedulerBroker broker;
033
034    JobSchedulerFacade(SchedulerBroker broker) {
035        this.broker = broker;
036    }
037
038    @Override
039    public void addListener(JobListener l) throws Exception {
040        JobScheduler js = this.broker.getInternalScheduler();
041        if (js != null) {
042            js.addListener(l);
043        }
044    }
045
046    @Override
047    public List<Job> getAllJobs() throws Exception {
048        JobScheduler js = this.broker.getInternalScheduler();
049        if (js != null) {
050            return js.getAllJobs();
051        }
052        return Collections.emptyList();
053    }
054
055    @Override
056    public List<Job> getAllJobs(long start, long finish) throws Exception {
057        JobScheduler js = this.broker.getInternalScheduler();
058        if (js != null) {
059            return js.getAllJobs(start, finish);
060        }
061        return Collections.emptyList();
062    }
063
064    @Override
065    public String getName() throws Exception {
066        JobScheduler js = this.broker.getInternalScheduler();
067        if (js != null) {
068            return js.getName();
069        }
070        return "";
071    }
072
073    @Override
074    public List<Job> getNextScheduleJobs() throws Exception {
075        JobScheduler js = this.broker.getInternalScheduler();
076        if (js != null) {
077            return js.getNextScheduleJobs();
078        }
079        return Collections.emptyList();
080    }
081
082    @Override
083    public long getNextScheduleTime() throws Exception {
084        JobScheduler js = this.broker.getInternalScheduler();
085        if (js != null) {
086            return js.getNextScheduleTime();
087        }
088        return 0;
089    }
090
091    @Override
092    public void remove(long time) throws Exception {
093        JobScheduler js = this.broker.getInternalScheduler();
094        if (js != null) {
095            js.remove(time);
096        }
097    }
098
099    @Override
100    public void remove(String jobId) throws Exception {
101        JobScheduler js = this.broker.getInternalScheduler();
102        if (js != null) {
103            js.remove(jobId);
104        }
105    }
106
107    @Override
108    public void removeAllJobs() throws Exception {
109        JobScheduler js = this.broker.getInternalScheduler();
110        if (js != null) {
111            js.removeAllJobs();
112        }
113    }
114
115    @Override
116    public void removeAllJobs(long start, long finish) throws Exception {
117        JobScheduler js = this.broker.getInternalScheduler();
118        if (js != null) {
119            js.removeAllJobs(start, finish);
120        }
121    }
122
123    @Override
124    public void removeListener(JobListener l) throws Exception {
125        JobScheduler js = this.broker.getInternalScheduler();
126        if (js != null) {
127            js.removeListener(l);
128        }
129    }
130
131    @Override
132    public void schedule(String jobId, ByteSequence payload, long delay) throws Exception {
133        JobScheduler js = this.broker.getInternalScheduler();
134        if (js != null) {
135            js.schedule(jobId, payload, delay);
136        }
137    }
138
139    @Override
140    public void schedule(String jobId, ByteSequence payload, String cronEntry, long start, long period, int repeat) throws Exception {
141        JobScheduler js = this.broker.getInternalScheduler();
142        if (js != null) {
143            js.schedule(jobId, payload, cronEntry, start, period, repeat);
144        }
145    }
146
147    @Override
148    public void schedule(String jobId, ByteSequence payload, String cronEntry) throws Exception {
149        JobScheduler js = this.broker.getInternalScheduler();
150        if (js != null) {
151            js.schedule(jobId, payload, cronEntry);
152        }
153    }
154
155    @Override
156    public void startDispatching() throws Exception {
157        JobScheduler js = this.broker.getInternalScheduler();
158        if (js != null) {
159            js.startDispatching();
160        }
161    }
162
163    @Override
164    public void stopDispatching() throws Exception {
165        JobScheduler js = this.broker.getInternalScheduler();
166        if (js != null) {
167            js.stopDispatching();
168        }
169    }
170}