KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > wsdl > AbstractWsdl2AJava


1 /*
2  * AbstractWsdl2AJava.java
3  *
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package org.apache.beehive.wsm.wsdl;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.Writer JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.util.Properties JavaDoc;
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 /**
43  * @author dmehrtas
44  *
45  */

46 public abstract class AbstractWsdl2AJava {
47
48     VelocityEngine ve = null;
49     Template template = null;
50
51     
52     protected abstract BeehiveWsTypeMetadata getWSObjectModel(InputStream JavaDoc wsdl) throws Exception JavaDoc;
53
54     
55     /**
56      * @throws Exception
57      *
58      */

59     public AbstractWsdl2AJava() throws Exception JavaDoc {
60         super();
61     }
62
63     /**
64      * @throws Exception
65      * @throws ResourceNotFoundException
66      * @throws ParseErrorException
67      * @throws MethodInvocationException
68      */

69     public void init() throws Exception JavaDoc, ResourceNotFoundException,
70             ParseErrorException, MethodInvocationException {
71
72         Properties JavaDoc p = new Properties JavaDoc();
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 JavaDoc templateFileName = "org/apache/beehive/wsm/wsdl/wsdl2ajava.vm";
79
80         try {
81             template = ve.getTemplate(templateFileName);
82         } catch (ResourceNotFoundException e) {
83             // couldn't find the template
84
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             // syntax error : problem parsing the template
91
e.printStackTrace();
92             throw e;
93         } catch (MethodInvocationException e) {
94             // something invoked in the template
95
// threw an exception
96
e.printStackTrace();
97             throw e;
98         } catch (Exception JavaDoc e) {
99             e.printStackTrace();
100             throw e;
101         }
102     }
103
104     public void generateAnnotatedJavaFromWSDL(String JavaDoc wsdlFileName,
105             String JavaDoc baseSrcDir) throws Exception JavaDoc {
106
107         InputStream JavaDoc wsdl = new FileInputStream JavaDoc(wsdlFileName);
108         BeehiveWsTypeMetadata om = getWSObjectModel(wsdl);
109                 
110         String JavaDoc className = om.getWsName();
111         String JavaDoc packageName = "web"; // Later see if the target name service is
112
// better option
113
String JavaDoc packageDirName = packageName.replaceAll("\\.", "/"); // nop right
114
// now,
115
// useful if
116
// the
117
// target
118
// package
119
// name
120

121         File JavaDoc packageDir = new File JavaDoc(baseSrcDir + "/" + packageDirName);
122         if (!packageDir.exists())
123             packageDir.mkdirs();
124
125         String JavaDoc fullFilePath = baseSrcDir + "/" + packageDirName + "/"
126                 + className + ".java";
127
128         File JavaDoc srcFile = new File JavaDoc(fullFilePath);
129         srcFile.createNewFile();
130         Writer JavaDoc w = new FileWriter JavaDoc(srcFile, false);
131         generateAnnotatedJavaFromOM(om, w);
132     }
133
134
135     public void generateAnnotatedJavaFromWSDL(InputStream JavaDoc wsdl, Writer JavaDoc w)
136             throws Exception JavaDoc {
137
138         BeehiveWsTypeMetadata om = getWSObjectModel(wsdl);
139         generateAnnotatedJavaFromOM(om, w);
140     }
141
142     /**
143      * @param w
144      * @param om
145      * @throws ResourceNotFoundException
146      * @throws ParseErrorException
147      * @throws MethodInvocationException
148      * @throws Exception
149      * @throws IOException
150      */

151     public void generateAnnotatedJavaFromOM(BeehiveWsTypeMetadata om, Writer JavaDoc w)
152             throws ResourceNotFoundException, ParseErrorException,
153             MethodInvocationException, Exception JavaDoc, IOException JavaDoc {
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