KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > tools > ant > axis > AdminClientTask


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.tools.ant.axis;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.client.AdminClient;
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.DirectoryScanner;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.taskdefs.Java;
25 import org.apache.tools.ant.taskdefs.MatchingTask;
26 import org.apache.tools.ant.types.Path;
27 import org.apache.tools.ant.types.Reference;
28
29 import javax.xml.rpc.ServiceException JavaDoc;
30 import java.io.File JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.LinkedList JavaDoc;
33
34 /**
35  * Task to administer a local or remote Axis server. Remember, for remote admin,
36  * the server has to be accept remote management calls.
37  *
38  * @ant.task category="axis" name="axis-admin"
39  */

40 public class AdminClientTask extends MatchingTask {
41
42     /**
43      * flag to control action on execution trouble
44      */

45     private boolean failOnError = true;
46     private String JavaDoc hostname;
47     private int port = 0;
48     private String JavaDoc servletPath;
49     private File JavaDoc srcDir = null;
50     private File JavaDoc xmlFile;
51     private String JavaDoc transportChain;
52     private String JavaDoc username;
53     private String JavaDoc password;
54     private String JavaDoc fileProtocol;
55     private String JavaDoc action = "";
56     private String JavaDoc url;
57     private boolean debug;
58     private String JavaDoc newPassword;
59     private LinkedList JavaDoc argslist;
60     private boolean fork = false;
61     private Path classpath = null;
62
63     /**
64      * set a debug flag
65      *
66      * @param debug
67      */

68     public void setDebug(boolean debug) {
69         this.debug = debug;
70     }
71
72     /**
73      * set a new password; only valid if action=passwd
74      *
75      * @param newPassword
76      */

77     public void setNewPassword(String JavaDoc newPassword) {
78         this.newPassword = newPassword;
79     }
80
81     /**
82      * full url to the admin endpoint
83      *
84      * @param url
85      */

86     public void setUrl(String JavaDoc url) {
87         this.url = url;
88     }
89
90     /**
91      * specifies that a simple file protocol be used
92      *
93      * @param fileProtocol
94      */

95     public void setFileProtocol(String JavaDoc fileProtocol) {
96         this.fileProtocol = fileProtocol;
97     }
98
99     /**
100      * name the host to admin
101      *
102      * @param hostname
103      */

104     public void setHostname(String JavaDoc hostname) {
105         this.hostname = hostname;
106     }
107
108     /**
109      * the admin password
110      *
111      * @param password
112      */

113     public void setPassword(String JavaDoc password) {
114         this.password = password;
115     }
116
117     /**
118      * the port to connect to
119      *
120      * @param port
121      */

122     public void setPort(int port) {
123         this.port = port;
124     }
125
126     /**
127      * the path to the AxisAdmin servlet
128      *
129      * @param servletPath
130      */

131     public void setServletPath(String JavaDoc servletPath) {
132         this.servletPath = servletPath;
133     }
134
135     /**
136      * Set the source dir to find the source text files.
137      */

138     public void setSrcdir(File JavaDoc srcDir) {
139         this.srcDir = srcDir;
140     }
141
142     /**
143      * the name of the XML file containing deployment information
144      *
145      * @param xmlFile
146      */

147     public void setXmlFile(File JavaDoc xmlFile) {
148         this.xmlFile = xmlFile;
149     }
150
151     /**
152      * set the transport chain to use
153      *
154      * @param transportChain
155      */

156     public void setTransportChain(String JavaDoc transportChain) {
157         this.transportChain = transportChain;
158     }
159
160     /**
161      * username to log in as
162      *
163      * @param username
164      */

165     public void setUsername(String JavaDoc username) {
166         this.username = username;
167     }
168
169     /**
170      * Whether or not the build should halt if this task fails.
171      * Defaults to <code>true</code>.
172      */

173     public void setFailOnError(boolean fail) {
174         failOnError = fail;
175     }
176
177     /**
178      * If true, forks the ant invocation.
179      *
180      * @param f "true|false|on|off|yes|no"
181      */

182     public void setFork(boolean f) {
183         fork = f;
184     }
185
186     /**
187      * validation code
188      *
189      * @throws org.apache.tools.ant.BuildException
190      * if validation failed
191      */

192     protected void validate()
193             throws BuildException {
194         if (srcDir != null) {
195             if (!srcDir.exists()) {
196                 throw new BuildException("srcdir does not exist!");
197             }
198             if (!srcDir.isDirectory()) {
199                 throw new BuildException("srcdir is not a directory!");
200             }
201         }
202     }
203
204     /**
205      * trace out parameters
206      *
207      * @param logLevel to log at
208      * @see org.apache.tools.ant.Project#log
209      */

210     public void traceParams(int logLevel) {
211         log("Running axis-admin with parameters:", logLevel);
212         log(" action:" + action, logLevel);
213         log(" url:" + url, logLevel);
214         log(" hostname:" + hostname, logLevel);
215         log(" port:" + port, logLevel);
216         log(" servletPath:" + servletPath, logLevel);
217         log(" fileProtocol:" + fileProtocol, logLevel);
218         log(" username:" + username, logLevel);
219         log(" password:" + password, logLevel);
220         log(" transportChain:" + transportChain, logLevel);
221         log(" debug:" + debug, logLevel);
222     }
223
224     /**
225      * <p>Processes a set of administration commands.</p>
226      * <p>The following commands are available:</p>
227      * <ul>
228      * <li><code>-l<i>url</i></code> sets the AxisServlet URL</li>
229      * <li><code>-h<i>hostName</i></code> sets the AxisServlet host</li>
230      * <li><code>-p<i>portNumber</i></code> sets the AxisServlet port</li>
231      * <li><code>-s<i>servletPath</i></code> sets the path to the
232      * AxisServlet</li>
233      * <li><code>-f<i>fileName</i></code> specifies that a simple file
234      * protocol should be used</li>
235      * <li><code>-u<i>username</i></code> sets the username</li>
236      * <li><code>-w<i>password</i></code> sets the password</li>
237      * <li><code>-d</code> sets the debug flag (for instance, -ddd would
238      * set it to 3)</li>
239      * <li><code>-t<i>name</i></code> sets the transport chain touse</li>
240      * <li><code>list</code> will list the currently deployed services</li>
241      * <li><code>quit</code> will quit (???)</li>
242      * <li><code>passwd <i>value</i></code> changes the admin password</li>
243      * <li><code><i>xmlConfigFile</i></code> deploys or undeploys
244      * Axis components and web services</li>
245      * </ul>
246      * <p>If <code>-l</code> or <code>-h -p -s</code> are not set, the
247      * AdminClient will invoke
248      * <code>http://localhost:8080/axis/servlet/AxisServlet</code>.</p>
249      * <p/>
250      * outputs XML result or null in case of failure. In the case of multiple
251      * commands, the XML results will be concatenated, separated by \n
252      *
253      * @throws BuildException something went wrong
254      */

255     public void execute() throws BuildException {
256         traceParams(Project.MSG_VERBOSE);
257         validate();
258         argslist = new LinkedList JavaDoc();
259
260         //build an array of args
261
addArgs("-l", url, url != null);
262         addArgs("-h", hostname, hostname != null);
263         addArgs("-p", Integer.toString(port), port != 0);
264         addArgs("-s", servletPath, servletPath != null);
265         addArgs("-f", fileProtocol, fileProtocol != null);
266         addArgs("-u", username, username != null);
267         addArgs("-w", password, password != null);
268         addArgs("-t", transportChain, transportChain != null);
269         addArg("-d", debug);
270         //action
271
addArg(action, action != null);
272         //action extras
273
if ("passwd".equals(action)) {
274             if (newPassword == null) {
275                 throw new BuildException("No newpassword set for passwd");
276             }
277             addArg(newPassword);
278         } else {
279             if (newPassword != null) {
280                 throw new BuildException(
281                         "newpassword is only used when action=passwd");
282             }
283         }
284
285         //final param is the xml file(s)
286
if (xmlFile != null) {
287             if (!xmlFile.exists()) {
288                 throw new BuildException("File " + xmlFile + " no found");
289             }
290             addArg(xmlFile.toString());
291         }
292         if (srcDir != null) {
293             DirectoryScanner ds = super.getDirectoryScanner(srcDir);
294             String JavaDoc[] files = ds.getIncludedFiles();
295             for (int i = 0; i < files.length; i++) {
296                 File JavaDoc srcFile = new File JavaDoc(srcDir, files[i]);
297                 if (!srcFile.exists()) {
298                     throw new BuildException("File " + srcFile + " no found");
299                 }
300                 addArg(srcFile.getAbsolutePath());
301             }
302         }
303         
304         //turn the list into an array
305
int counter = 0;
306         String JavaDoc[] args = new String JavaDoc[argslist.size()];
307         Iterator JavaDoc it = argslist.iterator();
308         while (it.hasNext()) {
309             String JavaDoc arg = (String JavaDoc) it.next();
310             args[counter] = arg;
311             counter++;
312         }
313         if (fork) {
314             executeInForkedVM(args);
315         } else {
316             executeInCurrentVM(args);
317         }
318     }
319
320     private void executeInForkedVM(String JavaDoc[] args) {
321         try {
322             // Create an instance of the compiler, redirecting output to
323
// the project log
324
Java java = (Java) (getProject().createTask("java"));
325             getProject().log("using classpath: " + classpath,
326                     Project.MSG_DEBUG);
327             java.setClasspath(classpath);
328             java.setClassname("org.apache.axis.client.AdminClient");
329             for (int i = 0; i < args.length; i++) {
330                 java.createArg().setValue(args[i]);
331             }
332             java.setFailonerror(failOnError);
333             //we are forking here to be sure that if AdminClient calls
334
//System.exit() it doesn't halt the build
335
java.setFork(true);
336             java.setTaskName("AdminClient");
337             java.execute();
338         } catch (BuildException e) {
339             //rethrow these
340
throw e;
341         } catch (Exception JavaDoc e) {
342             throw new BuildException("Exception in " + getTaskName(), e);
343         }
344     }
345
346     private void executeInCurrentVM(String JavaDoc[] args) {
347         //now create a client and invoke it
348
AdminClient admin = null;
349         try {
350             admin = new AdminClient(true);
351         } catch (ServiceException JavaDoc e) {
352             throw new BuildException("failed to start the axis engine", e);
353         }
354         String JavaDoc result = null;
355         try {
356             result = admin.process(args);
357             if (result != null) {
358                 log(result);
359             } else {
360                 logOrThrow(getTaskName() + " got a null response");
361             }
362         } catch (AxisFault fault) {
363             log(fault.dumpToString(), Project.MSG_ERR);
364             traceParams(Project.MSG_ERR);
365             logOrThrow(getTaskName()
366                     + " failed with "
367                     + fault.getFaultCode().toString()
368                     + " " + fault.getFaultString());
369         } catch (BuildException e) {
370             //rethrow these
371
throw e;
372         } catch (Exception JavaDoc e) {
373             throw new BuildException("Exception in " + getTaskName(), e);
374         }
375     }
376
377     private void logOrThrow(String JavaDoc text) throws BuildException {
378         if (failOnError) {
379             throw new BuildException(text);
380         } else {
381             log(text, Project.MSG_ERR);
382         }
383     }
384
385     /**
386      * add one arg
387      *
388      * @param argument
389      */

390     protected void addArg(String JavaDoc argument) {
391         argslist.add(argument);
392     }
393
394     /**
395      * add one arg
396      *
397      * @param argument
398      */

399     protected void addArg(String JavaDoc argument, boolean test) {
400         if (test) {
401             argslist.add(argument);
402         }
403     }
404
405     /**
406      * add an arg pair
407      *
408      * @param argument
409      * @param param
410      */

411     protected void addArgs(String JavaDoc argument, String JavaDoc param) {
412         addArg(argument);
413         addArg(param);
414     }
415
416     /**
417      * add an arg pair if the test is true
418      *
419      * @param argument first arg
420      * @param param param to accompany
421      * @param test test to trigger argument add
422      */

423     protected void addArgs(String JavaDoc argument, String JavaDoc param, boolean test) {
424         if (test) {
425             addArg(argument);
426             addArg(param);
427         }
428     }
429
430     /**
431      * Set the optional classpath
432      *
433      * @param classpath the classpath to use when loading class
434      */

435     public void setClasspath(Path classpath) {
436         createClasspath().append(classpath);
437     }
438
439     /**
440      * Set the optional classpath
441      *
442      * @return a path instance to be configured by the Ant core.
443      */

444     public Path createClasspath() {
445         if (classpath == null) {
446             classpath = new Path(getProject());
447         }
448         return classpath.createPath();
449     }
450
451     /**
452      * Set the reference to an optional classpath
453      *
454      * @param r the id of the Ant path instance to act as the classpath
455      */

456     public void setClasspathRef(Reference r) {
457         createClasspath().setRefid(r);
458     }
459 }
460
Popular Tags