KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.tools.ant.taskdefs.optional.jmx.connector.jboss.ejb;
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
54 import java.net.InetAddress JavaDoc;
55 import java.net.UnknownHostException JavaDoc;
56 import java.util.Hashtable JavaDoc;
57 import javax.management.MBeanServer JavaDoc;
58 import javax.naming.Context JavaDoc;
59 import javax.naming.NamingException JavaDoc;
60 import org.apache.tools.ant.BuildException;
61 import org.apache.tools.ant.taskdefs.optional.jmx.connector.AbstractJMXConnector;
62 import org.jboss.jmx.adaptor.interfaces.AdaptorHome;
63 import org.jboss.jmx.connector.ejb.EJBConnector;
64 import org.jboss.security.SecurityAssociation;
65 import org.jboss.security.SimplePrincipal;
66 import org.jnp.interfaces.NamingContextFactory;
67
68
69
70 /**
71  * JBoss specific implementation of JMXConnector.
72  * Allows Ant-JMX taskdefs to lookup JBoss servers via JNDI
73  * and connect to a MBeanServer using the EJB JMX adaptor.
74  *
75  * @author <a HREF="mailto:Eric.Jain@isb-sib.ch">Eric Jain</a>
76  * @version $Id: Connector.java,v 1.2 2003/05/26 10:12:23 bdueck Exp $
77  */

78
79 public class Connector
80     extends AbstractJMXConnector
81 {
82     private static final String JavaDoc DEFAULT_PROTOCOL = "jnp";
83     private static final String JavaDoc DEFAULT_HOST = "localhost";
84     private static final int DEFAULT_PORT = 1099;
85     private static final String JavaDoc DEFAULT_JNDI_NAME = "ejb/jmx/ejb/Adaptor";
86     
87     
88     public Hashtable JavaDoc getInitialContextProperties(Hashtable JavaDoc contextProps)
89     {
90         if (!contextProps.containsKey(Context.PROVIDER_URL))
91         {
92             String JavaDoc hostname = null;
93             
94             try
95             {
96                 hostname = InetAddress.getLocalHost().getHostName();
97             }
98             
99             catch (UnknownHostException JavaDoc e)
100             {
101                 hostname = DEFAULT_HOST;
102             }
103             
104             contextProps.put(Context.PROVIDER_URL,
105                 DEFAULT_PROTOCOL + "://" + hostname + ":" + DEFAULT_PORT);
106         }
107         
108         contextProps.put(Context.INITIAL_CONTEXT_FACTORY,
109             NamingContextFactory.class.getName());
110         
111         contextProps.put(Context.URL_PKG_PREFIXES,
112             "org.jboss.naming:org.jnp.interfaces");
113         
114         return contextProps;
115     }
116     
117     
118     private String JavaDoc getJndiName(String JavaDoc jndiName) {
119         if (jndiName == null || jndiName.length() == 0) {
120             return DEFAULT_JNDI_NAME;
121         }
122         
123         return jndiName;
124     }
125     
126     public MBeanServer JavaDoc getMBeanServer(Hashtable JavaDoc contextProps, String JavaDoc jndiName)
127         throws BuildException {
128         setSecurity(contextProps);
129         return super.getMBeanServer(contextProps,jndiName);
130     }
131     
132     
133     public MBeanServer JavaDoc getMBeanServer(final Context JavaDoc context, String JavaDoc jndiName) throws NamingException JavaDoc, org.apache.tools.ant.BuildException {
134         String JavaDoc actualJndiName = getJndiName(jndiName);
135         Object JavaDoc jndiEntry = context.lookup(actualJndiName);
136     
137         try {
138             if (jndiEntry == null) {
139                 throw new NamingException JavaDoc("JNDI entry ["+actualJndiName+"] does not exist. Please check jndiName property.");
140             }
141     
142             if (!(jndiEntry instanceof AdaptorHome)) {
143                 throw new NamingException JavaDoc(
144                     "JNDI entry ["
145                         + actualJndiName
146                         + "] is incorrect type. Was expecting ["
147                         + AdaptorHome.class.getName()
148                         + "], found ["
149                         + jndiEntry.getClass().getName()
150                         + "] Please check jndiName property.");
151             }
152     
153             return new EJBConnector((AdaptorHome) jndiEntry);
154         } catch (Exception JavaDoc e) {
155             e.printStackTrace();
156             throw new BuildException(e);
157         }
158     }
159         
160     protected void setSecurity(Hashtable JavaDoc contextProps) {
161         String JavaDoc username = (String JavaDoc) contextProps.get(Context.SECURITY_PRINCIPAL);
162         String JavaDoc password = (String JavaDoc) contextProps.get(Context.SECURITY_CREDENTIALS);
163         SecurityAssociation.setPrincipal(username != null ? new SimplePrincipal(username) : null);
164         SecurityAssociation.setCredential(password != null ? password.toCharArray() : null);
165
166     }
167 }
168
169 /*
170  * $Log: Connector.java,v $
171  * Revision 1.2 2003/05/26 10:12:23 bdueck
172  * *** empty log message ***
173  *
174  * Revision 1.1 2003/04/21 15:29:03 bdueck
175  * Initial check-in.
176  *
177  *
178  */

179
180  
181  
Popular Tags