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.activemq.broker.jmx;
018    
019    import java.io.IOException;
020    import java.util.List;
021    import javax.management.openmbean.CompositeDataSupport;
022    import javax.management.openmbean.CompositeType;
023    import javax.management.openmbean.TabularData;
024    import javax.management.openmbean.TabularDataSupport;
025    import javax.management.openmbean.TabularType;
026    import org.apache.activemq.broker.jmx.OpenTypeSupport.OpenTypeFactory;
027    import org.apache.activemq.broker.scheduler.Job;
028    import org.apache.activemq.broker.scheduler.JobImpl;
029    import org.apache.activemq.broker.scheduler.JobScheduler;
030    
031    public class JobSchedulerView implements JobSchedulerViewMBean {
032    
033        private final JobScheduler jobScheduler;
034    
035        public JobSchedulerView(JobScheduler jobScheduler) {
036            this.jobScheduler = jobScheduler;
037        }
038    
039        public TabularData getAllJobs() throws Exception {
040            OpenTypeFactory factory = OpenTypeSupport.getFactory(Job.class);
041            CompositeType ct = factory.getCompositeType();
042            TabularType tt = new TabularType("Scheduled Jobs", "Scheduled Jobs", ct, new String[] { "jobId" });
043            TabularDataSupport rc = new TabularDataSupport(tt);
044            List<Job> jobs = this.jobScheduler.getAllJobs();
045            for (Job job : jobs) {
046                rc.put(new CompositeDataSupport(ct, factory.getFields(job)));
047            }
048            return rc;
049        }
050    
051        public TabularData getAllJobs(String startTime, String finishTime) throws Exception {
052            OpenTypeFactory factory = OpenTypeSupport.getFactory(Job.class);
053            CompositeType ct = factory.getCompositeType();
054            TabularType tt = new TabularType("Scheduled Jobs", "Scheduled Jobs", ct, new String[] { "jobId" });
055            TabularDataSupport rc = new TabularDataSupport(tt);
056            long start = JobImpl.getDataTime(startTime);
057            long finish = JobImpl.getDataTime(finishTime);
058            List<Job> jobs = this.jobScheduler.getAllJobs(start, finish);
059            for (Job job : jobs) {
060                rc.put(new CompositeDataSupport(ct, factory.getFields(job)));
061            }
062            return rc;
063        }
064    
065        public TabularData getNextScheduleJobs() throws Exception {
066            OpenTypeFactory factory = OpenTypeSupport.getFactory(Job.class);
067            CompositeType ct = factory.getCompositeType();
068            TabularType tt = new TabularType("Scheduled Jobs", "Scheduled Jobs", ct, new String[] { "jobId" });
069            TabularDataSupport rc = new TabularDataSupport(tt);
070            List<Job> jobs = this.jobScheduler.getNextScheduleJobs();
071            for (Job job : jobs) {
072                rc.put(new CompositeDataSupport(ct, factory.getFields(job)));
073            }
074            return rc;
075        }
076    
077        public String getNextScheduleTime() throws Exception {
078            long time = this.jobScheduler.getNextScheduleTime();
079            return JobImpl.getDateTime(time);
080        }
081    
082        public void removeAllJobs() throws Exception {
083            this.jobScheduler.removeAllJobs();
084    
085        }
086    
087        public void removeAllJobs(String startTime, String finishTime) throws Exception {
088            long start = JobImpl.getDataTime(startTime);
089            long finish = JobImpl.getDataTime(finishTime);
090            this.jobScheduler.removeAllJobs(start, finish);
091    
092        }
093    
094        public void removeJob(String jobId) throws Exception {
095            this.jobScheduler.remove(jobId);
096    
097        }
098    
099        public void removeJobAtScheduledTime(String time) throws IOException {
100            // TODO Auto-generated method stub
101    
102        }
103    
104    }