View Javadoc

1   /*
2    * Copyright 2001-2004 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  
17  package org.apache.geronimo.ews.ws4j2ee.toWs;
18  
19  import org.apache.axis.wsdl.fromJava.Emitter;
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.geronimo.ews.ws4j2ee.context.J2EEWebServiceContext;
23  import org.apache.geronimo.ews.ws4j2ee.context.j2eeDD.EJBContext;
24  import org.apache.geronimo.ews.ws4j2ee.context.j2eeDD.WebContext;
25  import org.apache.geronimo.ews.ws4j2ee.context.webservices.server.interfaces.WSCFContext;
26  import org.apache.geronimo.ews.ws4j2ee.context.webservices.server.interfaces.WSCFPortComponent;
27  import org.apache.geronimo.ews.ws4j2ee.context.webservices.server.interfaces.WSCFWebserviceDescription;
28  import org.apache.geronimo.ews.ws4j2ee.module.Module;
29  import org.apache.geronimo.ews.ws4j2ee.toWs.impl.Ws4J2eeFactoryImpl;
30  import org.apache.geronimo.ews.ws4j2ee.utils.FileUtils;
31  import org.apache.geronimo.ews.ws4j2ee.utils.MiscFactory;
32  
33  import java.io.File;
34  import java.io.FileNotFoundException;
35  import java.io.FileOutputStream;
36  import java.io.IOException;
37  import java.io.InputStream;
38  
39  /***
40   * <p>this class genarate the code when the WSDL does not presents.</p>
41   */
42  public class Ws4J2ee implements Generator {
43      protected static Log log =
44              LogFactory.getLog(Ws4J2ee.class.getName());
45      private J2EEWebServiceContext wscontext;
46      private boolean verbose = false;
47      private Ws4J2eeDeployContext clparser;
48      private WSCFPortComponent port;
49      private ClassLoader classloader;
50  
51      private Module module;
52      private String wsdlImplFilename;
53  
54      private InputStream wscffile;
55      private InputStream ejbddin;
56      private InputStream webddin;
57      private InputStream wsdlFile;
58      private InputStream jaxrpcmappingFile;
59      private String ejbLink;
60      private Emitter emitter;
61      private Ws4J2eeFactory factory;
62  
63      public Ws4J2ee(Ws4J2eeDeployContext doployContext, Emitter emitter)
64              throws GenerationFault {
65          if (emitter == null) {
66              this.emitter = new Emitter();
67          } else {
68              this.emitter = emitter;
69          }
70          this.clparser = doployContext;
71          //create the context
72          prepareContext();
73          //parse the arguments 
74          parseCLargs();
75      }
76  
77      /***
78       * genarate. what is genarated is depend on genarators included.
79       *
80       * @see org.apache.geronimo.ews.ws4j2ee.toWs.Generator#genarate()
81       */
82      public void generate() throws GenerationFault {
83          try {
84              //create the wscf context
85              WSCFWebserviceDescription wscfwsdis = parseTheWSCF();
86              populateWebserviceInfo(wscfwsdis);
87              pareseJ2eeModule();
88              checkAndGenerateWsdlAndMapping(wscfwsdis);
89              Ws4J2eeEmitter ws4j2eeEmitter = new Ws4J2eeEmitter(wscontext);
90              ws4j2eeEmitter.emmit();
91          } finally {
92              cleanup();
93          }
94      }
95  
96      private void prepareContext() {
97          factory = new Ws4J2eeFactoryImpl();
98          this.wscontext = factory.getContextFactory().getJ2EEWsContext(false);
99          this.wscontext.setFactory(factory);
100         this.wscontext.setMiscInfo(factory.getContextFactory().createMiscInfo(clparser.getProperties()));
101         wscontext.getMiscInfo().setVerbose(verbose);
102     }
103 
104     private void parseCLargs() throws GenerationFault {
105         module = clparser.getModule();
106         classloader = module.getClassLoaderWithPackageLoaded();
107 
108         wscontext.getMiscInfo().setClassloader(classloader);
109         wscontext.getMiscInfo().setOutputPath(clparser.getOutPutLocation());
110         wscontext.getMiscInfo().setImplStyle(clparser.getImplStyle());
111         wscontext.getMiscInfo().setTargetJ2EEContainer(clparser.getContanier());
112         wscontext.getMiscInfo().setCompile(clparser.isCompile());
113         wsdlImplFilename = clparser.getWsdlImplFilename();
114         this.wscffile = module.getWscfFile();
115         ejbddin = module.getEjbJarfile();
116         webddin = module.getWebddfile();
117         wscontext.getMiscInfo().setWsconffile(MiscFactory.getInputFile(this.wscffile));
118         wscontext.getMiscInfo().setClassPathElements(module.getClassPathElements());
119     }
120 
121     private WSCFWebserviceDescription parseTheWSCF() throws GenerationFault {
122         WSCFContext wscfcontext =
123                 factory.getParserFactory().parseWSCF(wscontext, this.wscffile);
124         wscontext.setWSCFContext(wscfcontext);
125         if (verbose)
126             log.info(wscffile + " parsed ..");
127         WSCFWebserviceDescription[] wscfwsdiss =
128                 wscontext.getWSCFContext().getWebServicesDescription();
129         //TODO fix this to handle multiple discriptions let us take the first discription
130         if (wscfwsdiss == null || wscfwsdiss.length == 0)
131             throw new UnrecoverableGenerationFault("no webservice discription "
132                     + "found in the webservices.xml file");
133         wscontext.getWSCFContext().setWscfdWsDescription(wscfwsdiss[0]);
134         return wscfwsdiss[0];
135     }
136 
137     private void generateTheWSDLfile() throws GenerationFault {
138         Generator wsdlgen
139                 = factory.getGenerationFactory().createWSDLGenerator(wscontext, emitter, clparser);
140         wsdlgen.generate();
141     }
142 
143     /***
144      * generate the jaxrpcmapping file
145      */
146     private void generateTheJaxrpcmappingFile() throws GenerationFault {
147         // everything is good
148         if (verbose)
149             log.info("genarating jaxrpc-mapper.xml ..............");
150         Generator jaxrpcfilegen = factory.getGenerationFactory()
151                 .createJaxrpcMapperGenerator(wscontext);
152         jaxrpcfilegen.generate();
153     }
154 
155     private void parseEJBModule() throws GenerationFault {
156         try {
157             File file = null;
158             if (ejbddin != null) {
159                 EJBContext ejbcontext = factory.getParserFactory()
160                         .parseEJBDDContext(wscontext, ejbddin);
161                 wscontext.setEJBDDContext(ejbcontext);
162                 ejbddin.close();
163                 checkForImplBean(ejbcontext.getImplBean());
164             } else {
165                 throw new GenerationFault("ejb-jar.xml file file does not exsits");
166             }
167         } catch (IOException e) {
168             log.error(e);
169             throw GenerationFault.createGenerationFault(e);
170         }
171     }
172 
173     public void parseWebModule() throws GenerationFault {
174         wscontext.getMiscInfo().setImplwithEJB(false);
175         if (webddin != null) {
176             WebContext webcontext = factory.getParserFactory().parseWebDD(wscontext, webddin);
177             wscontext.setWebDDContext(webcontext);
178             checkForImplBean(webcontext.getServletClass());
179         } else {
180             throw new GenerationFault("web.xml file does not exsits");
181         }
182     }
183 
184     private void checkForImplBean(String implBean) {
185         //TODO fix this java.lang.NoClassDefFoundError: javax/ejb/SessionBean
186         //from the dependancy class
187         if (implBean == null) {
188             wscontext.getMiscInfo().setImplAvalible(false);
189         } else {
190             try {
191                 Class.forName(implBean, true, classloader);
192             } catch (ClassNotFoundException e) {
193                 wscontext.getMiscInfo().setImplAvalible(false);
194             } catch (java.lang.NoClassDefFoundError e) {
195                 wscontext.getMiscInfo().setImplAvalible(false);
196             }
197         }
198     }
199 
200     private void populateWebserviceInfo(WSCFWebserviceDescription wscfwsdis) {
201         wscontext.getWSCFContext().setWscfdWsDescription(wscfwsdis);
202         wscontext.getMiscInfo().setSEIExists(true);
203         WSCFPortComponent[] ports = wscfwsdis.getPortComponent();
204         //TODO how to create the correct port type 
205         if (ports == null || ports.length == 0)
206             throw new UnrecoverableGenerationFault("no port discription"
207                     + " found in the webservices.xml file");
208         this.port = ports[0];
209         wscontext.getWSCFContext().setWscfport(port);
210         this.ejbLink = port.getServiceImplBean().getEjblink();
211         String seiName = port.getServiceEndpointInterface();
212         wscontext.getMiscInfo().setJaxrpcSEI(seiName);
213         wscontext.getMiscInfo().setHandlers(port.getHandlers());
214     }
215 
216     private void pareseJ2eeModule() throws GenerationFault {
217         if (ejbLink != null) {
218             wscontext.getMiscInfo().setJ2eeComponetLink(ejbLink);
219             wscontext.getMiscInfo().setImplwithEJB(true);
220             parseEJBModule();
221         } else {
222             wscontext.getMiscInfo().setJ2eeComponetLink(port.getServiceImplBean().getServletlink());
223             wscontext.getMiscInfo().setImplwithEJB(false);
224             parseWebModule();
225         }
226     }
227 
228     private void checkAndGenerateWsdlAndMapping(WSCFWebserviceDescription wscfwsdis)
229             throws GenerationFault {
230         try {
231             String wsdlFilename = wscfwsdis.getWsdlFile();
232             String jaxrpcMappingFileName = wscfwsdis.getJaxrpcMappingFile();
233             this.wsdlFile = module.findFileInModule(wsdlFilename);
234             if (this.wsdlFile == null) {
235                 String wsdlabsoluteFile = wscontext.getMiscInfo().getOutPutPath() + "/" + wsdlFilename;
236                 wscontext.getMiscInfo().setWsdlFile(MiscFactory.getInputFile(wsdlabsoluteFile));
237                 File jaxrpcfile = new File(wscontext.getMiscInfo().getOutPutPath() + "/" + jaxrpcMappingFileName);
238                 wscontext.getMiscInfo().setJaxrpcfile(MiscFactory.getInputFile(jaxrpcfile.getAbsolutePath()));
239                 generateTheWSDLfile();
240                 generateTheJaxrpcmappingFile();
241                 wscontext.getMiscInfo().setSEIExists(true);
242             } else {
243                 File file = new File(wscontext.getMiscInfo().getOutPutPath() + "/" + wsdlFilename);
244                 file.getParentFile().mkdirs();
245                 FileUtils.copyFile(wsdlFile, new FileOutputStream(file));
246                 wscontext.getMiscInfo().setWsdlFile(MiscFactory.getInputFile(file.getAbsolutePath()));
247                 jaxrpcmappingFile = module.findFileInModule(jaxrpcMappingFileName);
248                 file = new File(wscontext.getMiscInfo().getOutPutPath() + "/" + jaxrpcMappingFileName);
249                 file.getParentFile().mkdirs();
250                 FileUtils.copyFile(jaxrpcmappingFile, new FileOutputStream(file));
251                 if (jaxrpcmappingFile == null) {
252                     throw new GenerationFault("if the wsdlfile avalible the jaxrpcmapping file must be avalible");
253                 } else {
254                     wscontext.getMiscInfo().setJaxrpcfile(MiscFactory.getInputFile(file.getAbsolutePath()));
255                 }
256                 wscontext.getMiscInfo().setSEIExists(false);
257             }
258         } catch (FileNotFoundException e) {
259             log.error(e);
260             throw GenerationFault.createGenerationFault(e);
261         }
262     }
263 
264     public void cleanup() throws GenerationFault {
265         try {
266             wscffile.close();
267             if (ejbddin != null) {
268                 ejbddin.close();
269             }
270             if (webddin != null) {
271                 webddin.close();
272             }
273             if (jaxrpcmappingFile != null) {
274                 jaxrpcmappingFile.close();
275             }
276             if (wsdlFile != null) {
277                 wsdlFile.close();
278             }
279         } catch (Exception e) {
280             log.error(e);
281             throw GenerationFault.createGenerationFault(e);
282         }
283     }
284 
285     /***
286      * args is String array s.t.
287      * 1)first argument is webservices.xml file
288      * 2)Other arguments are any option that can given to Java2WSDL
289      * 3)the SEI and the service Implementation bean should be avalible on the class path
290      * 4)the ws4j2ee will search for the web.xml or ejb-jar.xml
291      * a)same directory as the webservices.xml file
292      * b)file should be in the class path s.t META-INF/web.xml or META-INF/ejb-jar.xml
293      * 5)if no file found at the #4 the ws4j2ee continue assuming the Impl bean and the
294      * DD is not avalible. This is additional to spec.
295      *
296      * @param args
297      * @throws Exception
298      */
299     public static void main(String[] args) throws Exception {
300         Ws4J2ee gen = null;
301         Emitter emitter = new Emitter();
302         Ws4J2eeDeployContext deployContext = new Ws4J2eeServerCLOptionParser(args, emitter);
303         gen = new Ws4J2ee(deployContext, emitter);
304         gen.generate();
305     }
306 }