KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > panoptes > registry > ConfiguratorRegistry


1 /*
2  * Created on 22.mar.2003
3  *
4  */

5 package net.sf.panoptes.registry;
6
7 import java.io.IOException JavaDoc;
8 import java.util.Enumeration JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.HashSet JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Set JavaDoc;
14
15 import javax.management.JMException JavaDoc;
16 import javax.management.MBeanServerConnection JavaDoc;
17 import javax.management.MalformedObjectNameException JavaDoc;
18 import javax.management.ObjectName JavaDoc;
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 /**
27  * @author Dag Liodden
28  * @version 0.1
29  */

30 public class ConfiguratorRegistry {
31
32     private Log log = LogFactory.getLog(getClass());
33     private HashMap JavaDoc configurators = new HashMap JavaDoc();
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 JavaDoc name) {
45         return (ConfiguratorEntry) configurators.get(name);
46     }
47
48     /**
49      * Adds a <code>Configurator</code> to the registry.
50      *
51      * @param name
52      * the nickname of the configurator (to be used in configuration
53      * files)
54      * @param className
55      * the FQCN of the mbean
56      * @param patterns
57      * comma-delimited string of patterns that can be parsed as
58      * <code>ComponentName</code>
59      * @throws ConfiguratorRegistryException
60      * if configurator already is added or has an invalid className
61      * @see net.sf.panoptes.view.configurator.ComponentConfigurator
62      */

63     public ConfiguratorEntry addConfigurator(
64         String JavaDoc name,
65         String JavaDoc guiType,
66         String JavaDoc configuratorClassName,
67         ObjectName JavaDoc objectName,
68         String JavaDoc mbeanClassName,
69         String JavaDoc className,
70         String JavaDoc iconName,
71         HashMap JavaDoc attributes)
72         throws EditorRegistryException, MalformedObjectNameException JavaDoc {
73         if (configurators.get(name) != null)
74             throw new EditorRegistryException("Duplicate configurator name " + name);
75
76         try {
77             // Check that the classname is valid
78
Class JavaDoc 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 JavaDoc e) {
92             throw new EditorRegistryException(
93                 "Configurator class '" + className + "' does not exists",
94                 e);
95         }
96     }
97
98     /**
99      * Fetches configurators that match the <code>Componentname</code>.
100      *
101      * @param componentName
102      * the name of the component. If it's null, only configurators
103      * with patterns that match any component are returned
104      * @return
105      */

106     public Set JavaDoc findConfigurators(String JavaDoc guiType, Node node) throws JMException JavaDoc, IOException JavaDoc {
107         HashSet JavaDoc configs = new HashSet JavaDoc();
108         for (Iterator JavaDoc 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 JavaDoc objectName = linkNode.getObjectName();
117                 MBeanServerConnection JavaDoc server = linkNode.getServer().getServerConnection();
118                 // Do
119
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 JavaDoc 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     /**
159      * Checks if this instance matches a pattern.
160      *
161      * @param pattern
162      * the pattern
163      * @return true if it matches
164      */

165     private boolean match(ObjectName JavaDoc name, ObjectName JavaDoc pattern) {
166         // We do casts here without any checking since the keys and values have
167
// been
168
// checked in the ComponentName constructor.
169

170         // Check if the domain matches
171
if ((!pattern.getDomain().equals("*") && !pattern.getDomain().equals(name.getDomain())))
172             return false;
173
174         // Check the keys in pattern and see if there is a match;
175
Hashtable JavaDoc props = pattern.getKeyPropertyList();
176         for (Enumeration JavaDoc e = props.keys(); e.hasMoreElements();) {
177             String JavaDoc key = (String JavaDoc) e.nextElement();
178             String JavaDoc value = (String JavaDoc) 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