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.misc;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.apache.geronimo.ews.ws4j2ee.context.InputOutputFile;
22  import org.apache.geronimo.ews.ws4j2ee.context.J2EEWebServiceContext;
23  import org.apache.geronimo.ews.ws4j2ee.toWs.GenerationConstants;
24  import org.apache.geronimo.ews.ws4j2ee.toWs.GenerationFault;
25  import org.apache.geronimo.ews.ws4j2ee.toWs.Generator;
26  import org.apache.geronimo.ews.ws4j2ee.toWs.dd.JaxrpcMapperGenerator;
27  
28  import java.io.File;
29  import java.io.FileInputStream;
30  import java.io.FileWriter;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.PrintWriter;
34  import java.util.ArrayList;
35  import java.util.Properties;
36  import java.util.StringTokenizer;
37  
38  /***
39   * @author Srinath Perera(hemapani@opensource.lk)
40   */
41  public class BuildFileGenerator implements Generator {
42      private J2EEWebServiceContext j2eewscontext;
43  
44      protected static Log log =
45              LogFactory.getLog(JaxrpcMapperGenerator.class.getName());
46  
47      public BuildFileGenerator(J2EEWebServiceContext j2eewscontext) throws GenerationFault {
48          this.j2eewscontext = j2eewscontext;
49      }
50  
51      public void generate() throws GenerationFault {
52          try {
53              String buildfile = j2eewscontext.getMiscInfo().getOutPutPath() + "/build.xml";
54              if (j2eewscontext.getMiscInfo().isVerbose())
55                  log.info("genarating " + buildfile + ".................");
56              PrintWriter out = new PrintWriter(new FileWriter(buildfile));
57              out.write("<?xml version=\"1.0\"?>\n");
58              out.write("<project basedir=\".\" default=\"dist\">\n");
59              out.write("	<property name=\"build.sysclasspath\" value=\"last\"/>\n");
60              out.write("	<property name=\"src\" location=\".\"/>\n");
61              out.write("	<property name=\"build\" location=\"build\"/>\n");
62              out.write("	<property name=\"build.classes\" location=\"${build}/classes\"/>\n");
63              out.write("	<property name=\"build.lib\" location=\"${build}/lib\"/>\n");
64              out.write("	<property name=\"lib\" location=\"lib\"/>\n");
65              Properties pro = new Properties();
66              InputStream prpertyIn = null;
67              File file = new File(GenerationConstants.WS4J2EE_PROPERTY_FILE);
68              if (file.exists()) {
69                  prpertyIn = new FileInputStream(file);
70              } else {
71                  file = new File("modules/axis/target/" + GenerationConstants.WS4J2EE_PROPERTY_FILE);
72                  if (file.exists()) {
73                      prpertyIn = new FileInputStream(file);
74                  } else {
75                      file = new File("target/" + GenerationConstants.WS4J2EE_PROPERTY_FILE);
76                      if (file.exists()) {
77                          prpertyIn = new FileInputStream(file);
78                      } else {
79                          prpertyIn = getClass().getClassLoader().getResourceAsStream(GenerationConstants.WS4J2EE_PROPERTY_FILE);
80                      }
81                  }
82              }
83              if (prpertyIn != null) {
84                  String location = null;
85                  try {
86                      pro.load(prpertyIn);
87                      location = pro.getProperty(GenerationConstants.MAVEN_LOCAL_REPOSITARY);
88                  } finally {
89                      prpertyIn.close();
90                  }
91                  if (location != null) {
92                      out.write("	<property name=\"maven.repo.local\" location=\"" + location + "\"/>\n");
93                      if (!(new File(location)).exists()) {
94                          j2eewscontext.getMiscInfo().setCompile(false);
95                      }
96                  } else {
97                      prpertyIn = null;
98                  }
99              } else {
100                 System.out.println("property file not found");
101             }
102             //out.write("	<property file=\"ws4j2ee.properties\"/>\n");
103 
104             out.write("	<path id=\"classpath\" >\n");
105             File tempfile = null;
106             if (file != null) {
107                 tempfile = new File(file.getParent(), "classes");
108             }
109             if (tempfile == null) {
110                 tempfile = new File("./target/classes");
111             }
112             out.write("		<pathelement location=\"" + tempfile.getCanonicalPath() + "\"/>");
113             tempfile = new File("target/test-classes");
114             out.write("		<pathelement location=\"" + tempfile.getCanonicalPath() + "\"/>");
115             ArrayList classpathelements = j2eewscontext.getMiscInfo().getClasspathElements();
116             if (classpathelements != null) {
117                 for (int i = 0; i < classpathelements.size(); i++) {
118                     out.write("		<pathelement location=\""
119                             + ((File) classpathelements.get(i)).getCanonicalPath() + "\"/>\n");
120                 }
121             }
122             if (prpertyIn != null) {
123                 out.write("		<fileset dir=\"${maven.repo.local}\">\n");
124                 out.write("		    <include name=\"axis/**/*.jar\"/>\n");
125                 out.write("			<include name=\"commons-logging/**/*.jar\"/>\n");
126                 out.write("			<include name=\"commons-discovery/**/*.jar\"/>\n");
127                 out.write("			<include name=\"geronimo-spec/**/*.jar\"/>\n");
128                 out.write("			<include name=\"geronimo/**/*.jar\"/>\n");
129                 out.write("			<include name=\"sec/**/*.jar\"/>\n");
130                 out.write("			<include name=\"dom4j/**/*.jar\"/>\n");
131                 out.write("			<include name=\"jaxb-ri/**/*.jar\"/>\n");
132                 out.write("			<include name=\"xerces/**/*.jar\"/>\n");
133                 out.write("         <include name=\"ews/**/*.jar\"/>\n");
134                 out.write("         <include name=\"openejb/**/*.jar\"/>\n");
135                 out.write("		</fileset>\n");
136             }
137 
138             out.write("	</path>\n");
139             out.write("	<target name=\"compile\">\n");
140             out.write("	   <mkdir dir=\"${build.classes}\"/>\n");
141             out.write("    <delete>\n");
142             out.write("         <fileset dir=\"${build.classes}\">\n");
143             out.write("             <include name=\"**\"/>\n");
144             out.write("         </fileset>\n");
145             out.write("    </delete>\n");
146             out.write("	   <mkdir dir=\"${build.lib}\"/>\n");
147             out.write("		<javac destdir=\"${build.classes}\" source=\"1.3\" debug=\"on\">\n");
148             out.write("			<classpath refid=\"classpath\" />\n");
149             out.write("			<src path=\"${src}\"/>\n");
150             out.write("		</javac>\n");
151             out.write("	</target>\n");
152             out.write("	<target name=\"jar\" depends=\"compile\">\n");
153             out.write("		<mkdir dir=\"${build.classes}/META-INF/\"/>\n");
154             writeFileCopyStatement(j2eewscontext.getMiscInfo().getJaxrpcfile(), out);
155             writeFileCopyStatement(j2eewscontext.getMiscInfo().getWsdlFile(), out);
156             writeFileCopyStatement(j2eewscontext.getMiscInfo().getWsconffile(), out);
157 			
158             out.write("		<copy todir=\"${build.classes}\">\n");
159             out.write("			<fileset dir=\"${src}\">\n");
160             out.write("             <include name=\"*.properties\"/>\n");
161             out.write("				<include name=\"META-INF/*.xml\"/>\n");
162             out.write("				<include name=\"WEB-INF/*.xml\"/>\n");
163             out.write("				<include name=\"META-INF/*.wsdl\"/>\n");
164             out.write("				<include name=\"META-INF/*.wsdd\"/>\n");
165             out.write("             <include name=\"WEB-INF/*.wsdl\"/>\n");
166             out.write("             <include name=\"WEB-INF/*.wsdd\"/>\n");
167             out.write("             <include name=\"*.wsdl\"/>\n");
168             out.write("             <include name=\"*.wsdd\"/>\n");
169             out.write("             <exclude name=\"build**\"/>\n");
170             out.write("			</fileset>\n");
171             out.write("		</copy>\n");
172             String jarName = j2eewscontext.getWSDLContext().getTargetPortType().getName().toLowerCase();
173             int index = jarName.lastIndexOf(".");
174             if (index > 0) {
175                 jarName = jarName.substring(index + 1);
176             }
177             String finalJarFile = j2eewscontext.getMiscInfo().getOutPutPath() + "/" + jarName + "-ewsimpl.jar";
178             File jarFile = new File(finalJarFile);
179             String tempFile = "${build}/lib/" + jarName + "-temp.jar";
180             out.write("		<jar jarfile=\"" + tempFile + "\" basedir=\"${build.classes}\" >\n");
181             out.write("		<include name=\"**\" />\n");
182             out.write("		<manifest>\n");
183             out.write("			<section name=\"org/apache/ws4j2ee\">\n");
184             out.write("			<attribute name=\"Implementation-Title\" value=\"Apache jsr109 impl\"/>\n");
185             out.write("			<attribute name=\"Implementation-Vendor\" value=\"Apache Web Services\"/>\n");
186             out.write("			</section>\n");
187             out.write("		</manifest>\n");
188             out.write("		</jar>\n");
189             out.write("     <java classname=\"org.apache.geronimo.ews.ws4j2ee.utils.packager.Packager\" fork=\"no\" >\n");
190             out.write("     	<arg value=\"" + jarFile.getCanonicalPath() + "\"/>\n");
191             out.write("     	<arg value=\"" + tempFile + "\"/>\n");
192             out.write("     	<classpath refid=\"classpath\" />\n");
193             for (int i = 0; i < classpathelements.size(); i++) {
194                 out.write("     	<arg value=\""
195                         + ((File) classpathelements.get(i)).getCanonicalPath() + "\"/>\n");
196             }
197             out.write("     </java>\n");
198             out.write("	</target>\n");
199             out.write("	<target name=\"dist\" depends=\"jar\"/>\n  ");
200             out.write("	<target name=\"clean\">\n");
201             out.write("		<delete dir=\"${build}\"/>\n");
202             out.write("	</target>\n");
203 			
204             out.write("</project>\n");
205             out.close();
206         } catch (IOException e) {
207             log.error(e);
208             throw GenerationFault.createGenerationFault(e);
209         }
210     }
211 
212     private StringTokenizer getClasspathComponets() {
213         String classpath = System.getProperty("java.class.path");
214         String spearator = System.getProperties().getProperty("path.separator");
215         return new StringTokenizer(classpath, spearator);
216     }
217 
218     private void writeFileCopyStatement(InputOutputFile file, PrintWriter out) throws GenerationFault {
219         try {
220             if (file != null) {
221                 String fileName = file.fileName();
222                 if (fileName != null) {
223                     File absFile = new File(fileName);
224                     if (absFile.exists())
225                         out.write("		<copy file =\"" + absFile.getCanonicalPath() + "\" todir=\"${build.classes}/META-INF\"/>\n");
226                 }
227             }
228         } catch (Exception e) {
229         }
230     }
231 }