KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > framework > BasePrimary


1 package org.oddjob.framework;
2
3
4 import java.util.EventObject JavaDoc;
5
6 import org.apache.log4j.Logger;
7 import org.oddjob.arooa.ArooaConstants;
8 import org.oddjob.arooa.ArooaContext;
9 import org.oddjob.arooa.ArooaRuntime;
10 import org.oddjob.arooa.ConfigurationListener;
11 import org.oddjob.arooa.Location;
12 import org.oddjob.images.IconHelper;
13 import org.xml.sax.Locator JavaDoc;
14
15 /**
16  * An abstract implementation of a component which provides commen functionality to
17  * concrete sub classes.
18  *
19  * @author Rob Gordon
20  */

21
22 public abstract class BasePrimary extends BaseComponent {
23     /** provides a unique logger per component. */
24     private static int instanceCount;
25
26     /** The logger. */
27     private Logger theLogger;
28     
29     /**
30      * @oddjob.property
31      * @oddjob.description A name, can be any text.
32      * @oddjob.required No.
33      */

34     private String JavaDoc name;
35
36     /**
37      * This flag is set by the stop method and should
38      * be examined by any Stoppable sub classes in
39      * their processing loop.
40      */

41     protected transient volatile boolean stop;
42     
43     /**
44      * Allow sub classes access to the logger.
45      *
46      * @return The logger.
47      */

48     protected Logger logger() {
49         if (theLogger == null) {
50             int count = 0;
51             synchronized (BaseComponent.class) {
52                 count = instanceCount++;
53             }
54             theLogger = Logger.getLogger(this.getClass().getName()
55                     + "." + count);
56         }
57         return theLogger;
58     }
59         
60     /**
61      * Called during construction by the framework to allow a component access
62      * to the context that's creating it.
63      * <p>
64      * If this method returns true then it is an independent component responsible
65      * for it's own configuration and won't have it's configuration linked to
66      * its parents.
67      * <p>
68      * Subclasses overriding this method should always call super.setContext().
69      *
70      * @param context The ArooaXMLContext.
71      * @return true/false depending on subClasses implementation of independent();
72      */

73     public boolean setContext(ArooaContext context) {
74         Locator JavaDoc locator = context.getLocator();
75         if (locator != null) {
76             Location location = new Location(locator.getSystemId(),
77                     locator.getLineNumber(),
78                     locator.getColumnNumber());
79             this.location = location;
80         }
81         ArooaRuntime arooaRuntime = (ArooaRuntime) context.get(ArooaConstants.CURRENTLY_CONFIGURING);
82         if (arooaRuntime != null) {
83             arooaRuntime.addConfigurationListener(new ConfigurationListener() {
84                 public void configurationComplete(EventObject JavaDoc event) {
85                     BasePrimary.this.configurationComplete();
86                 }
87             });
88         }
89         arooaRuntime(arooaRuntime);
90         return independant();
91     }
92         
93     protected boolean independant() {
94         return false;
95     }
96     
97     protected void setJobStateReady() {
98         super.setJobStateReady();
99         stop = false;
100     }
101     
102     /**
103      * Utility method to sleep a certain time.
104      *
105      * @param waitTime Milliseconds to sleep for.
106      */

107     protected void sleep(long waitTime) {
108         if (destroyed) {
109             throw new IllegalStateException JavaDoc("[" + this + "] destroyed");
110         }
111         iconHelper.changeIcon(IconHelper.SLEEPING);
112         logger().debug("[" + this + "] Sleeping for [" + waitTime + "] milli seconds." );
113         try {
114             synchronized (this) {
115                 wait(waitTime);
116             }
117         } catch (InterruptedException JavaDoc e) {
118             logger().debug("Sleep interupted.");
119         }
120         iconHelper.changeIcon(IconHelper.EXECUTING);
121     }
122     
123         
124     /**
125      * Called after a component is configured.
126      *
127      */

128     protected void configurationComplete() {
129         
130     }
131     
132     /**
133      * Set the job name. Used by subclasses to set the job name.
134      *
135      * @param name The name of the job.
136      */

137     synchronized public void setName(String JavaDoc name) {
138         if (destroyed) {
139             throw new IllegalStateException JavaDoc("[" + this + "] destroyed");
140         }
141         String JavaDoc old = this.name;
142         this.name = name;
143         changes.firePropertyChange("name", old, name);
144     }
145
146     /**
147      * Get the job name.
148      *
149      * @return The job name.
150      */

151     synchronized public String JavaDoc getName() {
152         if (name == null && arooaRuntime() != null) {
153             return arooaRuntime().getAttribute("name");
154         }
155         return name;
156     }
157     
158     /**
159      * @return Returns the logger.
160      */

161     public String JavaDoc getLogger() {
162         return logger().getName();
163     }
164     
165     /**
166      * @param logger The logger to set.
167      */

168     public void setLogger(String JavaDoc logger) {
169         if (logger == null) {
170             return;
171         }
172         if (theLogger != null) {
173             theLogger.debug("Logger being replaced by [" + logger + "]");
174         }
175         theLogger = Logger.getLogger(logger);
176     }
177     
178     /**
179      * Override toString.
180      */

181     public String JavaDoc toString() {
182         if (getName() == null) {
183             return getClass().getName();
184         }
185         else {
186             return getName();
187         }
188     }
189     
190 }
191
Popular Tags