1 7 package org.ejtools.util.state; 8 9 import java.lang.reflect.Method ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 import java.util.Stack ; 14 import org.w3c.dom.Node ; 15 16 import org.apache.log4j.Logger; 17 import org.ejtools.beans.Sort; 18 19 20 24 public class DefaultStoreVisitor implements StoreVisitor 25 { 26 27 private Stack nodes = new Stack (); 28 29 private Map persistMappings = new HashMap (); 30 31 private static Logger logger = Logger.getLogger(DefaultStoreVisitor.class); 32 33 34 35 36 public DefaultStoreVisitor() { } 37 38 39 44 public void persist(Iterator iterator) 45 { 46 Iterator it = Sort.getChildrenByClass(iterator, Storable.class); 47 for (; it.hasNext(); ) 48 { 49 Storable p = (Storable) it.next(); 50 p.accept(this); 51 } 52 } 53 54 55 60 public void persist(Object o) 61 { 62 Class clazz = o.getClass(); 63 Method m = (Method ) this.persistMappings.get(clazz); 64 try 65 { 66 m.invoke(this, new Object []{o}); 67 } 68 catch (Exception e) 69 { 70 logger.debug("Object of type " + o.getClass().getName() + " will not be persist"); 71 } 72 } 73 74 75 80 protected Node peekCurrentNode() 81 { 82 return (Node ) this.nodes.peek(); 83 } 84 85 86 87 92 protected Node popCurrentNode() 93 { 94 return (Node ) this.nodes.pop(); 95 } 96 97 98 99 104 protected void pushCurrentNode(Node node) 105 { 106 this.nodes.push(node); 107 } 108 109 110 115 public final void registerForPersistence(Class clazz) 116 { 117 try 118 { 119 Method m = this.getClass().getMethod("persist", new Class []{clazz}); 120 this.persistMappings.put(clazz, m); 121 } 122 catch (NoSuchMethodException nsme) 123 { 124 logger.warn("Can't register for persistence class " + clazz.getName()); 125 } 126 } 127 } 128 | Popular Tags |