KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.oddjob.monitor.action;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.lang.reflect.Array JavaDoc;
6
7 import org.apache.commons.beanutils.BeanUtilsBean;
8 import org.apache.log4j.Logger;
9 import org.oddjob.arooa.ArooaRuntime;
10 import org.oddjob.arooa.ArooaContext;
11 import org.oddjob.arooa.ArooaConstants;
12 import org.oddjob.arooa.RuntimeConfiguration;
13 import org.oddjob.arooa.handlers.DocumentStartHandler;
14 import org.oddjob.arooa.handlers.MainHandler;
15 import org.oddjob.arooa.handlers.XmlHandler;
16 import org.oddjob.arooa.reflect.BeanUtilsBeanHelper;
17 import org.oddjob.arooa.reflect.IntrospectionHelper;
18 import org.oddjob.arooa.xml.XMLDefinitionHelper;
19 import org.oddjob.designer.arooa.DesignParser;
20 import org.oddjob.designer.elements.PropertyDE;
21 import org.oddjob.designer.elements.simple.SimpleTextAttribute;
22 import org.oddjob.designer.model.DesignDefinition;
23 import org.oddjob.monitor.model.ExplorerContext;
24 import org.oddjob.monitor.model.JobAction;
25 import org.oddjob.values.types.ListType;
26 import org.xml.sax.InputSource JavaDoc;
27 import org.xml.sax.SAXParseException JavaDoc;
28
29 /**
30  * An action that sets a property on a job.
31  *
32  * @author Rob Gordon
33  */

34
35 public class SetPropertyAction extends JobAction {
36     private static final Logger logger = Logger.getLogger(SetPropertyAction.class);
37     
38     /** The job */
39     private Object JavaDoc job = null;
40     
41     private boolean enabled;
42     
43     private PropertyDE de;
44     
45     private ArooaContext context;
46     
47     public String JavaDoc getName() {
48         return "Set Property";
49     }
50
51     public boolean enabled() {
52         return enabled;
53     }
54     
55     protected void select(Object JavaDoc component, ExplorerContext eContext) {
56         job = component;
57         enabled = true;
58         context = eContext.getArooaContext();
59     }
60     
61     protected void deSelect() {
62         enabled = false;
63     }
64
65     public DesignDefinition form() {
66         de = new PropertyDE();
67         return de.detail();
68     }
69
70     public void action() {
71         String JavaDoc name = de.getName().attribute();
72         if (name == null || "".equals(name.trim())) {
73             logger.debug("No name.");
74             return;
75         }
76         // blank the name because we don't want it back in the xml.
77
de.getName().attribute("");
78         
79         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
80         XmlHandler handler = new XmlHandler(out);
81         DesignParser dp = new DesignParser(new ArooaContext());
82         try {
83             dp.parse("property", de, new MainHandler(handler));
84         } catch (SAXParseException JavaDoc ex) {
85             ex.printStackTrace();
86         }
87         logger.debug("Property XML:\n" + out.toString());
88         
89         ArooaContext ourcon = new ArooaContext(context);
90         ListType surrogate = new ListType();
91
92         RuntimeConfiguration rtc = new ArooaRuntime(surrogate, "property", ourcon);
93         ourcon.set(ArooaConstants.CURRENTLY_CONFIGURING, rtc);
94         ourcon.set(ArooaConstants.RTC_LEAVE_PROXY, new Boolean JavaDoc(true));
95         
96         XMLDefinitionHelper ph = new XMLDefinitionHelper(ourcon);
97
98         InputSource JavaDoc inputSource = new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(out.toByteArray()));
99         ph.parse(inputSource, new MainHandler(
100                 new DocumentStartHandler(surrogate)));
101         rtc.configure();
102
103         BeanUtilsBeanHelper bubh = new BeanUtilsBeanHelper(
104                 (BeanUtilsBean) context.get(ArooaConstants.BEAN_UTILS_BEAN));
105         Class JavaDoc type = bubh.getPropertyType(job, name);
106         Object JavaDoc arrayType = Array.newInstance(type, 0);
107         
108         Object JavaDoc[] list = (Object JavaDoc[]) surrogate.valueFor(arrayType.getClass());
109         if (list.length == 0) {
110             logger.debug("No property.");
111             return;
112         }
113         Object JavaDoc property = list[0];
114         
115         Object JavaDoc value = IntrospectionHelper.valueFor(property, type);
116                 
117         bubh.setProperty(job, name, value);
118     }
119     
120 }
121
Popular Tags