KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > persist > PersisterBase


1 package org.oddjob.persist;
2
3
4 import java.io.Serializable JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import org.oddjob.Stateful;
11 import org.oddjob.Structural;
12 import org.oddjob.arooa.registry.ComponentRegistry;
13 import org.oddjob.framework.Transient;
14 import org.oddjob.state.JobState;
15 import org.oddjob.state.JobStateEvent;
16 import org.oddjob.state.JobStateListener;
17 import org.oddjob.structural.StructuralEvent;
18 import org.oddjob.structural.StructuralListener;
19
20 /**
21  * @oddjob.description This job persists its children
22  * to disk. It will persist the job
23  * everytime the state changes to either READY, COMPLETE, or NOT
24  * COMPLETE.
25  * <p>
26  * For a chld to be persisted it must:
27  * <ul>
28  * <li>Be Stateful</li>
29  * <li>Be Serializable</li>
30  * <li>Have an id attribute set in the configuration file.</li>
31  * <li>Doesn't have an isTransient property or it is false.</li>
32  * </ul>
33  *
34  * @author Rob Gordon
35  */

36
37 abstract public class PersisterBase implements ComponentPersister {
38
39     /** */
40     private Object JavaDoc root;
41
42     private OddjobLayer tracker;
43     
44     private List JavaDoc include;
45     
46     private List JavaDoc exclude;
47     
48     private ComponentRegistry componentRegistry;
49         
50     /**
51      * Set the root component.
52      *
53      * @param root The root component.
54      */

55     public void setRoot(Object JavaDoc root) {
56         this.root = root;
57     }
58     
59     /**
60      * Get the root.
61      *
62      * @return The root if set, null otherwise.
63      */

64     public Object JavaDoc getRoot() {
65         return root;
66     }
67     
68     
69     
70     /**
71      * Initialise.
72      *
73      */

74     public void initialise(ComponentRegistry componentRegistry) {
75         if (root == null) {
76             throw new IllegalStateException JavaDoc("No root.");
77         }
78         if (this.componentRegistry != null) {
79             throw new IllegalStateException JavaDoc("Already initialised.");
80         }
81         this.componentRegistry = componentRegistry;
82         
83         tracker = new OddjobLayer();
84         tracker.addNode(root);
85                 
86     }
87     
88     abstract public void persist(String JavaDoc id, Object JavaDoc component);
89
90     abstract public void remove(String JavaDoc id);
91     
92     public void close() {
93         tracker.destroy();
94         componentRegistry = null;
95         onClose();
96     }
97     
98     public void onClose() { }
99     
100     /**
101      * Each nested oddjob in the job hierachy can provide it's
102      * own persistIn directory and id resolution. This class
103      * represents a nested Oddjob layer in the hierarchy.
104      */

105     class OddjobLayer implements StructuralListener {
106         
107         private final Map JavaDoc serializing = new HashMap JavaDoc();
108         
109         private final JobStateListener persistOnChange = new JobStateListener() {
110             public void jobStateChange(JobStateEvent event) {
111                 JobState state = event.getJobState();
112                 if (state == JobState.READY
113                         || state == JobState.COMPLETE) {
114                     Object JavaDoc o = event.getSource();
115                     String JavaDoc id = (String JavaDoc) serializing.get(o);
116                     persist(id, o);
117                 }
118             }
119         };
120
121         private void addNode(Object JavaDoc node) {
122             if (node instanceof Structural
123                     && !componentRegistry.isOwner(node)) {
124                 ((Structural)node).addStructuralListener(this);
125             }
126
127             if (!(node instanceof Serializable JavaDoc)) {
128                 return;
129             }
130             if ((node instanceof Transient)) {
131                 return;
132             }
133             if (!(node instanceof Stateful)) {
134                 return;
135             }
136             String JavaDoc id = componentRegistry.getIdForComponent(node);
137             if (id == null) {
138                 return;
139             }
140             if (include != null && !include.contains(id)) {
141                 return;
142             }
143             if (exclude != null && exclude.contains(id)) {
144                 return;
145             }
146                 
147             serializing.put(node, id);
148             ((Stateful)node).addJobStateListener(persistOnChange);
149         }
150         
151         /**
152          * Called when a child is added to a Structural object.
153          *
154          * @param event The strucural event.
155          */

156         public void childAdded(StructuralEvent event) {
157             addNode(event.getChild());
158         }
159         
160         /**
161          * Called when a child is removed from a Strucutral object.
162          *
163          * @param event The strucutral event.
164          */

165         public void childRemoved(StructuralEvent event) {
166             Object JavaDoc child = event.getChild();
167             if (child instanceof Structural) {
168                 ((Structural)child).removeStructuralListener(this);
169             }
170             String JavaDoc id = (String JavaDoc) serializing.remove(child);
171             if (id != null) {
172                 remove(id);
173             }
174         }
175         
176         public void destroy() {
177             for (Iterator JavaDoc it = serializing.keySet().iterator(); it.hasNext(); ) {
178                 Object JavaDoc node = it.next();
179                 ((Stateful)node).removeJobStateListener(persistOnChange);
180                 if (node instanceof Structural) {
181                     ((Structural)node).removeStructuralListener(this);
182                 }
183                 it.remove();
184             }
185         }
186     }
187     
188 }
189
Popular Tags