KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > jmx > JndiLookup


1 package org.apache.tools.ant.taskdefs.optional.jmx;
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 org.apache.tools.ant.BuildException;
54 import org.apache.tools.ant.taskdefs.optional.jmx.connector.JMXConnector;
55
56
57 /**
58  * This is task allows a lookup to be performed within a JNDI tree.
59  * If the lookup is successful, the specified property will be set
60  * with a value. The result of the lookup is stored in a reference
61  * called named refid.
62  *
63  * Refer to the user documentation for more information and examples on how to use
64  * this task.
65  *
66  * @author <a HREF="mailto:bdueck@yahoo.com">Brian Dueck</a>
67  * @version $Id: JndiLookup.java,v 1.3 2003/05/28 22:28:26 bdueck Exp $
68  *
69  */

70 public class JndiLookup extends AbstractManagementTask {
71     
72     private String JavaDoc property = null;
73     private String JavaDoc refid = null;
74     
75     public JndiLookup() {
76     }
77     
78     
79     /**
80      * Execute the task.
81      *
82      * @throws BuildException If an error occurs.
83      */

84     public void execute() throws BuildException {
85         try {
86             
87             javax.naming.Context JavaDoc namingContext = getNamingContext(
88                                     getProviderUrl(),
89                                     getJndiName(),
90                                     getUser(),
91                                     getPassword());
92             
93             try {
94                 Object JavaDoc result = namingContext.lookup(this.getJndiName());
95
96                 if (getProperty() != null) {
97                     getProject().setProperty(getProperty(),"true");
98                 }
99
100                 if (getRefid() != null) {
101                     getProject().addReference(getRefid(),result);
102                 }
103             } catch (javax.naming.NamingException JavaDoc ne) {
104                 if (this.getFailOnError()) {
105                     ne.printStackTrace();
106                     throw new BuildException("Error! " + ne, ne);
107                 }
108             }
109         } catch (Exception JavaDoc x) {
110             if (this.getFailOnError()) {
111                 x.printStackTrace();
112                 throw new BuildException("Error! " + x, x);
113             } else {
114                 log("Warning! " + x,org.apache.tools.ant.Project.MSG_WARN);
115             }
116         }
117     }
118             
119             
120     /**
121      * Logs into a JMX server and returns the MBeanServer to
122      * use during task execution. The caller is responsible
123      * for invoking this method prior to executing any other
124      * methods on this interface.
125      *
126      * @param providerUrl The url of the JNDI provider.
127      * @param jndiName The JNDI name of the MBeanServer.
128      * @param user The user name to use for authentication.
129      * @param password The password to use for authentication.
130      * @return An MBeanServer.
131      * @raise BuildException if an error occurs attempting to login.
132      */

133     protected javax.naming.Context JavaDoc getNamingContext(String JavaDoc providerUrl, String JavaDoc jndiName, String JavaDoc user, String JavaDoc password) throws javax.naming.NamingException JavaDoc {
134         try {
135
136             if (getContext().getServerType() == null) {
137                 log("Warning! serverType not specified or unrecognized type. Defaulting to WebLogic.");
138                 getContext().setServerType("weblogic");
139             }
140
141             JMXConnector jmxServer = org.apache.tools.ant.taskdefs.optional.jmx.connector.JMXConnectorFactory.createConnector(getContext().getServerType());
142             java.util.Hashtable JavaDoc properties = jmxServer.getInitialContextProperties(providerUrl,user,password);
143             
144             return new javax.naming.InitialContext JavaDoc(properties);
145             
146         } catch (Exception JavaDoc e) {
147             e.printStackTrace();
148             throw new BuildException("JNDI Error. " + e.getMessage(), e);
149         }
150     }
151     
152     /** Getter for property.
153      * @return Value of property.
154      *
155      */

156     public java.lang.String JavaDoc getProperty() {
157         return property;
158     }
159     
160     /** Setter for property.
161      * @param property New value of property.
162      *
163      */

164     public void setProperty(java.lang.String JavaDoc property) {
165         this.property = property;
166     }
167     
168     /** Getter for property refid.
169      * @return Value of property refid.
170      *
171      */

172     public java.lang.String JavaDoc getRefid() {
173         return refid;
174     }
175     
176     /** Setter for property refid.
177      * @param refid New value of property refid.
178      *
179      */

180     public void setRefid(java.lang.String JavaDoc refid) {
181         this.refid = refid;
182     }
183     
184 }
185
186 /*
187  * $Log: JndiLookup.java,v $
188  * Revision 1.3 2003/05/28 22:28:26 bdueck
189  * *** empty log message ***
190  *
191  * Revision 1.2 2003/04/21 15:29:41 bdueck
192  * Various changes in preparation for version 1.2.
193  *
194  * Revision 1.1 2003/04/01 22:12:03 bdueck
195  * Initial revision.
196  *
197  * Revision 1.1 2003/01/17 12:33:55 bdueck
198  * Initial check-in
199  *
200  *
201  */
Popular Tags