KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > wsgen > generator > Generator


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2003-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: Generator.java,v 1.7 2004/10/11 13:16:14 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ws.wsgen.generator;
27
28 import java.io.File JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import org.objectweb.common.Cmd;
33
34 import org.objectweb.jonas_lib.I18n;
35 import org.objectweb.jonas_lib.genbase.GenBaseException;
36 import org.objectweb.jonas_lib.genbase.generator.AbsGenerator;
37 import org.objectweb.jonas_lib.genbase.generator.Config;
38
39 import org.objectweb.jonas_ws.wsgen.WsGenException;
40
41 import org.objectweb.util.monolog.api.BasicLevel;
42
43 /**
44  * Generators provide a structure to be extended for specific generation
45  * mecanisms. Axis will provide an AxisWsClientGenerator that will be in charge
46  * of client side generation (WSDL2Java tool) and an AxisWsEndpointGenerator
47  * that will bother with server side artifact generation.
48  *
49  * @author Guillaume Sauthier
50  */

51 public abstract class Generator extends AbsGenerator {
52
53     /**
54      * i18n
55      */

56     private static I18n i18n = I18n.getInstance(Generator.class);
57
58     /**
59      * Creates a new Generator with the given Config.
60      *
61      * @param config internal configuration object.
62      *
63      * @throws GenBaseException When sources and target temporary directory cannot
64      * be created
65      */

66     public Generator(Config config) throws GenBaseException {
67         super(config);
68     }
69
70     /**
71      * Generate files.
72      *
73      * @throws WsGenException When generation fails.
74      */

75     public abstract void generate() throws WsGenException;
76
77     /**
78      * Compile generated java files into classes directory.
79      *
80      * @throws WsGenException When compilation fails
81      */

82     public void compile() throws WsGenException {
83         try {
84             // get all java files contained in the sources dir
85
Vector JavaDoc srcs = new Vector JavaDoc();
86             addJavaSources(getSources(), srcs);
87
88             Cmd cmd = new Cmd(getConfig().getJavaHomeBin() + getConfig().getNameJavac());
89             cmd.addArgument("-classpath");
90             cmd.addArgument(getConfig().getClasspath());
91             cmd.addArgument("-d");
92             cmd.addArgument(getClasses().getCanonicalPath());
93             cmd.addArguments(getConfig().getJavacOpts());
94
95             if (srcs.size() != 0) {
96                 for (Enumeration JavaDoc e = srcs.elements(); e.hasMoreElements();) {
97                     String JavaDoc srcName = ((File JavaDoc) e.nextElement()).getCanonicalPath();
98                     cmd.addArgument(srcName);
99                 }
100
101                 getLogger().log(BasicLevel.DEBUG, "Running '" + cmd.toString() + "'");
102                 if (cmd.run()) {
103                     getLogger().log(BasicLevel.INFO, "WebServices Classes successfully compiled.");
104                 } else {
105                     String JavaDoc err = getI18n().getMessage("Generator.compile.error");
106                     getLogger().log(BasicLevel.ERROR, err);
107                     throw new WsGenException(err);
108                 }
109             }
110         } catch (Exception JavaDoc e) {
111             String JavaDoc err = getI18n().getMessage("Generator.compile.error");
112             getLogger().log(BasicLevel.ERROR, err);
113             throw new WsGenException(err, e);
114         }
115     }
116
117
118     /**
119      * @return the i18n.
120      */

121     protected static I18n getI18n() {
122         return i18n;
123     }
124 }
Popular Tags