KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > client > AdminClient


1 /*
2  * Copyright 2001-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.client ;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.EngineConfiguration;
21 import org.apache.axis.components.logger.LogFactory;
22 import org.apache.axis.deployment.wsdd.WSDDConstants;
23 import org.apache.axis.message.SOAPBodyElement;
24 import org.apache.axis.utils.Messages;
25 import org.apache.axis.utils.Options;
26 import org.apache.axis.utils.StringUtils;
27 import org.apache.commons.logging.Log;
28
29 import javax.xml.rpc.ServiceException JavaDoc;
30 import java.io.ByteArrayInputStream JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.util.Vector JavaDoc;
35
36
37 /**
38  * An admin client object that can be used both from the command line
39  * and programmatically.
40  *
41  * @author Rob Jellinghaus (robj@unrealities.com)
42  * @author Doug Davis (dug@us.ibm.com)
43  * @author Simeon Simeonov (simeons@macromedia.com)
44  */

45
46 public class AdminClient
47 {
48     protected static Log log =
49         LogFactory.getLog(AdminClient.class.getName());
50
51     private static ThreadLocal JavaDoc defaultConfiguration = new ThreadLocal JavaDoc();
52
53     /**
54      * If the user calls this with an EngineConfiguration object, all
55      * AdminClients on this thread will use that EngineConfiguration
56      * rather than the default one. This is primarily to enable the
57      * deployment of custom transports and handlers.
58      *
59      * @param config the EngineConfiguration which should be used
60      */

61     public static void setDefaultConfiguration(EngineConfiguration config)
62     {
63         defaultConfiguration.set(config);
64     }
65
66     private static String JavaDoc getUsageInfo()
67     {
68         String JavaDoc lSep = System.getProperty("line.separator");
69         StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
70         // 26 is the # of lines in resources.properties
71
msg.append(Messages.getMessage("acUsage00")).append(lSep);
72         msg.append(Messages.getMessage("acUsage01")).append(lSep);
73         msg.append(Messages.getMessage("acUsage02")).append(lSep);
74         msg.append(Messages.getMessage("acUsage03")).append(lSep);
75         msg.append(Messages.getMessage("acUsage04")).append(lSep);
76         msg.append(Messages.getMessage("acUsage05")).append(lSep);
77         msg.append(Messages.getMessage("acUsage06")).append(lSep);
78         msg.append(Messages.getMessage("acUsage07")).append(lSep);
79         msg.append(Messages.getMessage("acUsage08")).append(lSep);
80         msg.append(Messages.getMessage("acUsage09")).append(lSep);
81         msg.append(Messages.getMessage("acUsage10")).append(lSep);
82         msg.append(Messages.getMessage("acUsage11")).append(lSep);
83         msg.append(Messages.getMessage("acUsage12")).append(lSep);
84         msg.append(Messages.getMessage("acUsage13")).append(lSep);
85         msg.append(Messages.getMessage("acUsage14")).append(lSep);
86         msg.append(Messages.getMessage("acUsage15")).append(lSep);
87         msg.append(Messages.getMessage("acUsage16")).append(lSep);
88         msg.append(Messages.getMessage("acUsage17")).append(lSep);
89         msg.append(Messages.getMessage("acUsage18")).append(lSep);
90         msg.append(Messages.getMessage("acUsage19")).append(lSep);
91         msg.append(Messages.getMessage("acUsage20")).append(lSep);
92         msg.append(Messages.getMessage("acUsage21")).append(lSep);
93         msg.append(Messages.getMessage("acUsage22")).append(lSep);
94         msg.append(Messages.getMessage("acUsage23")).append(lSep);
95         msg.append(Messages.getMessage("acUsage24")).append(lSep);
96         msg.append(Messages.getMessage("acUsage25")).append(lSep);
97         msg.append(Messages.getMessage("acUsage26")).append(lSep);
98         return msg.toString();
99     }
100
101
102     /**
103      * the object that represents our call
104      */

105     protected Call call;
106
107     /**
108      * Construct an admin client w/o a logger.
109      * If the client cannot create a call object, then it does not throw an exception.
110      * Instead it prints a message to {@link System.err}.
111      * This is for 'historical reasons'
112      */

113     public AdminClient()
114     {
115         try {
116             initAdminClient();
117         } catch (ServiceException JavaDoc e) {
118             System.err.println(Messages.getMessage("couldntCall00") + ": " + e);
119             call = null;
120         }
121     }
122
123
124     /**
125      * this is a somwhat contrived variant constructor, one that throws an exception
126      * if things go wrong.
127      * @param ignored
128      */

129     public AdminClient(boolean ignored) throws ServiceException JavaDoc {
130         initAdminClient();
131     }
132
133     /**
134      * core initialisation routine
135      *
136      * @throws ServiceException
137      */

138     private void initAdminClient() throws ServiceException JavaDoc {
139         // Initialize our Service - allow the user to override the
140
// default configuration with a thread-local version (see
141
// setDefaultConfiguration() above)
142
EngineConfiguration config =
143                 (EngineConfiguration) defaultConfiguration.get();
144         Service service;
145         if (config != null) {
146             service = new Service(config);
147         } else {
148             service = new Service();
149         }
150         call = (Call) service.createCall();
151     }
152
153
154     /**
155      * External access to our <code>Call</code< object.
156      * This will be null if the non-excepting constructor was used
157      * and the construction failed.
158      * @return the <code>Call</code> object this instance uses
159      */

160     public Call getCall()
161     {
162         return call;
163     }
164
165     /**
166      * process the options then run a list call
167      * @param opts
168      * @return
169      * @throws Exception
170      */

171     public String JavaDoc list(Options opts) throws Exception JavaDoc {
172         processOpts( opts );
173         return list();
174     }
175
176     /**
177      * send a list command
178      * @return the response from the call
179      * @throws Exception
180      */

181     public String JavaDoc list() throws Exception JavaDoc {
182         log.debug( Messages.getMessage("doList00") );
183         String JavaDoc str = "<m:list xmlns:m=\"" + WSDDConstants.URI_WSDD + "\"/>" ;
184         ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc(str.getBytes());
185         return process(input);
186     }
187
188     /**
189      * process the command line ops, then send a quit command
190      * @param opts
191      * @return
192      * @throws Exception
193      */

194     public String JavaDoc quit(Options opts) throws Exception JavaDoc {
195         processOpts( opts );
196         return quit();
197     }
198
199     /**
200      * root element of the undeploy request
201      */

202     protected static final String JavaDoc ROOT_UNDEPLOY= WSDDConstants.QNAME_UNDEPLOY.getLocalPart();
203
204     /**
205      * make a quit command
206      * @return
207      * @throws Exception
208      */

209     public String JavaDoc quit() throws Exception JavaDoc {
210         log.debug(Messages.getMessage("doQuit00"));
211         String JavaDoc str = "<m:quit xmlns:m=\"" + WSDDConstants.URI_WSDD + "\"/>";
212         ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc(str.getBytes());
213         return process(input);
214     }
215
216     /**
217      * undeploy a handler
218      * @param handlerName name of the handler to undeploy
219      * @return
220      * @throws Exception
221      */

222     public String JavaDoc undeployHandler(String JavaDoc handlerName) throws Exception JavaDoc {
223         log.debug(Messages.getMessage("doQuit00"));
224         String JavaDoc str = "<m:"+ROOT_UNDEPLOY +" xmlns:m=\"" + WSDDConstants.URI_WSDD + "\">" +
225                                      "<handler name=\"" + handlerName + "\"/>"+
226                                      "</m:"+ROOT_UNDEPLOY +">" ;
227         ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc(str.getBytes());
228         return process(input);
229     }
230
231     /**
232      * undeploy a service
233      * @param serviceName name of service
234      * @return
235      * @throws Exception
236      */

237     public String JavaDoc undeployService(String JavaDoc serviceName) throws Exception JavaDoc {
238         log.debug(Messages.getMessage("doQuit00"));
239         String JavaDoc str = "<m:"+ROOT_UNDEPLOY +" xmlns:m=\"" + WSDDConstants.URI_WSDD + "\">" +
240                                      "<service name=\"" + serviceName + "\"/>"+
241                                      "</m:"+ROOT_UNDEPLOY +">" ;
242         ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc(str.getBytes());
243         return process(input);
244     }
245
246     /**
247      * <p>Processes a set of administration commands.</p>
248      * <p>The following commands are available:</p>
249      * <ul>
250      * <li><code>-l<i>url</i></code> sets the AxisServlet URL</li>
251      * <li><code>-h<i>hostName</i></code> sets the AxisServlet host</li>
252      * <li><code>-p<i>portNumber</i></code> sets the AxisServlet port</li>
253      * <li><code>-s<i>servletPath</i></code> sets the path to the
254      * AxisServlet</li>
255      * <li><code>-f<i>fileName</i></code> specifies that a simple file
256      * protocol should be used</li>
257      * <li><code>-u<i>username</i></code> sets the username</li>
258      * <li><code>-w<i>password</i></code> sets the password</li>
259      * <li><code>-d</code> sets the debug flag (for instance, -ddd would
260      * set it to 3)</li>
261      * <li><code>-t<i>name</i></code> sets the transport chain touse</li>
262      * <li><code>list</code> will list the currently deployed services</li>
263      * <li><code>quit</code> will quit (???)</li>
264      * <li><code>passwd <i>value</i></code> changes the admin password</li>
265      * <li><code><i>xmlConfigFile</i></code> deploys or undeploys
266      * Axis components and web services</li>
267      * </ul>
268      * <p>If <code>-l</code> or <code>-h -p -s</code> are not set, the
269      * AdminClient will invoke
270      * <code>http://localhost:8080/axis/servlet/AxisServlet</code>.</p>
271      *
272      * @param args Commands to process
273      * @return XML result or null in case of failure. In the case of multiple
274      * commands, the XML results will be concatenated, separated by \n
275      * @exception Exception Could be an IO exception, an AxisFault or something else
276      */

277
278     public String JavaDoc process(String JavaDoc[] args) throws Exception JavaDoc {
279         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
280
281         Options opts = new Options( args );
282         opts.setDefaultURL("http://localhost:8080/axis/services/AdminService");
283
284         if (opts.isFlagSet('d') > 0) {
285             // Set logger properties... !!!
286
}
287
288         args = opts.getRemainingArgs();
289
290         if ( args == null || opts.isFlagSet('?') > 0) {
291             System.out.println(Messages.getMessage("usage00","AdminClient [Options] [list | <deployment-descriptor-files>]"));
292             System.out.println("");
293             System.out.println(getUsageInfo());
294             return null;
295         }
296
297         for ( int i = 0 ; i < args.length ; i++ ) {
298             InputStream JavaDoc input = null;
299
300             if ( args[i].equals("list") )
301               sb.append( list(opts) );
302             else if (args[i].equals("quit"))
303               sb.append( quit(opts) );
304             else if (args[i].equals("passwd")) {
305                 System.out.println(Messages.getMessage("changePwd00"));
306                 if (args[i + 1] == null) {
307                     System.err.println(Messages.getMessage("needPwd00"));
308                     return null;
309                 }
310                 String JavaDoc str = "<m:passwd xmlns:m=\"http://xml.apache.org/axis/wsdd/\">";
311                 str += args[i + 1];
312                 str += "</m:passwd>";
313                 input = new ByteArrayInputStream JavaDoc(str.getBytes());
314                 i++;
315                 sb.append( process(opts, input) );
316             }
317             else {
318                 if(args[i].indexOf(java.io.File.pathSeparatorChar)==-1){
319                     System.out.println( Messages.getMessage("processFile00", args[i]) );
320                     sb.append( process(opts, args[i] ) );
321                 } else {
322                     java.util.StringTokenizer JavaDoc tokenizer = null ;
323                     tokenizer = new java.util.StringTokenizer JavaDoc(args[i],
324                                                  java.io.File.pathSeparator);
325                     while(tokenizer.hasMoreTokens()) {
326                         String JavaDoc file = tokenizer.nextToken();
327                         System.out.println( Messages.getMessage("processFile00", file) );
328                         sb.append( process(opts, file) );
329                         if(tokenizer.hasMoreTokens())
330                             sb.append("\n");
331                     }
332                 }
333             }
334         }
335
336         return sb.toString();
337     }
338
339     /**
340      * go from the (parsed) command line to setting properties on our call object.
341      * @param opts
342      * @throws Exception if call==null
343      */

344     public void processOpts(Options opts) throws Exception JavaDoc {
345         if (call == null) {
346             throw new Exception JavaDoc(Messages.getMessage("nullCall00"));
347         }
348
349         URL JavaDoc address = new URL JavaDoc(opts.getURL());
350         setTargetEndpointAddress(address);
351         setLogin(opts.getUser(), opts.getPassword());
352
353         String JavaDoc tName = opts.isValueSet( 't' );
354         setTransport(tName);
355     }
356
357     /**
358      * set the username and password
359      * requires that call!=null
360      * @param user username
361      * @param password password
362      */

363     public void setLogin(String JavaDoc user, String JavaDoc password) {
364         call.setUsername( user );
365         call.setPassword( password );
366     }
367
368     /**
369      * set the URL to deploy to
370      * requires that call!=null
371      * @param address
372      */

373     public void setTargetEndpointAddress(URL JavaDoc address) {
374         call.setTargetEndpointAddress( address );
375     }
376
377     /**
378      * set the transport to deploy with.
379      * requires that call!=null
380      * @param transportName a null or empty value does not trigger a setting
381      */

382     public void setTransport(String JavaDoc transportName) {
383         if(transportName != null && !transportName.equals("")) {
384             call.setProperty( Call.TRANSPORT_NAME, transportName );
385         }
386     }
387
388     public String JavaDoc process(InputStream JavaDoc input) throws Exception JavaDoc {
389         return process(null, input );
390     }
391
392     public String JavaDoc process(URL JavaDoc xmlURL) throws Exception JavaDoc {
393         return process(null, xmlURL.openStream() );
394     }
395
396     /**
397      * process an XML file containing a pre-prepared admin message
398      * @param xmlFile file to load
399      * @return
400      * @throws Exception
401      */

402     public String JavaDoc process(String JavaDoc xmlFile) throws Exception JavaDoc {
403         FileInputStream JavaDoc in = new FileInputStream JavaDoc(xmlFile);
404         String JavaDoc result = process(null, in );
405         return result ;
406     }
407
408     public String JavaDoc process(Options opts, String JavaDoc xmlFile) throws Exception JavaDoc {
409         processOpts( opts );
410         return process( xmlFile );
411     }
412
413     /**
414      * submit the input stream's contents to the endpoint, return the results as a string.
415      * The input stream is always closed after the call, whether the request worked or not
416      * @param opts options -can be null
417      * @param input -input stream for request
418      * @return
419      * @throws Exception if the call was null
420      * @throws AxisFault if the invocation returned an empty response
421      */

422     public String JavaDoc process(Options opts, InputStream JavaDoc input) throws Exception JavaDoc {
423         try {
424             if (call == null) {
425                 //validate that the call is not null
426
throw new Exception JavaDoc(Messages.getMessage("nullCall00"));
427             }
428
429             if ( opts != null ) {
430                 //process options if supplied
431
processOpts( opts );
432             }
433
434             call.setUseSOAPAction( true);
435             call.setSOAPActionURI( "urn:AdminService");
436
437             Vector JavaDoc result = null ;
438             Object JavaDoc[] params = new Object JavaDoc[] { new SOAPBodyElement(input) };
439             result = (Vector JavaDoc) call.invoke( params );
440
441             if (result == null || result.isEmpty()) {
442                 throw new AxisFault(Messages.getMessage("nullResponse00"));
443             }
444
445             SOAPBodyElement body = (SOAPBodyElement) result.elementAt(0);
446             return body.toString();
447         } finally {
448             input.close();
449         }
450     }
451
452     /**
453      * Creates in instance of <code>AdminClient</code> and
454      * invokes <code>process(args)</code>.
455      * <p>Diagnostic output goes to <code>log.info</code>.</p>
456      * @param args Commands to process
457      */

458     public static void main (String JavaDoc[] args)
459     {
460         try {
461             AdminClient admin = new AdminClient();
462
463             String JavaDoc result = admin.process(args);
464             if (result != null) {
465                 System.out.println( StringUtils.unescapeNumericChar(result) );
466             } else {
467                 System.exit(1);
468             }
469     } catch (AxisFault ae) {
470             System.err.println(Messages.getMessage("exception00") + " " + ae.dumpToString());
471             System.exit(1);
472         } catch (Exception JavaDoc e) {
473             System.err.println(Messages.getMessage("exception00") + " " + e.getMessage());
474             System.exit(1);
475         }
476     }
477 }
478
479
Popular Tags