KickJava   Java API By Example, From Geeks To Geeks.

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


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.Task;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.types.Environment;
28 import org.apache.tools.ant.taskdefs.Java;
29 import org.apache.tools.ant.taskdefs.Echo;
30 import org.apache.tools.ant.taskdefs.Delete;
31 import org.apache.tools.ant.types.Path;
32
33 import java.io.File JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.FileInputStream JavaDoc;
36 import java.util.Properties JavaDoc;
37 /*
38 import java.io.IOException;
39 import java.util.Set;
40 import java.util.Iterator;
41 import com.sun.enterprise.util.FileUtil;
42 */

43
44 /**
45  * This is the base class of all the custom ant tasks. It has
46  * the common code required by the tasks.
47  *
48  * @author Vikas Awasthi.
49  */

50 public class AVKTasks extends Task {
51
52     protected String JavaDoc jvmArgs = null;
53     protected boolean failOnError = true;
54     protected String JavaDoc j2ee_home = null;
55     protected String JavaDoc avk_home = null;
56     protected String JavaDoc java_home = null;
57     protected String JavaDoc resultDir = null;
58     protected Echo echo = null;
59
60     public AVKTasks() {
61         java_home = System.getProperty("java.home")+"/../";//"java.home returns jdk/jre, hence ....
62
}
63
64     public void setJvmArgs(String JavaDoc jvmArgs) {
65         this.jvmArgs = jvmArgs;
66     }
67
68     public void setFailOnError(boolean failOnError) {
69         this.failOnError = failOnError;
70     }
71     
72     protected void setResultDir(String JavaDoc resultDir) {
73         this.resultDir = resultDir.trim();
74     }
75
76     private void createEchoTask () {
77         echo = (Echo)getProject().createTask("echo");
78     }
79
80     /**
81      * Create a java task with classpath and JVM arguments set
82      * @return Java task
83      */

84     protected Java createJavaTask() {
85         Java java = (Java)project.createTask("java");
86         java.setClasspath(constructJVMPath());
87         if(jvmArgs == null)
88             jvmArgs = "-Xss512k -Xms128m -Xmx256m";
89         setCommonVMSettings(java);
90         setSystemProperties(java);
91         return java;
92     }
93
94     protected void setSystemProperties(Java java) {
95         Environment.Variable j2ee_app_home = new Environment.Variable();
96         j2ee_app_home.setKey("j2ee.appverification.home");
97         j2ee_app_home.setValue(avk_home);
98         java.addSysproperty(j2ee_app_home);
99
100         Environment.Variable java_ext_dir = new Environment.Variable();
101         java_ext_dir.setKey("java.ext.dirs");
102         java_ext_dir.setValue(System.getProperty("java.ext.dirs") +
103                               File.pathSeparator + j2ee_home +
104                               File.separator + "domains" +
105                               File.separator + "domain1" +
106                               File.separator + "lib" +
107                               File.separator + "ext");
108         java.addSysproperty(java_ext_dir);
109
110         Environment.Variable installRoot = new Environment.Variable();
111         installRoot.setKey("com.sun.aas.installRoot");
112         installRoot.setValue(j2ee_home);
113         java.addSysproperty(installRoot);
114
115     // following 2 are required for the migration code
116
Environment.Variable j2eeHome = new Environment.Variable();
117         j2eeHome.setKey("j2ee.home");
118         j2eeHome.setValue(j2ee_home);
119         java.addSysproperty(j2eeHome);
120
121         Environment.Variable javaHome = new Environment.Variable();
122         javaHome.setKey("as.java");
123         javaHome.setValue(java_home);
124         java.addSysproperty(javaHome);
125
126         Environment.Variable endorsedDir = new Environment.Variable();
127         endorsedDir.setKey("java.endorsed.dirs");
128         endorsedDir.setValue(j2ee_home+File.separator + "lib" + File.separator + "endorsed");
129         java.addSysproperty(endorsedDir);
130
131         Environment.Variable xsl = new Environment.Variable();
132         xsl.setKey("com.sun.aas.verifier.xsl");
133         xsl.setValue(avk_home + File.separator + "xsl");
134         java.addSysproperty(xsl);
135     }
136
137     protected Path constructJVMPath() {
138         StringBuffer JavaDoc classPathBuffer = new StringBuffer JavaDoc();
139         classPathBuffer.append(avk_home+"/lib/javke.jar:");
140         classPathBuffer.append(j2ee_home+"/lib/appserv-rt.jar:");
141         classPathBuffer.append(j2ee_home+"/lib/javaee.jar:");
142         classPathBuffer.append(j2ee_home+"/lib/appserv-ext.jar:");
143         classPathBuffer.append(j2ee_home+"/lib/appserv-cmp.jar:");
144         classPathBuffer.append(j2ee_home+"/lib/appserv-admin.jar:");
145         classPathBuffer.append(j2ee_home+"/lib/install/applications/jmsra/imqjmsra.jar:");
146         classPathBuffer.append(j2ee_home+"/lib/jhall.jar:");
147
148 // classPathBuffer.append(Path.systemClasspath);
149
return new Path(getProject(),classPathBuffer.toString());
150     }
151
152     protected void getInstallHomes() throws BuildException {
153 // create the echo task that displays the messages
154
createEchoTask();
155
156         if(avk_home == null)
157             try {
158                 avk_home = new File JavaDoc(getProject().getProperty("avk.home")).getCanonicalPath();
159             } catch (IOException JavaDoc e) {//we donot expect this exception. Continue with absolutePath anyway
160
avk_home = new File JavaDoc(getProject().getProperty("avk.home")).getAbsolutePath();
161             }
162
163         try {
164             File JavaDoc avkENV = new File JavaDoc(avk_home+File.separatorChar+"config", "avkenv.conf");
165             Properties JavaDoc props = new Properties JavaDoc();
166             props.load(new FileInputStream JavaDoc(avkENV));
167             if(j2ee_home == null)
168                 try {
169                     j2ee_home = new File JavaDoc(props.getProperty("J2EE_HOME")).getCanonicalPath();
170                 } catch (IOException JavaDoc e) {//we donot expect this exception. Continue with absolutePath anyway
171
j2ee_home = new File JavaDoc(props.getProperty("J2EE_HOME")).getAbsolutePath();
172                 }
173         } catch (IOException JavaDoc e) {
174             echo.setMessage("Problem in getting J2EE_HOME. Please ensure that avkenv.conf " +
175                     "is kept in the config directory. "+e.getMessage());
176             echo.execute();
177         }
178     }
179     
180     protected void setCommonVMSettings(Java java) {
181         java.setFork(true);
182         java.setFailonerror(failOnError);
183 // java.setDir(new File(avk_home, "bin"));
184
java.createJvmarg().setLine(jvmArgs);
185     }
186     
187     /**
188      * creates the directory dirName ( static, scan or translate) in the
189      * "avk_home/reporttool" directory.
190      * @param dirName
191      */

192     protected void createResultDir(String JavaDoc dirName) {
193         if(resultDir == null)
194             resultDir = avk_home+File.separator + "reporttool";
195         resultDir = resultDir + File.separator +dirName;
196         File JavaDoc file = new File JavaDoc(resultDir);
197         try {
198             cleanDir(resultDir);
199             if ( file.getParent() !=null) {
200                 (new File JavaDoc (file.getParent())).mkdirs();
201                 file.mkdir();
202             }
203         }catch( Exception JavaDoc e) {
204             throw new BuildException("Could not create output directory "+resultDir);
205         }
206     }
207
208     protected void setArgs(Java java, String JavaDoc[] args) {
209         for (int i = 0; i < args.length; i++) {
210             java.createArg().setLine(args[i] + " ");
211         }
212     }
213
214     protected void cleanDir(String JavaDoc resultDir) {
215         Delete delete = (Delete)getProject().createTask("delete");
216         delete.setDir(new File JavaDoc(resultDir));
217         delete.setQuiet(true);
218         delete.setIncludeEmptyDirs(true);
219         delete.execute();
220     }
221
222 }
223
Popular Tags