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.utils;
18  
19  import java.io.File;
20  import java.util.jar.JarOutputStream;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.geronimo.ews.ws4j2ee.context.J2EEWebServiceContext;
25  import org.apache.geronimo.ews.ws4j2ee.toWs.GenerationFault;
26  import org.apache.tools.ant.Project;
27  import org.apache.tools.ant.taskdefs.Ant;
28  
29  /***
30   * <p>To call this Class and execute a ant task the $JAVA_HOME/lib/tool.jar need
31   * to be in the class path. And so far I use the commnad line to call the Ant.
32   * It should be replaced by call to the Project class.</p>
33   *
34   * @author hemapani
35   */
36  public class AntExecuter {
37      private J2EEWebServiceContext wscontext;
38      private JarOutputStream jarFile;
39  
40      protected static Log log =
41              LogFactory.getLog(AntExecuter.class.getName());
42  
43      public AntExecuter(J2EEWebServiceContext wscontext) {
44          this.wscontext = wscontext;
45      }
46  
47      public void execute(String buildFile) throws GenerationFault {
48          //wait till the ant jar added
49          try {
50              Class.forName("com.sun.tools.javac.Main");
51              Project project = new Project();
52              project.setCoreLoader(Thread.currentThread().getContextClassLoader());
53              project.init();
54              Ant ant = new Ant();
55              ant.setProject(project);
56              ant.init();
57              ant.setInheritAll(true);
58              ant.setInheritRefs(true);
59              File file = new File(buildFile);
60              ant.setAntfile(file.getAbsolutePath());
61              ant.setDir(file.getParentFile());
62              ant.execute();
63          } catch (ClassNotFoundException e) {
64              System.out.println("Ant file will not be run programatcally as the " +
65                      "$JAVA_HOME/lib/tool.jar is not in the class path. To run the ant " +
66                      "prgramatically add that jar to classpath");
67  //NOW as the code is used by Geronimo we can not afford to let the build
68  //failure tests beside if you use maven it works fine. it will find your maven 
69  //repository itself :) 
70  //        }catch(BuildException e){
71  //			System.out.println(e.getMessage() +
72  //			"if it is a compile error you may not have set the maven reposiroty " +
73  //			"directory in the conf/ws4j2ee.propertites Build fill ignore the faliure");
74          }
75      }
76  
77  //    public void execute() throws GenerationFault {
78  //        try {
79  //            Class.forName("com.sun.tools.javac.Main");
80  //            File outDir = new File(wscontext.getMiscInfo().getOutPutPath());
81  //            File dest = new File(outDir, "build/classes");
82  //            compile(outDir, dest);
83  //            createModule(outDir, dest);
84  //        } catch (IOException e) {
85  //            log.error(e);
86  //            throw GenerationFault.createGenerationFault(e);
87  //        } catch (ClassNotFoundException e) {
88  //            System.out.println("Ant file will not be run programatcally as the " +
89  //                    "$JAVA_HOME/lib/tool.jar is not in the class path. To run the ant " +
90  //                    "prgramatically add that jar to classpath");
91  //NOW as the code is used by Geronimo we can not afford to let the build
92  //failure tests beside if you use maven it works fine. it will find your maven 
93  //repository itself :) 
94  //        }catch(BuildException e){
95  //          System.out.println(e.getMessage() +
96  //          "if it is a compile error you may not have set the maven reposiroty " +
97  //          "directory in the conf/ws4j2ee.propertites Build fill ignore the faliure");
98  //        }
99  //    }
100 //
101 //    private void createModule(File outDir, File dest) throws IOException {
102 //        String jarName = wscontext.getWSDLContext().getTargetPortType().getName().toLowerCase();
103 //        int index = jarName.lastIndexOf(".");
104 //        if (index > 0) {
105 //            jarName = jarName.substring(index + 1);
106 //        }
107 //        ModulePackager module = new ModulePackager(new File(outDir, jarName + "-ewsImpl.jar"));
108 //        module.addClassFiles(dest);
109 //        ArrayList classpathelements = wscontext.getMiscInfo().getClasspathElements();
110 //        if (classpathelements != null) {
111 //            for (int i = 0; i < classpathelements.size(); i++) {
112 //                module.addJarFile((File) classpathelements.get(i));
113 //            }
114 //        }
115 //        File dir = outDir;
116 //        File[] files = dir.listFiles();
117 //        if (files != null) {
118 //            for (int i = 0; i < files.length; i++) {
119 //                String file = files[i].getName();
120 //                if (files[i].isFile() &&
121 //                        !(file.endsWith(".jar") ||
122 //                        file.endsWith(".zip") ||
123 //                        file.endsWith(".war") ||
124 //                        file.endsWith(".ear") ||
125 //                        file.endsWith(".java"))
126 //                ) {
127 //                    module.addFileToJar(file, files[i]);
128 //                }
129 //            }
130 //        }
131 //        dir = new File(outDir, "META-INF");
132 //        files = dir.listFiles();
133 //        if (files != null) {
134 //            for (int i = 0; i < files.length; i++) {
135 //                String file = files[i].getName();
136 //                if (files[i].isFile() &&
137 //                        !(file.endsWith(".jar") ||
138 //                        file.endsWith(".zip") ||
139 //                        file.endsWith(".war") ||
140 //                        file.endsWith(".ear") ||
141 //                        file.endsWith(".java"))
142 //                ) {
143 //                    module.addFileToJar("META-INF/" + file, files[i]);
144 //                }
145 //            }
146 //        }
147 //        module.finalizeJar();
148 //    }
149 //
150 //    private void compile(File outDir, File destDir) throws IOException {
151 //        Project project = new Project();
152 //        project.init();
153 //        project.setCoreLoader(Thread.currentThread().getContextClassLoader());
154 //        Javac comp = new Javac();
155 //        comp.setProject(project);
156 //        destDir.mkdirs();
157 //        comp.setDestdir(destDir);
158 //        Path path = new Path(project);
159 //        path.setLocation(outDir);
160 //        comp.setSrcdir(path);
161 //        File repository = findMavenRepository();
162 //        if (repository != null) {
163 //            Path compileClasspath = new Path(project);
164 //            FileSet fileset = new FileSet();
165 //            fileset.setDir(repository);
166 //            fileset.setIncludes("axis/**/*.jar");
167 //            fileset.setIncludes("geronimo-spec/**/*.jar");
168 //            fileset.setIncludes("geronimo/**/*.jar");
169 //            fileset.setIncludes("sec/**/*.jar");
170 //            fileset.setIncludes("dom4j/**/*.jar");
171 //            fileset.setIncludes("jaxb-ri/**/*.jar");
172 //            fileset.setIncludes("xerces/**/*.jar");
173 //            fileset.setIncludes("ews/**/*.jar");
174 //            fileset.setIncludes("openejb/**/*.jar");
175 //            compileClasspath.addFileset(fileset);
176 //            comp.setClasspath(compileClasspath);
177 //        } else {
178 //        }
179 //        comp.init();
180 //        comp.execute();
181 //    }
182 //
183 //    private File findMavenRepository() throws IOException {
184 //        Properties pro = new Properties();
185 //        InputStream prpertyIn = null;
186 //        File file = new File(GenerationConstants.WS4J2EE_PROPERTY_FILE);
187 //        if (file.exists()) {
188 //            prpertyIn = new FileInputStream(file);
189 //        } else {
190 //            file = new File("modules/axis/target/" + GenerationConstants.WS4J2EE_PROPERTY_FILE);
191 //            if (file.exists()) {
192 //                prpertyIn = new FileInputStream(file);
193 //            } else {
194 //                file = new File("target/" + GenerationConstants.WS4J2EE_PROPERTY_FILE);
195 //                if (file.exists()) {
196 //                    prpertyIn = new FileInputStream(file);
197 //                } else {
198 //                    prpertyIn = getClass().getClassLoader().getResourceAsStream(GenerationConstants.WS4J2EE_PROPERTY_FILE);
199 //                }
200 //            }
201 //        }
202 //        if (prpertyIn != null) {
203 //            String location = null;
204 //            try {
205 //                pro.load(prpertyIn);
206 //                location = pro.getProperty(GenerationConstants.MAVEN_LOCAL_REPOSITARY);
207 //            } finally {
208 //                prpertyIn.close();
209 //            }
210 //            if (location != null) {
211 //                File locationFile = new File(location);
212 //                if (locationFile.exists()) {
213 //                    return locationFile;
214 //                }
215 //            }
216 //        }
217 //        return null;
218 //    }
219 
220 }