KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > sun > verification > TranslateRuntimeDD


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package org.apache.tools.ant.taskdefs.optional.sun.verification;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.types.Path;
27 import org.apache.tools.ant.types.Environment;
28 import org.apache.tools.ant.taskdefs.Java;
29
30 import java.io.File JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33
34
35 /**
36  * This is the implementation for running runtime deployment descriptor
37  * translation through an Ant task.
38  * This task runs the following two classses -
39  * Invokes IASMTMain by passing the appropriate arguments,
40  * Invokes the org.apache.xalan.xslt.Process class for transformation.
41  * <p>
42  * The attributes of this task are: <br>
43  * archiveName ="REQUIRED" <br>
44  * srcServer ="REQUIRED" (Options are: sunone, weblogic5, weblogic6, weblogi61, websphere, tomcat)<br>
45  * failOnError="OPTIONAL" (default: true) <br>
46  * <p>
47  *
48  */

49 public class TranslateRuntimeDD extends AVKTasks {
50
51     private File JavaDoc archiveName = null;
52     private String JavaDoc srcServer = null;
53
54     public void setArchiveName(File JavaDoc archiveName) {
55         this.archiveName = archiveName;
56     }
57
58     public void setSrcServer(String JavaDoc srcServer) {
59         this.srcServer = srcServer;
60     }
61
62     /**
63      * This is called by the ant framework.
64      * @throws BuildException
65      */

66     public void execute() throws BuildException {
67
68         checkArgs();
69         getInstallHomes();
70
71 // temporary until migration code has CLIP interface as default
72
jvmArgs = "-hotspot -ms96m -mx512m -DENABLE_CLIP=true -Dasmt.home="+avk_home+" -DPACKAGE_PROPERTY_FILE="+avk_home+"/config/package.properties ";
73
74         if(invokeIASMT() != 0 && failOnError)
75             throw new BuildException("Problem in executing java command");
76
77         if(invokeGenReportTool() != 0 && failOnError)
78             throw new BuildException("Problem in executing java command");
79
80 // user needs to know what happened and what to do next.
81
echo.setMessage("See \""+resultDir+File.separator+"translationSummary.html\" for results.");
82         echo.execute();
83     echo.setMessage("We attempted to create an ear file even if minor errors occurred. The application with translated deployment descriptors is in a new .ear file located in "+resultDir+File.separator+"Input_Archives"+File.separator+"asmtbuild ");
84         echo.execute();
85     }
86
87
88     /**
89      * Invokes the sun.iasmt.user.IASMTMain
90      * @return integer denoting the return status of the command. 1 is failure
91      */

92     private int invokeIASMT() {
93         Java java = (Java)project.createTask("java");
94         java.setClassname("sun.iasmt.user.IASMTMain");
95         setCommonVMSettings(java);
96         java.setClasspath(constructPath());
97         Environment.Variable sysp = new Environment.Variable();
98         sysp.setKey("asmt.home");
99         sysp.setValue(avk_home);
100
101         createResultDir("translate");
102         java.addSysproperty(sysp);
103         java.createArg().setLine("-c -t "+resultDir+
104                                  " -S "+srcServer+
105                                  " -x "+resultDir+File.separator+"result.xml "+
106                                  "-n -q -T sjs80PE "+ archiveName);
107
108         int returnCode = java.executeJava();
109
110         // when the migration code starts generating ear in case of
111
// failure, we will remove this method
112
createEar();
113
114         return returnCode;
115     }
116
117     /**
118      * create the ear if the migration code failed to generate. The method first checks
119      * whether the ear has been created by checking the
120      * "<avk_home>/reporttool/translate/Input_Archives/asmtbuild/<app.ear>" file. In
121      * case the file does not exist then java command is invoked on the build_ear.xml file
122      * present in this directory.
123      */

124
125     private void createEar() {
126         String JavaDoc archivePath = resultDir + File.separator + "Input_Archives";
127         File JavaDoc earFile = new File JavaDoc(archivePath + File.separator + "asmtbuild",
128                                 archiveName.getName());
129         String JavaDoc buildFile = archivePath+File.separator+"build_ear.xml";
130         if(!earFile.exists() && new File JavaDoc(buildFile).exists()) {
131             echo.setMessage("Some xml files may not have translated correctly.");
132             echo.execute();
133             Java java = (Java)project.createTask("java");
134
135             java.setClassname("org.apache.tools.ant.Main");
136             setCommonVMSettings(java);
137             java.setClasspath(getPathForCreateEar());
138             addEnvVariablesForCreateEar(java);
139             java.createArg().setLine("-buildfile "+buildFile+" -quiet");
140             int returnStatus = java.executeJava();
141             if(returnStatus==1) {
142                 echo.setMessage("Some errors occurred while building the ear. You can find " +
143                                 "the generated code at ["+archivePath+"]");
144                 echo.execute();
145             }else
146             echo.setMessage( "The EAR file may not deploy successfully in "+
147                 "the Application Server. Please check the " +
148                             "runtime descriptors before deploying.");
149
150         }
151     }
152
153     // setting the classpath required for running the java command for createEar
154
private Path getPathForCreateEar() {
155     String JavaDoc j2ee_lib = j2ee_home + File.separator + "lib";
156         StringBuffer JavaDoc classPathBuffer = new StringBuffer JavaDoc();
157         String JavaDoc[] CLASSPATH_ELEMENTS = { "lib"+File.separator+"asmt-rt.jar",
158                     "lib"+File.separator+"ant.jar"};
159         classPathBuffer.append(java_home+"lib" + File.separator+
160                                "tools.jar"+File.pathSeparator);
161         classPathBuffer.append(j2ee_lib + File.separator+
162                    "javaee.jar"+File.pathSeparator);
163 // classPathBuffer.append(j2ee_lib+ File.separator + "ant" +
164
// "lib"+ File.separator +
165
// "ant.jar"+File.pathSeparator);
166
for(int i=0;i<CLASSPATH_ELEMENTS.length;i++) {
167             classPathBuffer.append((new File JavaDoc(avk_home,CLASSPATH_ELEMENTS[i])).getPath());
168             classPathBuffer.append(File.pathSeparator);
169         }
170         return new Path(getProject(),classPathBuffer.toString());
171     }
172
173     // setting the Environment Variables required for running the java command for createEar
174
private void addEnvVariablesForCreateEar(Java java) {
175         Environment.Variable asmt = new Environment.Variable();
176         asmt.setKey("asmt.home");
177         asmt.setValue(avk_home);
178         java.addSysproperty(asmt);
179
180         Environment.Variable vml = new Environment.Variable();
181         vml.setKey("vml");
182         vml.setValue("true");
183         java.addSysproperty(vml);
184
185         Environment.Variable javahome = new Environment.Variable();
186         javahome.setKey("javahome");
187         javahome.setValue(java_home);
188         java.addSysproperty(javahome);
189
190         Environment.Variable iashome = new Environment.Variable();
191         iashome.setKey("ias.home");
192         iashome.setValue(j2ee_home);
193         java.addSysproperty(iashome);
194     }
195
196     /**
197      * Invokes the com.sun.enterprise.appverification.tools.GenReportTool
198      * @return integer denoting the return status of the command. 1 is failure
199      */

200     private int invokeGenReportTool() {
201         Java java = createJavaTask();
202         List JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>();
203
204         list.add(resultDir + File.separator + "result.xml");
205         list.add(resultDir);
206         list.add("translationSummary");
207         String JavaDoc[] args = (String JavaDoc[])list.toArray(new String JavaDoc[1]);
208
209         java.setClassname("com.sun.enterprise.appverification.tools.GenReportTool");
210         setArgs(java, args);
211
212         return java.executeJava();
213     }
214
215
216     private Path constructPath() {
217     String JavaDoc j2ee_lib = j2ee_home + File.separator + "lib";
218         StringBuffer JavaDoc classPathBuffer = new StringBuffer JavaDoc();
219
220
221         String JavaDoc[] CLASSPATH_ELEMENTS = {"lib"+File.separator+"xalan.jar",
222                                        "lib"+File.separator +"ant.jar",
223                                        "lib"+File.separator+"dom4j.jar",
224                                        "lib"+File.separator+"namespace.jar",
225                                        "lib"+File.separator+"asmt.jar",
226                                        "lib"+File.separator+"relaxngDatatype.jar",
227                                        "lib"+File.separator+"jaxb-api.jar",
228                                        "lib"+File.separator+"jaxb-impl.jar",
229                                        "lib"+File.separator+"jaxb-libs.jar",
230                                        "lib"+File.separator+"jax-qname.jar",
231                                        "lib"+File.separator+"jhall.jar",
232                                        "lib"+File.separator+"asmt_en.jar"};
233         classPathBuffer.append(java_home+"lib" +File.separator+
234                 "tools.jar"+File.pathSeparator);
235
236         for(int i=0;i<CLASSPATH_ELEMENTS.length;i++) {
237             classPathBuffer.append((new File JavaDoc(avk_home,CLASSPATH_ELEMENTS[i])).getPath());
238             classPathBuffer.append(File.pathSeparator);
239         }
240         classPathBuffer.append(j2ee_lib+File.separator+
241                 "j2ee.jar"+File.pathSeparator);
242         return new Path(getProject(),classPathBuffer.toString());
243     }
244
245     /**
246      * Parse the source server argument. Throw exception if invalid
247      * option is found.
248      * @throws BuildException
249      */

250     private void checkArgs() throws BuildException {
251         if(archiveName == null || !archiveName.exists())
252             throw new BuildException("Provide a valid fully qualified " +
253                                      "path of the archive to be translated: archiveName = "+archiveName);
254
255         if( !archiveName.isFile())
256             throw new BuildException("Provide a valid fullly qualified " +
257                                      "archive be translated: archiveName = "+archiveName);
258
259         if(srcServer == null)
260             throw new BuildException("Must specify which app server was used to create the application");
261         else if(srcServer.equalsIgnoreCase("sunone65"))
262             srcServer = "as65";
263         else if(srcServer.equalsIgnoreCase("sjsappserver7"))
264             srcServer = "as70";
265         else if(srcServer.equalsIgnoreCase("weblogic5"))
266             srcServer = "wl51";
267         else if(srcServer.equalsIgnoreCase("weblogic6"))
268             srcServer = "wl60";
269         else if(srcServer.equalsIgnoreCase("weblogic61"))
270             srcServer = "wl61";
271         else if(srcServer.equalsIgnoreCase("weblogic81"))
272             srcServer = "wl81";
273         else if(srcServer.equalsIgnoreCase("websphere4"))
274             srcServer = "ws40";
275         else if(srcServer.equalsIgnoreCase("websphere51"))
276             srcServer = "ws50";
277         else if(srcServer.equalsIgnoreCase("tomcat"))
278             srcServer = "tc41";
279         else if(srcServer.equalsIgnoreCase("J2EESDK13"))
280             srcServer = "ri13";
281         else if(srcServer.equalsIgnoreCase("J2EESDK14"))
282             srcServer = "ri14";
283         else if(srcServer.equalsIgnoreCase("JBoss3"))
284             srcServer = "jb30";
285         else
286             throw new BuildException("srcServer = "+srcServer+" is not one of " +
287                                      "(sunone65, sjsappserver7, weblogic5, weblogic6, weblogic61, weblogic81, websphere4, websphere51, j2eesdk13, j2eesdk14, jboss3 and tomcat)");
288
289     }
290 }
291
Popular Tags