1 package net.sf.panoptes.model.component.jmx;
2
3 import java.net.InetAddress;
4 import java.net.UnknownHostException;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.Set;
8 import java.util.Vector;
9
10 import javax.management.JMException;
11 import javax.management.MBeanInfo;
12 import javax.management.ObjectInstance;
13 import javax.management.ObjectName;
14 import javax.naming.Context;
15
16 import net.sf.panoptes.model.component.Component;
17 import net.sf.panoptes.model.component.registry.ComponentEntry;
18 import net.sf.panoptes.model.component.registry.ComponentName;
19 import net.sf.panoptes.model.component.registry.ComponentRegistry;
20 import net.sf.panoptes.model.node.ComponentNode;
21 import net.sf.panoptes.model.node.NodeDescriptor;
22 import net.sf.panoptes.model.node.INode;
23 import net.sf.panoptes.model.node.jmx.DomainNode;
24
25 import org.jboss.jmx.connector.rmi.RMIConnectorImpl;
26
27 /***
28 * MBeanServerComponent.java
29 *
30 * Wraps a JMX server connector.
31 *
32 * @author Dag Liodden
33 * @version 0.1
34 */
35 public class MBeanServerComponent extends Component {
36
37 private ComponentRegistry componentRegistry;
38 private String name;
39 private String host;
40 private int port;
41 private RMIConnectorImpl server = null;
42 private MBeanComponent[] mBeans = new MBeanComponent[0];
43 private HashMap domains = new HashMap();
44
45 private static Vector connections = new Vector();
46
47 /***
48 * Constructor for MBeanServerComponent.
49 */
50 public MBeanServerComponent(ComponentRegistry componentRegistry, String name, String host, int port) {
51 this.name = name;
52 this.host = host;
53 this.port = port;
54 this.componentRegistry = componentRegistry;
55 connections.add(this);
56 }
57
58 public static MBeanServerComponent[] listConnections() {
59 return (MBeanServerComponent[]) connections.toArray(new MBeanServerComponent[connections.size()]);
60 }
61
62 public RMIConnectorImpl getConnector() {
63 return server;
64 }
65
66 public void connect() throws JMException, UnknownHostException {
67 String serverName = getHost();
68 if (serverName.equals("localhost")) {
69 serverName = InetAddress.getLocalHost().getHostName();
70 }
71
72 System.setProperty(Context.PROVIDER_URL, "jnp://" + serverName + ":" + port);
73 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
74 System.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
75
76 /***
77 * Ripped out of org.jboss.jmx.gui.
78 * Have no idea why this should be done. :)
79 */
80 ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
81 try {
82 Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
83 server = new RMIConnectorImpl(RMIConnectorImpl.NOTIFICATION_TYPE_RMI, null, serverName);
84 } catch (Exception e) {
85 throw new JMException(e.getMessage());
86 } finally {
87 Thread.currentThread().setContextClassLoader(oldCL);
88 }
89 refresh();
90 }
91
92 /***
93 * Returns the host.
94 * @return String
95 */
96 public String getHost() {
97 return host;
98 }
99
100 /***
101 * Returns the name.
102 * @return String
103 */
104 public String getName() {
105 return name;
106 }
107
108 /***
109 * Returns the port.
110 * @return int
111 */
112 public int getPort() {
113 return port;
114 }
115
116 /***
117 * Sets the host.
118 * @param host The host to set
119 */
120 public void setHost(String host) {
121 this.host = host;
122 }
123
124 /***
125 * Sets the name.
126 * @param name The name to set
127 */
128 public void setName(String name) {
129 this.name = name;
130 }
131
132 /***
133 * Sets the port.
134 * @param port The port to set
135 */
136 public void setPort(int port) {
137 this.port = port;
138 }
139
140 /***
141 * Returns the server.
142 * @return RMIConnectorImpl
143 */
144 public RMIConnectorImpl getServer() {
145 return server;
146 }
147
148 public void refresh() {
149 try {
150
151 Set s = server.queryMBeans(new ObjectName("*:*"), null);
152
153 Iterator it = s.iterator();
154 domains.clear();
155
156 int i = 0;
157 while (it.hasNext()) {
158 ObjectInstance instance = (ObjectInstance) it.next();
159 ObjectName objectName = instance.getObjectName();
160 /***
161 * Instanciate domains
162 */
163 DomainNode domain;
164 String domainName = objectName.getDomain();
165 if (!domains.containsKey(domainName)) {
166 domain = new DomainNode(domainName);
167 domains.put(domainName, domain);
168 } else {
169 domain = (DomainNode) domains.get(domainName);
170 }
171
172 MBeanInfo info = server.getMBeanInfo(objectName);
173 MBeanComponent bean2 = new MBeanComponent(info, this, objectName);
174 String name = objectName.toString();
175
176 name = name.substring(name.indexOf(':') + 1) + ",domain=" + objectName.getDomain();
177 ComponentName componentName = new ComponentName(ComponentName.DOMAIN_MBEAN, name);
178 componentRegistry.registerComponent(bean2, componentName);
179 domain.addChild(new ComponentNode(componentRegistry, componentName));
180 for (Iterator iter =
181 componentRegistry.findEntries(new ComponentName(ComponentName.DOMAIN_MBEAN + ":*")).iterator();
182 iter.hasNext();
183 ) {
184 ComponentEntry entry = (ComponentEntry) iter.next();
185 }
186 }
187 } catch (Exception e) {
188 e.printStackTrace();
189 mBeans = new MBeanComponent[0];
190
191 }
192 }
193
194 public DomainNode[] getDomains() {
195 return (DomainNode[]) domains.values().toArray(new DomainNode[domains.size()]);
196 }
197
198 /***
199 * @see com.gllt.troodon.console.INode#getChildren()
200 */
201 public INode[] getChildren() {
202 return getDomains();
203 }
204
205 /***
206 * @see com.gllt.troodon.console.INode#getIconName()
207 */
208 public String getIconName() {
209 return INode.ICON_SERVER;
210 }
211
212 /***
213 * @see com.gllt.troodon.console.INode#getConfigDescriptor()
214 */
215 public NodeDescriptor getConfigDescriptor() {
216 return new NodeDescriptor(name, "Connection to server " + host + ":" + port);
217 }
218
219 /* (non-Javadoc)
220 * @see net.sf.panoptes.view.configurator.INode#acceptsChildren()
221 */
222 public boolean acceptsChildren() {
223 return false;
224 }
225
226 }
This page was automatically generated by Maven