KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > jmx > connector > AbstractJMXConnector


1 package org.apache.tools.ant.taskdefs.optional.jmx.connector;
2
3 /*
4  * ============================================================================
5  * The Apache Software License, Version 1.1
6  * ============================================================================
7  *
8  * Copyright (C) 2000-2002 The Apache Software Foundation. All
9  * rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modifica-
12  * tion, are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any, must
22  * include the following acknowledgment: "This product includes software
23  * developed by the Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself, if
25  * and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Ant" and "Apache Software Foundation" must not be used to
28  * endorse or promote products derived from this software without prior
29  * written permission. For written permission, please contact
30  * apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache", nor may
33  * "Apache" appear in their name, without prior written permission of the
34  * Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
37  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
38  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
39  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
40  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
41  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
42  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
45  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  * This software consists of voluntary contributions made by many individuals
48  * on behalf of the Apache Software Foundation. For more information on the
49  * Apache Software Foundation, please see <http://www.apache.org/>.
50  *
51  */

52
53 import java.util.Hashtable JavaDoc;
54 import javax.management.MBeanRegistrationException JavaDoc;
55 import javax.management.MBeanServer JavaDoc;
56 import javax.management.ObjectName JavaDoc;
57 import javax.naming.Context JavaDoc;
58 import javax.naming.NamingException JavaDoc;
59 import org.apache.tools.ant.BuildException;
60
61
62
63 /**
64  * Abstract base implementation of JMXConnector for JMX server types that
65  * are located through JNDI.
66  *
67  * @author <a HREF="mailto:bdueck@yahoo.com">Brian Dueck</a>
68  * @version $Id: AbstractJMXConnector.java,v 1.5 2003/05/28 22:28:26 bdueck Exp $
69  */

70 public abstract class AbstractJMXConnector implements JMXConnector {
71     
72     /** Creates a new instance of AbstractJMXConnector */
73     public AbstractJMXConnector() {
74     }
75     
76     public Hashtable JavaDoc getInitialContextProperties(String JavaDoc providerUrl, String JavaDoc user, String JavaDoc password) {
77         Hashtable JavaDoc properties = new Hashtable JavaDoc();
78
79         if (providerUrl != null) {
80             properties.put(Context.PROVIDER_URL,providerUrl);
81         }
82
83         if (user != null) {
84             properties.put(Context.SECURITY_PRINCIPAL,user);
85         }
86
87         if (password != null) {
88             properties.put(Context.SECURITY_CREDENTIALS,password);
89         }
90         
91         return getInitialContextProperties(properties);
92         
93     }
94     
95     
96     /**
97      * This implementation simply calls mbserver.createMBean().
98      */

99     public ObjectName JavaDoc createMBean(String JavaDoc type, ObjectName JavaDoc objectName, MBeanServer JavaDoc mbserver) throws MBeanRegistrationException JavaDoc {
100         try {
101             return mbserver.createMBean(type,objectName).getObjectName();
102         } catch (Exception JavaDoc ex) {
103             throw new MBeanRegistrationException JavaDoc(ex);
104         }
105     }
106     
107     /**
108      * This implementation returns mbserver.getDefaultDomain()
109      * as the active/default domain.
110      *
111      */

112     public String JavaDoc getActiveDomain(MBeanServer JavaDoc mbserver) {
113         return mbserver.getDefaultDomain();
114     }
115
116     /**
117      * This implementation returns the contextProps unmodified.
118      *
119      */

120     public Hashtable JavaDoc getInitialContextProperties(Hashtable JavaDoc contextProps) {
121         return contextProps;
122     }
123     
124     public MBeanServer JavaDoc getMBeanServer(Hashtable JavaDoc contextProps, String JavaDoc jndiLookupName) throws BuildException {
125         Context JavaDoc context = null;
126         try {
127             context = new javax.naming.InitialContext JavaDoc(contextProps);
128
129             return getMBeanServer(context,jndiLookupName);
130         } catch (NamingException JavaDoc x) {
131             String JavaDoc message = x.getMessage();
132             if (message == null) {
133                 if (x.getRootCause() != null) {
134                     message = x.getRootCause().getMessage();
135                 }
136             } else {
137                 message = x.toString();
138             }
139             throw new BuildException(message);
140         } finally {
141             try {
142                 if (context != null) {
143                     context.close();
144                 }
145             } catch (NamingException JavaDoc eatMe) {
146                 // intentionally ignoring this message
147
System.err.println("Warning! Could not close naming context. " + eatMe);
148             }
149         }
150     }
151     
152     /** Connects to a JMX server and returns the MBeanServer to
153      * use during task execution. The caller is responsible
154      * for invoking this method prior to invoking createMBean()
155      * or getActiveDomain() methods on this interface.
156      *
157      * The getInitialContextProperties() method will be invoked
158      * prior to this method.
159      *
160      * @param context The JNDI naming context to use to lookup
161      * the jndiLookupName entry. The caller is responsible for
162      * closing this context.
163      * @param jndiLookupName The JNDI entry name for the MBeanServer.
164      * @raise BuildException if an error occurs attempting to login.
165      */

166     public abstract MBeanServer JavaDoc getMBeanServer(Context JavaDoc context, String JavaDoc jndiLookupName) throws NamingException JavaDoc, BuildException;
167     
168 }
169
170 /*
171  * $Log: AbstractJMXConnector.java,v $
172  * Revision 1.5 2003/05/28 22:28:26 bdueck
173  * *** empty log message ***
174  *
175  * Revision 1.4 2003/05/14 14:12:26 bdueck
176  * Get root exception message if the default exception message is null.
177  *
178  * Revision 1.3 2003/04/21 15:29:39 bdueck
179  * Various changes in preparation for version 1.2.
180  *
181  * Revision 1.2 2003/04/01 22:14:06 bdueck
182  * Added convenience version of getInitialContextProperties().
183  *
184  * Revision 1.1 2003/01/17 12:34:00 bdueck
185  * Initial check-in
186  *
187  *
188  */

189
Popular Tags