KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > jndi > JndiMBeanManager


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 /*
25  * JndiMBeanManager.java
26  *
27  * Created on March 9, 2004, 1:46 PM
28  */

29
30 package com.sun.enterprise.admin.monitor.jndi;
31
32 import com.sun.enterprise.admin.common.constant.AdminConstants;
33 import com.sun.enterprise.util.i18n.StringManager;
34 import java.util.logging.Logger JavaDoc;
35 import javax.management.MBeanServer JavaDoc;
36 import javax.management.MBeanServerFactory JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38
39 /**
40  * The JndiMBeanManager is responsible for registering and
41  * unregistering the JndiMBean.
42  *
43  * @author Rob Ruyak
44  */

45 public class JndiMBeanManager {
46     
47     /** MBeanServer for registering/unregistering JndiMBean **/
48     private MBeanServer JavaDoc server;
49     private static final Logger JavaDoc logger =
50         Logger.getLogger(AdminConstants.kLoggerName);
51     private static final StringManager sm =
52         StringManager.getManager(JndiMBeanManager.class);
53     
54     /** Creates a new instance of JndiMBeanManager */
55     public JndiMBeanManager() {
56         server = this.getMBeanServer();
57     }
58     
59     /**
60      * Registers the JndiMBean.
61      *
62      * @param objectName The objectName of the JndiMBean.
63      */

64     public void registerMBean(ObjectName JavaDoc objectName) {
65         try {
66             if (server.isRegistered(objectName)) {
67                 logger.fine(sm.getString("monitor.jndi.already_registered",
68                     new Object JavaDoc[] {objectName.toString()}));
69                 return;
70             }
71             server.registerMBean(new JndiMBeanImpl(), objectName);
72             logger.finer(sm.getString("monitor.jndi.registered",
73                     new Object JavaDoc[]{objectName.toString()}));
74         } catch(Exception JavaDoc e) {
75             logger.fine(sm.getString("monitor.jndi.register_exception",
76                     new Object JavaDoc[]{objectName}));
77             logger.throwing(JndiMBeanManager.class.getName(),
78                     "registerMBean()", e);
79         }
80     }
81     
82     /**
83      * Unegisters the JndiMBean.
84      *
85      * @param objectName The objectName of the JndiMBean.
86      */

87     public void unregisterMBean(ObjectName JavaDoc objectName) {
88         try {
89             if (server.isRegistered(objectName)) {
90                 server.unregisterMBean(objectName);
91                 logger.fine(sm.getString("monitor.jndi.unregistered",
92                         new Object JavaDoc[]{objectName.toString()}));
93             } else {
94                 logger.fine(sm.getString("monitor.jndi.never_registered",
95                         new Object JavaDoc[]{objectName.toString()}));
96             }
97         } catch(Exception JavaDoc e) {
98             logger.fine(sm.getString("monitor.jndi.register_failed",
99                     new Object JavaDoc[]{objectName.toString()}));
100         }
101     }
102     
103     /**
104      * Gets the MBeanServer for use within the JndiMBeanManager.
105      *
106      * @return The MBeanServer used to register the mbean.
107      */

108     MBeanServer JavaDoc getMBeanServer() {
109         MBeanServer JavaDoc server = null;
110         java.util.ArrayList JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
111         if(!servers.isEmpty()){
112             server = (MBeanServer JavaDoc)servers.get(0);
113         }
114         return server;
115     }
116 }
117
Popular Tags