package org.apache.tools.ant.taskdefs.optional.jmx;
/*
* ============================================================================
* 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 org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.optional.jmx.connector.JMXConnector;
/**
* This is task allows a lookup to be performed within a JNDI tree.
* If the lookup is successful, the specified property will be set
* with a value. The result of the lookup is stored in a reference
* called named refid.
*
* Refer to the user documentation for more information and examples on how to use
* this task.
*
* @author <a href="mailto:bdueck@yahoo.com">Brian Dueck</a>
* @version $Id: JndiLookup.java,v 1.3 2003/05/28 22:28:26 bdueck Exp $
*
*/
public class JndiLookup extends AbstractManagementTask {
private String property = null;
private String refid = null;
public JndiLookup() {
}
/**
* Execute the task.
*
* @throws BuildException If an error occurs.
*/
public void execute() throws BuildException {
try {
javax.naming.Context namingContext = getNamingContext(
getProviderUrl(),
getJndiName(),
getUser(),
getPassword());
try {
Object result = namingContext.lookup(this.getJndiName());
if (getProperty() != null) {
getProject().setProperty(getProperty(),"true");
}
if (getRefid() != null) {
getProject().addReference(getRefid(),result);
}
} catch (javax.naming.NamingException ne) {
if (this.getFailOnError()) {
ne.printStackTrace();
throw new BuildException("Error! " + ne, ne);
}
}
} catch (Exception x) {
if (this.getFailOnError()) {
x.printStackTrace();
throw new BuildException("Error! " + x, x);
} else {
log("Warning! " + x,org.apache.tools.ant.Project.MSG_WARN);
}
}
}
/**
* Logs into a JMX server and returns the MBeanServer to
* use during task execution. The caller is responsible
* for invoking this method prior to executing any other
* methods on this interface.
*
* @param providerUrl The url of the JNDI provider.
* @param jndiName The JNDI name of the MBeanServer.
* @param user The user name to use for authentication.
* @param password The password to use for authentication.
* @return An MBeanServer.
* @raise BuildException if an error occurs attempting to login.
*/
protected javax.naming.Context getNamingContext(String providerUrl, String jndiName, String user, String password) throws javax.naming.NamingException {
try {
if (getContext().getServerType() == null) {
log("Warning! serverType not specified or unrecognized type. Defaulting to WebLogic.");
getContext().setServerType("weblogic");
}
JMXConnector jmxServer = org.apache.tools.ant.taskdefs.optional.jmx.connector.JMXConnectorFactory.createConnector(getContext().getServerType());
java.util.Hashtable properties = jmxServer.getInitialContextProperties(providerUrl,user,password);
return new javax.naming.InitialContext(properties);
} catch (Exception e) {
e.printStackTrace();
throw new BuildException("JNDI Error. " + e.getMessage(), e);
}
}
/** Getter for property.
* @return Value of property.
*
*/
public java.lang.String getProperty() {
return property;
}
/** Setter for property.
* @param property New value of property.
*
*/
public void setProperty(java.lang.String property) {
this.property = property;
}
/** Getter for property refid.
* @return Value of property refid.
*
*/
public java.lang.String getRefid() {
return refid;
}
/** Setter for property refid.
* @param refid New value of property refid.
*
*/
public void setRefid(java.lang.String refid) {
this.refid = refid;
}
}
/*
* $Log: JndiLookup.java,v $
* Revision 1.3 2003/05/28 22:28:26 bdueck
* *** empty log message ***
*
* Revision 1.2 2003/04/21 15:29:41 bdueck
* Various changes in preparation for version 1.2.
*
* Revision 1.1 2003/04/01 22:12:03 bdueck
* Initial revision.
*
* Revision 1.1 2003/01/17 12:33:55 bdueck
* Initial check-in
*
*
*/