KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JndiMBeanHelper.java
26  *
27  * Created on March 10, 2004, 9:36 AM
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.ArrayList JavaDoc;
35 import java.util.logging.Level JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.naming.NameNotFoundException JavaDoc;
39 import javax.naming.NamingEnumeration JavaDoc;
40 import javax.naming.NamingException JavaDoc;
41
42 /**
43  * The JndiMBeanHelper serves as a delegator object for the JndiMBeanImpl
44  * for interacting with the application server's naming service. The client
45  * of the JndiMBean will call the getNames method which in turn calls this
46  * object's getJndiEntriesByContextPath to retrieve all those names bound
47  * in that naming service's subcontext.
48  *
49  * @author Rob Ruyak
50  */

51 public class JndiMBeanHelper {
52     
53     private InitialContext JavaDoc context;
54     private static final Logger JavaDoc logger =
55         Logger.getLogger(AdminConstants.kLoggerName);
56     private static final StringManager sm =
57         StringManager.getManager(JndiMBeanHelper.class);
58     private final String JavaDoc SYSTEM_SUBCONTEXT = "__SYSTEM";
59     
60     /** Creates a new instance of JndiMBeanHelper */
61     public JndiMBeanHelper() {
62         initialize();
63     }
64     
65     /**
66      * Initializes the JndiMBeanHelper object upon creation. It specifically
67      * creates an InitialContext instance for querying the naming service
68      * during certain method invocations.
69      */

70     void initialize() {
71         try {
72             context = new InitialContext JavaDoc();
73         } catch(javax.naming.NamingException JavaDoc e) {
74             logger.log(Level.WARNING, e.getMessage(), e);
75         }
76     }
77     
78     /**
79      * Gets the jndi entries from the application server's naming
80      * service given a particular context/subcontext.
81      *
82      * @param contextPath The naming context or subcontext to query.
83      * @return An {@link ArrayList} of {@link NameClassPair} objects.
84      * @throws NamingException if an error occurs when connection with
85      * the naming service is established or retrieval fails.
86      */

87     ArrayList JavaDoc getJndiEntriesByContextPath(String JavaDoc contextPath)
88             throws NamingException JavaDoc {
89         ArrayList JavaDoc names = new ArrayList JavaDoc();
90         NamingEnumeration JavaDoc ee = null;
91         if(contextPath == null) { contextPath = ""; }
92         try {
93             ee = context.list(contextPath);
94         } catch(NameNotFoundException JavaDoc e) {
95             String JavaDoc msg = sm.getString("monitor.jndi.context_notfound",
96                 new Object JavaDoc[]{contextPath});
97             logger.log(Level.WARNING, msg);
98             NamingException JavaDoc ne = new NamingException JavaDoc(msg);
99             //ne.initCause(e);
100
System.out.println("Exception print: " + ne);
101             throw ne;
102         }
103         names = toNameClassPairArray(ee);
104         return names;
105     }
106     
107     /**
108      * Changes a NamingEnumeration object into an ArrayList of
109      * NameClassPair objects.
110      *
111      * @param ee An {@link NamingEnumeration} object to be transformed.
112      * @return An {@link ArrayList} of {@link NameClassPair} objects.
113      * @throws Exception if an error occurs when iterating over the enum.
114      */

115     ArrayList JavaDoc toNameClassPairArray(NamingEnumeration JavaDoc ee)
116             throws javax.naming.NamingException JavaDoc{
117         ArrayList JavaDoc names = new ArrayList JavaDoc();
118         while(ee.hasMore()) {
119             // don't add the __SYSTEM subcontext - Fix for 6041360
120
Object JavaDoc o = ee.next();
121             if(o.toString().indexOf(SYSTEM_SUBCONTEXT) == -1) {
122                 names.add(o);
123             }
124         }
125         return names;
126     }
127 }
128
Popular Tags