1 package org.oddjob.jobs.structural; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.util.ArrayList ; 6 import java.util.Arrays ; 7 import java.util.List ; 8 9 import org.oddjob.Stoppable; 10 import org.oddjob.arooa.ArooaHandler; 11 import org.oddjob.arooa.ArooaContext; 12 import org.oddjob.arooa.ArooaConstants; 13 import org.oddjob.arooa.handlers.DefaultComponentHandler; 14 import org.oddjob.arooa.handlers.MainHandler; 15 import org.oddjob.arooa.handlers.SkipLevelHandler; 16 import org.oddjob.arooa.handlers.XmlHandler; 17 import org.oddjob.arooa.registry.ComponentRegistry; 18 import org.oddjob.arooa.registry.Path; 19 import org.oddjob.arooa.xml.XMLDefinitionHelper; 20 import org.oddjob.framework.StructuralJob; 21 import org.xml.sax.InputSource ; 22 23 24 55 56 public class ForEachJob extends StructuralJob 57 implements Stoppable { 58 59 64 private List types = new ArrayList (); 65 66 71 private Object current; 72 73 79 private int index; 80 81 88 private boolean loadOnly; 89 90 private ByteArrayOutputStream storedXml; 91 private ArooaContext storedContext; 92 private ComponentRegistry realRegisty; 93 private String id; 94 95 private volatile boolean loaded; 96 97 98 103 public void addComponentChild(Runnable child) { 104 childHelper.addChild(child); 105 } 106 107 112 public Object getCurrent() { 113 return current; 114 } 115 116 122 public void setValues(Object [] values) { 123 lock.accquire("Setting values"); 124 try { 125 types = Arrays.asList(values); 126 } finally { 127 lock.release(); 128 } 129 } 130 131 protected void load() { 132 logger().debug("Creating children from: " + storedXml.toString()); 133 134 for (int i = 0; i < types.size(); ++i) { 136 Object o = types.get(i); 137 logger().debug("creating child for [" + o + "]"); 138 PsudoRegistry pr = new PsudoRegistry(i, o); 139 storedContext.set(ArooaConstants.COMPONENT_REGISTRY, pr); 140 storedContext.set(ArooaConstants.ELEMENT_NAME, "child"); 141 XMLDefinitionHelper ph = new XMLDefinitionHelper(storedContext); 142 ph.parse(new InputSource (new ByteArrayInputStream (storedXml.toByteArray())), 143 new MainHandler(new SkipLevelHandler(new DefaultComponentHandler()))); 144 } 145 childHelper.initialise(); 146 loaded = true; 147 } 148 149 public boolean configure() { 150 if (loaded) { 153 return true; 154 } 155 return super.configure(); 156 } 157 158 162 protected void execute() throws Exception { 163 if (types == null) { 164 throw new IllegalStateException ("No values supplied."); 165 } 166 if (storedXml.size() == 0) { 167 throw new IllegalStateException ("No child jobs to run"); 168 } 169 if (!loaded) { 170 load(); 171 } 172 if (loadOnly) { 173 return; 174 } 175 176 Object [] children = childHelper.getChildren(); 178 for (index = 0; index < types.size() && !stop; ++index) { 179 current = types.get(index); 180 if (childHelper.anyNotComplete()) { 181 return; 182 } 183 if (childHelper.anyExceptions() != null) { 184 return; 185 } 186 187 Runnable job = (Runnable ) children[index]; 188 job.run(); 189 } 190 } 191 192 198 public ArooaHandler handlerForChild(ArooaContext context) { 199 storedXml = new ByteArrayOutputStream (); 200 storedContext = context; 201 realRegisty = (ComponentRegistry) context.get(ArooaConstants.COMPONENT_REGISTRY); 202 return new XmlHandler(storedXml); 203 } 204 205 208 public int getIndex() { 209 return index; 210 } 211 212 215 public static class LocalBean { 216 private final int index; 217 private final Object current; 218 LocalBean (int index, Object value) { 219 this.index = index; 220 this.current = value; 221 } 222 public Object getCurrent() { 223 return current; 224 } 225 public int getIndex() { 226 return index; 227 } 228 } 229 230 public class PsudoRegistry extends ComponentRegistry { 231 232 PsudoRegistry(int index, Object value) { 233 if (id != null) { 234 register(id, new LocalBean(index, value)); 235 } 236 } 237 238 242 public Object objectForPath(Path path) { 243 Object component = super.objectForPath(path); 244 if (component == null) { 245 return realRegisty.objectForPath(path); 246 } 247 return component; 248 } 249 250 254 public String getIdForComponent(Object component) { 255 return null; 256 } 257 } 258 259 262 public void setId(String id) { 263 this.id = id; 264 } 265 268 public boolean isLoadOnly() { 269 return loadOnly; 270 } 271 274 public void setLoadOnly(boolean onlyLoad) { 275 this.loadOnly = onlyLoad; 276 } 277 278 281 public void hardReset() { 282 lock.accquire("Hard Reset in progress."); 283 try { 284 childHelper.destroyAll(); 285 stop = false; 286 loaded = false; 287 } finally { 288 lock.release(); 289 } 290 } 291 } 292 293 | Popular Tags |