1 package org.oddjob.monitor.action; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.lang.reflect.Array ; 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 ; 27 import org.xml.sax.SAXParseException ; 28 29 34 35 public class SetPropertyAction extends JobAction { 36 private static final Logger logger = Logger.getLogger(SetPropertyAction.class); 37 38 39 private Object job = null; 40 41 private boolean enabled; 42 43 private PropertyDE de; 44 45 private ArooaContext context; 46 47 public String getName() { 48 return "Set Property"; 49 } 50 51 public boolean enabled() { 52 return enabled; 53 } 54 55 protected void select(Object 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 name = de.getName().attribute(); 72 if (name == null || "".equals(name.trim())) { 73 logger.debug("No name."); 74 return; 75 } 76 de.getName().attribute(""); 78 79 ByteArrayOutputStream out = new ByteArrayOutputStream (); 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 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 (true)); 95 96 XMLDefinitionHelper ph = new XMLDefinitionHelper(ourcon); 97 98 InputSource inputSource = new InputSource (new ByteArrayInputStream (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 type = bubh.getPropertyType(job, name); 106 Object arrayType = Array.newInstance(type, 0); 107 108 Object [] list = (Object []) surrogate.valueFor(arrayType.getClass()); 109 if (list.length == 0) { 110 logger.debug("No property."); 111 return; 112 } 113 Object property = list[0]; 114 115 Object value = IntrospectionHelper.valueFor(property, type); 116 117 bubh.setProperty(job, name, value); 118 } 119 120 } 121 | Popular Tags |