1 4 package org.oddjob; 5 6 import java.io.ByteArrayInputStream ; 7 import java.io.ByteArrayOutputStream ; 8 import java.io.IOException ; 9 import java.io.ObjectInput ; 10 import java.io.ObjectInputStream ; 11 import java.io.ObjectOutput ; 12 import java.io.ObjectOutputStream ; 13 14 import org.oddjob.arooa.ArooaConstants; 15 import org.oddjob.arooa.ArooaContext; 16 import org.oddjob.arooa.ArooaFactory; 17 import org.oddjob.arooa.ArooaRuntime; 18 import org.oddjob.arooa.PropertyProxyResolver; 19 import org.oddjob.arooa.SimpleObjectFactory; 20 import org.oddjob.arooa.handlers.MainHandler; 21 import org.oddjob.arooa.handlers.MappedPropertyHandler; 22 import org.oddjob.arooa.handlers.NestedPropertyHandler; 23 import org.oddjob.arooa.handlers.ValueHandler; 24 import org.oddjob.arooa.xml.XMLDefinitionHelper; 25 import org.oddjob.framework.BeanUtilsProvider; 26 import org.oddjob.state.JobState; 27 import org.oddjob.state.JobStateEvent; 28 import org.oddjob.state.JobStateListener; 29 import org.xml.sax.InputSource ; 30 31 36 public class Helper { 37 38 public static JobState getJobState(Object o) { 39 Stateful stateful = (Stateful) o; 40 JSL l = new JSL(); 41 stateful.addJobStateListener(l); 42 stateful.removeJobStateListener(l); 43 return l.js; 44 } 45 46 static class JSL implements JobStateListener { 47 JobState js; 48 public void jobStateChange(JobStateEvent event) { 49 js = event.getJobState(); 50 } 51 }; 52 53 public static Object copy(Object object) throws IOException , ClassNotFoundException { 54 55 ByteArrayOutputStream out = new ByteArrayOutputStream (); 56 ObjectOutput oo = new ObjectOutputStream (out); 57 oo.writeObject(object); 58 oo.close(); 59 60 ByteArrayInputStream in = new ByteArrayInputStream (out.toByteArray()); 61 ObjectInput oi = new ObjectInputStream (in); 62 Object o = oi.readObject(); 63 oi.close(); 64 return o; 65 } 66 67 68 public static class Surrogate { 69 Object value; 70 public void addConfiguredWhatever(Object value) { 71 this.value = value; 72 } 73 } 74 75 public static Object createTypeFromXml(String xml) { 76 77 SimpleObjectFactory valueFactory = new SimpleObjectFactory(); 78 valueFactory.addResource(Oddjob.TYPE_PROPERTIES); 79 80 PropertyProxyResolver propertyProxyResolver = new PropertyProxyResolver(); 81 propertyProxyResolver.addResource(Oddjob.PROXY_PROPERTIES); 82 83 ArooaContext context = new ArooaContext(); 84 context.set(ArooaConstants.VALUE_FACTORY, valueFactory); 85 context.set(ArooaConstants.PROPERTY_PROXY_RESOLVER, propertyProxyResolver); 86 context.set(ArooaConstants.PROPERTY_HANDLER, new NestedPropertyHandler()); 87 context.set(ArooaConstants.MAPPED_HANDLER, new MappedPropertyHandler()); 88 context.set(ArooaConstants.BEAN_UTILS_BEAN, BeanUtilsProvider.beanFor(Oddjob.CONVERTER_PROPERTIES)); 89 context.set(ArooaConstants.RTC_LEAVE_PROXY, new Boolean (true)); 90 Surrogate surrogate = new Surrogate(); 91 92 ArooaRuntime runtime = new ArooaRuntime(surrogate, "test", context); 93 context.set(ArooaConstants.CURRENTLY_CONFIGURING, runtime); 94 95 InputSource inputSource = new InputSource (new ByteArrayInputStream (xml.getBytes())); 96 97 XMLDefinitionHelper ph = new XMLDefinitionHelper(context); 98 ph.parse(inputSource, new MainHandler(new ValueHandler("whatever"))); 99 100 runtime.configure(); 101 return surrogate.value; 102 } 103 104 public static Object createComponentFromXml(String xml) { 105 106 SimpleObjectFactory componentFactory = new SimpleObjectFactory(); 107 componentFactory.addResource(Oddjob.JOB_PROPERTIES); 108 109 SimpleObjectFactory valueFactory = new SimpleObjectFactory(); 110 valueFactory.addResource(Oddjob.TYPE_PROPERTIES); 111 112 PropertyProxyResolver propertyProxyResolver = new PropertyProxyResolver(); 113 propertyProxyResolver.addResource(Oddjob.PROXY_PROPERTIES); 114 115 ArooaFactory af = new ArooaFactory(); 116 af.setComponentFactory(componentFactory); 117 af.setValueFactory(valueFactory); 118 af.setPropertProxyResolver(propertyProxyResolver); 119 af.setBeanUtilsBean(BeanUtilsProvider.beanFor(Oddjob.CONVERTER_PROPERTIES)); 120 121 return af.build(xml); 122 123 } 124 } 125 | Popular Tags |