01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: MemoryScheduler.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.scheduler.schedulermanagers;
09:
10: import java.net.URL;
11:
12: import com.uwyn.rife.resources.ResourceFinder;
13: import com.uwyn.rife.scheduler.Scheduler;
14: import com.uwyn.rife.scheduler.SchedulerFactory;
15: import com.uwyn.rife.scheduler.exceptions.SchedulerManagerException;
16: import com.uwyn.rife.scheduler.schedulermanagers.exceptions.InitializationErrorException;
17: import com.uwyn.rife.scheduler.schedulermanagers.exceptions.SchedulerNotFoundException;
18: import com.uwyn.rife.scheduler.taskmanagers.MemoryTasks;
19: import com.uwyn.rife.scheduler.taskoptionmanagers.MemoryTaskoptions;
20: import com.uwyn.rife.selector.XmlSelectorResolver;
21: import com.uwyn.rife.xml.exceptions.XmlErrorException;
22:
23: public class MemoryScheduler implements SchedulerFactory {
24: private Scheduler mScheduler = null;
25: private String mXmlPath = null;
26: private ResourceFinder mResourceFinder = null;
27: private Xml2MemoryScheduler mXmlMemoryScheduler = null;
28:
29: public MemoryScheduler() {
30: }
31:
32: public MemoryScheduler(String xmlPath, ResourceFinder resourceFinder)
33: throws SchedulerManagerException {
34: if (null == xmlPath)
35: throw new IllegalArgumentException("xmlPath can't be null.");
36: if (0 == xmlPath.length())
37: throw new IllegalArgumentException(
38: "xmlPath can't be empty.");
39: if (null == resourceFinder)
40: throw new IllegalArgumentException(
41: "resourceFinder can't be null.");
42:
43: mResourceFinder = resourceFinder;
44:
45: String datasource_resolved = XmlSelectorResolver.resolve(
46: xmlPath, mResourceFinder, "rep/scheduler-");
47: if (null == datasource_resolved) {
48: throw new SchedulerNotFoundException(xmlPath);
49: }
50:
51: URL datasource_resource = mResourceFinder
52: .getResource(datasource_resolved);
53: if (null == datasource_resource) {
54: throw new SchedulerNotFoundException(xmlPath,
55: datasource_resolved);
56: }
57:
58: mXmlPath = datasource_resolved;
59:
60: mXmlMemoryScheduler = new Xml2MemoryScheduler();
61: initialize();
62: mXmlMemoryScheduler = null;
63: }
64:
65: public Scheduler getScheduler() {
66: if (null == mScheduler) {
67: mScheduler = new Scheduler(new MemoryTasks(),
68: new MemoryTaskoptions());
69: }
70: return mScheduler;
71: }
72:
73: private void initialize() throws SchedulerManagerException {
74: try {
75: mXmlMemoryScheduler.processXml(mXmlPath, mResourceFinder);
76: mScheduler = mXmlMemoryScheduler.getScheduler();
77: } catch (XmlErrorException e) {
78: throw new InitializationErrorException(mXmlPath, e);
79: }
80: }
81:
82: /**
83: * Retrieves the path of the XML document that populated this
84: * <code>MemoryScheduler</code> instance
85: *
86: * @return the path of the XML document that populated this
87: * <code>MemoryScheduler</code> instance
88: *
89: * @since 1.0
90: */
91: public String getXmlPath() {
92: return mXmlPath;
93: }
94: }
|