1 57 58 package org.apache.soap.server; 59 60 import java.net.URL ; 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 78 public class ServiceManagerClient { 79 URL routerURL; 80 Vector params = new Vector (); 81 Call call = new Call (); 82 String userName; 83 String password; 84 85 public ServiceManagerClient (URL routerURL) { 86 Serializer bs = new org.apache.soap.encoding.soapenc.BeanSerializer (); 87 88 this.routerURL = routerURL; 89 SOAPMappingRegistry smr = call.getSOAPMappingRegistry (); 90 91 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 userName) { 104 this.userName = userName; 105 } 106 107 public void setPassword (String password) { 108 this.password = password; 109 } 110 111 private Response invokeMethod (String 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 serviceName) throws SOAPException { 146 Parameter p1 = new Parameter ("name", String .class, serviceName, null); 147 invokeMethod ("undeploy", p1); 148 } 149 150 public String [] list () throws SOAPException { 151 Response resp = invokeMethod ("list", null); 152 if (!resp.generatedFault ()) { 153 Parameter result = resp.getReturnValue (); 154 return (String []) result.getValue (); 155 } else { 156 return null; 157 } 158 } 159 160 public DeploymentDescriptor query (String serviceName) throws SOAPException { 161 Parameter p1 = new Parameter ("name", String .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 191 public static void main (String [] args) throws Exception { 192 URL routerURL; 193 String op; 194 String userName = null; 195 String 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) { 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 (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 [] 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 |