package org.apache.tools.ant.taskdefs.optional.jmx.connector;
/*
* ============================================================================
* The Apache Software License, Version 1.1
* ============================================================================
*
* Copyright (C) 2000-2002 The Apache Software Foundation. All
* rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
* include the following acknowledgment: "This product includes software
* developed by the Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself, if
* and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Ant" and "Apache Software Foundation" must not be used to
* endorse or promote products derived from this software without prior
* written permission. For written permission, please contact
* apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache", nor may
* "Apache" appear in their name, without prior written permission of the
* Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
* DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* on behalf of the Apache Software Foundation. For more information on the
* Apache Software Foundation, please see <http://www.apache.org/>.
*
*/
import java.util.Hashtable;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.naming.Context;
import javax.naming.NamingException;
import org.apache.tools.ant.BuildException;
/**
* Abstract base implementation of JMXConnector for JMX server types that
* are located through JNDI.
*
* @author <a href="mailto:bdueck@yahoo.com">Brian Dueck</a>
* @version $Id: AbstractJMXConnector.java,v 1.5 2003/05/28 22:28:26 bdueck Exp $
*/
public abstract class AbstractJMXConnector implements JMXConnector {
/** Creates a new instance of AbstractJMXConnector */
public AbstractJMXConnector() {
}
public Hashtable getInitialContextProperties(String providerUrl, String user, String password) {
Hashtable properties = new Hashtable();
if (providerUrl != null) {
properties.put(Context.PROVIDER_URL,providerUrl);
}
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL,user);
}
if (password != null) {
properties.put(Context.SECURITY_CREDENTIALS,password);
}
return getInitialContextProperties(properties);
}
/**
* This implementation simply calls mbserver.createMBean().
*/
public ObjectName createMBean(String type, ObjectName objectName, MBeanServer mbserver) throws MBeanRegistrationException {
try {
return mbserver.createMBean(type,objectName).getObjectName();
} catch (Exception ex) {
throw new MBeanRegistrationException(ex);
}
}
/**
* This implementation returns mbserver.getDefaultDomain()
* as the active/default domain.
*
*/
public String getActiveDomain(MBeanServer mbserver) {
return mbserver.getDefaultDomain();
}
/**
* This implementation returns the contextProps unmodified.
*
*/
public Hashtable getInitialContextProperties(Hashtable contextProps) {
return contextProps;
}
public MBeanServer getMBeanServer(Hashtable contextProps, String jndiLookupName) throws BuildException {
Context context = null;
try {
context = new javax.naming.InitialContext(contextProps);
return getMBeanServer(context,jndiLookupName);
} catch (NamingException x) {
String message = x.getMessage();
if (message == null) {
if (x.getRootCause() != null) {
message = x.getRootCause().getMessage();
}
} else {
message = x.toString();
}
throw new BuildException(message);
} finally {
try {
if (context != null) {
context.close();
}
} catch (NamingException eatMe) {
// intentionally ignoring this message
System.err.println("Warning! Could not close naming context. " + eatMe);
}
}
}
/** Connects to a JMX server and returns the MBeanServer to
* use during task execution. The caller is responsible
* for invoking this method prior to invoking createMBean()
* or getActiveDomain() methods on this interface.
*
* The getInitialContextProperties() method will be invoked
* prior to this method.
*
* @param context The JNDI naming context to use to lookup
* the jndiLookupName entry. The caller is responsible for
* closing this context.
* @param jndiLookupName The JNDI entry name for the MBeanServer.
* @raise BuildException if an error occurs attempting to login.
*/
public abstract MBeanServer getMBeanServer(Context context, String jndiLookupName) throws NamingException, BuildException;
}
/*
* $Log: AbstractJMXConnector.java,v $
* Revision 1.5 2003/05/28 22:28:26 bdueck
* *** empty log message ***
*
* Revision 1.4 2003/05/14 14:12:26 bdueck
* Get root exception message if the default exception message is null.
*
* Revision 1.3 2003/04/21 15:29:39 bdueck
* Various changes in preparation for version 1.2.
*
* Revision 1.2 2003/04/01 22:14:06 bdueck
* Added convenience version of getInitialContextProperties().
*
* Revision 1.1 2003/01/17 12:34:00 bdueck
* Initial check-in
*
*
*/