001: /**
002: * The XMOJO Project 5
003: * Copyright © 2003 XMOJO.org. All rights reserved.
004:
005: * NO WARRANTY
006:
007: * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
008: * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
009: * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
010: * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
011: * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
012: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
013: * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
014: * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
015: * REPAIR OR CORRECTION.
016:
017: * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
018: * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
019: * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
020: * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
021: * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
022: * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
023: * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
024: * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
025: * SUCH DAMAGES.
026: **/package javax.management;
027:
028: //---------------------Importing the Java packages ---------------------------//
029: import java.util.Hashtable;
030: import java.util.List;
031: import java.util.ArrayList;
032: import java.util.Vector;
033: import java.util.Enumeration;
034: import java.util.Properties;
035: import java.io.InputStream;
036: import java.net.InetAddress;
037: import java.net.UnknownHostException;
038:
039: /**
040: * Represents the MBeanServer from the management point of view.
041: * The MBeanServerDelegate MBean emits the MBeanServerNotifications
042: * when a MBean is registered/deregistered in the MBean server.
043: */
044: public class MBeanServerDelegate implements MBeanServerDelegateMBean,
045: NotificationBroadcaster {
046: //----------------------------Variable Declaration------------------------//
047:
048: private String implProductName = "The XMOJO Project";
049: private String implVendorName = "XMOJO (sponsored by AdventNet Inc.)";
050: private String implVersion = "5.0";
051: private String specName = "Java Management Extensions Instrumentation and Agent Specification";
052: private String specVendorName = "Sun Microsystems Inc.";
053: private String specVersion = "1.0, Final Release";
054:
055: /** The MBean server agent identification.*/
056: private String serverId = "unknown";
057:
058: private Hashtable notifTable = new Hashtable();
059: private Hashtable info = new Hashtable();
060:
061: /** Variable used to generate the serverId uniquely for each server **/
062: private int genrandom = 1;
063:
064: private static int uniqueID = 1;
065:
066: private Properties properties = null;
067:
068: /**
069: * Create a MBeanServerDelegate object.
070: */
071: public MBeanServerDelegate() {
072: String host = "localhost";
073:
074: try {
075: host = InetAddress.getLocalHost().getHostName();
076: //Generate a serverId with host and random number.
077: genrandom = uniqueID++;
078: //genrandom = generateRandomInt(genrandom);
079: serverId = host + "_" + genrandom;
080: } catch (UnknownHostException UHE) {
081: serverId = "unknown_" + uniqueID++;
082: }
083:
084: initializeFromPropertiesFile();
085: }
086:
087: /**
088: * Returns the JMX implementation name (the name of this product).
089: *
090: * @return The JMX implementation name is returned
091: */
092: public String getImplementationName() {
093: if (properties.getProperty("IMPL_NAME") != null)
094: return properties.getProperty("IMPL_NAME");
095: else
096: return implProductName;
097: }
098:
099: /**
100: * Returns the JMX implementation vendor (the vendor of this product)
101: *
102: * @return The JMX implementation vendor is returned
103: */
104: public String getImplementationVendor() {
105: if (properties.getProperty("VENDOR_NAME") != null)
106: return properties.getProperty("VENDOR_NAME");
107: else
108: return implVendorName;
109: }
110:
111: /**
112: * Returns the JMX implementation version (the version of this product).
113: *
114: * @return The JMX implementation version is returned
115: */
116: public String getImplementationVersion() {
117: if (properties.getProperty("VERSION") != null)
118: return properties.getProperty("VERSION");
119: else
120: return implVersion;
121: }
122:
123: /**
124: * Get the MBeanServer agent identification.
125: *
126: * @return MBeanServer ID is returned
127: */
128: public java.lang.String getMBeanServerId() {
129: return serverId;
130: }
131:
132: /**
133: * Returns the full name of the JMX specification implemented by this
134: * product.
135: *
136: * @return The name of the JMX specification implemented by
137: * this product is returned
138: */
139: public String getSpecificationName() {
140: if (properties.getProperty("SPEC_NAME") != null)
141: return properties.getProperty("SPEC_NAME");
142: else
143: return specName;
144: }
145:
146: /**
147: * Returns the vendor of the JMX specification implemented by this product.
148: *
149: * @return This returns the vendor of the JMX specification
150: * implemented by this product
151: */
152: public String getSpecificationVendor() {
153: if (properties.getProperty("SPEC_VENDOR") != null)
154: return properties.getProperty("SPEC_VENDOR");
155: else
156: return specVendorName;
157: }
158:
159: /**
160: * Returns the version of the JMX specification implemented by this product.
161: *
162: * @return The version of the JMX specification implemented by
163: * this product is returned
164: */
165: public String getSpecificationVersion() {
166: if (properties.getProperty("SPEC_VERSION") != null)
167: return properties.getProperty("SPEC_VERSION");
168: else
169: return specVersion;
170: }
171:
172: /**
173: * Enables a couple (listener,handback) for a registered MBean to be added.
174: * Specified by:
175: * addNotificationListener in interface NotificationBroadcaster
176: *
177: * param listener The listener object which will handles notifications
178: * emitted by the registered MBean.
179: *
180: * @param filter The filter object. If not specified, no filtering
181: * will be performed before
182: *
183: * @param listener handling notifications.
184: *
185: * @param handback The context to be sent to the listener when a
186: * notification is emitted.
187: *
188: * @throws java.lang.IllegalArgumentException - Listener parameter is null.
189: */
190: public void addNotificationListener(NotificationListener listener,
191: NotificationFilter filter, Object handback)
192: throws IllegalArgumentException {
193: if (listener == null)
194: throw new java.lang.IllegalArgumentException();
195:
196: ArrayList[] obj = (ArrayList[]) notifTable.get(listener);
197: if (obj == null) {
198: obj = new ArrayList[2];
199: obj[0] = new ArrayList();
200: obj[1] = new ArrayList();
201: obj[0].add(filter);
202: obj[1].add(handback);
203:
204: notifTable.put(listener, obj);
205: } else {
206: boolean flag = false;
207: Object[] handbacks = obj[1].toArray();
208: for (int i = 0; i < handbacks.length; i++) {
209: if (handbacks[i].equals(handback)) {
210: flag = true;
211: break;
212: }
213: }
214:
215: if (!flag) {
216: obj[0].add(filter);
217: obj[1].add(handback);
218: }
219: }
220: }
221:
222: /**
223: * Enables a listener for an MBean to be removed. All couple (listener,
224: * handback) are removed.
225: *
226: * @param listener The listener object which will handles notifications
227: * emitted by the registered MBean.
228: *
229: * @throws ListenerNotFoundException The listener is not registered
230: * in the MBean.
231: */
232: //Specified by:
233: //removeNotificationListener in interface NotificationBroadcaster
234: public void removeNotificationListener(NotificationListener listener)
235: throws ListenerNotFoundException {
236: notifTable.remove(listener);
237: }
238:
239: /**
240: * Returns a NotificationInfo object contaning the name of the Java class
241: * of the notification and the notification types sent.
242: *
243: * @return This returns the array of MBeanNotificationInfo which
244: * contains the notification information
245: */
246: public MBeanNotificationInfo[] getNotificationInfo() {
247: MBeanNotificationInfo[] toRet = new MBeanNotificationInfo[info
248: .size()];
249: int i = 0;
250:
251: for (Enumeration e = info.keys(); e.hasMoreElements();) {
252: String key = (String) e.nextElement();
253: List list = (List) info.get(key);
254: toRet[i++] = new MBeanNotificationInfo((String[]) list
255: .toArray(), key, "Notification description");
256: }
257:
258: return toRet;
259: }
260:
261: /**
262: * Enables the MBean server to send a notification.
263: *
264: * @param notif The notification to send.
265: */
266: public void sendNotification(Notification notif) {
267: for (Enumeration e = notifTable.keys(); e.hasMoreElements();) {
268: NotificationListener nl = (NotificationListener) e
269: .nextElement();
270:
271: ArrayList[] list = (ArrayList[]) notifTable.get(nl);
272:
273: Object[] filters = list[0].toArray();
274: Object[] handbacks = list[1].toArray();
275:
276: try {
277: for (int i = 0; i < filters.length; i++) {
278: NotificationHandler hdlr = new NotificationHandler(
279: (NotificationFilter) filters[i], nl,
280: handbacks[i], notif, info);
281: Thread th = new Thread(hdlr);
282: th.start();
283: }
284: } catch (Exception ex) {
285: }
286: }
287: }
288:
289: ////////////////////////////// Package Methods /////////////////////////////
290:
291: class NotificationHandler implements Runnable {
292: NotificationFilter nf;
293: NotificationListener nl;
294: Object handback;
295: Notification notif;
296: Hashtable info;
297:
298: NotificationHandler(NotificationFilter nf,
299: NotificationListener nl, Object handback,
300: Notification notif, Hashtable info) {
301: this .nf = nf;
302: this .nl = nl;
303: this .handback = handback;
304: this .notif = notif;
305: this .info = info;
306: }
307:
308: public void run() {
309: if (nf != null) {
310: if (nf.isNotificationEnabled(notif)) {
311: nl.handleNotification(notif, handback);
312:
313: String name = notif.getClass().getName();
314: List alist = (List) info.get(name);
315:
316: if (alist == null) {
317: alist = new Vector();
318: alist.add(notif.getType());
319:
320: info.put(name, alist);
321: } else
322: alist.add(notif.getType());
323: }
324: } else
325: nl.handleNotification(notif, handback);
326: }//End run()
327: }
328:
329: ////////////////////////////// Private Methods /////////////////////////////
330:
331: /**
332: * This method returns a random integer
333: */
334: private int generateRandomInt(int prevId) {
335: int random;
336: random = ++prevId;
337: random &= 0x7FFFFFFF;
338:
339: if (random > 0x3FFFFFFF) {
340: random = random - 0x3FFFFFFF;
341: }
342:
343: return random;
344: }
345:
346: private void initializeFromPropertiesFile() {
347: properties = new Properties();
348:
349: InputStream in = MBeanServerDelegate.class
350: .getResourceAsStream("/delegate.properties");
351:
352: try {
353: properties.load(in);
354: } catch (Exception e) {
355: System.out
356: .println("Could Not load entries from the Property File delegate.properties ");
357: }
358: }
359: }
|