KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.tools.ant.taskdefs.optional.jmx.connector.weblogic;
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
55 import javax.management.MBeanRegistrationException JavaDoc;
56 import javax.management.MBeanServer JavaDoc;
57 import javax.management.ObjectName JavaDoc;
58 import javax.naming.Context JavaDoc;
59 import javax.naming.NamingException JavaDoc;
60
61 import weblogic.management.MBeanHome;
62
63
64
65 /**
66  *
67  * WebLogic specific implementation of JMXConnector.
68  * Allows Ant-JMX taskdefs to lookup WebLogic servers via JNDI
69  * and connect to a MBeanServer via t3 or an alternate
70  * protocol.
71  *
72  * @author <a HREF="mailto:bdueck@yahoo.com">Brian Dueck</a>
73  * @version $Id: Connector.java,v 1.5 2003/05/28 22:28:26 bdueck Exp $
74  */

75 public class Connector extends org.apache.tools.ant.taskdefs.optional.jmx.connector.AbstractJMXConnector {
76     
77     private MBeanHome home = null;
78     private static final String JavaDoc DEFAULT_PROTOCOL = "t3";
79     private static final String JavaDoc DEFAULT_HOST = "localhost";
80     private static final int DEFAULT_PORT = 7001;
81     private static final String JavaDoc DEFAULT_JNDI_NAME = "weblogic.management.adminhome";
82
83     /** Creates a new instance of WebLogicAdaptor */
84     public Connector() {
85     }
86     
87     public Hashtable JavaDoc getInitialContextProperties(Hashtable JavaDoc contextProps) {
88         if (!(contextProps.containsKey(Context.PROVIDER_URL))) {
89             try {
90                 contextProps.put(Context.PROVIDER_URL,DEFAULT_PROTOCOL + "://"+java.net.InetAddress.getLocalHost().getHostName()+":"+DEFAULT_PORT);
91             } catch (java.net.UnknownHostException JavaDoc eatMe) {
92                 contextProps.put(Context.PROVIDER_URL,DEFAULT_PROTOCOL + "://"+DEFAULT_HOST+":"+DEFAULT_PORT);
93             }
94         }
95         contextProps.put(Context.INITIAL_CONTEXT_FACTORY, weblogic.jndi.WLInitialContextFactory.class.getName());
96         return contextProps;
97     }
98     
99     private String JavaDoc getJndiName(String JavaDoc jndiName) {
100         if ( (jndiName == null) || (jndiName.length() == 0)) {
101             return DEFAULT_JNDI_NAME;
102         }
103         
104         return jndiName;
105         
106     }
107     
108     public MBeanServer JavaDoc getMBeanServer(final Context JavaDoc context, String JavaDoc jndiName) throws NamingException JavaDoc, org.apache.tools.ant.BuildException {
109         String JavaDoc actualJndiName = getJndiName(jndiName);
110         Object JavaDoc jndiEntry = (MBeanHome)context.lookup(actualJndiName);
111
112         if (jndiEntry == null) {
113             throw new NamingException JavaDoc("JNDI entry ["+actualJndiName+"] does not exist. Please check jndiName property.");
114         }
115
116         if (!(jndiEntry instanceof MBeanHome)) {
117             throw new NamingException JavaDoc("JNDI entry ["+actualJndiName+"] is incorrect type. Was expecting [MBeanHome], found ["+jndiEntry.getClass().getName()+"] Please check jndiName property.");
118         }
119
120         home = (MBeanHome)jndiEntry;
121         
122         // ensure the TargetMbean value converter for WebLogic is registered
123
// with the ValueFactory
124
//
125
org.apache.tools.ant.taskdefs.optional.jmx.converter.ValueFactory.getInstance().registerValueConverter(new WebLogicMBeanValueConverter(home));
126         
127         return new WebLogicMBeanServer(home.getMBeanServer());
128     }
129     
130     
131     public ObjectName JavaDoc createMBean(String JavaDoc type, ObjectName JavaDoc objectName, MBeanServer JavaDoc mbserver) throws MBeanRegistrationException JavaDoc {
132         try {
133             // try to create the MBean using the MBeanHome interface
134
// first - this should succeed for built-in BEA WLS type
135
// MBeans (e.g. JDBCConnectionPool etc.)
136
//
137
// if it fails, then we're probably trying to create a
138
// user-debfined MBean, which we can create using the MBeanServer
139
//
140
String JavaDoc nameProp = objectName.getKeyProperty("Name");
141             String JavaDoc domain = objectName.getDomain();
142             return new ObjectName JavaDoc(home.createAdminMBean(nameProp,type,domain).getObjectName().getCanonicalName());
143             
144         } catch (Exception JavaDoc x) {
145             try {
146                 objectName.getKeyPropertyList().put("Type", type);
147                 return mbserver.createMBean(type,objectName).getObjectName();
148             } catch (Exception JavaDoc ex) {
149                 // last try using type type argument
150
//
151
try {
152                     return mbserver.createMBean(type,objectName).getObjectName();
153                 } catch (Exception JavaDoc exc) {
154                     throw new MBeanRegistrationException JavaDoc(exc);
155                 }
156             }
157         }
158     }
159
160     public String JavaDoc getActiveDomain(MBeanServer JavaDoc mbserver) {
161         return home.getMBeanServer().getActiveDomain().getName();
162     }
163     
164     
165 }
166
167 /*
168  * $Log: Connector.java,v $
169  * Revision 1.5 2003/05/28 22:28:26 bdueck
170  * *** empty log message ***
171  *
172  * Revision 1.4 2003/05/26 22:08:01 bdueck
173  * Added special logic required to support WebLogic TargetMBean
174  * and other descendents of WebLogicMBean.
175  *
176  * Revision 1.3 2003/04/21 15:29:42 bdueck
177  * Various changes in preparation for version 1.2.
178  *
179  *
180  */
Popular Tags