KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > system > webservice > generator > ExtensionMaker


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

18 package org.apache.beehive.controls.system.webservice.generator;
19
20 import java.io.*;
21 import java.util.List JavaDoc;
22
23 import javax.jws.WebParam;
24 import javax.xml.namespace.QName JavaDoc;
25 import javax.xml.rpc.Service JavaDoc;
26 import javax.xml.rpc.ServiceFactory JavaDoc;
27 import javax.xml.rpc.encoding.TypeMapping JavaDoc;
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 /*******************************************************************************
52  *
53  *
54  * @author Jonathan Colwell
55  */

56 public class ExtensionMaker {
57
58     private static final String JavaDoc[] 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 JavaDoc mWSDLPath = "<path to WSDL>";
67
68     String JavaDoc packageName = null;
69
70     private String JavaDoc serviceURL = null;
71
72     public ExtensionMaker(File outputDir) {
73         mOutputDir = outputDir;
74     }
75
76     public void setWSDLPath(String JavaDoc wsdlPath) {
77         mWSDLPath = wsdlPath.replace('\\', '/');
78     }
79
80     /**
81      * @param serviceURL
82      * The serviceURL to set.
83      */

84     public void setServiceURL(String JavaDoc serviceURL) {
85         this.serviceURL = serviceURL;
86     }
87
88     public void writeJCX(BeehiveWsTypeMetadata wsm) throws Exception JavaDoc {
89         String JavaDoc serviceName = wsm.getWsServiceName();
90
91         // TODO: Should the class generation depend on Axis?
92
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 JavaDoc 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                     // String returnVal = "Object";
129
// Class javaType = method.getJavaReturnType();
130
// if (javaType != null) {
131
// returnVal = getClassName(javaType);
132
// }
133
//
134
// QName q = method.getXmlReturnType();
135
// if (q != null) {
136
// returnVal = Utils.makePackageName(q.getNamespaceURI())
137
// + q.getLocalPart();
138
// }
139
//
140
// jcxWriter.print(returnVal);
141
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 JavaDoc getClassName(Class JavaDoc cls) {
156         if (cls.isArray()) {
157             return getClassName(cls.getComponentType()) + "[]";
158         } else {
159             return cls.getName().replace('$', '.');
160         }
161     }
162
163     // NOTE: For OUT and IN/OUT parameters this method can use the GenericHolder
164
// class...TBD
165
private void printParameters(
166            List JavaDoc< ? extends BeehiveWsParameterMetadata> params, PrintWriter pw) {
167         int paramPos = 0;
168         for (BeehiveWsParameterMetadata param : params) {
169             if (paramPos > 0) {
170                 pw.print(", ");
171             }
172
173             // String paramType = "Object";
174
// Class javaType = param.getJavaType();
175
// if (javaType != null) {
176
// paramType = getClassName(javaType);
177
// }
178
//
179
// QName q = param.getXmlType();
180
// if (q != null) {
181
// paramType = Utils.makePackageName(q.getNamespaceURI())
182
// + q.getLocalPart();
183
// }
184
//
185
// pw.print(paramType);
186
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 JavaDoc paramName = param.getWpName();
193             if (paramName == null) {
194                 paramName = "param" + paramPos;
195             }
196             pw.print(paramName);
197             paramPos++;
198         }
199     }
200
201     private String JavaDoc getHolderForType(Class JavaDoc 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 // OptionBuilder.hasArg();
230
// OptionBuilder.withArgName("URL");
231
// OptionBuilder.withDescription("URL to the web service.");
232
// option = OptionBuilder.create("serviceURL");
233
// OptionBuilder.isRequired(false); // if specified, it will overwrite
234
// // the one in WSDL
235
// options.addOption(option);
236

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 JavaDoc[] args) throws Exception JavaDoc {
255
256         String JavaDoc outFileName = null;
257         String JavaDoc wsdlDirName = null;
258         String JavaDoc serviceURL = null;
259         String JavaDoc wsdlPathAnnotation = null;
260         String JavaDoc 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("serviceURL"))
270
// serviceURL = line.getOptionValue("serviceURL");
271
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             // oops, something went wrong
280
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     /**
292      * @param wsdlDirName
293      * @param serviceURL
294      * @param pkgName
295      * @param em
296      * @throws IOException
297      * @throws Exception
298      * @throws FileNotFoundException
299      * @throws XmlException
300      * @throws IllegalAccessException
301      * @throws NoSuchFieldException
302      */

303     private void genJCX(String JavaDoc wsdlDirName, String JavaDoc serviceURL, String JavaDoc pkgName,
304             String JavaDoc wsdlPathAnnotation) throws IOException, Exception JavaDoc,
305             FileNotFoundException, XmlException, IllegalAccessException JavaDoc,
306             NoSuchFieldException JavaDoc {
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 JavaDoc("no WSDL location specified at: " + wsdlDir.getCanonicalPath());
319         }
320     }
321
322     /**
323      * @param serviceURL
324      * @param wsdlPathAnnotation
325      * @param wsdlFile
326      * @throws IOException
327      * @throws Exception
328      * @throws FileNotFoundException
329      * @throws XmlException
330      * @throws IllegalAccessException
331      * @throws NoSuchFieldException
332      */

333     private void genJCXForWSDLFile(String JavaDoc wsdlPathAnnotation, File wsdlFile) throws IOException, Exception JavaDoc, FileNotFoundException, XmlException, IllegalAccessException JavaDoc, NoSuchFieldException JavaDoc {
334         if (wsdlPathAnnotation != null) {
335             setWSDLPath(wsdlPathAnnotation + "/" + wsdlFile.getName());
336         } else {
337             setWSDLPath(wsdlFile.getName());
338
339         }
340         // get additional wsdl information
341
WSDLParser parser = new WSDLParser(
342                 new FileInputStream(wsdlFile));
343 // if (null == serviceURL) { // none has been defined, get it
344
// // from wsdl
345
// }
346

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     /**
357      * @param packageName
358      * The packageName to set.
359      */

360     public void setPackageName(String JavaDoc packageName) {
361         this.packageName = packageName;
362     }
363 }
364
Popular Tags