1 package net.sf.panoptes.module.jmx;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.HashMap;
7 import java.util.Iterator;
8
9 import javax.management.Attribute;
10 import javax.management.InstanceNotFoundException;
11 import javax.management.MBeanAttributeInfo;
12 import javax.management.MBeanException;
13 import javax.management.MBeanInfo;
14 import javax.management.MBeanOperationInfo;
15 import javax.management.ObjectName;
16 import javax.management.ReflectionException;
17
18 import net.sf.panoptes.model.component.Component;
19 import net.sf.panoptes.model.node.Node;
20 import net.sf.panoptes.model.node.NodeDescriptor;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.eclipse.swt.graphics.Image;
25
26 /***
27
28 * MBeanComponent.java
29 *
30 * Wraps an MBean and implements helper methods. Original code by Stefan Groschupf(sg@media-style.com).
31 *
32 * @author Dag Liodden
33 * @version 0.1
34 *
35 */
36
37 public class MBeanComponent extends Component {
38
39 private MBeanServerComponent server;
40 private HashMap mBeanAttributes = null;
41 private HashMap mBeanOperations = null;
42 private ObjectName mBeanName;
43 private MBeanInfo mBeanInfo;
44 private Object proxy = null;
45
46 private Log log = LogFactory.getLog(getClass());
47
48 /***
49 * Constructor for MBeanComponent.
50 */
51 public MBeanComponent() {
52 }
53
54 /***
55 * Sets the <code>ObjectName</code> of the MBean this component wraps..
56 * @param name
57 */
58 protected void setMBeanName(ObjectName name) {
59 this.mBeanName = name;
60 }
61
62 public void setMBeanServerComponent(MBeanServerComponent server) {
63 this.server = server;
64 }
65
66 /***
67 * Method getAttributes.
68 * @return Attribute[]
69 */
70 public Attribute[] getAttributes() {
71 Collection collection = this.getMBeanAttributes().values();
72 return (Attribute[]) collection.toArray(new Attribute[collection.size()]);
73
74 }
75 /***
76 * Method getOperations.
77 */
78 public Operation[] getOperations() {
79 Collection collection = getMBeanOperations().values();
80 return (Operation[]) collection.toArray(new Operation[collection.size()]);
81 }
82
83 /***
84 * Method setMBeanInfo.
85 * @param info
86 */
87 public void setMBeanInfo(MBeanInfo info) {
88 this.mBeanInfo = info;
89 }
90
91 public MBeanInfo getMBeanInfo() {
92 return this.mBeanInfo;
93 }
94
95 public MBeanServerComponent getServer() {
96 return server;
97 }
98
99 /***
100 * Simple method to test invocation from Jelly script. Doesn't support
101 * parameters or return types.
102 *
103 * @param operation name of the operation to invoke
104 */
105 public void invokeWithoutParams(String operation)
106 throws InstanceNotFoundException, MBeanException, ReflectionException, IOException {
107 System.out.println("Invoking");
108 server.invoke(mBeanName, operation, new Object[0], new String[0]);
109 }
110
111 public Object getProxy() throws Exception {
112 if (proxy == null)
113 proxy =
114 MBeanProxyFactory.getInstance().createProxy(
115 this.getObjectName(),
116 this.getServer().getServerConnection());
117 return proxy;
118 }
119
120 /***
121 * Method refresh.
122 */
123 public void refresh() {
124 //refreshOperations();
125 //refreshAttributes();
126 }
127 /***
128 * Method refreshAttributes.
129 */
130 private void refreshAttributes() {
131 MBeanAttributeInfo[] at = mBeanInfo.getAttributes();
132 mBeanAttributes = new HashMap();
133
134 for (int i = 0; i < at.length; i++) {
135 Attribute attribute =
136 new Attribute(at[i].getName(), getAttributeValue(at[i].getName()));
137 getMBeanAttributes().put(attribute.getName(), attribute);
138 }
139 }
140 /***
141 * Method getAttrbuteValue.
142 * @param string
143 */
144 private void getAttrbuteValue(String string) {
145 }
146 /***
147 * Method refreshOperations.
148 */
149 private void refreshOperations() {
150 //operations
151 MBeanOperationInfo[] op = mBeanInfo.getOperations();
152 mBeanOperations = new HashMap();
153
154 for (int k = 0; k < op.length; k++) {
155 Operation operation = new Operation(op[k], this);
156 getMBeanOperations().put(operation.getName(), operation);
157 }
158
159 }
160
161 public ObjectName getObjectName() {
162 return this.mBeanName;
163
164 }
165 public String toString() {
166 return this.mBeanName.toString();
167 }
168 /***
169 * Method countAttributes.
170 * @return int
171 */
172 public int countAttributes() {
173 return getMBeanAttributes().size();
174 }
175
176 private HashMap getMBeanAttributes() {
177 if (mBeanAttributes == null)
178 refreshAttributes();
179 return mBeanAttributes;
180 }
181
182 private HashMap getMBeanOperations() {
183 if (mBeanOperations == null)
184 refreshOperations();
185 return mBeanOperations;
186 }
187
188 // property stuff.
189
190 /***
191 * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
192 */
193 public Object getEditableValue() {
194 return null;
195 }
196
197 /***
198 * Method getAttrbute.
199 * @param string
200 * @return String
201 */
202 public Object getAttributeValue(String attributeName) {
203 Object value = null;
204 try {
205 ObjectName objectName = getObjectName();
206 value = server.getServerConnection().getAttribute(objectName, attributeName);
207 } catch (Exception e) {
208 log.warn("Can't get Attribute '" + attributeName + "'", e);
209 }
210 return value;
211 }
212
213 public void setAttrbuteValue(String attributeName, Object value) {
214
215 /*RMIConnectorImpl server = getParent().getServer();
216 ObjectName objectName = getObjectName();
217 javax.management.Attribute attribute = new javax.management.Attribute(attributeName, value);
218 try {
219 server.setAttribute(objectName, attribute);
220 } catch (InstanceNotFoundException e) {
221 System.err.println("InstanceNotFoundException "+e);
222 } catch (AttributeNotFoundException e) {
223 System.err.println("AttributeNotFoundException " +e);
224 } catch (InvalidAttributeValueException e) {
225 System.err.println("InvalidAttributeValueException " + e);
226 } catch (MBeanException e) {
227 System.err.println("MBeanException" + e);
228 } catch (ReflectionException e) {
229 System.err.println("ReflectionException" +e);
230 }
231
232 */
233 }
234
235 /* (non-Javadoc)
236 * @see net.sf.panoptes.model.node.Node#getChildren()
237 */
238 public Node[] getChildren() {
239
240 ArrayList c = new ArrayList();
241
242 getOperations();
243 for (Iterator i = mBeanOperations.values().iterator(); i.hasNext();) {
244 Operation op = (Operation) i.next();
245 c.add(new OperationNode(op));
246 }
247 return (Node[]) c.toArray(new Node[c.size()]);
248 }
249
250 /***
251 * @see com.gllt.troodon.console.Node#getIconName()
252 */
253 public Image getIcon() {
254 return Node.ICON_MBEAN;
255 }
256
257 /***
258 * @see com.gllt.troodon.console.Node#getConfigDescriptor()
259 */
260 public NodeDescriptor getConfigDescriptor() {
261 String name = getObjectName().getCanonicalName();
262 name = name.substring(name.indexOf(':') + 1);
263 return new NodeDescriptor(name, getObjectName().toString());
264 }
265
266 public String[] getConfiguratorScripts() {
267 return null;
268 }
269
270 public void init() {
271
272 }
273
274 private class OperationNode extends Node {
275 private Operation op;
276
277 private OperationNode(Operation op) {
278 this.op = op;
279 }
280 public boolean acceptsChildren() {
281 return false;
282 }
283
284 public Node[] getChildren() {
285 return new Node[0];
286 }
287
288 public NodeDescriptor getConfigDescriptor() {
289 return new NodeDescriptor(op.getName(), "Operation" + op.getName());
290 }
291
292 public String[] getConfiguratorScripts() {
293 return null;
294 }
295
296 public Image getIcon() {
297 return Node.ICON_OPERATION;
298 }
299 }
300 }
301
This page was automatically generated by Maven