KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > util > state > DefaultStoreVisitor


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.util.state;
8
9 import java.lang.reflect.Method JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Stack JavaDoc;
14 import org.w3c.dom.Node JavaDoc;
15
16 import org.apache.log4j.Logger;
17 import org.ejtools.beans.Sort;
18
19
20 /**
21  * @author Laurent Etiemble
22  * @version $Revision: 1.2 $
23  */

24 public class DefaultStoreVisitor implements StoreVisitor
25 {
26    /** Description of the Field */
27    private Stack JavaDoc nodes = new Stack JavaDoc();
28    /** Description of the Field */
29    private Map JavaDoc persistMappings = new HashMap JavaDoc();
30    /** Description of the Field */
31    private static Logger logger = Logger.getLogger(DefaultStoreVisitor.class);
32
33
34
35    /**Constructor for the ReflectivePersistenceStore object */
36    public DefaultStoreVisitor() { }
37
38
39    /**
40     * Description of the Method
41     *
42     * @param iterator Description of the Parameter
43     */

44    public void persist(Iterator JavaDoc iterator)
45    {
46       Iterator JavaDoc 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    /**
56     * Description of the Method
57     *
58     * @param o Description of the Parameter
59     */

60    public void persist(Object JavaDoc o)
61    {
62       Class JavaDoc clazz = o.getClass();
63       Method JavaDoc m = (Method JavaDoc) this.persistMappings.get(clazz);
64       try
65       {
66          m.invoke(this, new Object JavaDoc[]{o});
67       }
68       catch (Exception JavaDoc e)
69       {
70          logger.debug("Object of type " + o.getClass().getName() + " will not be persist");
71       }
72    }
73
74
75    /**
76     * Description of the Method
77     *
78     * @return Description of the Return Value
79     */

80    protected Node JavaDoc peekCurrentNode()
81    {
82       return (Node JavaDoc) this.nodes.peek();
83    }
84
85
86
87    /**
88     * Description of the Method
89     *
90     * @return Description of the Return Value
91     */

92    protected Node JavaDoc popCurrentNode()
93    {
94       return (Node JavaDoc) this.nodes.pop();
95    }
96
97
98
99    /**
100     * Description of the Method
101     *
102     * @param node Description of the Parameter
103     */

104    protected void pushCurrentNode(Node JavaDoc node)
105    {
106       this.nodes.push(node);
107    }
108
109
110    /**
111     * Description of the Method
112     *
113     * @param clazz Description of the Parameter
114     */

115    public final void registerForPersistence(Class JavaDoc clazz)
116    {
117       try
118       {
119          Method JavaDoc m = this.getClass().getMethod("persist", new Class JavaDoc[]{clazz});
120          this.persistMappings.put(clazz, m);
121       }
122       catch (NoSuchMethodException JavaDoc nsme)
123       {
124          logger.warn("Can't register for persistence class " + clazz.getName());
125       }
126    }
127 }
128
Popular Tags