KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > core > QuartzSchedulerResources


1
2 /*
3  * Copyright 2004-2005 OpenSymphony
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6  * use this file except in compliance with the License. You may obtain a copy
7  * of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations
15  * under the License.
16  *
17  */

18
19 /*
20  * Previously Copyright (c) 2001-2004 James House
21  */

22 package org.quartz.core;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.quartz.spi.JobStore;
28 import org.quartz.spi.SchedulerPlugin;
29 import org.quartz.spi.ThreadPool;
30
31 /**
32  * <p>
33  * Contains all of the resources (<code>JobStore</code>,<code>ThreadPool</code>,
34  * etc.) necessary to create a <code>{@link QuartzScheduler}</code> instance.
35  * </p>
36  *
37  * @see QuartzScheduler
38  *
39  * @author James House
40  */

41 public class QuartzSchedulerResources {
42
43     /*
44      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45      *
46      * Data members.
47      *
48      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49      */

50
51     public static final String JavaDoc CREATE_REGISTRY_NEVER = "never";
52
53     public static final String JavaDoc CREATE_REGISTRY_ALWAYS = "always";
54
55     public static final String JavaDoc CREATE_REGISTRY_AS_NEEDED = "as_needed";
56
57     private String JavaDoc name;
58
59     private String JavaDoc instanceId;
60
61     private String JavaDoc threadName;
62     
63     private String JavaDoc rmiRegistryHost = null;
64
65     private int rmiRegistryPort = 1099;
66
67     private int rmiServerPort = -1;
68
69     private String JavaDoc rmiCreateRegistryStrategy = CREATE_REGISTRY_NEVER;
70
71     private ThreadPool threadPool;
72
73     private JobStore jobStore;
74
75     private JobRunShellFactory jobRunShellFactory;
76
77     private ArrayList JavaDoc schedulerPlugins = new ArrayList JavaDoc(10);
78     
79     private boolean makeSchedulerThreadDaemon = false;
80
81     private String JavaDoc rmiBindName;
82     
83     private boolean jmxExport;
84     
85     private String JavaDoc jmxObjectName;
86     
87     /*
88      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89      *
90      * Constructors.
91      *
92      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
93      */

94
95     /**
96      * <p>
97      * Create an instance with no properties initialized.
98      * </p>
99      */

100     public QuartzSchedulerResources() {
101         // do nothing...
102
}
103
104     /*
105      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106      *
107      * Interface.
108      *
109      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110      */

111
112     /**
113      * <p>
114      * Get the name for the <code>{@link QuartzScheduler}</code>.
115      * </p>
116      */

117     public String JavaDoc getName() {
118         return name;
119     }
120
121     /**
122      * <p>
123      * Set the name for the <code>{@link QuartzScheduler}</code>.
124      * </p>
125      *
126      * @exception IllegalArgumentException
127      * if name is null or empty.
128      */

129     public void setName(String JavaDoc name) {
130         if (name == null || name.trim().length() == 0) {
131             throw new IllegalArgumentException JavaDoc(
132                     "Scheduler name cannot be empty.");
133         }
134
135         this.name = name;
136         
137         if (threadName == null) {
138             // thread name not already set, use default thread name
139
setThreadName(name + "_QuartzSchedulerThread");
140         }
141     }
142
143     /**
144      * <p>
145      * Get the instance Id for the <code>{@link QuartzScheduler}</code>.
146      * </p>
147      */

148     public String JavaDoc getInstanceId() {
149         return instanceId;
150     }
151
152     /**
153      * <p>
154      * Set the name for the <code>{@link QuartzScheduler}</code>.
155      * </p>
156      *
157      * @exception IllegalArgumentException
158      * if name is null or empty.
159      */

160     public void setInstanceId(String JavaDoc instanceId) {
161         if (instanceId == null || instanceId.trim().length() == 0) {
162             throw new IllegalArgumentException JavaDoc(
163                     "Scheduler instanceId cannot be empty.");
164         }
165
166         this.instanceId = instanceId;
167     }
168
169     public static String JavaDoc getUniqueIdentifier(String JavaDoc schedName,
170             String JavaDoc schedInstId) {
171         return schedName + "_$_" + schedInstId;
172     }
173
174     public String JavaDoc getUniqueIdentifier() {
175         return getUniqueIdentifier(name, instanceId);
176     }
177
178     /**
179      * <p>
180      * Get the host name of the RMI Registry that the scheduler should export
181      * itself to.
182      * </p>
183      */

184     public String JavaDoc getRMIRegistryHost() {
185         return rmiRegistryHost;
186     }
187
188     /**
189      * <p>
190      * Set the host name of the RMI Registry that the scheduler should export
191      * itself to.
192      * </p>
193      */

194     public void setRMIRegistryHost(String JavaDoc hostName) {
195         this.rmiRegistryHost = hostName;
196     }
197
198     /**
199      * <p>
200      * Get the port number of the RMI Registry that the scheduler should export
201      * itself to.
202      * </p>
203      */

204     public int getRMIRegistryPort() {
205         return rmiRegistryPort;
206     }
207
208     /**
209      * <p>
210      * Set the port number of the RMI Registry that the scheduler should export
211      * itself to.
212      * </p>
213      */

214     public void setRMIRegistryPort(int port) {
215         this.rmiRegistryPort = port;
216     }
217
218
219     /**
220      * <p>
221      * Get the port number the scheduler server will be bound to.
222      * </p>
223      */

224     public int getRMIServerPort() {
225         return rmiServerPort;
226     }
227
228     /**
229      * <p>
230      * Set the port number the scheduler server will be bound to.
231      * </p>
232      */

233     public void setRMIServerPort(int port) {
234         this.rmiServerPort = port;
235     }
236     
237     /**
238      * <p>
239      * Get the setting of whether or not Quartz should create an RMI Registry,
240      * and if so, how.
241      * </p>
242      */

243     public String JavaDoc getRMICreateRegistryStrategy() {
244         return rmiCreateRegistryStrategy;
245     }
246
247     /**
248      * <p>
249      * Get the name for the <code>{@link QuartzSchedulerThread}</code>.
250      * </p>
251      */

252     public String JavaDoc getThreadName() {
253         return threadName;
254     }
255
256     /**
257      * <p>
258      * Set the name for the <code>{@link QuartzSchedulerThread}</code>.
259      * </p>
260      *
261      * @exception IllegalArgumentException
262      * if name is null or empty.
263      */

264     public void setThreadName(String JavaDoc threadName) {
265         if (threadName == null || threadName.trim().length() == 0) {
266             throw new IllegalArgumentException JavaDoc(
267                     "Scheduler thread name cannot be empty.");
268         }
269
270         this.threadName = threadName;
271     }
272     
273     /**
274      * <p>
275      * Set whether or not Quartz should create an RMI Registry, and if so, how.
276      * </p>
277      *
278      * @see #CREATE_REGISTRY_ALWAYS
279      * @see #CREATE_REGISTRY_AS_NEEDED
280      * @see #CREATE_REGISTRY_NEVER
281      */

282     public void setRMICreateRegistryStrategy(String JavaDoc rmiCreateRegistryStrategy) {
283         if (rmiCreateRegistryStrategy == null
284                 || rmiCreateRegistryStrategy.trim().length() == 0) {
285             rmiCreateRegistryStrategy = CREATE_REGISTRY_NEVER;
286         } else if (rmiCreateRegistryStrategy.equalsIgnoreCase("true")) {
287             rmiCreateRegistryStrategy = CREATE_REGISTRY_AS_NEEDED;
288         } else if (rmiCreateRegistryStrategy.equalsIgnoreCase("false")) {
289             rmiCreateRegistryStrategy = CREATE_REGISTRY_NEVER;
290         } else if (rmiCreateRegistryStrategy.equalsIgnoreCase(CREATE_REGISTRY_ALWAYS)) {
291             rmiCreateRegistryStrategy = CREATE_REGISTRY_ALWAYS;
292         } else if (rmiCreateRegistryStrategy.equalsIgnoreCase(CREATE_REGISTRY_AS_NEEDED)) {
293             rmiCreateRegistryStrategy = CREATE_REGISTRY_AS_NEEDED;
294         } else if (rmiCreateRegistryStrategy.equalsIgnoreCase(CREATE_REGISTRY_NEVER)) {
295             rmiCreateRegistryStrategy = CREATE_REGISTRY_NEVER;
296         } else {
297             throw new IllegalArgumentException JavaDoc(
298                     "Faild to set RMICreateRegistryStrategy - strategy unknown: '"
299                             + rmiCreateRegistryStrategy + "'");
300         }
301
302         this.rmiCreateRegistryStrategy = rmiCreateRegistryStrategy;
303     }
304
305     /**
306      * <p>
307      * Get the <code>{@link ThreadPool}</code> for the <code>{@link QuartzScheduler}</code>
308      * to use.
309      * </p>
310      */

311     public ThreadPool getThreadPool() {
312         return threadPool;
313     }
314
315     /**
316      * <p>
317      * Set the <code>{@link ThreadPool}</code> for the <code>{@link QuartzScheduler}</code>
318      * to use.
319      * </p>
320      *
321      * @exception IllegalArgumentException
322      * if threadPool is null.
323      */

324     public void setThreadPool(ThreadPool threadPool) {
325         if (threadPool == null) {
326             throw new IllegalArgumentException JavaDoc("ThreadPool cannot be null.");
327         }
328
329         this.threadPool = threadPool;
330     }
331
332     /**
333      * <p>
334      * Get the <code>{@link JobStore}</code> for the <code>{@link QuartzScheduler}</code>
335      * to use.
336      * </p>
337      */

338     public JobStore getJobStore() {
339         return jobStore;
340     }
341
342     /**
343      * <p>
344      * Set the <code>{@link JobStore}</code> for the <code>{@link QuartzScheduler}</code>
345      * to use.
346      * </p>
347      *
348      * @exception IllegalArgumentException
349      * if jobStore is null.
350      */

351     public void setJobStore(JobStore jobStore) {
352         if (jobStore == null) {
353             throw new IllegalArgumentException JavaDoc("JobStore cannot be null.");
354         }
355
356         this.jobStore = jobStore;
357     }
358
359     /**
360      * <p>
361      * Get the <code>{@link JobRunShellFactory}</code> for the <code>{@link QuartzScheduler}</code>
362      * to use.
363      * </p>
364      */

365     public JobRunShellFactory getJobRunShellFactory() {
366         return jobRunShellFactory;
367     }
368
369     /**
370      * <p>
371      * Set the <code>{@link JobRunShellFactory}</code> for the <code>{@link QuartzScheduler}</code>
372      * to use.
373      * </p>
374      *
375      * @exception IllegalArgumentException
376      * if jobRunShellFactory is null.
377      */

378     public void setJobRunShellFactory(JobRunShellFactory jobRunShellFactory) {
379         if (jobRunShellFactory == null) {
380             throw new IllegalArgumentException JavaDoc(
381                     "JobRunShellFactory cannot be null.");
382         }
383
384         this.jobRunShellFactory = jobRunShellFactory;
385     }
386
387     /**
388      * <p>
389      * Add the given <code>{@link org.quartz.spi.SchedulerPlugin}</code> for the
390      * <code>{@link QuartzScheduler}</code> to use. This method expects the plugin's
391      * "initialize" method to be invoked externally (either before or after
392      * this method is called).
393      * </p>
394      */

395     public void addSchedulerPlugin(SchedulerPlugin plugin) {
396         schedulerPlugins.add(plugin);
397     }
398     
399     /**
400      * <p>
401      * Get the <code>List</code> of all
402      * <code>{@link org.quartz.spi.SchedulerPlugin}</code>s for the
403      * <code>{@link QuartzScheduler}</code> to use.
404      * </p>
405      */

406     public List JavaDoc getSchedulerPlugins() {
407         return schedulerPlugins;
408     }
409
410     /**
411      * Get whether to mark the Quartz scheduling thread as daemon.
412      *
413      * @see Thread#setDaemon(boolean)
414      */

415     public boolean getMakeSchedulerThreadDaemon() {
416         return makeSchedulerThreadDaemon;
417     }
418
419     /**
420      * Set whether to mark the Quartz scheduling thread as daemon.
421      *
422      * @see Thread#setDaemon(boolean)
423      */

424     public void setMakeSchedulerThreadDaemon(boolean makeSchedulerThreadDaemon) {
425         this.makeSchedulerThreadDaemon = makeSchedulerThreadDaemon;
426     }
427
428     /**
429      * Get the name under which to bind the QuartzScheduler in RMI. Will
430      * return the value of the uniqueIdentifier property if explict RMI bind
431      * name was never set.
432      *
433      * @see #getUniqueIdentifier()
434      */

435     public String JavaDoc getRMIBindName() {
436         return (rmiBindName == null) ? getUniqueIdentifier() : rmiBindName;
437     }
438
439     /**
440      * Set the name under which to bind the QuartzScheduler in RMI. If unset,
441      * defaults to the value of the uniqueIdentifier property.
442      *
443      * @see #getUniqueIdentifier()
444      */

445     public void setRMIBindName(String JavaDoc rmiBindName) {
446         this.rmiBindName = rmiBindName;
447     }
448
449     /**
450      * Get whether the QuartzScheduler should be registered with the local
451      * MBeanServer.
452      */

453     public boolean getJMXExport() {
454         return jmxExport;
455     }
456
457     /**
458      * Set whether the QuartzScheduler should be registered with the local
459      * MBeanServer.
460      */

461     public void setJMXExport(boolean jmxExport) {
462         this.jmxExport = jmxExport;
463     }
464
465     /**
466      * Get the name under which the QuartzScheduler should be registered with
467      * the local MBeanServer. If unset, defaults to the value calculated by
468      * <code>generateJMXObjectName<code>.
469      *
470      * @see #generateJMXObjectName(String, String)
471      */

472     public String JavaDoc getJMXObjectName() {
473         return (jmxObjectName == null) ? generateJMXObjectName(name, instanceId) : jmxObjectName;
474     }
475
476     /**
477      * Set the name under which the QuartzScheduler should be registered with
478      * the local MBeanServer. If unset, defaults to the value calculated by
479      * <code>generateJMXObjectName<code>.
480      *
481      * @see #generateJMXObjectName(String, String)
482      */

483     public void setJMXObjectName(String JavaDoc jmxObjectName) {
484         this.jmxObjectName = jmxObjectName;
485     }
486     
487     /**
488      * Create the name under which this scheduler should be registered in JMX.
489      * <p>
490      * The name is composed as:
491      * quartz:type=QuartzScheduler,name=<i>[schedName]</i>,instance=<i>[schedInstId]</i>
492      * </p>
493      */

494     public static String JavaDoc generateJMXObjectName(String JavaDoc schedName, String JavaDoc schedInstId) {
495         return "quartz:type=QuartzScheduler" +
496             ",name=" + schedName +
497             ",instance=" + schedInstId;
498     }
499 }
500
Popular Tags