KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > webapp > model > JobInfoLookup


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.webapp.model;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import org.oddjob.Iconic;
12 import org.oddjob.Stateful;
13 import org.oddjob.Structural;
14 import org.oddjob.images.IconEvent;
15 import org.oddjob.images.IconListener;
16 import org.oddjob.logging.ConsoleArchiver;
17 import org.oddjob.logging.LogArchiver;
18 import org.oddjob.logging.LogLevel;
19 import org.oddjob.logging.LogListener;
20 import org.oddjob.monitor.model.Describer;
21 import org.oddjob.monitor.model.ExplorerModel;
22 import org.oddjob.state.JobStateEvent;
23 import org.oddjob.state.JobStateListener;
24 import org.oddjob.util.ThreadManager;
25
26 /**
27  * Provide a lookup facility for job information.
28  *
29  * @author Rob Gordon.
30  */

31 public class JobInfoLookup {
32
33     /** Icon registry. */
34     private final IconRegistry iconRegistry;
35     
36     /** Jobs by refId. */
37     private Map JavaDoc/*<String, JobTreeNode >*/ jobs = new HashMap JavaDoc();
38     
39     /** The root refId */
40     private String JavaDoc rootRefId;
41
42     /** The explorer model. */
43     private final ExplorerModel explorerModel = new ExplorerModel();
44     
45     /**
46      * Constructor.
47      *
48      * @param iconRegistry The icon registry.
49      */

50     public JobInfoLookup(IconRegistry iconRegistry) {
51         if (iconRegistry == null) {
52             throw new NullPointerException JavaDoc("Icon Registry must not be null");
53         }
54         this.iconRegistry = iconRegistry;
55     }
56
57     public void setThreadManager(ThreadManager threadManager) {
58         explorerModel.setThreadManager(threadManager);
59     }
60     
61     /**
62      * Set the root node. This method must be called before the lookup can be used.
63      * Setting logFormat and other such properties must be done before this method
64      * is called.
65      *
66      * @param root The root node.
67      */

68     public void setRoot(Object JavaDoc root) {
69         if (root == null) {
70             throw new NullPointerException JavaDoc("Root node must not be null");
71         }
72         if (rootRefId != null) {
73             throw new IllegalStateException JavaDoc("Can't change root.");
74         }
75
76         explorerModel.setRoot(root);
77         
78         TreeNode rootNode = new TreeNode(explorerModel, this);
79         if (root instanceof Structural) {
80             ((Structural) root).addStructuralListener(rootNode);
81         }
82
83         jobs.put(rootNode.getRefId(), rootNode);
84         
85         this.rootRefId = rootNode.getRefId();
86     }
87     
88     /**
89      * Get the root reference id.
90      *
91      * @return The reference id.
92      */

93     public String JavaDoc getRootRefId() {
94         return rootRefId;
95     }
96         
97     /**
98      * The log format.
99      *
100      * @param logFormat The log format.
101      */

102     public void setLogFormat(String JavaDoc logFormat) {
103         explorerModel.setLogFormat(logFormat);
104     }
105     
106     /**
107      * Get the last state event for the given refId.
108      *
109      * @param refId The refId.
110      * @return The last state event.
111      */

112     public JobStateEvent stateFor(String JavaDoc refId) {
113         Object JavaDoc object = objectFor(refId);
114         if (object == null) {
115             throw new IllegalStateException JavaDoc("[" + refId + "] does not exist!");
116         }
117         
118         if (!(object instanceof Stateful)) {
119             return null;
120         }
121
122         class JSL implements JobStateListener {
123             JobStateEvent lastEvent;
124             public void jobStateChange(org.oddjob.state.JobStateEvent event) {
125                 lastEvent = event;
126             };
127         }
128         JSL jsl = new JSL();
129         
130         Stateful stateful = (Stateful) object;
131         stateful.addJobStateListener(jsl);
132         stateful.removeJobStateListener(jsl);
133         return jsl.lastEvent;
134     }
135     
136     /**
137      * Get TreeNodeInfo for the given refId.
138      *
139      * @param refId The refId.
140      * @return A TreeNodeInfo ojbect.
141      */

142     public NodeInfo nodeInfoFor(String JavaDoc refId) {
143         TreeNode treeNode = (TreeNode) jobs.get(refId);
144         Object JavaDoc object = objectFor(refId);
145         
146         String JavaDoc iconId = null;
147
148         if (object instanceof Iconic) {
149             class IL implements IconListener {
150                 IconEvent lastEvent;
151                 public void iconEvent(IconEvent event) {
152                     lastEvent = event;
153                 }
154             }
155             IL il = new IL();
156             
157             ((Iconic) object).addIconListener(il);
158             ((Iconic) object).removeIconListener(il);
159             iconId = il.lastEvent.getIconId();
160             iconRegistry.register(iconId, (Iconic) object);
161         }
162         
163         return new NodeInfo(treeNode.getNodeName(),
164                 treeNode.getChildRefIds(),
165                 iconId);
166     }
167         
168     /**
169      * Get a map of properties for the properties tab.
170      *
171      * @param refId The refId.
172      * @return A map of properties.
173      */

174     public Map JavaDoc propertiesFor(String JavaDoc refId) {
175         Object JavaDoc object = objectFor(refId);
176         return Describer.describe(object);
177     }
178     
179     /**
180      * Provide a list of console LogEvents.
181      *
182      * @param refId The refId of the job.
183      * @return A list of LogEvent objects.
184      */

185     public List JavaDoc consoleEventsFor(String JavaDoc refId) {
186         TreeNode treeNode = (TreeNode) jobs.get(refId);
187         if (treeNode == null) {
188             throw new IllegalStateException JavaDoc("[" + refId + "] does not exist!");
189         }
190         
191         class LL implements LogListener {
192             List JavaDoc list = new ArrayList JavaDoc();
193             public void logEvent(org.oddjob.logging.LogEvent logEvent) {
194                 list.add(logEvent);
195             }
196         }
197         
198         LL ll = new LL();
199         Object JavaDoc object = treeNode.getComponent();
200         ConsoleArchiver consoleArchiver = treeNode.getExplorerContext().getConsoleArchiver();
201         consoleArchiver.addConsoleListener(ll, object, -1, 1000);
202         consoleArchiver.removeConsoleListener(ll, object);
203         return ll.list;
204     }
205
206     /**
207      * Provide a list of logger LogEvents.
208      *
209      * @param refId The refId of the job.
210      * @return A list of LogEVent objects.
211      */

212     public List JavaDoc logEventsFor(String JavaDoc refId) {
213         TreeNode treeNode = (TreeNode) jobs.get(refId);
214         if (treeNode == null) {
215             throw new IllegalStateException JavaDoc("[" + refId + "] does not exist!");
216         }
217         
218         class LL implements LogListener {
219             List JavaDoc list = new ArrayList JavaDoc();
220             public void logEvent(org.oddjob.logging.LogEvent logEvent) {
221                 list.add(logEvent);
222             }
223         }
224         LL ll = new LL();
225         
226         Object JavaDoc object = treeNode.getComponent();
227         LogArchiver logArchiver = treeNode.getExplorerContext().getLogArchiver();
228         logArchiver.addLogListener(ll, object, LogLevel.DEBUG, -1, 1000);
229         logArchiver.removeLogListener(ll, object);
230         return ll.list;
231     }
232
233     public WebJobActions actionsFor(String JavaDoc refId) {
234         TreeNode treeNode = (TreeNode) jobs.get(refId);
235         if (treeNode == null) {
236             throw new IllegalStateException JavaDoc("[" + refId + "] does not exist!");
237         }
238         WebJobActions actions = new WebJobActions();
239         actions.select(treeNode.getComponent(), treeNode.getExplorerContext());
240         return actions;
241     }
242     
243     /**
244      * Helper function to resolve an object (the job) from the
245      * refId.
246      *
247      * @param refId The refId.
248      * @return The Job.
249      *
250      * @throws IllegalStateException If the refId doesn't
251      * reference a job.
252      */

253     Object JavaDoc objectFor(String JavaDoc refId) throws IllegalStateException JavaDoc {
254         TreeNode treeNode = (TreeNode) jobs.get(refId);
255         if (treeNode == null) {
256             throw new IllegalStateException JavaDoc("[" + refId + "] does not exist!");
257         }
258         return treeNode.getComponent();
259     }
260     
261     /**
262      * Used by TreeNode to add itself to the lookup.
263      *
264      * @param treeNode The TreeNode.
265      */

266     void addChild(TreeNode treeNode) {
267         jobs.put(treeNode.getRefId(), treeNode);
268     }
269     
270     /**
271      * Used by TreeNode to remove the node.
272      *
273      * @param refId The name of the node.
274      */

275     void removeChild(String JavaDoc refId) {
276         jobs.remove(refId);
277     }
278     
279     /**
280      * Free up resources.
281      *
282      */

283     public void destroy() {
284         explorerModel.destroy();
285         jobs.clear();
286     }
287 }
288
Popular Tags