1 18 package org.apache.beehive.controls.system.webservice.generator; 19 20 import java.io.*; 21 import java.util.List ; 22 23 import javax.jws.WebParam; 24 import javax.xml.namespace.QName ; 25 import javax.xml.rpc.Service ; 26 import javax.xml.rpc.ServiceFactory ; 27 import javax.xml.rpc.encoding.TypeMapping ; 28 29 import org.apache.axis.wsdl.toJava.Namespaces; 30 import org.apache.axis.wsdl.toJava.Utils; 31 import org.apache.beehive.wsm.axis.ant.WSDLFilter; 32 33 import org.apache.beehive.wsm.axis.databinding.SystemTypeLookupService; 34 import org.apache.beehive.wsm.model.BeehiveWsMethodMetadata; 35 import org.apache.beehive.wsm.model.BeehiveWsParameterMetadata; 36 import org.apache.beehive.wsm.model.BeehiveWsTypeMetadata; 37 import org.apache.beehive.wsm.model.wsdl.XmlBeanWSDLProcessor; 38 39 import org.apache.beehive.wsm.wsdl.WSDLParser; 40 import org.apache.commons.cli.CommandLine; 41 import org.apache.commons.cli.CommandLineParser; 42 import org.apache.commons.cli.GnuParser; 43 import org.apache.commons.cli.Option; 44 import org.apache.commons.cli.OptionBuilder; 45 import org.apache.commons.cli.Options; 46 import org.apache.commons.cli.ParseException; 47 import org.apache.commons.cli.PosixParser; 48 import org.apache.xmlbeans.XmlException; 49 50 51 56 public class ExtensionMaker { 57 58 private static final String [] standardImports = { 59 "org.apache.beehive.controls.api.bean.ControlExtension", 60 "org.apache.beehive.controls.system.webservice.ServiceControl", 61 "org.apache.beehive.controls.system.webservice.ServiceControl.Location", 62 "org.apache.beehive.controls.system.webservice.ServiceControl.WSDL", }; 63 64 File mOutputDir; 65 66 String mWSDLPath = "<path to WSDL>"; 67 68 String packageName = null; 69 70 private String serviceURL = null; 71 72 public ExtensionMaker(File outputDir) { 73 mOutputDir = outputDir; 74 } 75 76 public void setWSDLPath(String wsdlPath) { 77 mWSDLPath = wsdlPath.replace('\\', '/'); 78 } 79 80 84 public void setServiceURL(String serviceURL) { 85 this.serviceURL = serviceURL; 86 } 87 88 public void writeJCX(BeehiveWsTypeMetadata wsm) throws Exception { 89 String serviceName = wsm.getWsServiceName(); 90 91 if (packageName == null) { 93 packageName = Utils.makePackageName(wsm.getWsTargetNamespace()); 94 } 95 if (serviceName != null && packageName != null) { 96 File subDir = new File(mOutputDir, new Namespaces(null) 97 .toDir(packageName)); 98 subDir.mkdirs(); 99 if (subDir.isDirectory()) { 100 File jcx = new File(subDir, serviceName + ".jcx"); 101 PrintWriter jcxWriter = new PrintWriter(jcx, "UTF-8"); 102 103 jcxWriter.print("package "); 104 jcxWriter.print(packageName); 105 jcxWriter.print(";\n\n"); 106 107 for (String imp : standardImports) { 108 jcxWriter.print("import "); 109 jcxWriter.print(imp); 110 jcxWriter.print(";\n"); 111 } 112 113 jcxWriter.print("@ControlExtension\n"); 114 jcxWriter.print("@Location(urls = {\""); 115 jcxWriter.print(serviceURL); 116 jcxWriter.print("\"})\n"); 117 jcxWriter.print("@WSDL(path = \""); 118 jcxWriter.print(mWSDLPath); 119 jcxWriter.print("\",\n\tservice = \""); 120 jcxWriter.print(serviceName); 121 jcxWriter.print("\")\n"); 122 jcxWriter.print("\n\npublic interface "); 123 jcxWriter.print(serviceName); 124 jcxWriter.print(" extends ServiceControl {\n\n"); 125 126 for (BeehiveWsMethodMetadata method : wsm.getMethods()) { 127 jcxWriter.print("public "); 128 jcxWriter.print(method.getJavaReturnTypeFullName()); 142 jcxWriter.write(' '); 143 jcxWriter.print(method.getWmOperationName()); 144 jcxWriter.write('('); 145 printParameters(method.getParams(), jcxWriter); 146 jcxWriter.print(") throws Exception;\n\n"); 147 } 148 149 jcxWriter.print("}\n"); 150 jcxWriter.close(); 151 } 152 } 153 } 154 155 private String getClassName(Class cls) { 156 if (cls.isArray()) { 157 return getClassName(cls.getComponentType()) + "[]"; 158 } else { 159 return cls.getName().replace('$', '.'); 160 } 161 } 162 163 private void printParameters( 166 List < ? extends BeehiveWsParameterMetadata> params, PrintWriter pw) { 167 int paramPos = 0; 168 for (BeehiveWsParameterMetadata param : params) { 169 if (paramPos > 0) { 170 pw.print(", "); 171 } 172 173 if( param.getWpMode() == WebParam.Mode.INOUT || param.getWpMode() == WebParam.Mode.OUT) { 187 pw.print(getHolderForType(param.getJavaType())); 188 } else { 189 pw.print(param.getJavaTypeFullName()); 190 } 191 pw.write(' '); 192 String paramName = param.getWpName(); 193 if (paramName == null) { 194 paramName = "param" + paramPos; 195 } 196 pw.print(paramName); 197 paramPos++; 198 } 199 } 200 201 private String getHolderForType(Class clazz) { 202 if( clazz == int.class) return "javax.xml.rpc.holders.IntHolder"; 203 else if( clazz == boolean.class) return "javax.xml.rpc.holders.BooleanHolder"; 204 else if (clazz == new byte[0].getClass()) return "javax.xml.rpc.holders.ByteArrayHolder"; 205 else if (clazz == byte.class) return "javax.xml.rpc.holders.ByteHolder"; 206 else if (clazz == double.class) return "javax.xml.rpc.holders.DoubleHolder"; 207 else if (clazz == float.class) return "javax.xml.rpc.holders.FloatHolder"; 208 else if (clazz == long.class) return "javax.xml.rpc.holders.FloatHolder"; 209 else if (clazz == short.class) return "javax.xml.rpc.holders.ShortHolder"; 210 else 211 return "org.apache.beehive.wsm.databinding.GenericHolder<" + clazz.getCanonicalName() + ">"; 212 } 213 private static Options buildOptions() { 214 Options options = new Options(); 215 OptionBuilder.hasArg(); 216 OptionBuilder.withArgName("dir"); 217 OptionBuilder.withDescription("Base directory of the wsdl file(s)"); 218 OptionBuilder.isRequired(true); 219 Option option = OptionBuilder.create("wsdl"); 220 options.addOption(option); 221 222 OptionBuilder.hasArg(); 223 OptionBuilder.withArgName("dir"); 224 OptionBuilder.withDescription("Root directory for the jcx file."); 225 OptionBuilder.isRequired(true); 226 option = OptionBuilder.create("gen_root"); 227 options.addOption(option); 228 229 237 OptionBuilder.hasArg(); 238 OptionBuilder.withArgName("dir"); 239 OptionBuilder.isRequired(false); 240 OptionBuilder.withDescription("path annotation to use in the jcx"); 241 option = OptionBuilder.create("wsdl_path_annotation"); 242 options.addOption(option); 243 244 OptionBuilder.hasArg(); 245 OptionBuilder.withArgName("package_name"); 246 OptionBuilder.isRequired(false); 247 OptionBuilder.withDescription("Package name of the jcx"); 248 option = OptionBuilder.create("pkg"); 249 options.addOption(option); 250 251 return options; 252 } 253 254 public static void main(String [] args) throws Exception { 255 256 String outFileName = null; 257 String wsdlDirName = null; 258 String serviceURL = null; 259 String wsdlPathAnnotation = null; 260 String pkgName = null; 261 262 try { 263 Options options = buildOptions(); 264 CommandLineParser parser = new GnuParser(); 265 CommandLine line = parser.parse(options, args); 266 267 outFileName = line.getOptionValue("gen_root"); 268 wsdlDirName = line.getOptionValue("wsdl"); 269 if (line.hasOption("wsdl_path_annotation")) 272 wsdlPathAnnotation = line 273 .getOptionValue("wsdl_path_annotation"); 274 275 if (line.hasOption("pkg")) 276 pkgName = line.getOptionValue("pkg"); 277 278 } catch (ParseException exp) { 279 System.err.println("Parsing failed. Reason: " + exp.getMessage()); 281 System.exit(-1); 282 } 283 284 File out = new File(outFileName); 285 ExtensionMaker em = new ExtensionMaker(out); 286 287 em.genJCX(wsdlDirName, serviceURL, pkgName, wsdlPathAnnotation); 288 289 } 290 291 303 private void genJCX(String wsdlDirName, String serviceURL, String pkgName, 304 String wsdlPathAnnotation) throws IOException, Exception , 305 FileNotFoundException, XmlException, IllegalAccessException , 306 NoSuchFieldException { 307 setServiceURL(serviceURL); 308 setPackageName(pkgName); 309 File wsdlDir = new File(wsdlDirName); 310 if (wsdlDir.isDirectory()) { 311 for (File wsdlFile : wsdlDir.listFiles(new WSDLFilter())) { 312 genJCXForWSDLFile( wsdlPathAnnotation, wsdlFile); 313 } 314 } else if (wsdlDir.isFile()) { 315 genJCXForWSDLFile(wsdlPathAnnotation, wsdlDir); 316 317 } else { 318 throw new Exception ("no WSDL location specified at: " + wsdlDir.getCanonicalPath()); 319 } 320 } 321 322 333 private void genJCXForWSDLFile(String wsdlPathAnnotation, File wsdlFile) throws IOException, Exception , FileNotFoundException, XmlException, IllegalAccessException , NoSuchFieldException { 334 if (wsdlPathAnnotation != null) { 335 setWSDLPath(wsdlPathAnnotation + "/" + wsdlFile.getName()); 336 } else { 337 setWSDLPath(wsdlFile.getName()); 338 339 } 340 WSDLParser parser = new WSDLParser( 342 new FileInputStream(wsdlFile)); 343 347 serviceURL = parser.getSoapAddressLocation(); 348 349 350 XmlBeanWSDLProcessor wsdlProcessor = new XmlBeanWSDLProcessor( new FileInputStream(wsdlFile)); 351 352 353 writeJCX(wsdlProcessor.getObjectModel( new SystemTypeLookupService())); 354 } 355 356 360 public void setPackageName(String packageName) { 361 this.packageName = packageName; 362 } 363 } 364 | Popular Tags |