Subversion Repositories XServices

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
70 brianR 1
/*
87 brianR 2
 *   Copyright 2012 Brian Rosenberger (Brutex Network)
70 brianR 3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
package net.brutex.xservices.ws.impl;
17
 
18
 
19
import java.util.ArrayList;
20
import java.util.GregorianCalendar;
21
import java.util.List;
22
import java.util.Set;
23
import java.util.TimeZone;
24
import java.util.UUID;
25
 
26
import javax.jws.WebService;
27
 
28
import org.quartz.JobBuilder;
29
import org.quartz.JobDataMap;
30
import org.quartz.JobDetail;
31
import org.quartz.JobKey;
32
import org.quartz.Scheduler;
33
import org.quartz.SchedulerException;
34
import org.quartz.SimpleTrigger;
35
import org.quartz.Trigger;
36
import org.quartz.TriggerKey;
37
import org.quartz.impl.StdSchedulerFactory;
38
import org.quartz.impl.matchers.GroupMatcher;
87 brianR 39
import static org.quartz.TriggerBuilder.*;
70 brianR 40
import net.brutex.xservices.types.ScheduledJob;
87 brianR 41
import net.brutex.xservices.types.SchedulerStatisticsType;
70 brianR 42
import net.brutex.xservices.util.BrutexNamespaces;
43
import net.brutex.xservices.util.JobWrapper;
44
import net.brutex.xservices.ws.JobService;
45
import net.brutex.xservices.ws.XServicesFault;
46
 
47
/**
48
 * @author Brian Rosenberger
49
 * @since 0.5.0
50
 *
51
 */
52
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.JobService", serviceName = JobService.SERVICE_NAME)
53
public class JobServiceImpl implements JobService {
54
 
55
 
56
	public List<ScheduledJob> getJobList() throws XServicesFault {
57
		List<ScheduledJob> joblist = new ArrayList<ScheduledJob>();
58
		try {
59
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
60
			List<String> jobgroups = scheduler.getJobGroupNames();
61
			for (String g : jobgroups) {
62
				GroupMatcher m = GroupMatcher.groupContains(g);
63
				Set<JobKey> keyset = scheduler.getJobKeys(m);
64
				for (JobKey key : keyset) {
65
					JobDataMap detail = scheduler.getJobDetail(key)
66
							.getJobDataMap();
67
					ScheduledJob job = new ScheduledJob(key.getName(),
68
							(GregorianCalendar) detail.get("date"),
69
							detail.getString("script"));
70
					joblist.add(job);
71
				}
72
 
73
			}
74
 
75
		} catch (SchedulerException e) {
76
			throw new XServicesFault(e);
77
		}
78
		return joblist;
79
	}
80
 
81
 
82
	public ScheduledJob getJob(String uuid) throws XServicesFault {
83
		try {
84
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
85
			JobDetail job = scheduler.getJobDetail(new JobKey(uuid, "DEFAULT"));
86
			if (job == null)
87
				throw new XServicesFault("Job not found.");
88
			Trigger t = scheduler.getTrigger(new TriggerKey(uuid));
89
			GregorianCalendar cal = new GregorianCalendar(TimeZone.getDefault());
90
			cal.setTime(t.getStartTime());
91
			return new ScheduledJob(uuid, cal, job.getJobDataMap().getString(
92
					"script"), job.getDescription());
93
		} catch (SchedulerException e) {
94
			e.printStackTrace();
95
			throw new XServicesFault(e);
96
		}
97
	}
98
 
99
 
100
	public void deleteJob(String uuid) throws XServicesFault {
101
		try {
102
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
103
			JobKey key = new JobKey(uuid, "DEFAULT");
104
			JobDetail job = scheduler.getJobDetail(key);
105
			if (job == null)
106
				throw new XServicesFault("Job not found.");
107
			Trigger t = scheduler.getTrigger(new TriggerKey(uuid));
108
			scheduler.deleteJob(key);
109
		} catch (SchedulerException e) {
110
			throw new XServicesFault(e);
111
		}
112
 
113
	}
114
 
115
 
116
	public String scheduleJob(ScheduledJob job) throws XServicesFault {
117
		try {
118
			// Grab the Scheduler instance from the Factory
119
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
120
 
121
			// and start it off
122
 
123
			if (!scheduler.isStarted())
124
				scheduler.start();
125
			if (scheduler.isInStandbyMode())
126
				scheduler.resumeAll();
127
 
128
			String identity = UUID.randomUUID().toString();
129
			//String identity = "test";
130
			JobDetail job2 = JobBuilder.newJob(JobWrapper.class)
131
					.withIdentity(identity).build();
132
 
133
 
134
			job2.getJobDataMap().put("script", job.getScript());
135
			job2.getJobDataMap().put("description", job.getDescription());
136
			job2.getJobDataMap().put("date", job.getDate());
137
 
138
			SimpleTrigger t = (SimpleTrigger) newTrigger()
139
					.withIdentity(identity).startAt(job.getDate().getTime())
140
					.build();
141
			;
142
 
143
			scheduler.scheduleJob(job2, t);
144
			return identity;
145
		} catch (SchedulerException e) {
146
			e.printStackTrace();
147
			throw new XServicesFault(e);
148
		}
149
	}
150
 
87 brianR 151
 
152
	public SchedulerStatisticsType getStatistics() throws XServicesFault {
153
		try {
154
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
155
			return new SchedulerStatisticsType(scheduler);
156
		} catch (SchedulerException e) {
157
			throw new XServicesFault(e);
158
		}
159
	}
160
 
70 brianR 161
}