1 5 package net.sf.panoptes.registry; 6 7 import java.io.IOException ; 8 import java.util.Enumeration ; 9 import java.util.HashMap ; 10 import java.util.HashSet ; 11 import java.util.Hashtable ; 12 import java.util.Iterator ; 13 import java.util.Set ; 14 15 import javax.management.JMException ; 16 import javax.management.MBeanServerConnection ; 17 import javax.management.MalformedObjectNameException ; 18 import javax.management.ObjectName ; 19 20 import net.sf.panoptes.component.jmx.model.MBeanLinkNode; 21 import net.sf.panoptes.model.node.Node; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 30 public class ConfiguratorRegistry { 31 32 private Log log = LogFactory.getLog(getClass()); 33 private HashMap configurators = new HashMap (); 34 private static ConfiguratorRegistry instance = null; 35 private ConfiguratorRegistry() { 36 } 37 38 public static ConfiguratorRegistry getInstance() { 39 if (instance == null) 40 instance = new ConfiguratorRegistry(); 41 return instance; 42 } 43 44 public ConfiguratorEntry getConfigurator(String name) { 45 return (ConfiguratorEntry) configurators.get(name); 46 } 47 48 63 public ConfiguratorEntry addConfigurator( 64 String name, 65 String guiType, 66 String configuratorClassName, 67 ObjectName objectName, 68 String mbeanClassName, 69 String className, 70 String iconName, 71 HashMap attributes) 72 throws EditorRegistryException, MalformedObjectNameException { 73 if (configurators.get(name) != null) 74 throw new EditorRegistryException("Duplicate configurator name " + name); 75 76 try { 77 Class clazz = Class.forName(configuratorClassName); 79 ConfiguratorEntry entry = 80 new ConfiguratorEntry( 81 name, 82 guiType, 83 clazz, 84 objectName, 85 mbeanClassName, 86 className, 87 attributes, 88 iconName); 89 configurators.put(name, entry); 90 return entry; 91 } catch (ClassNotFoundException e) { 92 throw new EditorRegistryException( 93 "Configurator class '" + className + "' does not exists", 94 e); 95 } 96 } 97 98 106 public Set findConfigurators(String guiType, Node node) throws JMException , IOException { 107 HashSet configs = new HashSet (); 108 for (Iterator i = configurators.values().iterator(); i.hasNext();) { 109 boolean match = true; 110 ConfiguratorEntry e = (ConfiguratorEntry) i.next(); 111 if (!guiType.equals(e.getGUIType())) 112 continue; 113 114 if (node instanceof MBeanLinkNode) { 115 MBeanLinkNode linkNode = (MBeanLinkNode) node; 116 ObjectName objectName = linkNode.getObjectName(); 117 MBeanServerConnection server = linkNode.getServer().getServerConnection(); 118 if (objectName == null) { 120 if (e.getPattern() == null || e.getPattern().toString().equals("*.*")); 121 else 122 match = false; 123 } else if (e.getPattern() == null || match(objectName, e.getPattern())) { 124 } else 125 match = false; 126 127 if (server == null) { 128 log.error("No MBeanServer provided"); 129 continue; 130 } 131 132 if (e.getMBeanClassName() != null) { 133 if (!server.isInstanceOf(objectName, e.getMBeanClassName())) 134 match = false; 135 } 136 } else { 137 match = false; 138 } 139 140 if (e.getClassName() != null) { 141 if (node.getBean() != null) { 142 try { 143 if (Class.forName(e.getClassName()).isInstance(node.getBean())) 144 match = true; 145 } catch (ClassNotFoundException e1) { 146 log.warn("Invalid classname" + e.getClassName()); 147 } 148 } 149 } 150 if (match) 151 configs.add(e); 152 } 153 154 return configs; 155 156 } 157 158 165 private boolean match(ObjectName name, ObjectName pattern) { 166 170 if ((!pattern.getDomain().equals("*") && !pattern.getDomain().equals(name.getDomain()))) 172 return false; 173 174 Hashtable props = pattern.getKeyPropertyList(); 176 for (Enumeration e = props.keys(); e.hasMoreElements();) { 177 String key = (String ) e.nextElement(); 178 String value = (String ) name.getKeyPropertyList().get(key); 179 if ((value == null) || !(value.equals(props.get(key)))) 180 return false; 181 } 182 return true; 183 } 184 185 public void removeAll() { 186 configurators.clear(); 187 } 188 } 189 | Popular Tags |