1 19 package org.apache.beehive.wsm.wsdl; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileWriter ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.Writer ; 27 import java.net.MalformedURLException ; 28 import java.util.Properties ; 29 30 31 import org.apache.beehive.wsm.model.BeehiveWsTypeMetadata; 32 import org.apache.beehive.wsm.model.wsdl.XmlBeanWSDLProcessor; 33 import org.apache.velocity.Template; 34 import org.apache.velocity.VelocityContext; 35 import org.apache.velocity.app.VelocityEngine; 36 import org.apache.velocity.exception.MethodInvocationException; 37 import org.apache.velocity.exception.ParseErrorException; 38 import org.apache.velocity.exception.ResourceNotFoundException; 39 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; 40 import org.apache.xmlbeans.XmlException; 41 42 46 public abstract class AbstractWsdl2AJava { 47 48 VelocityEngine ve = null; 49 Template template = null; 50 51 52 protected abstract BeehiveWsTypeMetadata getWSObjectModel(InputStream wsdl) throws Exception ; 53 54 55 59 public AbstractWsdl2AJava() throws Exception { 60 super(); 61 } 62 63 69 public void init() throws Exception , ResourceNotFoundException, 70 ParseErrorException, MethodInvocationException { 71 72 Properties p = new Properties (); 73 p.setProperty(VelocityEngine.RESOURCE_LOADER, "class"); 74 p.setProperty("class." + VelocityEngine.RESOURCE_LOADER + ".class", 75 ClasspathResourceLoader.class.getName()); 76 ve = new VelocityEngine(); 77 ve.init(p); 78 String templateFileName = "org/apache/beehive/wsm/wsdl/wsdl2ajava.vm"; 79 80 try { 81 template = ve.getTemplate(templateFileName); 82 } catch (ResourceNotFoundException e) { 83 System.out.println("Failed to find the tempate file: " 85 + templateFileName + " in classpath: " 86 + System.getProperty("java.class.path")); 87 e.printStackTrace(); 88 throw e; 89 } catch (ParseErrorException e) { 90 e.printStackTrace(); 92 throw e; 93 } catch (MethodInvocationException e) { 94 e.printStackTrace(); 97 throw e; 98 } catch (Exception e) { 99 e.printStackTrace(); 100 throw e; 101 } 102 } 103 104 public void generateAnnotatedJavaFromWSDL(String wsdlFileName, 105 String baseSrcDir) throws Exception { 106 107 InputStream wsdl = new FileInputStream (wsdlFileName); 108 BeehiveWsTypeMetadata om = getWSObjectModel(wsdl); 109 110 String className = om.getWsName(); 111 String packageName = "web"; String packageDirName = packageName.replaceAll("\\.", "/"); 121 File packageDir = new File (baseSrcDir + "/" + packageDirName); 122 if (!packageDir.exists()) 123 packageDir.mkdirs(); 124 125 String fullFilePath = baseSrcDir + "/" + packageDirName + "/" 126 + className + ".java"; 127 128 File srcFile = new File (fullFilePath); 129 srcFile.createNewFile(); 130 Writer w = new FileWriter (srcFile, false); 131 generateAnnotatedJavaFromOM(om, w); 132 } 133 134 135 public void generateAnnotatedJavaFromWSDL(InputStream wsdl, Writer w) 136 throws Exception { 137 138 BeehiveWsTypeMetadata om = getWSObjectModel(wsdl); 139 generateAnnotatedJavaFromOM(om, w); 140 } 141 142 151 public void generateAnnotatedJavaFromOM(BeehiveWsTypeMetadata om, Writer w) 152 throws ResourceNotFoundException, ParseErrorException, 153 MethodInvocationException, Exception , IOException { 154 VelocityContext context = new VelocityContext(); 155 context.put("webServiceOM", om); 156 template.merge(context, w); 157 w.close(); 158 } 159 160 161 162 } 163 | Popular Tags |