KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > server > ServiceManagerClient


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 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 "SOAP" 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 and was
52  * originally based on software copyright (c) 2000, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.soap.server;
59
60 import java.net.URL JavaDoc;
61 import java.io.*;
62 import java.util.*;
63 import javax.xml.parsers.*;
64 import org.w3c.dom.*;
65 import org.xml.sax.*;
66 import org.apache.soap.util.xml.*;
67 import org.apache.soap.*;
68 import org.apache.soap.encoding.SOAPMappingRegistry;
69 import org.apache.soap.transport.http.SOAPHTTPConnection;
70 import org.apache.soap.rpc.*;
71
72 /**
73  * This is a client to talk to an Apache SOAP ServiceManager to manage services
74  * deployed on the server.
75  *
76  * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
77  */

78 public class ServiceManagerClient {
79   URL JavaDoc routerURL;
80   Vector params = new Vector ();
81   Call call = new Call ();
82   String JavaDoc userName;
83   String JavaDoc password;
84
85   public ServiceManagerClient (URL JavaDoc routerURL) {
86     Serializer bs = new org.apache.soap.encoding.soapenc.BeanSerializer ();
87
88     this.routerURL = routerURL;
89     SOAPMappingRegistry smr = call.getSOAPMappingRegistry ();
90
91     // register serializer/deserializer for DeploymentDescriptor.class
92
// and TypeMapping.class
93
smr.mapTypes (Constants.NS_URI_SOAP_ENC,
94                   new QName (Constants.NS_URI_XML_SOAP,
95                              "DeploymentDescriptor"),
96                   DeploymentDescriptor.class, bs, (Deserializer) bs);
97     bs = new TypeMappingSerializer ();
98     smr.mapTypes (Constants.NS_URI_SOAP_ENC,
99                   new QName (Constants.NS_URI_XML_SOAP, "TypeMapping"),
100                   TypeMapping.class, bs, (Deserializer) bs);
101   }
102
103   public void setUserName (String JavaDoc userName) {
104     this.userName = userName;
105   }
106
107   public void setPassword (String JavaDoc password) {
108     this.password = password;
109   }
110
111   private Response invokeMethod (String JavaDoc methodName, Parameter param)
112        throws SOAPException {
113     call.setTargetObjectURI (ServerConstants.SERVICE_MANAGER_SERVICE_NAME);
114     call.setMethodName (methodName);
115     call.setEncodingStyleURI (Constants.NS_URI_SOAP_ENC);
116     if (userName != null) {
117       SOAPHTTPConnection hc = new SOAPHTTPConnection ();
118       hc.setUserName (userName);
119       hc.setPassword (password);
120       call.setSOAPTransport (hc);
121     }
122     if (param != null) {
123       params.removeAllElements ();
124       params.addElement (param);
125       call.setParams (params);
126     } else {
127       call.setParams (null);
128     }
129     Response resp = call.invoke (routerURL, "");
130     if (resp.generatedFault ()) {
131       Fault fault = resp.getFault ();
132       System.out.println ("Ouch, the call failed: ");
133       System.out.println (" Fault Code = " + fault.getFaultCode ());
134       System.out.println (" Fault String = " + fault.getFaultString ());
135     }
136     return resp;
137   }
138
139   public void deploy (DeploymentDescriptor dd) throws SOAPException {
140     Parameter p1 = new Parameter ("descriptor", DeploymentDescriptor.class,
141                                   dd, null);
142     invokeMethod ("deploy", p1);
143   }
144
145   public void undeploy (String JavaDoc serviceName) throws SOAPException {
146     Parameter p1 = new Parameter ("name", String JavaDoc.class, serviceName, null);
147     invokeMethod ("undeploy", p1);
148   }
149   
150   public String JavaDoc[] list () throws SOAPException {
151     Response resp = invokeMethod ("list", null);
152     if (!resp.generatedFault ()) {
153       Parameter result = resp.getReturnValue ();
154       return (String JavaDoc[]) result.getValue ();
155     } else {
156       return null;
157     }
158   }
159
160   public DeploymentDescriptor query (String JavaDoc serviceName) throws SOAPException {
161     Parameter p1 = new Parameter ("name", String JavaDoc.class, serviceName, null);
162     Response resp = invokeMethod ("query", p1);
163     if (!resp.generatedFault ()) {
164       Parameter result = resp.getReturnValue ();
165       return (DeploymentDescriptor) result.getValue ();
166     } else {
167       return null;
168     }
169   }
170
171   private static void badUsage () {
172     System.err.println ("Usage: java " +
173                         ServiceManagerClient.class.getName () +
174                         " [-auth username:password] url operation arguments");
175     System.err.println ("where");
176     System.err.println ("\tusername and password is the HTTP Basic" +
177             " authentication info");
178     System.err.println ("\turl is the Apache SOAP router's URL whose" +
179                         " services are managed");
180     System.err.println ("\toperation and arguments are:");
181     System.err.println ("\t\tdeploy deployment-descriptor-file.xml");
182     System.err.println ("\t\tlist");
183     System.err.println ("\t\tquery service-name");
184     System.err.println ("\t\tundeploy service-name");
185     System.exit (1);
186   }
187
188   /**
189    * Command-line app for managing services on an Apache SOAP server.
190    */

191   public static void main (String JavaDoc[] args) throws Exception JavaDoc {
192     URL JavaDoc routerURL;
193     String JavaDoc op;
194     String JavaDoc userName = null;
195     String JavaDoc password = null;
196
197     if (args.length < 2) {
198       badUsage ();
199     }
200
201     int base = 0;
202     if (args[0].equals ("-auth")) {
203       if (args.length < 4) { // -auth user:pass + url + op is minimal
204
badUsage ();
205       }
206       StringTokenizer st = new StringTokenizer (args[1], ":");
207       if (st.countTokens () != 2) {
208     badUsage ();
209       }
210       userName = st.nextToken ();
211       password = st.nextToken ();
212       base = 2;
213     }
214
215     ServiceManagerClient smc =
216       new ServiceManagerClient (new URL JavaDoc (args[base]));
217     if (base == 2) {
218       smc.setUserName (userName);
219       smc.setPassword (password);
220     }
221
222     op = args[base+1];
223     if (op.equals ("deploy")) {
224       if (args.length != base+3) {
225         badUsage ();
226       }
227       FileReader fr = new FileReader (args[base+2]);
228       DocumentBuilder xdb = XMLParserUtils.getXMLDocBuilder();
229       Document doc = xdb.parse(new InputSource(fr));
230       smc.deploy (DeploymentDescriptor.fromXML (doc.getDocumentElement ()));
231     } else if (op.equals ("undeploy")) {
232       if (args.length != base+3) {
233         badUsage ();
234       }
235       smc.undeploy (args[base+2]);
236     } else if (op.equals ("list")) {
237       String JavaDoc[] sms = smc.list ();
238       if (sms != null) {
239         System.out.println ("Deployed Services:");
240         for (int i = 0; i < sms.length; i++) {
241           System.out.println ("\t" + sms[i]);
242         }
243       }
244     } else if (op.equals ("query")) {
245       if (args.length != base+3) {
246         badUsage ();
247       }
248       DeploymentDescriptor dd = smc.query (args[base+2]);
249       if (dd != null) {
250         dd.toXML (new OutputStreamWriter (System.out));
251       }
252     } else {
253       badUsage ();
254     }
255   }
256 }
257
Popular Tags