KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > event > MBeanLocatorImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.event;
25
26 import java.util.Set JavaDoc;
27 import com.sun.enterprise.admin.common.MBeanServerFactory;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.QueryExp JavaDoc;
30 import javax.management.MalformedObjectNameException JavaDoc;
31
32 import com.sun.enterprise.admin.common.constant.AdminConstants;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36
37 /**
38  * Implementation of mbeanLocator by using QueryNames
39  *
40  * @author Satish Viswanatham
41  */

42 public class MBeanLocatorImpl implements MBeanLocator{
43
44     static Logger JavaDoc logger = Logger.getLogger(AdminConstants.kLoggerName);
45
46     static final String JavaDoc UNSUPPORTED_TYPE = "event.event_key_type_not_supported";
47
48     static final String JavaDoc MALFORMED_OBJECT_KEY = "event.event_key_is_malformed";
49
50     /**
51      * This method returns the mbean of interest for the event/notification
52      *
53      * @param Object object key -- it could EventKey, ObjectName or String
54      * @return Object proxy for the mbean that is of interest
55      */

56     public Object JavaDoc locate(Object JavaDoc objectKey) {
57                  
58         ObjectName JavaDoc on = null;
59         QueryExp JavaDoc e = null;
60
61         if ( objectKey instanceof EventKey ) {
62             EventKey eKey = (EventKey) objectKey;
63             on = eKey.getObjectName();
64             e = eKey.getQuery();
65         } else if ( objectKey instanceof ObjectName JavaDoc) {
66             on = (ObjectName JavaDoc) objectKey;
67         } else if ( objectKey instanceof String JavaDoc ) {
68             try {
69                 on = new ObjectName JavaDoc( (String JavaDoc) objectKey);
70             } catch ( Exception JavaDoc exp ) {
71                logger.log(Level.WARNING, MALFORMED_OBJECT_KEY, objectKey);
72                return null;
73             }
74         } else {
75             if ( objectKey != null )
76                 logger.log(Level.WARNING, UNSUPPORTED_TYPE, objectKey.getClass().getName());
77             return null;
78         }
79
80         return MBeanServerFactory.getMBeanServer().queryMBeans(on, e);
81     }
82
83     public static void main (String JavaDoc[] args) {
84
85         /* fix this later so that
86          * local can take MBeanServerproxy can be tested that way
87          */

88         if ( args.length < 1 )
89         {
90             System.out.println("Usage: <object name> ");
91             System.exit(3);
92         }
93
94         String JavaDoc name = args[0];
95         System.out.println("name is " + args[0]);
96         MBeanLocatorImpl m = new MBeanLocatorImpl();
97         Set JavaDoc s = null;
98         try {
99            s = (Set JavaDoc)m.locate(new ObjectName JavaDoc(name));
100         } catch (MalformedObjectNameException JavaDoc e ) {
101             System.out.println("Please enter a valid object name ");
102             System.exit(1);
103         } catch ( NullPointerException JavaDoc e) {
104             System.out.println("Please enter a non null object name ");
105             System.exit(2);
106         }
107         System.out.println(" The number of beans matched that description " + s.size());
108         
109     }
110
111 }
112
Popular Tags