1 17 18 package org.apache.geronimo.kernel.basic; 19 20 import org.apache.geronimo.gbean.AbstractName; 21 import org.apache.geronimo.gbean.AbstractNameQuery; 22 import org.apache.geronimo.kernel.DependencyManager; 23 import org.apache.geronimo.kernel.lifecycle.LifecycleAdapter; 24 import org.apache.geronimo.kernel.lifecycle.LifecycleListener; 25 import org.apache.geronimo.kernel.lifecycle.LifecycleMonitor; 26 27 import javax.management.ObjectName ; 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 47 public class BasicDependencyManager implements DependencyManager { 48 52 private final LifecycleMonitor lifecycleMonitor; 53 54 57 private final LifecycleListener lifecycleListener = new DependencyManagerLifecycleListener(); 58 59 62 private final Map childToParentMap = new HashMap (); 63 64 67 private final Map parentToChildMap = new HashMap (); 68 69 public BasicDependencyManager(LifecycleMonitor lifecycleMonitor) throws Exception { 70 assert lifecycleMonitor != null; 71 this.lifecycleMonitor = lifecycleMonitor; 72 lifecycleMonitor.addLifecycleListener(lifecycleListener, new AbstractNameQuery(null, Collections.EMPTY_MAP, Collections.EMPTY_SET)); 73 } 74 75 public synchronized void close() { 76 lifecycleMonitor.removeLifecycleListener(lifecycleListener); 77 childToParentMap.clear(); 78 parentToChildMap.clear(); 79 } 80 81 87 public synchronized void addDependency(AbstractName child, AbstractName parent) { 88 Set parents = (Set ) childToParentMap.get(child); 89 if (parents == null) { 90 parents = new HashSet (); 91 childToParentMap.put(child, parents); 92 } 93 parents.add(parent); 94 95 Set children = (Set ) parentToChildMap.get(parent); 96 if (children == null) { 97 children = new HashSet (); 98 parentToChildMap.put(parent, children); 99 } 100 children.add(child); 101 } 102 103 109 public synchronized void removeDependency(AbstractName child, AbstractName parent) { 110 Set parents = (Set ) childToParentMap.get(child); 111 if (parents != null) { 112 parents.remove(parent); 113 } 114 115 Set children = (Set ) parentToChildMap.get(parent); 116 if (children != null) { 117 children.remove(child); 118 } 119 } 120 121 126 public synchronized void removeAllDependencies(AbstractName child) { 127 Set parents = (Set ) childToParentMap.remove(child); 128 if (parents == null) { 129 return; 130 } 131 for (Iterator iterator = parents.iterator(); iterator.hasNext();) { 132 ObjectName parent = (ObjectName ) iterator.next(); 133 Set children = (Set ) parentToChildMap.get(parent); 134 if (children != null) { 135 children.remove(child); 136 } 137 138 } 139 } 140 141 147 public synchronized void addDependencies(AbstractName child, Set parents) { 148 Set existingParents = (Set ) childToParentMap.get(child); 149 if (existingParents == null) { 150 existingParents = new HashSet (parents); 151 childToParentMap.put(child, existingParents); 152 } else { 153 existingParents.addAll(parents); 154 } 155 156 for (Iterator i = parents.iterator(); i.hasNext();) { 157 Object startParent = i.next(); 158 Set children = (Set ) parentToChildMap.get(startParent); 159 if (children == null) { 160 children = new HashSet (); 161 parentToChildMap.put(startParent, children); 162 } 163 children.add(child); 164 } 165 } 166 167 173 public synchronized Set getParents(AbstractName child) { 174 Set parents = (Set ) childToParentMap.get(child); 175 if (parents == null) { 176 return Collections.EMPTY_SET; 177 } 178 return new HashSet (parents); 179 } 180 181 187 public synchronized Set getChildren(AbstractName parent) { 188 Set children = (Set ) parentToChildMap.get(parent); 189 if (children == null) { 190 return Collections.EMPTY_SET; 191 } 192 return new HashSet (children); 193 } 194 195 196 private class DependencyManagerLifecycleListener extends LifecycleAdapter { 197 public void unloaded(AbstractName abstractName) { 198 synchronized (BasicDependencyManager.this) { 199 removeAllDependencies(abstractName); 200 } 201 202 } 203 } 204 } 205 | Popular Tags |