KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.client;
57
58 import org.jboss.axis.AxisFault;
59 import org.jboss.axis.deployment.wsdd.WSDDConstants;
60 import org.jboss.axis.message.SOAPBodyElementAxisImpl;
61 import org.jboss.axis.utils.Messages;
62 import org.jboss.axis.utils.Options;
63 import org.jboss.logging.Logger;
64
65 import javax.xml.rpc.ServiceException JavaDoc;
66 import java.io.ByteArrayInputStream JavaDoc;
67 import java.io.File JavaDoc;
68 import java.io.FileInputStream JavaDoc;
69 import java.io.FileNotFoundException JavaDoc;
70 import java.io.InputStream JavaDoc;
71 import java.net.URL JavaDoc;
72 import java.util.Vector JavaDoc;
73
74
75 /**
76  * An admin client object that can be used both from the command line
77  * and programmatically.
78  *
79  * @author Rob Jellinghaus (robj@unrealities.com)
80  * @author Doug Davis (dug@us.ibm.com)
81  * @author Simeon Simeonov (simeons@macromedia.com)
82  */

83
84 public class AdminClient
85 {
86    private static Logger log = Logger.getLogger(AdminClient.class.getName());
87
88    private static String JavaDoc getUsageInfo()
89    {
90       String JavaDoc lSep = System.getProperty("line.separator");
91       StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
92       // 26 is the # of lines in resources.properties
93
msg.append(Messages.getMessage("acUsage00")).append(lSep);
94       msg.append(Messages.getMessage("acUsage01")).append(lSep);
95       msg.append(Messages.getMessage("acUsage02")).append(lSep);
96       msg.append(Messages.getMessage("acUsage03")).append(lSep);
97       msg.append(Messages.getMessage("acUsage04")).append(lSep);
98       msg.append(Messages.getMessage("acUsage05")).append(lSep);
99       msg.append(Messages.getMessage("acUsage06")).append(lSep);
100       msg.append(Messages.getMessage("acUsage07")).append(lSep);
101       msg.append(Messages.getMessage("acUsage08")).append(lSep);
102       msg.append(Messages.getMessage("acUsage09")).append(lSep);
103       msg.append(Messages.getMessage("acUsage10")).append(lSep);
104       msg.append(Messages.getMessage("acUsage11")).append(lSep);
105       msg.append(Messages.getMessage("acUsage12")).append(lSep);
106       msg.append(Messages.getMessage("acUsage13")).append(lSep);
107       msg.append(Messages.getMessage("acUsage14")).append(lSep);
108       msg.append(Messages.getMessage("acUsage15")).append(lSep);
109       msg.append(Messages.getMessage("acUsage16")).append(lSep);
110       msg.append(Messages.getMessage("acUsage17")).append(lSep);
111       msg.append(Messages.getMessage("acUsage18")).append(lSep);
112       msg.append(Messages.getMessage("acUsage19")).append(lSep);
113       msg.append(Messages.getMessage("acUsage20")).append(lSep);
114       msg.append(Messages.getMessage("acUsage21")).append(lSep);
115       msg.append(Messages.getMessage("acUsage22")).append(lSep);
116       msg.append(Messages.getMessage("acUsage23")).append(lSep);
117       msg.append(Messages.getMessage("acUsage24")).append(lSep);
118       msg.append(Messages.getMessage("acUsage25")).append(lSep);
119       msg.append(Messages.getMessage("acUsage26")).append(lSep);
120       return msg.toString();
121    }
122
123
124    protected Call call;
125
126    /**
127     * Construct an admin client w/o a logger
128     */

129    public AdminClient()
130    {
131       try
132       {
133          Service service = new Service();
134          call = (Call)service.createCall();
135       }
136       catch (ServiceException JavaDoc e)
137       {
138          System.err.println(Messages.getMessage("couldntCall00") + ": " + e);
139          call = null;
140       }
141    }
142
143    /**
144     * External access to our Call object
145     */

146    public Call getCall()
147    {
148       return call;
149    }
150
151    public String JavaDoc list(Options opts) throws Exception JavaDoc
152    {
153       processOpts(opts);
154       return list();
155    }
156
157    public String JavaDoc list() throws Exception JavaDoc
158    {
159       log.debug(Messages.getMessage("doList00"));
160       String JavaDoc str = "<m:list xmlns:m=\"" + WSDDConstants.URI_WSDD + "\"/>";
161       ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc(str.getBytes());
162       return process(input);
163    }
164
165    public String JavaDoc quit(Options opts) throws Exception JavaDoc
166    {
167       processOpts(opts);
168       return quit();
169    }
170
171    protected static final String JavaDoc ROOT_UNDEPLOY = WSDDConstants.QNAME_UNDEPLOY.getLocalPart();
172
173    public String JavaDoc quit() throws Exception JavaDoc
174    {
175       log.debug(Messages.getMessage("doQuit00"));
176       String JavaDoc str = "<m:quit xmlns:m=\"" + WSDDConstants.URI_WSDD + "\"/>";
177       ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc(str.getBytes());
178       return process(input);
179    }
180
181    public String JavaDoc undeployHandler(String JavaDoc handlerName) throws Exception JavaDoc
182    {
183       log.debug(Messages.getMessage("doQuit00"));
184       String JavaDoc str = "<m:" + ROOT_UNDEPLOY + " xmlns:m=\"" + WSDDConstants.URI_WSDD + "\">" +
185               "<handler name=\"" + handlerName + "\"/>" +
186               "</m:" + ROOT_UNDEPLOY + ">";
187       ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc(str.getBytes());
188       return process(input);
189    }
190
191    public String JavaDoc undeployService(String JavaDoc serviceName) throws Exception JavaDoc
192    {
193       log.debug(Messages.getMessage("doQuit00"));
194       String JavaDoc str = "<m:" + ROOT_UNDEPLOY + " xmlns:m=\"" + WSDDConstants.URI_WSDD + "\">" +
195               "<service name=\"" + serviceName + "\"/>" +
196               "</m:" + ROOT_UNDEPLOY + ">";
197       ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc(str.getBytes());
198       return process(input);
199    }
200
201    /**
202     * <p>Processes a set of administration commands.</p>
203     * <p>The following commands are available:</p>
204     * <ul>
205     * <li><code>-l<i>url</i></code> sets the AxisServlet URL</li>
206     * <li><code>-h<i>hostName</i></code> sets the AxisServlet host</li>
207     * <li><code>-p<i>portNumber</i></code> sets the AxisServlet port</li>
208     * <li><code>-s<i>servletPath</i></code> sets the path to the
209     * AxisServlet</li>
210     * <li><code>-f<i>fileName</i></code> specifies that a simple file
211     * protocol should be used</li>
212     * <li><code>-u<i>username</i></code> sets the username</li>
213     * <li><code>-w<i>password</i></code> sets the password</li>
214     * <li><code>-d</code> sets the debug flag (for instance, -ddd would
215     * set it to 3)</li>
216     * <li><code>-t<i>name</i></code> sets the transport chain touse</li>
217     * <li><code>list</code> will list the currently deployed services</li>
218     * <li><code>quit</code> will quit (???)</li>
219     * <li><code>passwd <i>value</i></code> changes the admin password</li>
220     * <li><code><i>xmlConfigFile</i></code> deploys or undeploys
221     * Axis components and web services</li>
222     * </ul>
223     * <p>If <code>-l</code> or <code>-h -p -s</code> are not set, the
224     * AdminClient will invoke
225     * <code>http://localhost:8080/axis/servlet/AxisServlet</code>.</p>
226     *
227     * @param args Commands to process
228     * @return XML result or null in case of failure. In the case of multiple
229     * commands, the XML results will be concatenated, separated by \n
230     * @throws Exception Could be an IO exception, an AxisFault or something else
231     */

232
233    public String JavaDoc process(String JavaDoc[] args) throws Exception JavaDoc
234    {
235       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
236
237       Options opts = new Options(args);
238       opts.setDefaultURL("http://localhost:8080/axis/services/AdminService");
239
240       if (opts.isFlagSet('d') > 0)
241       {
242          // Set logger properties... !!!
243
}
244
245       args = opts.getRemainingArgs();
246
247       if (args == null || opts.isFlagSet('?') > 0)
248       {
249          System.out.println(Messages.getMessage("usage00", "AdminClient [Options] [list | <deployment-descriptor-files>]"));
250          System.out.println("");
251          System.out.println(getUsageInfo());
252          return null;
253       }
254
255       for (int i = 0; i < args.length; i++)
256       {
257          InputStream JavaDoc input = null;
258
259          if (args[i].equals("list"))
260             sb.append(list(opts));
261          else if (args[i].equals("quit"))
262             sb.append(quit(opts));
263          else if (args[i].equals("passwd"))
264          {
265             System.out.println(Messages.getMessage("changePwd00"));
266             if (args[i + 1] == null)
267             {
268                System.err.println(Messages.getMessage("needPwd00"));
269                return null;
270             }
271             String JavaDoc str = "<m:passwd xmlns:m=\"http://xml.apache.org/axis/wsdd/\">";
272             str += args[i + 1];
273             str += "</m:passwd>";
274             input = new ByteArrayInputStream JavaDoc(str.getBytes());
275             i++;
276             sb.append(process(opts, input));
277          }
278          else
279          {
280             if (args[i].indexOf(java.io.File.pathSeparatorChar) == -1)
281             {
282                System.out.println(Messages.getMessage("processFile00", args[i]));
283                sb.append(process(opts, args[i]));
284             }
285             else
286             {
287                java.util.StringTokenizer JavaDoc tokenizer = null;
288                tokenizer = new java.util.StringTokenizer JavaDoc(args[i],
289                        java.io.File.pathSeparator);
290                while (tokenizer.hasMoreTokens())
291                {
292                   String JavaDoc file = tokenizer.nextToken();
293                   System.out.println(Messages.getMessage("processFile00", file));
294                   sb.append(process(opts, file));
295                   if (tokenizer.hasMoreTokens())
296                      sb.append("\n");
297                }
298             }
299          }
300       }
301
302       return sb.toString();
303    }
304
305    public void processOpts(Options opts) throws Exception JavaDoc
306    {
307       if (call == null)
308          throw new Exception JavaDoc(Messages.getMessage("nullCall00"));
309
310       call.setTargetEndpointAddress(new URL JavaDoc(opts.getURL()));
311       call.setUsername(opts.getUser());
312       call.setPassword(opts.getPassword());
313
314       String JavaDoc tName = opts.isValueSet('t');
315       if (tName != null && !tName.equals(""))
316          call.setProperty(Call.TRANSPORT_NAME, tName);
317    }
318
319    public String JavaDoc process(InputStream JavaDoc input) throws Exception JavaDoc
320    {
321       return process(null, input);
322    }
323
324    public String JavaDoc process(URL JavaDoc xmlURL) throws Exception JavaDoc
325    {
326       return process(null, xmlURL.openStream());
327    }
328
329    public String JavaDoc process(String JavaDoc xmlFile) throws Exception JavaDoc
330    {
331
332       InputStream JavaDoc in = null;
333       if (new File JavaDoc(xmlFile).exists())
334          in = new FileInputStream JavaDoc(xmlFile);
335       else
336          in = getClass().getClassLoader().getResourceAsStream(xmlFile);
337
338       if (in == null)
339          throw new FileNotFoundException JavaDoc(xmlFile);
340
341       String JavaDoc result = process(null, in);
342       in.close();
343       return result;
344    }
345
346    public String JavaDoc process(Options opts, String JavaDoc xmlFile) throws Exception JavaDoc
347    {
348       processOpts(opts);
349       return process(xmlFile);
350    }
351
352    public String JavaDoc process(Options opts, InputStream JavaDoc input) throws Exception JavaDoc
353    {
354       if (call == null)
355          throw new Exception JavaDoc(Messages.getMessage("nullCall00"));
356
357       if (opts != null) processOpts(opts);
358
359       call.setUseSOAPAction(true);
360       call.setSOAPActionURI("AdminService");
361
362       Vector JavaDoc result = null;
363       Object JavaDoc[] params = new Object JavaDoc[]{new SOAPBodyElementAxisImpl(input)};
364       result = (Vector JavaDoc)call.invoke(params);
365
366       input.close();
367
368       if (result == null || result.isEmpty())
369          throw new AxisFault(Messages.getMessage("nullResponse00"));
370
371       SOAPBodyElementAxisImpl body = (SOAPBodyElementAxisImpl)result.elementAt(0);
372       return body.toString();
373    }
374
375    /**
376     * Creates in instance of <code>AdminClient</code> and
377     * invokes <code>process(args)</code>.
378     * <p>Diagnostic output goes to <code>log.info</code>.</p>
379     *
380     * @param args Commands to process
381     */

382    public static void main(String JavaDoc[] args)
383    {
384       try
385       {
386          AdminClient admin = new AdminClient();
387
388          String JavaDoc result = admin.process(args);
389          if (result != null)
390          {
391             System.out.println(result);
392          }
393          else
394          {
395             System.exit(1);
396          }
397       }
398       catch (Exception JavaDoc e)
399       {
400          log.error(Messages.getMessage("exception00"), e);
401          System.exit(1);
402       }
403    }
404 }
405
406
Popular Tags