KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package org.oddjob.framework;
5
6 import java.lang.reflect.Proxy JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Arrays JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.apache.commons.beanutils.DynaBean;
14 import org.apache.commons.beanutils.DynaClass;
15 import org.apache.commons.beanutils.PropertyUtils;
16 import org.apache.log4j.Logger;
17 import org.oddjob.Reserved;
18 import org.oddjob.Resetable;
19 import org.oddjob.Stateful;
20 import org.oddjob.Stoppable;
21 import org.oddjob.arooa.ArooaConstants;
22 import org.oddjob.arooa.ArooaContext;
23 import org.oddjob.arooa.ArooaRuntime;
24 import org.oddjob.arooa.Lifecycle;
25 import org.oddjob.arooa.Location;
26 import org.oddjob.images.IconHelper;
27 import org.oddjob.logging.LogEnabled;
28 import org.oddjob.logging.LogHelper;
29 import org.oddjob.state.JobState;
30 import org.xml.sax.Locator JavaDoc;
31
32 abstract public class BaseWrapper extends BaseComponent
33 implements Runnable JavaDoc, Stateful, Resetable, DynaBean, Stoppable,
34         LogEnabled, ContextAware, Initializable, Destroyable {
35
36     private static final Map JavaDoc loggers = new HashMap JavaDoc();
37     
38     private transient Logger theLogger;
39     
40     abstract protected Object JavaDoc getWrapped();
41     abstract protected DynaBean getDynaBean();
42     abstract protected Object JavaDoc getProxy();
43     
44     public void init() {
45         Lifecycle.init(getWrapped());
46     }
47     
48     protected Logger logger() {
49         if (theLogger == null) {
50             String JavaDoc logger = LogHelper.getLogger(getWrapped());
51             if (logger == null) {
52                 synchronized (loggers) {
53                     Integer JavaDoc count = (Integer JavaDoc) loggers.get(getWrapped().getClass().getName());
54                     int c = 0;
55                     if (count != null) {
56                         c = count.intValue();
57                     }
58                     logger = getWrapped().getClass().getName() + "." + c++;
59                     loggers.put(getWrapped().getClass().getName(), new Integer JavaDoc(c));
60                 }
61             }
62             theLogger = Logger.getLogger(logger);
63         }
64         return theLogger;
65     }
66
67     public String JavaDoc getLogger() {
68         return logger().getName();
69     }
70     
71     /**
72      * Called during construction by the framework to allow a component access
73      * to the context that's creating it.
74      * <p>
75      *
76      * @param context The ArooaXMLContext.
77      * @return true.;
78      */

79     public boolean setContext(ArooaContext context) {
80         Locator JavaDoc locator = context.getLocator();
81         if (locator != null) {
82             Location location = new Location(locator.getSystemId(),
83                     locator.getLineNumber(),
84                     locator.getColumnNumber());
85             this.location = location;
86         }
87         ArooaRuntime arooaRuntime = (ArooaRuntime) context.get(ArooaConstants.CURRENTLY_CONFIGURING);
88         arooaRuntime(arooaRuntime);
89         
90         Lifecycle.setContext(getWrapped(), context);
91         return true;
92     }
93         
94     /**
95      * Perform a soft reset on the job.
96      */

97     public void softReset() {
98         lock.accquire("Soft reset in progress.");
99         try {
100             logger().debug("Thread [" + Thread.currentThread().getName()
101                     + "] soft reset for [" + getWrapped() + "].");
102             if (canSoftReset()) {
103                 if (getWrapped() instanceof Resetable) {
104                     ((Resetable) getWrapped() ).softReset();
105                 }
106                 setJobStateReady();
107                 logger().info("Reset job [" + getWrapped() + "]");
108             }
109         }
110         finally {
111             lock.release();
112         }
113     }
114     
115     /**
116      * Perform a hard reset on the job.
117      */

118     public void hardReset() {
119         lock.accquire("Hard reset in progress.");
120         try {
121             logger().debug("Thread [" + Thread.currentThread().getName()
122                     + "] hard reset for [" + getWrapped() + "].");
123             if (canHardReset()) {
124                 if (getWrapped() instanceof Resetable) {
125                     ((Resetable) getWrapped() ).hardReset();
126                 }
127                 setJobStateReady();
128                 logger().info("Reset job [" + getWrapped() + "]");
129             }
130         }
131         finally {
132             lock.release();
133         }
134     }
135     
136     /*
137      * (non-Javadoc)
138      * @see java.lang.Object#equals(java.lang.Object)
139      */

140     public boolean equals(Object JavaDoc other) {
141         if (!(other instanceof Proxy JavaDoc)) {
142             return false;
143         }
144         Object JavaDoc ih = Proxy.getInvocationHandler((Proxy JavaDoc) other);
145         return ih == this;
146     }
147     
148     /*
149      * (non-Javadoc)
150      * @see java.lang.Object#toString()
151      */

152     public String JavaDoc toString() {
153         return getWrapped().toString();
154     }
155     
156     public boolean contains(String JavaDoc name, String JavaDoc key) {
157         return getDynaBean().contains(name, key);
158     }
159     
160     public Object JavaDoc get(String JavaDoc name) {
161         return getDynaBean().get(name);
162     }
163
164     public Object JavaDoc get(String JavaDoc name, int index) {
165         return getDynaBean().get(name, index);
166     }
167     
168     public Object JavaDoc get(String JavaDoc name, String JavaDoc key) {
169         return getDynaBean().get(name, key);
170     }
171     
172     public DynaClass getDynaClass() {
173         return getDynaBean().getDynaClass();
174     }
175     
176     public void remove(String JavaDoc name, String JavaDoc key) {
177         lock.accquire("Removing property.");
178         try {
179             getDynaBean().remove(name, key);
180         } finally {
181             lock.release();
182         }
183     }
184     
185     public void set(String JavaDoc name, int index, Object JavaDoc value) {
186         lock.accquire("Setting property.");
187         try {
188             getDynaBean().set(name, index, value);
189         } finally {
190             lock.release();
191         }
192     }
193     
194     public void set(String JavaDoc name, Object JavaDoc value) {
195         lock.accquire("Setting property.");
196         try {
197             getDynaBean().set(name, value);
198         } finally {
199             lock.release();
200         }
201     }
202     
203     public void set(String JavaDoc name, String JavaDoc key, Object JavaDoc value) {
204         lock.accquire("Setting property.");
205         try {
206             getDynaBean().set(name, key, value);
207         } finally {
208             lock.release();
209         }
210     }
211     
212     public final void stop() {
213         if (destroyed) {
214             throw new IllegalStateException JavaDoc("[" + this + "] destroyed");
215         }
216         if (stateHandler.getJobState() != JobState.EXECUTING) {
217             return;
218         }
219         logger().debug("Thread [" + Thread.currentThread().getName()
220                 + "] requested [" + getWrapped() + "] stop.");
221         iconHelper.changeIcon(IconHelper.STOPPING);
222         try {
223             onStop();
224         } catch (RuntimeException JavaDoc e) {
225             iconHelper.changeIcon(IconHelper.EXECUTING);
226             throw e;
227         }
228     }
229     
230     protected void onStop() {}
231     
232     /**
233      *
234      * @return
235      */

236     protected int getResult() {
237         try {
238             Integer JavaDoc result = (Integer JavaDoc) PropertyUtils.getProperty(
239                     getWrapped(), Reserved.RESULT_PROPERTY);
240             return result.intValue();
241         }
242         catch (Exception JavaDoc e) {
243             return 0;
244         }
245     }
246     
247     public void onDestroy() {
248         Lifecycle.destroy(getWrapped());
249     }
250
251     public static Class JavaDoc[] interfacesFor(Object JavaDoc object) {
252         List JavaDoc results = new ArrayList JavaDoc();
253         for (Class JavaDoc cl = object.getClass(); cl != null; cl = cl.getSuperclass()) {
254             results.addAll(Arrays.asList(cl.getInterfaces()));
255         }
256         return (Class JavaDoc[]) results.toArray(new Class JavaDoc[0]);
257     }
258     
259 }
260
Popular Tags