Subversion Repositories XServices

Rev

Rev 198 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
70 brianR 1
/*
97 brianR 2
 *   Copyright 2011 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
 
97 brianR 18
import static org.quartz.TriggerBuilder.newTrigger;
70 brianR 19
 
20
import java.util.ArrayList;
21
import java.util.GregorianCalendar;
22
import java.util.List;
23
import java.util.Set;
24
import java.util.TimeZone;
25
import java.util.UUID;
26
 
27
import javax.jws.WebService;
28
 
199 brianR 29
import org.quartz.*;
70 brianR 30
import org.quartz.impl.StdSchedulerFactory;
31
import org.quartz.impl.matchers.GroupMatcher;
97 brianR 32
 
70 brianR 33
import net.brutex.xservices.types.ScheduledJob;
34
import net.brutex.xservices.util.BrutexNamespaces;
35
import net.brutex.xservices.util.JobWrapper;
36
import net.brutex.xservices.ws.JobService;
37
import net.brutex.xservices.ws.XServicesFault;
38
 
39
/**
40
 * @author Brian Rosenberger
41
 * @since 0.5.0
42
 *
43
 */
44
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.JobService", serviceName = JobService.SERVICE_NAME)
45
public class JobServiceImpl implements JobService {
46
 
47
 
48
	public List<ScheduledJob> getJobList() throws XServicesFault {
49
		List<ScheduledJob> joblist = new ArrayList<ScheduledJob>();
50
		try {
51
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
52
			List<String> jobgroups = scheduler.getJobGroupNames();
53
			for (String g : jobgroups) {
54
				GroupMatcher m = GroupMatcher.groupContains(g);
55
				Set<JobKey> keyset = scheduler.getJobKeys(m);
56
				for (JobKey key : keyset) {
57
					JobDataMap detail = scheduler.getJobDetail(key)
58
							.getJobDataMap();
59
					ScheduledJob job = new ScheduledJob(key.getName(),
60
							(GregorianCalendar) detail.get("date"),
61
							detail.getString("script"));
62
					joblist.add(job);
63
				}
64
 
65
			}
66
 
67
		} catch (SchedulerException e) {
68
			throw new XServicesFault(e);
69
		}
70
		return joblist;
71
	}
72
 
73
 
74
	public ScheduledJob getJob(String uuid) throws XServicesFault {
75
		try {
76
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
77
			JobDetail job = scheduler.getJobDetail(new JobKey(uuid, "DEFAULT"));
78
			if (job == null)
79
				throw new XServicesFault("Job not found.");
80
			Trigger t = scheduler.getTrigger(new TriggerKey(uuid));
81
			GregorianCalendar cal = new GregorianCalendar(TimeZone.getDefault());
82
			cal.setTime(t.getStartTime());
83
			return new ScheduledJob(uuid, cal, job.getJobDataMap().getString(
84
					"script"), job.getDescription());
85
		} catch (SchedulerException e) {
86
			e.printStackTrace();
87
			throw new XServicesFault(e);
88
		}
89
	}
90
 
91
 
92
	public void deleteJob(String uuid) throws XServicesFault {
93
		try {
94
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
95
			JobKey key = new JobKey(uuid, "DEFAULT");
96
			JobDetail job = scheduler.getJobDetail(key);
97
			if (job == null)
98
				throw new XServicesFault("Job not found.");
99
			Trigger t = scheduler.getTrigger(new TriggerKey(uuid));
100
			scheduler.deleteJob(key);
101
		} catch (SchedulerException e) {
102
			throw new XServicesFault(e);
103
		}
104
 
105
	}
106
 
107
 
108
	public String scheduleJob(ScheduledJob job) throws XServicesFault {
109
		try {
110
			// Grab the Scheduler instance from the Factory
111
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
112
 
113
			// and start it off
114
 
115
			if (!scheduler.isStarted())
116
				scheduler.start();
117
			if (scheduler.isInStandbyMode())
118
				scheduler.resumeAll();
119
 
120
			String identity = UUID.randomUUID().toString();
121
			//String identity = "test";
122
			JobDetail job2 = JobBuilder.newJob(JobWrapper.class)
123
					.withIdentity(identity).build();
124
 
125
 
126
			job2.getJobDataMap().put("script", job.getScript());
127
			job2.getJobDataMap().put("description", job.getDescription());
128
			job2.getJobDataMap().put("date", job.getDate());
129
 
130
			SimpleTrigger t = (SimpleTrigger) newTrigger()
131
					.withIdentity(identity).startAt(job.getDate().getTime())
132
					.build();
133
			;
134
 
135
			scheduler.scheduleJob(job2, t);
136
			return identity;
137
		} catch (SchedulerException e) {
138
			e.printStackTrace();
139
			throw new XServicesFault(e);
140
		}
141
	}
142
 
199 brianR 143
 
70 brianR 144
}