KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > monitor > model > ExplorerContext


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

4 package org.oddjob.monitor.model;
5
6 import org.oddjob.Oddjob;
7 import org.oddjob.arooa.ArooaContext;
8 import org.oddjob.logging.ConsoleArchiver;
9 import org.oddjob.logging.LogArchiver;
10 import org.oddjob.scheduling.OddjobScheduler;
11 import org.oddjob.util.ThreadManager;
12
13 /**
14  * Explorer Context. Used to pass useful things down the job hierarchy.
15  * <p>
16  * A unique context will exist for each node in the hierarchy but where
17  * as the model has specific information about the node in the tree - it's
18  * children, is it visable etc, the context contains ancillary informaton
19  * about the nodes environment.
20  *
21  * @author Rob Gordon
22  */

23 public class ExplorerContext {
24
25     /** The component who's context this is. */
26     private final Object JavaDoc component;
27     
28     /** The parent context */
29     private final ExplorerContext parent;
30
31     /** Schedule Tracker */
32     private final SchedulerTraker schedulerTraker;
33     
34     /** The ExplorerModel - there can be only one. */
35     private final ExplorerModel explorerModel;
36     
37     /** The last Oddjob in the hierarchy. */
38     private final Oddjob oddjob;
39      
40     /** The current LogArchiver. */
41     private LogArchiver logArchiver;
42     
43     /** The current ConsoleArchiver. */
44     private ConsoleArchiver consoleArchiver;
45     
46     /**
47      * Constuctor for the top level context.
48      */

49     public ExplorerContext(ExplorerModel explorerModel) {
50         this.component = explorerModel.getRoot();
51         if (component == null) {
52             throw new NullPointerException JavaDoc("Component can't be null");
53         }
54         this.parent = null;
55         
56         // create the one and only shchedule tracker.
57
this.schedulerTraker = new SchedulerTraker();
58         this.explorerModel = explorerModel;
59         
60         // top level archivers.
61
this.logArchiver = explorerModel.getLogArchiver();
62         this.consoleArchiver = explorerModel.getConsoleArchiver();
63
64         if (component instanceof Oddjob) {
65             oddjob = ((Oddjob) component);
66         }
67         else {
68             oddjob = null;
69         }
70         
71         if (component instanceof OddjobScheduler) {
72             schedulerTraker.addScheduler(
73                     (OddjobScheduler) component);
74         }
75     }
76     
77     /**
78      * Constructor for a child context.
79      *
80      * @param parent The parent context.
81      */

82     public ExplorerContext(Object JavaDoc component, ExplorerContext parent) {
83         if (component == null) {
84             throw new NullPointerException JavaDoc("Component can't be null");
85         }
86         if (parent == null) {
87             throw new NullPointerException JavaDoc("Parent can't be null");
88         }
89         this.component = component;
90         this.parent = parent;
91
92         // there is only ever one schedule tracker. Maybe this should be in the model.
93
this.schedulerTraker = parent.schedulerTraker;
94         this.explorerModel = parent.explorerModel;
95         
96         // why change on parent logArchiver, not this logArchiver? Because
97
// a LogArchiver will typically change for a client job. The client
98
// job is a LogArchiver for it's remote nodes but it's log messages
99
// go to it's parents log archiver - not it's own.
100
if (parent.component instanceof LogArchiver) {
101             logArchiver = ((LogArchiver) parent.component);
102         }
103         
104         // ditto for console archiver.
105
if (parent.component instanceof ConsoleArchiver) {
106             consoleArchiver = ((ConsoleArchiver) parent.component);
107         }
108         
109         if (component instanceof Oddjob) {
110             oddjob = ((Oddjob) component);
111         }
112         else {
113             oddjob = null;
114         }
115         
116         if (component instanceof OddjobScheduler) {
117             schedulerTraker.addScheduler(
118                     (OddjobScheduler) component);
119         }
120     }
121     
122     /**
123      * Returns the Oddjob owner of this
124      * contexts node.
125      *
126      * @return An Oddjob instance or null if
127      * the Oddjob owner isn't known.
128      *
129      */

130     public Oddjob getOddjob() {
131         if (oddjob != null) {
132             return oddjob;
133         }
134         if (parent == null) {
135             return null;
136         }
137         return parent.getOddjob();
138     }
139     
140     public SchedulerTraker getSchedulerTraker() {
141         return schedulerTraker;
142     }
143
144     /**
145      * In a hierarchy the parent Oddjob has the ArooaContext for
146      * creating properties etc, if there is no Oddjob then use
147      * the Explorers own context.
148      *
149      * @return An ArooaContext.
150      */

151     public ArooaContext getArooaContext() {
152         if (oddjob != null) {
153             return oddjob.loadContext();
154         }
155         return explorerModel.getArooaContext();
156     }
157         
158     /**
159      * Returns the LogArchiver for this node.
160      *
161      * @return A LogArchiver. Should never be null.
162      *
163      */

164     public LogArchiver getLogArchiver() {
165         if (logArchiver != null) {
166             return logArchiver;
167         }
168         if (parent == null) {
169             throw new IllegalStateException JavaDoc("A LogArchiver must be set in the parent node.");
170         }
171         return parent.getLogArchiver();
172     }
173     
174     /**
175      * Set an LogArchiver for this context.
176      *
177      * @param logArchiver A LogArchiver.
178      */

179     public void setLogArchiver(LogArchiver logArchiver) {
180         this.logArchiver = logArchiver;
181     }
182     
183     /**
184      * Returns the ConsoleArchiver for this node.
185      *
186      * @return A ConsoleArchiver. Should never be null.
187      *
188      */

189     public ConsoleArchiver getConsoleArchiver() {
190         if (consoleArchiver != null) {
191             return consoleArchiver;
192         }
193         if (parent == null) {
194             throw new IllegalStateException JavaDoc("A ConsoleArchiver must be set in the parent node.");
195         }
196         return parent.getConsoleArchiver();
197     }
198     
199     /**
200      * Set an ConsoleArchiver for this context.
201      *
202      * @param consoleArchiver A ConsoleArchiver.
203      */

204     public void setConsoleArchiver(ConsoleArchiver consoleArchiver) {
205         this.consoleArchiver = consoleArchiver;
206     }
207     
208     public ThreadManager getThreadManager() {
209         return explorerModel.getThreadManager();
210     }
211     
212 }
213
Popular Tags