KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > iplanet > IasAdmin


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package org.apache.tools.ant.taskdefs.optional.iplanet;
25
26 import org.apache.tools.ant.Task;
27 import org.apache.tools.ant.BuildException;
28 import org.apache.tools.ant.Project;
29 import org.apache.tools.ant.AntClassLoader;
30 import org.apache.tools.ant.types.Path;
31
32 import java.io.File JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.lang.reflect.Method JavaDoc;
37 import java.lang.reflect.InvocationTargetException JavaDoc;
38
39 public abstract class IasAdmin extends Task {
40     private Server server;
41     private List JavaDoc servers = new ArrayList JavaDoc();
42     private File JavaDoc ias7home;
43
44     private static final boolean ACTUALLY_EXEC_COMMAND = true;
45
46     private static final String JavaDoc[] CLASSPATH_ELEMENTS = {
47                                     "ias7se/iWS/bin/https/jar/iascli.jar",
48                                     "ias7se/iWS/bin/https/jar/adminshared.jar",
49                                     "ias7se/iWS/bin/https/jar/jmxri.jar"};
50     private static final String JavaDoc CLASS_INPUTS_AND_OUTPUTS =
51                             "com.sun.enterprise.tools.cli.framework.InputsAndOutputs";
52     private static final String JavaDoc CLASS_IAS_ADMIN_MAIN =
53                             "com.sun.enterprise.tools.cli.IasAdminMain";
54     private static final String JavaDoc METHOD_INVOKE_CLI = "invokeCLI";
55
56     public void setUser(String JavaDoc user) {
57         getServer().setUser(user);
58     }
59
60     public void setPassword(String JavaDoc password) {
61         log("setPassword: " + password, Project.MSG_DEBUG);
62         getServer().setPassword(password);
63     }
64
65     public void setHost(String JavaDoc host) {
66         getServer().setHost(host);
67     }
68
69     public void setPort(int port) {
70         getServer().setPort(port);
71     }
72
73     public void setInstanceport(int instanceport) {
74         getServer().setInstanceport(instanceport);
75     }
76
77     public void setInstance(String JavaDoc instance) {
78         getServer().setInstance(instance);
79     }
80
81     public void setIas7home(File JavaDoc iashome) {
82         this.ias7home = ias7home;
83     }
84
85     /**
86      * Returns null if iashome hasn't been explicitly set and the property
87      * ias7.home isn't set.
88      */

89     protected File JavaDoc getIas7home() {
90         if (ias7home == null) {
91             String JavaDoc home = getProject().getProperty("ias7.home");
92             if (home != null) {
93                 ias7home = new File JavaDoc(home);
94             }
95         }
96         
97         return ias7home;
98     }
99
100     public Server createServer() {
101         log("createServer", Project.MSG_DEBUG);
102         Server aServer = new Server(server);
103         servers.add(aServer);
104         return aServer;
105     }
106
107     private Server getServer() {
108         if (server == null) {
109             server = new Server();
110             Iterator JavaDoc it = servers.iterator();
111             while (it.hasNext()) {
112                 Server aServer = (Server)it.next();
113                 aServer.setParent(server);
114             }
115         }
116         return server;
117     }
118
119     public void execute() throws BuildException {
120         prepareToExecute();
121         checkConfiguration();
122
123         Iterator JavaDoc it = servers.iterator();
124         while (it.hasNext()) {
125             Server aServer = (Server)it.next();
126             execute(aServer);
127         }
128     }
129
130     protected void prepareToExecute() throws BuildException {
131         if ((servers.size() == 0) && (server != null)) {
132             servers.add(server);
133         }
134     }
135
136     protected void checkConfiguration() throws BuildException {
137
138         if (servers.size() == 0) {
139             String JavaDoc msg = "At least one server must be specified.";
140             throw new BuildException(msg, getLocation());
141         }
142
143         Iterator JavaDoc it = servers.iterator();
144         while (it.hasNext()) {
145             Server aServer = (Server)it.next();
146             checkConfiguration(server);
147         }
148     }
149
150     protected void checkConfiguration(Server aServer) throws BuildException {
151         if (aServer.getPassword() == null) {
152             String JavaDoc msg = "A password must be specified for each server. A "
153                             + "password for " + aServer.getHost() + " was "
154                             + "not specified.";
155             throw new BuildException(msg, getLocation());
156         }
157     }
158
159     protected abstract void execute(Server server) throws BuildException;
160
161     protected void execIasCommand(String JavaDoc command) {
162         log("Executing: " + command, Project.MSG_INFO);
163
164         try {
165             Path iasClasspath = new Path(getProject(), getIasClasspath());
166             iasClasspath.append(Path.systemClasspath);
167             log("Using classpath: " + iasClasspath.toString(), Project.MSG_DEBUG);
168
169             ClassLoader JavaDoc antClassLoader = new AntClassLoader(getProject(), iasClasspath);
170
171             Class JavaDoc inputsAndOutputs =
172                         Class.forName(CLASS_INPUTS_AND_OUTPUTS, true, antClassLoader);
173             Class JavaDoc iasAdminMain =
174                         Class.forName(CLASS_IAS_ADMIN_MAIN, true, antClassLoader);
175
176             Class JavaDoc[] parameterClasses = {String JavaDoc.class, inputsAndOutputs};
177             Method JavaDoc invokeCLI =
178                     iasAdminMain.getDeclaredMethod(METHOD_INVOKE_CLI, parameterClasses);
179
180             Object JavaDoc[] parameters = {command, null};
181
182             if (ACTUALLY_EXEC_COMMAND) {
183                 invokeCLI.invoke(iasAdminMain, parameters);
184             }
185         } catch (ClassNotFoundException JavaDoc e) {
186             String JavaDoc msg = "An iPlanet Application Server 7.0 admin CLI "
187                             + "class could not be found (" + e.getMessage()
188                             + "). Use the ias7home attribute, set the "
189                             + "ias7.home property, or add the appropriate "
190                             + "JARs to the classpath.";
191             throw new BuildException(msg, getLocation());
192         } catch (NoSuchMethodException JavaDoc e) {
193             String JavaDoc msg = "The \"invokeCLI\" method couldn't be found on the "
194                             + "IasAdminMain class. The exception message "
195                             + "is: " + e.getMessage();
196             throw new BuildException(msg, getLocation());
197         } catch (InvocationTargetException JavaDoc e) {
198             String JavaDoc msg = "An exception occurred while running the command. The "
199                             + "exception message is: "
200                             + e.getTargetException().getMessage();
201             throw new BuildException(msg, getLocation());
202         } catch (IllegalAccessException JavaDoc e) {
203             String JavaDoc msg = "An exception occurred while trying to invoke the "
204                             + "\"invokeCLI\" method. The exception message "
205                             + "is: " + e.getMessage();
206             throw new BuildException(msg, getLocation());
207         }
208     }
209
210     private String JavaDoc getIasClasspath() {
211         StringBuffer JavaDoc classpath = new StringBuffer JavaDoc();
212         String JavaDoc[] elements = getClasspathElements();
213         for (int i = 0; i < elements.length; i++ ) {
214             classpath.append(new File JavaDoc(getIas7home(), elements[i]).getPath());
215             classpath.append(':');
216         }
217
218         return classpath.toString();
219     }
220
221
222     /**
223      * Returns the JARs and directories that should be added to the classpath
224      * when calling the IasAdminMain class. All elements are relative to the
225      * iPlanet Application Server 7.0 installation directory.
226      */

227     protected String JavaDoc[] getClasspathElements() {
228         return CLASSPATH_ELEMENTS;
229     }
230
231     public class Server {
232         private Server parent;
233         private String JavaDoc user;
234         private String JavaDoc password;
235         private String JavaDoc host;
236         private int port;
237         private int instanceport;
238         private String JavaDoc instance;
239
240         private static final String JavaDoc DEFAULT_USER = "admin";
241         private static final String JavaDoc DEFAULT_HOST = "localhost";
242         private static final int DEFAULT_PORT = 8000;
243
244         public Server() {
245             this(null);
246         }
247
248         public Server(Server parent) {
249             this.parent = parent;
250         }
251
252         public void setParent(Server parent) {
253             this.parent = parent;
254         }
255
256         public void setUser(String JavaDoc user) {
257             this.user = user;
258         }
259
260         protected String JavaDoc getUser() {
261             if (user == null) {
262                 return (parent == null) ? DEFAULT_USER : parent.getUser();
263             }
264             return user;
265         }
266
267         public void setPassword(String JavaDoc password) {
268             this.password = password;
269         }
270
271         protected String JavaDoc getPassword() {
272             if (password == null) {
273                 return (parent == null) ? null : parent.getPassword();
274             }
275             return password;
276         }
277
278         public void setHost(String JavaDoc host) {
279             this.host = host;
280         }
281
282         protected String JavaDoc getHost() {
283             if (host == null) {
284                 return (parent == null) ? DEFAULT_HOST : parent.getHost();
285             }
286             return host;
287         }
288
289         public void setPort(int port) {
290             this.port = port;
291         }
292
293         protected int getPort() {
294             if (port == 0) {
295                 return (parent == null) ? DEFAULT_PORT : parent.getPort();
296             }
297             return port;
298         }
299
300         public void setInstanceport(int instanceport) {
301             this.instanceport = instanceport;
302         }
303
304         protected int getInstanceport() {
305             if ((instanceport == 0) && (parent != null)) {
306                 return parent.getInstanceport();
307             }
308             return instanceport;
309         }
310
311         public void setInstance(String JavaDoc instance) {
312             this.instance = instance;
313         }
314
315         protected String JavaDoc getInstance() {
316             if (instance == null) {
317                 return (parent == null) ? null : parent.getInstance();
318             }
319             return instance;
320         }
321
322         protected String JavaDoc getCommandParameters(boolean includeInstance) {
323             StringBuffer JavaDoc cmdString = new StringBuffer JavaDoc();
324             cmdString.append(" --user ").append(getUser());
325             cmdString.append(" --password ").append(getPassword());
326             cmdString.append(" --host ").append(getHost());
327             cmdString.append(" --port ").append(getPort());
328             if (includeInstance && (getInstance() != null)) {
329                 cmdString.append(" --instance ").append(getInstance());
330             }
331
332             return cmdString.toString();
333         }
334     };
335 }
336
Popular Tags