KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > jmx > server > InterfaceManagerImpl


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.jmx.server;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Arrays JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import javax.management.MBeanAttributeInfo JavaDoc;
13 import javax.management.MBeanConstructorInfo JavaDoc;
14 import javax.management.MBeanException JavaDoc;
15 import javax.management.MBeanInfo JavaDoc;
16 import javax.management.MBeanNotificationInfo JavaDoc;
17 import javax.management.MBeanOperationInfo JavaDoc;
18 import javax.management.Notification JavaDoc;
19 import javax.management.ReflectionException JavaDoc;
20
21 public class InterfaceManagerImpl implements InterfaceManager {
22     
23     private final MBeanInfo JavaDoc mBeanInfo;
24     
25     private final Class JavaDoc[] interfaces;
26     
27     private final InterfaceHandler[] handlers;
28     
29     private final Map JavaDoc/*<String, InterfaceHandler.Instance> */operations = new HashMap JavaDoc();
30     
31     public InterfaceManagerImpl(Object JavaDoc target,
32             OddjobMBean ojmb,
33             InterfaceInfo[] definitions) {
34         
35         List JavaDoc attributeInfo = new ArrayList JavaDoc();
36         List JavaDoc operationInfo = new ArrayList JavaDoc();
37         List JavaDoc notificationInfo = new ArrayList JavaDoc();
38         
39         interfaces = new Class JavaDoc[definitions.length];
40         handlers = new InterfaceHandler[definitions.length];
41         
42         for (int i = 0; i < definitions.length; ++i) {
43             InterfaceInfo definition = definitions[i];
44             
45             interfaces[i] = definition.interfaceClass();
46             InterfaceHandler ihInstance
47                 = definition.attach(target, ojmb);
48             handlers[i] = ihInstance;
49             
50             attributeInfo.addAll(Arrays.asList(definition.getMBeanAttributeInfo()));
51             MBeanOperationInfo JavaDoc[] oInfo = definition.getMBeanOperationInfo();
52             for (int j = 0; j < oInfo.length; ++j) {
53                 operationInfo.add(oInfo[j]);
54                 operations.put(oInfo[j].getName(), ihInstance);
55             }
56             notificationInfo.addAll(Arrays.asList(definition.getMBeanNotificationInfo()));
57         }
58         
59         mBeanInfo = new MBeanInfo JavaDoc(target.toString(),
60                 "Description of " + target.toString(),
61                 (MBeanAttributeInfo JavaDoc[]) attributeInfo.toArray(new MBeanAttributeInfo JavaDoc[0]),
62                 new MBeanConstructorInfo JavaDoc[0],
63                 (MBeanOperationInfo JavaDoc[]) operationInfo.toArray(new MBeanOperationInfo JavaDoc[0]),
64                 (MBeanNotificationInfo JavaDoc[]) notificationInfo.toArray(new MBeanNotificationInfo JavaDoc[0]));
65     }
66     
67     public Class JavaDoc[] interfaces() {
68         return interfaces;
69     }
70     
71     public MBeanInfo JavaDoc getMBeanInfo() {
72         return mBeanInfo;
73     }
74     
75     public Object JavaDoc invoke(String JavaDoc actionName, Object JavaDoc[] params, String JavaDoc[] signature)
76     throws MBeanException JavaDoc, ReflectionException JavaDoc {
77         InterfaceHandler ihInstance = (InterfaceHandler) operations.get(actionName);
78         if (ihInstance == null) {
79             throw new IllegalArgumentException JavaDoc("No interface supports method [" + actionName + "]");
80         }
81         return ihInstance.invoke(actionName, params, signature);
82     }
83     
84     public Notification JavaDoc[] getLastNotifications() {
85         List JavaDoc notifications = new ArrayList JavaDoc();
86         for (int i = 0; i < handlers.length; ++i) {
87             Notification JavaDoc[] last = handlers[i].getLastNotifications();
88             if (last == null) {
89                 continue;
90             }
91             notifications.addAll(
92                     Arrays.asList(last));
93         }
94         return (Notification JavaDoc[]) notifications.toArray(new Notification JavaDoc[0]);
95     }
96     
97     public void destroy() {
98         for (int i = 0; i < handlers.length; ++i) {
99             handlers[i].destroy();
100             handlers[i] = null;
101         }
102     }
103 }
Popular Tags