1 package net.sf.panoptes.module.jmx;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.Set;
7
8 import javax.management.InstanceNotFoundException;
9 import javax.management.MBeanException;
10 import javax.management.MBeanInfo;
11 import javax.management.MBeanServerConnection;
12 import javax.management.ObjectInstance;
13 import javax.management.ObjectName;
14 import javax.management.ReflectionException;
15
16 import net.sf.panoptes.model.component.ComponentQuery;
17 import net.sf.panoptes.model.component.ComponentUtil;
18 import net.sf.panoptes.model.component.registry.ComponentAlreadyExistsException;
19 import net.sf.panoptes.model.component.registry.ComponentName;
20 import net.sf.panoptes.model.component.registry.MalformedComponentNameException;
21 import net.sf.panoptes.model.node.Node;
22 import net.sf.panoptes.model.node.NodeDescriptor;
23 import net.sf.panoptes.module.jmx.connector.MBeanServerConnector;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.eclipse.swt.graphics.Image;
28
29 /***
30 * MBeanServerComponent.java
31 *
32 * Wraps a <code>MBeanServerConnection</code>. No uniform way to identify or name
33 * such connections exist since there are various connector types and protocols, but to
34 * give earch connection a <code>ComponentName</code>, we require a hostname
35 * and a name for each component. These two combined must be unique. It would be
36 * possible to generate a unique ID (e.g and GUID) instead, but this would break
37 * the possibility to store configuration settings for a specific MBeanServer from
38 * session to session.
39 *
40 * All domains and MBeans that belongs to a MBeanServer will inherit the attributes
41 * identifying the server instance so that domains and MBeans can be queried.
42 * (This class is really just a <code>ComponentQuery</code> that returns all domains
43 * matching the server's attribtues).
44 *
45 * @author Dag Liodden
46 * @version 0.1
47 */
48 public class MBeanServerComponent extends ComponentQuery {
49
50 private String name;
51 private String hostName;
52 private MBeanServerConnection serverConnection = null;
53 private MBeanComponent[] mBeans = new MBeanComponent[0];
54 private HashMap domains = new HashMap();
55 private MBeanServerConnector connector;
56 private Log log = LogFactory.getLog(getClass());
57
58
59
60 /***
61 * Constructor for MBeanServerComponent.
62 */
63 public MBeanServerComponent(MBeanServerConnector connector) {
64 this.connector = connector;
65 }
66
67 public void init() {
68 ComponentName query;
69 try {
70 query = ComponentUtil.createMBeanServerName(this);
71 query.setDomain(ComponentName.DOMAIN_MBEAN_DOMAIN);
72 setQuery(new ComponentName(query.toString() + ",*"));
73 } catch (MalformedComponentNameException e) {
74 e.printStackTrace();
75 }
76 }
77
78 public void connect() throws Exception {
79 setServer(connector.connect());
80 }
81
82 protected void setServer(MBeanServerConnection connection) {
83 this.serverConnection = connection;
84 refresh();
85 }
86
87 /***
88 * Returns the host.
89 * @return String
90 */
91 public String getHostName() {
92 return hostName;
93 }
94
95 /***
96 * Returns the name.
97 * @return String
98 */
99 public String getName() {
100 return name;
101 }
102
103 /***
104 * Sets the host.
105 * @param host The host to set
106 */
107 public void setHostName(String host) {
108 this.hostName = host;
109 }
110
111 /***
112 * Sets the name.
113 * @param name The name to set
114 */
115 public void setName(String name) {
116 this.name = name;
117 }
118
119 /***
120 * Returns the server.
121 * @return MBeanServerConnection
122 */
123 public MBeanServerConnection getServerConnection() {
124 return serverConnection;
125 }
126
127 public void refresh() {
128 try {
129
130 Set s = serverConnection.queryMBeans(new ObjectName("*:*"), null);
131
132 Iterator it = s.iterator();
133 domains.clear();
134
135 int i = 0;
136 while (it.hasNext()) {
137 ObjectInstance instance = (ObjectInstance) it.next();
138 ObjectName objectName = instance.getObjectName();
139 /***
140 * Instanciate domains
141 */
142 DomainComponent domain;
143 String domainName = objectName.getDomain();
144 if (!domains.containsKey(domainName)) {
145
146 // Create the query that return the MBeans in this comain
147 ComponentName query = ComponentUtil.createDomainMBeanQuery(this, domainName);
148 //System.out.println(query.toString());
149
150 // Create the domain component
151 domain = new DomainComponent();
152 domain.setQuery(query);
153 domain.setDomainName(domainName);
154
155 // Cache it
156 domains.put(domainName, domain);
157
158 // Register the domain component
159 ComponentName componentName = ComponentUtil.createDomainName(this, domainName);
160 getComponentRegistry().registerComponent(domain, componentName);
161 } else {
162 domain = (DomainComponent) domains.get(domainName);
163 }
164
165 // Create and register the MBean Component
166 MBeanInfo info = serverConnection.getMBeanInfo(objectName);
167 MBeanComponent mBean = new MBeanComponent();
168 mBean.setMBeanInfo(info);
169 mBean.setMBeanServerComponent(this);
170 mBean.setMBeanName(objectName);
171
172 ComponentName componentName = ComponentUtil.createMBeanName(this, domain, objectName);
173 try {
174 getComponentRegistry().registerComponent(mBean, componentName);
175 } catch (ComponentAlreadyExistsException e) {
176 log.warn("Component " + componentName.toString() + " was is already registered in the registry.");
177 }
178
179 }
180 } catch (Exception e) {
181 e.printStackTrace();
182 mBeans = new MBeanComponent[0];
183
184 }
185 }
186
187 /***
188 * @see com.gllt.troodon.console.Node#getIconName()
189 */
190 public Image getIcon() {
191 return Node.ICON_SERVER;
192 }
193
194 /***
195 * @see com.gllt.troodon.console.Node#getConfigDescriptor()
196 */
197 public NodeDescriptor getConfigDescriptor() {
198 return new NodeDescriptor(name, "Connection to " + getHostName() + " on host " + getHostName());
199 }
200
201 /* (non-Javadoc)
202 * @see net.sf.panoptes.view.configurator.Node#acceptsChildren()
203 */
204 public boolean acceptsChildren() {
205 return false;
206 }
207
208
209 public Object invoke(ObjectName objectName, String operation, Object[] params, String[] paramClasses) throws InstanceNotFoundException, MBeanException, ReflectionException, IOException {
210 serverConnection.invoke(objectName, operation, params, paramClasses);
211 return null;
212 }
213 }
This page was automatically generated by Maven