Package org.apache.tools.ant.taskdefs.optional.jmx.connector.weblogic

Source Code of org.apache.tools.ant.taskdefs.optional.jmx.connector.weblogic.Connector

package org.apache.tools.ant.taskdefs.optional.jmx.connector.weblogic;

/*
* ============================================================================
*                   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 weblogic.management.MBeanHome;



/**
*
* WebLogic specific implementation of JMXConnector.
* Allows Ant-JMX taskdefs to lookup WebLogic servers via JNDI
* and connect to a MBeanServer via t3 or an alternate
* protocol.
*
* @author  <a href="mailto:bdueck@yahoo.com">Brian Dueck</a>
* @version $Id: Connector.java,v 1.5 2003/05/28 22:28:26 bdueck Exp $
*/
public class Connector extends org.apache.tools.ant.taskdefs.optional.jmx.connector.AbstractJMXConnector {
   
    private MBeanHome home = null;
    private static final String DEFAULT_PROTOCOL = "t3";
    private static final String DEFAULT_HOST = "localhost";
    private static final int DEFAULT_PORT = 7001;
    private static final String DEFAULT_JNDI_NAME = "weblogic.management.adminhome";

    /** Creates a new instance of WebLogicAdaptor */
    public Connector() {
    }
   
    public Hashtable getInitialContextProperties(Hashtable contextProps) {
        if (!(contextProps.containsKey(Context.PROVIDER_URL))) {
            try {
                contextProps.put(Context.PROVIDER_URL,DEFAULT_PROTOCOL + "://"+java.net.InetAddress.getLocalHost().getHostName()+":"+DEFAULT_PORT);
            } catch (java.net.UnknownHostException eatMe) {
                contextProps.put(Context.PROVIDER_URL,DEFAULT_PROTOCOL + "://"+DEFAULT_HOST+":"+DEFAULT_PORT);
            }
        }
        contextProps.put(Context.INITIAL_CONTEXT_FACTORY, weblogic.jndi.WLInitialContextFactory.class.getName());
        return contextProps;
    }
   
  private String getJndiName(String jndiName) {
    if ( (jndiName == null) || (jndiName.length() == 0)) {
      return DEFAULT_JNDI_NAME;
    }
   
    return jndiName;
   
  }
 
    public MBeanServer getMBeanServer(final Context context, String jndiName) throws NamingException, org.apache.tools.ant.BuildException {
        String actualJndiName = getJndiName(jndiName);
        Object jndiEntry = (MBeanHome)context.lookup(actualJndiName);

        if (jndiEntry == null) {
            throw new NamingException("JNDI entry ["+actualJndiName+"] does not exist. Please check jndiName property.");
        }

        if (!(jndiEntry instanceof MBeanHome)) {
            throw new NamingException("JNDI entry ["+actualJndiName+"] is incorrect type. Was expecting [MBeanHome], found ["+jndiEntry.getClass().getName()+"] Please check jndiName property.");
        }

        home = (MBeanHome)jndiEntry;
       
    // ensure the TargetMbean value converter for WebLogic is registered
    // with the ValueFactory
    //
    org.apache.tools.ant.taskdefs.optional.jmx.converter.ValueFactory.getInstance().registerValueConverter(new WebLogicMBeanValueConverter(home));
       
        return new WebLogicMBeanServer(home.getMBeanServer());
    }
   
   
    public ObjectName createMBean(String type, ObjectName objectName, MBeanServer mbserver) throws MBeanRegistrationException {
        try {
            // try to create the MBean using the MBeanHome interface
            // first - this should succeed for built-in BEA WLS type
            // MBeans (e.g. JDBCConnectionPool etc.)
            //
            // if it fails, then we're probably trying to create a
            // user-debfined MBean, which we can create using the MBeanServer
            //
            String nameProp = objectName.getKeyProperty("Name");
            String domain = objectName.getDomain();
            return new ObjectName(home.createAdminMBean(nameProp,type,domain).getObjectName().getCanonicalName());
           
        } catch (Exception x) {
            try {               
                objectName.getKeyPropertyList().put("Type", type);
                return mbserver.createMBean(type,objectName).getObjectName();
            } catch (Exception ex) {
                // last try using type type argument
                //
                try {
                    return mbserver.createMBean(type,objectName).getObjectName();
                } catch (Exception exc) {               
                    throw new MBeanRegistrationException(exc);
                }
            }
        }
    }

    public String getActiveDomain(MBeanServer mbserver) {
        return home.getMBeanServer().getActiveDomain().getName();
    }   
   
   
}

/*
* $Log: Connector.java,v $
* Revision 1.5  2003/05/28 22:28:26  bdueck
* *** empty log message ***
*
* Revision 1.4  2003/05/26 22:08:01  bdueck
* Added special logic required to support WebLogic TargetMBean
* and other descendents of WebLogicMBean.
*
* Revision 1.3  2003/04/21 15:29:42  bdueck
* Various changes in preparation for version 1.2.
*
*
*/
TOP

Related Classes of org.apache.tools.ant.taskdefs.optional.jmx.connector.weblogic.Connector

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.