KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > monitor > action > ScheduleAction


1 package org.oddjob.monitor.action;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5
6 import org.apache.log4j.Logger;
7 import org.oddjob.arooa.ArooaRuntime;
8 import org.oddjob.arooa.ArooaContext;
9 import org.oddjob.arooa.ArooaConstants;
10 import org.oddjob.arooa.RuntimeConfiguration;
11 import org.oddjob.arooa.handlers.MainHandler;
12 import org.oddjob.arooa.handlers.XmlHandler;
13 import org.oddjob.arooa.xml.XMLDefinitionHelper;
14 import org.oddjob.designer.arooa.DesignParser;
15 import org.oddjob.designer.elements.scheduling.ScheduleInstructionDE;
16 import org.oddjob.designer.elements.scheduling.ScheduleListDE;
17 import org.oddjob.designer.elements.simple.ReferenceAttribute;
18 import org.oddjob.designer.model.DesignDefinition;
19 import org.oddjob.monitor.model.ExplorerContext;
20 import org.oddjob.monitor.model.JobAction;
21 import org.oddjob.scheduling.OddjobScheduler;
22 import org.oddjob.scheduling.ScheduleInstruction;
23 import org.oddjob.scheduling.ScheduleInstructionFactory;
24 import org.oddjob.values.types.ListType;
25 import org.xml.sax.InputSource JavaDoc;
26
27 /**
28  * An action that schedules a job.
29  *
30  * @author Rob Gordon
31  */

32
33 public class ScheduleAction extends JobAction {
34     private static final Logger logger = Logger.getLogger(ScheduleAction.class);
35     
36     /** The job */
37     private Object JavaDoc job = null;
38         
39     private ArooaContext arooaContext;
40     
41     private boolean enabled;
42     
43     private ScheduleInstructionDE designElement;
44     
45     private OddjobScheduler oddjobScheduler;
46         
47     public ScheduleAction(OddjobScheduler oddjobScheduler, String JavaDoc type) {
48         this.oddjobScheduler = oddjobScheduler;
49         this.designElement = (ScheduleInstructionDE) new ScheduleListDE()
50                 .createType(type);
51     }
52     
53     /**
54      * Get the name of the action.
55      */

56     public String JavaDoc getName() {
57         return designElement.type();
58     }
59
60     public void select(Object JavaDoc component, ExplorerContext eContext) {
61         this.job = component;
62         enabled = true;
63         ReferenceAttribute jobReference = new ReferenceAttribute();
64         jobReference.attribute("Selected Job");
65         designElement.setJob(jobReference);
66
67         arooaContext = eContext.getArooaContext();
68     }
69     
70     public void deSelect() {
71         enabled = false;
72         arooaContext = null;
73     }
74     
75     public boolean enabled() {
76         return enabled;
77     }
78     
79     public DesignDefinition form() {
80         return designElement.detail();
81     }
82     
83     public void action() throws Exception JavaDoc {
84                         
85         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
86         XmlHandler handler = new XmlHandler(out);
87         DesignParser dp = new DesignParser(new ArooaContext());
88         dp.parse("ojschedule", designElement, new MainHandler(handler));
89         logger.debug("Schedule XML:\n" + out.toString());
90             
91         ArooaContext ourcon = new ArooaContext(arooaContext);
92         
93         ListType surrogate = new ListType();
94
95         RuntimeConfiguration rtc = new ArooaRuntime(surrogate,
96                 "surrogate", ourcon);
97         ourcon.set(ArooaConstants.CURRENTLY_CONFIGURING, rtc);
98         
99         XMLDefinitionHelper ph = new XMLDefinitionHelper(ourcon);
100
101         InputSource JavaDoc inputSource = new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(out.toByteArray()));
102         ph.parse(inputSource, new MainHandler(
103                 ScheduleInstructionFactory.handlerFor(ourcon)));
104         rtc.configure();
105         
106         Object JavaDoc[] list = (Object JavaDoc[]) surrogate.valueFor(Object JavaDoc.class);
107         if (list.length == 0) {
108             logger.debug("No property.");
109             return;
110         }
111         ScheduleInstruction scheduleInstruction = (ScheduleInstruction) list[0];
112         
113         scheduleInstruction.setJob(job);
114         oddjobScheduler.schedule(scheduleInstruction);
115     }
116     
117 }
118
Popular Tags