1 package net.sf.panoptes.model.node; 2 3 import java.io.File ; 4 import java.util.Collection ; 5 import java.util.Enumeration ; 6 import java.util.HashMap ; 7 import java.util.HashSet ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Map ; 11 import java.util.Set ; 12 import java.util.Vector ; 13 import java.util.Map.Entry; 14 15 import javax.swing.tree.DefaultMutableTreeNode ; 16 17 import net.sf.panoptes.component.jmx.model.MBeanServerNode; 18 19 import org.apache.commons.collections.MapUtils; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import net.sf.panoptes.swing.OperationFactory; 24 25 33 34 public abstract class NodeSupport implements Node { 35 36 protected Log log = LogFactory.getLog(getClass()); 37 private Node parent; 38 private Vector listeners = new Vector (); 39 private NodeDescriptor configDescriptor = new NodeDescriptor(); 40 private Context context; 41 42 private MBeanServerNode server = null; 43 private OperationFactory operationFactory = null; 44 45 public NodeSupport(Node parent) { 46 this.parent = parent; 47 if (parent == null) { 48 context = new Context(null); 49 } else { 50 context = new Context(parent.getContext()); 51 } 52 context.put("node", this); 53 54 } 55 56 public OperationFactory getOperationFactory() { 57 if (operationFactory == null) { 58 if (parent == null) 59 return OperationFactory.getFactory(); 60 else 61 return parent.getOperationFactory(); 62 } else 63 return operationFactory; 64 } 65 66 67 68 public Context getContext() { 69 return context; 70 } 71 72 public Node getParent() { 73 return parent; 74 } 75 76 public void setConfigDescriptor(NodeDescriptor configDescriptor) { 77 this.configDescriptor = configDescriptor; 78 } 79 80 public void addUpdateListener(NodeUpdateListener listener) { 81 listeners.add(listener); 82 } 83 84 public void removeUpdateListener(NodeUpdateListener listener) { 85 listeners.remove(listener); 86 } 87 88 public void notifyListeners(NodeUpdateEvent event) { 89 for (Enumeration e = listeners.elements(); e.hasMoreElements();) { 90 ((NodeUpdateListener) e.nextElement()).nodeUpdated(event); 91 } 92 } 93 94 public NodeDescriptor getConfigDescriptor() { 95 return configDescriptor; 96 } 97 98 public abstract List getChildren(); 99 public abstract void refresh(); 100 101 public void refreshRecursive() { 102 Collection children = getChildren(); 103 for (Iterator i = children.iterator(); i.hasNext();) { 104 Object o = i.next(); 105 try { 106 ((Node) o).refreshRecursive(); 107 } catch (ClassCastException e) { 108 log.error("Cast " + o.getClass().getName() + ", " + this.getClass().getName(), e); 109 } 110 111 } 112 refresh(); 113 } 114 115 public class Context extends HashMap { 116 117 private Context parentContext; 118 public Context(Context parentContext) { 119 this.parentContext = parentContext; 120 } 121 122 public Object get(Object key) { 123 if (super.get(key) == null && parentContext != null) 124 return parentContext.get(key); 125 else 126 return super.get(key); 127 } 128 129 public void putInMap(Map map) { 130 if (parent != null) 131 parent.getContext().putInMap(map); 132 for (Iterator i = super.entrySet().iterator(); i.hasNext();) { 133 Map.Entry e = (Entry) i.next(); 134 map.put(e.getKey(), e.getValue()); 135 } 136 } 137 138 public Set entrySet() { 139 if (parent != null) { 140 HashMap fullMap = new HashMap (); 141 putInMap(fullMap); 142 return fullMap.entrySet(); 143 } else 144 return super.entrySet(); 145 } 146 147 public Set keySet() { 148 if (parent != null) { 149 Set set = new HashSet (); 150 set.addAll(parent.getContext().keySet()); 151 set.addAll(super.keySet()); 152 return set; 153 } else 154 return super.keySet(); 155 } 156 157 } 158 } 159 | Popular Tags |