001: /*
002: * Copyright 2004-2005 OpenSymphony
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy
006: * of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations
014: * under the License.
015: *
016: */
017:
018: /*
019: * Previously Copyright (c) 2001-2004 James House
020: */
021: package org.quartz.core;
022:
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: import org.quartz.spi.JobStore;
027: import org.quartz.spi.SchedulerPlugin;
028: import org.quartz.spi.ThreadPool;
029:
030: /**
031: * <p>
032: * Contains all of the resources (<code>JobStore</code>,<code>ThreadPool</code>,
033: * etc.) necessary to create a <code>{@link QuartzScheduler}</code> instance.
034: * </p>
035: *
036: * @see QuartzScheduler
037: *
038: * @author James House
039: */
040: public class QuartzSchedulerResources {
041:
042: /*
043: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
044: *
045: * Data members.
046: *
047: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
048: */
049:
050: public static final String CREATE_REGISTRY_NEVER = "never";
051:
052: public static final String CREATE_REGISTRY_ALWAYS = "always";
053:
054: public static final String CREATE_REGISTRY_AS_NEEDED = "as_needed";
055:
056: private String name;
057:
058: private String instanceId;
059:
060: private String threadName;
061:
062: private String rmiRegistryHost = null;
063:
064: private int rmiRegistryPort = 1099;
065:
066: private int rmiServerPort = -1;
067:
068: private String rmiCreateRegistryStrategy = CREATE_REGISTRY_NEVER;
069:
070: private ThreadPool threadPool;
071:
072: private JobStore jobStore;
073:
074: private JobRunShellFactory jobRunShellFactory;
075:
076: private ArrayList schedulerPlugins = new ArrayList(10);
077:
078: private boolean makeSchedulerThreadDaemon = false;
079:
080: private String rmiBindName;
081:
082: private boolean jmxExport;
083:
084: private String jmxObjectName;
085:
086: /*
087: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
088: *
089: * Constructors.
090: *
091: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
092: */
093:
094: /**
095: * <p>
096: * Create an instance with no properties initialized.
097: * </p>
098: */
099: public QuartzSchedulerResources() {
100: // do nothing...
101: }
102:
103: /*
104: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
105: *
106: * Interface.
107: *
108: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109: */
110:
111: /**
112: * <p>
113: * Get the name for the <code>{@link QuartzScheduler}</code>.
114: * </p>
115: */
116: public String getName() {
117: return name;
118: }
119:
120: /**
121: * <p>
122: * Set the name for the <code>{@link QuartzScheduler}</code>.
123: * </p>
124: *
125: * @exception IllegalArgumentException
126: * if name is null or empty.
127: */
128: public void setName(String name) {
129: if (name == null || name.trim().length() == 0) {
130: throw new IllegalArgumentException(
131: "Scheduler name cannot be empty.");
132: }
133:
134: this .name = name;
135:
136: if (threadName == null) {
137: // thread name not already set, use default thread name
138: setThreadName(name + "_QuartzSchedulerThread");
139: }
140: }
141:
142: /**
143: * <p>
144: * Get the instance Id for the <code>{@link QuartzScheduler}</code>.
145: * </p>
146: */
147: public String getInstanceId() {
148: return instanceId;
149: }
150:
151: /**
152: * <p>
153: * Set the name for the <code>{@link QuartzScheduler}</code>.
154: * </p>
155: *
156: * @exception IllegalArgumentException
157: * if name is null or empty.
158: */
159: public void setInstanceId(String instanceId) {
160: if (instanceId == null || instanceId.trim().length() == 0) {
161: throw new IllegalArgumentException(
162: "Scheduler instanceId cannot be empty.");
163: }
164:
165: this .instanceId = instanceId;
166: }
167:
168: public static String getUniqueIdentifier(String schedName,
169: String schedInstId) {
170: return schedName + "_$_" + schedInstId;
171: }
172:
173: public String getUniqueIdentifier() {
174: return getUniqueIdentifier(name, instanceId);
175: }
176:
177: /**
178: * <p>
179: * Get the host name of the RMI Registry that the scheduler should export
180: * itself to.
181: * </p>
182: */
183: public String getRMIRegistryHost() {
184: return rmiRegistryHost;
185: }
186:
187: /**
188: * <p>
189: * Set the host name of the RMI Registry that the scheduler should export
190: * itself to.
191: * </p>
192: */
193: public void setRMIRegistryHost(String hostName) {
194: this .rmiRegistryHost = hostName;
195: }
196:
197: /**
198: * <p>
199: * Get the port number of the RMI Registry that the scheduler should export
200: * itself to.
201: * </p>
202: */
203: public int getRMIRegistryPort() {
204: return rmiRegistryPort;
205: }
206:
207: /**
208: * <p>
209: * Set the port number of the RMI Registry that the scheduler should export
210: * itself to.
211: * </p>
212: */
213: public void setRMIRegistryPort(int port) {
214: this .rmiRegistryPort = port;
215: }
216:
217: /**
218: * <p>
219: * Get the port number the scheduler server will be bound to.
220: * </p>
221: */
222: public int getRMIServerPort() {
223: return rmiServerPort;
224: }
225:
226: /**
227: * <p>
228: * Set the port number the scheduler server will be bound to.
229: * </p>
230: */
231: public void setRMIServerPort(int port) {
232: this .rmiServerPort = port;
233: }
234:
235: /**
236: * <p>
237: * Get the setting of whether or not Quartz should create an RMI Registry,
238: * and if so, how.
239: * </p>
240: */
241: public String getRMICreateRegistryStrategy() {
242: return rmiCreateRegistryStrategy;
243: }
244:
245: /**
246: * <p>
247: * Get the name for the <code>{@link QuartzSchedulerThread}</code>.
248: * </p>
249: */
250: public String getThreadName() {
251: return threadName;
252: }
253:
254: /**
255: * <p>
256: * Set the name for the <code>{@link QuartzSchedulerThread}</code>.
257: * </p>
258: *
259: * @exception IllegalArgumentException
260: * if name is null or empty.
261: */
262: public void setThreadName(String threadName) {
263: if (threadName == null || threadName.trim().length() == 0) {
264: throw new IllegalArgumentException(
265: "Scheduler thread name cannot be empty.");
266: }
267:
268: this .threadName = threadName;
269: }
270:
271: /**
272: * <p>
273: * Set whether or not Quartz should create an RMI Registry, and if so, how.
274: * </p>
275: *
276: * @see #CREATE_REGISTRY_ALWAYS
277: * @see #CREATE_REGISTRY_AS_NEEDED
278: * @see #CREATE_REGISTRY_NEVER
279: */
280: public void setRMICreateRegistryStrategy(
281: String rmiCreateRegistryStrategy) {
282: if (rmiCreateRegistryStrategy == null
283: || rmiCreateRegistryStrategy.trim().length() == 0) {
284: rmiCreateRegistryStrategy = CREATE_REGISTRY_NEVER;
285: } else if (rmiCreateRegistryStrategy.equalsIgnoreCase("true")) {
286: rmiCreateRegistryStrategy = CREATE_REGISTRY_AS_NEEDED;
287: } else if (rmiCreateRegistryStrategy.equalsIgnoreCase("false")) {
288: rmiCreateRegistryStrategy = CREATE_REGISTRY_NEVER;
289: } else if (rmiCreateRegistryStrategy
290: .equalsIgnoreCase(CREATE_REGISTRY_ALWAYS)) {
291: rmiCreateRegistryStrategy = CREATE_REGISTRY_ALWAYS;
292: } else if (rmiCreateRegistryStrategy
293: .equalsIgnoreCase(CREATE_REGISTRY_AS_NEEDED)) {
294: rmiCreateRegistryStrategy = CREATE_REGISTRY_AS_NEEDED;
295: } else if (rmiCreateRegistryStrategy
296: .equalsIgnoreCase(CREATE_REGISTRY_NEVER)) {
297: rmiCreateRegistryStrategy = CREATE_REGISTRY_NEVER;
298: } else {
299: throw new IllegalArgumentException(
300: "Faild to set RMICreateRegistryStrategy - strategy unknown: '"
301: + rmiCreateRegistryStrategy + "'");
302: }
303:
304: this .rmiCreateRegistryStrategy = rmiCreateRegistryStrategy;
305: }
306:
307: /**
308: * <p>
309: * Get the <code>{@link ThreadPool}</code> for the <code>{@link QuartzScheduler}</code>
310: * to use.
311: * </p>
312: */
313: public ThreadPool getThreadPool() {
314: return threadPool;
315: }
316:
317: /**
318: * <p>
319: * Set the <code>{@link ThreadPool}</code> for the <code>{@link QuartzScheduler}</code>
320: * to use.
321: * </p>
322: *
323: * @exception IllegalArgumentException
324: * if threadPool is null.
325: */
326: public void setThreadPool(ThreadPool threadPool) {
327: if (threadPool == null) {
328: throw new IllegalArgumentException(
329: "ThreadPool cannot be null.");
330: }
331:
332: this .threadPool = threadPool;
333: }
334:
335: /**
336: * <p>
337: * Get the <code>{@link JobStore}</code> for the <code>{@link QuartzScheduler}</code>
338: * to use.
339: * </p>
340: */
341: public JobStore getJobStore() {
342: return jobStore;
343: }
344:
345: /**
346: * <p>
347: * Set the <code>{@link JobStore}</code> for the <code>{@link QuartzScheduler}</code>
348: * to use.
349: * </p>
350: *
351: * @exception IllegalArgumentException
352: * if jobStore is null.
353: */
354: public void setJobStore(JobStore jobStore) {
355: if (jobStore == null) {
356: throw new IllegalArgumentException(
357: "JobStore cannot be null.");
358: }
359:
360: this .jobStore = jobStore;
361: }
362:
363: /**
364: * <p>
365: * Get the <code>{@link JobRunShellFactory}</code> for the <code>{@link QuartzScheduler}</code>
366: * to use.
367: * </p>
368: */
369: public JobRunShellFactory getJobRunShellFactory() {
370: return jobRunShellFactory;
371: }
372:
373: /**
374: * <p>
375: * Set the <code>{@link JobRunShellFactory}</code> for the <code>{@link QuartzScheduler}</code>
376: * to use.
377: * </p>
378: *
379: * @exception IllegalArgumentException
380: * if jobRunShellFactory is null.
381: */
382: public void setJobRunShellFactory(
383: JobRunShellFactory jobRunShellFactory) {
384: if (jobRunShellFactory == null) {
385: throw new IllegalArgumentException(
386: "JobRunShellFactory cannot be null.");
387: }
388:
389: this .jobRunShellFactory = jobRunShellFactory;
390: }
391:
392: /**
393: * <p>
394: * Add the given <code>{@link org.quartz.spi.SchedulerPlugin}</code> for the
395: * <code>{@link QuartzScheduler}</code> to use. This method expects the plugin's
396: * "initialize" method to be invoked externally (either before or after
397: * this method is called).
398: * </p>
399: */
400: public void addSchedulerPlugin(SchedulerPlugin plugin) {
401: schedulerPlugins.add(plugin);
402: }
403:
404: /**
405: * <p>
406: * Get the <code>List</code> of all
407: * <code>{@link org.quartz.spi.SchedulerPlugin}</code>s for the
408: * <code>{@link QuartzScheduler}</code> to use.
409: * </p>
410: */
411: public List getSchedulerPlugins() {
412: return schedulerPlugins;
413: }
414:
415: /**
416: * Get whether to mark the Quartz scheduling thread as daemon.
417: *
418: * @see Thread#setDaemon(boolean)
419: */
420: public boolean getMakeSchedulerThreadDaemon() {
421: return makeSchedulerThreadDaemon;
422: }
423:
424: /**
425: * Set whether to mark the Quartz scheduling thread as daemon.
426: *
427: * @see Thread#setDaemon(boolean)
428: */
429: public void setMakeSchedulerThreadDaemon(
430: boolean makeSchedulerThreadDaemon) {
431: this .makeSchedulerThreadDaemon = makeSchedulerThreadDaemon;
432: }
433:
434: /**
435: * Get the name under which to bind the QuartzScheduler in RMI. Will
436: * return the value of the uniqueIdentifier property if explict RMI bind
437: * name was never set.
438: *
439: * @see #getUniqueIdentifier()
440: */
441: public String getRMIBindName() {
442: return (rmiBindName == null) ? getUniqueIdentifier()
443: : rmiBindName;
444: }
445:
446: /**
447: * Set the name under which to bind the QuartzScheduler in RMI. If unset,
448: * defaults to the value of the uniqueIdentifier property.
449: *
450: * @see #getUniqueIdentifier()
451: */
452: public void setRMIBindName(String rmiBindName) {
453: this .rmiBindName = rmiBindName;
454: }
455:
456: /**
457: * Get whether the QuartzScheduler should be registered with the local
458: * MBeanServer.
459: */
460: public boolean getJMXExport() {
461: return jmxExport;
462: }
463:
464: /**
465: * Set whether the QuartzScheduler should be registered with the local
466: * MBeanServer.
467: */
468: public void setJMXExport(boolean jmxExport) {
469: this .jmxExport = jmxExport;
470: }
471:
472: /**
473: * Get the name under which the QuartzScheduler should be registered with
474: * the local MBeanServer. If unset, defaults to the value calculated by
475: * <code>generateJMXObjectName<code>.
476: *
477: * @see #generateJMXObjectName(String, String)
478: */
479: public String getJMXObjectName() {
480: return (jmxObjectName == null) ? generateJMXObjectName(name,
481: instanceId) : jmxObjectName;
482: }
483:
484: /**
485: * Set the name under which the QuartzScheduler should be registered with
486: * the local MBeanServer. If unset, defaults to the value calculated by
487: * <code>generateJMXObjectName<code>.
488: *
489: * @see #generateJMXObjectName(String, String)
490: */
491: public void setJMXObjectName(String jmxObjectName) {
492: this .jmxObjectName = jmxObjectName;
493: }
494:
495: /**
496: * Create the name under which this scheduler should be registered in JMX.
497: * <p>
498: * The name is composed as:
499: * quartz:type=QuartzScheduler,name=<i>[schedName]</i>,instance=<i>[schedInstId]</i>
500: * </p>
501: */
502: public static String generateJMXObjectName(String schedName,
503: String schedInstId) {
504: return "quartz:type=QuartzScheduler" + ",name=" + schedName
505: + ",instance=" + schedInstId;
506: }
507: }
|