1 package org.oddjob.persist; 2 3 4 import java.io.Serializable ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 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 36 37 abstract public class PersisterBase implements ComponentPersister { 38 39 40 private Object root; 41 42 private OddjobLayer tracker; 43 44 private List include; 45 46 private List exclude; 47 48 private ComponentRegistry componentRegistry; 49 50 55 public void setRoot(Object root) { 56 this.root = root; 57 } 58 59 64 public Object getRoot() { 65 return root; 66 } 67 68 69 70 74 public void initialise(ComponentRegistry componentRegistry) { 75 if (root == null) { 76 throw new IllegalStateException ("No root."); 77 } 78 if (this.componentRegistry != null) { 79 throw new IllegalStateException ("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 id, Object component); 89 90 abstract public void remove(String id); 91 92 public void close() { 93 tracker.destroy(); 94 componentRegistry = null; 95 onClose(); 96 } 97 98 public void onClose() { } 99 100 105 class OddjobLayer implements StructuralListener { 106 107 private final Map serializing = new HashMap (); 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 o = event.getSource(); 115 String id = (String ) serializing.get(o); 116 persist(id, o); 117 } 118 } 119 }; 120 121 private void addNode(Object node) { 122 if (node instanceof Structural 123 && !componentRegistry.isOwner(node)) { 124 ((Structural)node).addStructuralListener(this); 125 } 126 127 if (!(node instanceof Serializable )) { 128 return; 129 } 130 if ((node instanceof Transient)) { 131 return; 132 } 133 if (!(node instanceof Stateful)) { 134 return; 135 } 136 String 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 156 public void childAdded(StructuralEvent event) { 157 addNode(event.getChild()); 158 } 159 160 165 public void childRemoved(StructuralEvent event) { 166 Object child = event.getChild(); 167 if (child instanceof Structural) { 168 ((Structural)child).removeStructuralListener(this); 169 } 170 String id = (String ) serializing.remove(child); 171 if (id != null) { 172 remove(id); 173 } 174 } 175 176 public void destroy() { 177 for (Iterator it = serializing.keySet().iterator(); it.hasNext(); ) { 178 Object 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 |