KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.oddjob.monitor.action;
2
3 import org.oddjob.Oddjob;
4 import org.oddjob.Stateful;
5 import org.oddjob.designer.model.DesignDefinition;
6 import org.oddjob.monitor.model.ExplorerContext;
7 import org.oddjob.monitor.model.JobAction;
8 import org.oddjob.state.JobState;
9 import org.oddjob.state.JobStateEvent;
10 import org.oddjob.state.JobStateListener;
11 import org.oddjob.util.ThreadManager;
12
13 /**
14  * An action that executes a job.
15  *
16  * @author Rob Gordon
17  */

18 public class ExecuteAction extends JobAction
19 implements JobStateListener {
20
21     /** The job */
22     private Object JavaDoc job = null;
23     
24     /** The ThreadManager that will run the job. */
25     private ThreadManager threadManager;
26     
27     /** The classloader to run the job with. */
28     private ClassLoader JavaDoc classLoader;
29
30     /** The enabled. */
31     private boolean enabled;
32     
33     /*
34      * (non-Javadoc)
35      * @see org.oddjob.monitor.model.JobOption#getName()
36      */

37     public String JavaDoc getName() {
38         return "Run";
39     }
40
41     /*
42      * (non-Javadoc)
43      * @see org.oddjob.monitor.model.JobOption#select(java.lang.Object, org.oddjob.monitor.model.ExplorerContext)
44      */

45     protected void select(Object JavaDoc component, ExplorerContext eContext) {
46         if (!(component instanceof Runnable JavaDoc)) {
47             this.job = null;
48             this.enabled = false;
49             return;
50         }
51         
52         this.job = component;
53         this.enabled = true;
54         this.threadManager = eContext.getThreadManager();
55         
56         this.classLoader = null;
57         Oddjob oj = eContext.getOddjob();
58         if (oj != null) {
59             classLoader = oj.getClassLoader();
60         }
61         
62         if (job instanceof Stateful) {
63             ((Stateful) job).addJobStateListener(this);
64         }
65     }
66     
67     /*
68      * (non-Javadoc)
69      * @see org.oddjob.monitor.model.JobOption#deSelect()
70      */

71     protected void deSelect() {
72         if (job instanceof Stateful) {
73             ((Stateful) job).removeJobStateListener(this);
74         }
75         job = null;
76     }
77     
78     /*
79      * (non-Javadoc)
80      * @see org.oddjob.monitor.model.JobOption#enabled()
81      */

82     public boolean enabled() {
83         return enabled;
84     }
85     
86     /*
87      * (non-Javadoc)
88      * @see org.oddjob.monitor.model.JobAction#form()
89      */

90     public DesignDefinition form() {
91         return null;
92     }
93     
94     /*
95      * (non-Javadoc)
96      * @see org.oddjob.monitor.model.JobAction#action()
97      */

98     public void action() throws Exception JavaDoc {
99         threadManager.run(((Runnable JavaDoc) job), "Executing " + job, classLoader);
100     }
101     
102     /*
103      * (non-Javadoc)
104      * @see org.oddjob.state.JobStateListener#jobStateChange(org.oddjob.state.JobStateEvent)
105      */

106     public void jobStateChange(JobStateEvent event) {
107         if (event.getJobState() == JobState.READY
108                 && event.getSource() instanceof Runnable JavaDoc) {
109             enabled = true;
110         } else {
111             enabled = false;
112         }
113         setChanged();
114         notifyObservers();
115     }
116 }
117
Popular Tags