001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)AntRunner.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.installer;
030:
031: import java.io.File;
032: import java.io.FileOutputStream;
033: import java.io.*;
034: import java.util.logging.Logger;
035: import java.util.Properties;
036:
037: /**
038: * AntRunner is a class that can be used to run
039: * an ant task from a specified ant file in a specified directory.
040: *
041: * @author Sun Microsystems, Inc.
042: */
043: public class AntRunner {
044:
045: /**
046: * Run a target in an ant file.
047: *
048: * @param basedir the ant basedir
049: * @param antfn the name of the ant file.
050: * @param targetname the target you want to run
051: * @return true if ant task is successfully run
052: */
053: public static boolean runAnt(String appserverInstallDir,
054: String antfn, String targetname, Properties props,
055: String logFileName) throws Exception {
056: String antHome = null;
057: String asant = null;
058: String antLauncher = null;
059: String space = " ";
060: if (System.getProperty("os.name").indexOf("Windows") != -1) {
061:
062: antHome = appserverInstallDir + File.separator + "lib"
063: + File.separator + "ant";
064:
065: antLauncher = antHome + File.separator + "lib"
066: + File.separator + "ant-launcher.jar";
067:
068: //if appserver installdir has spaces, deal with it
069: //surround the entire command with quotes
070: if (appserverInstallDir.indexOf(' ') != -1) {
071: antHome = "\"" + antHome + "\"";
072: }
073:
074: if (appserverInstallDir.indexOf(' ') != -1) {
075: antLauncher = "\"" + antLauncher + "\"";
076: }
077:
078: String asJdk = getAppserverJDK(appserverInstallDir, true);
079: String asJava = asJdk + File.separator + "bin"
080: + File.separator + "java.exe";
081:
082: if (asJdk.indexOf(' ') != -1) {
083: asJava = "\"" + asJava + "\"";
084: }
085:
086: String javaHome = asJdk;
087: if (asJdk.indexOf(' ') != -1) {
088: javaHome = "\"" + asJdk + "\"";
089: }
090:
091: asant = asJava + space + "-classpath" + space + antLauncher
092: + space + "-Dant.home=" + antHome + space
093: + "-Djava.home=" + javaHome + space
094: + "org.apache.tools.ant.launch.Launcher" + space;
095: }//windows
096: else {
097: asant = appserverInstallDir + File.separator + "bin"
098: + File.separator + "asant --noconfig";
099: }
100: File propertiesFile = null;
101: FileOutputStream outStream = null;
102: try {
103: propertiesFile = File.createTempFile("antPropertiesFile",
104: null);
105: propertiesFile.deleteOnExit();
106: outStream = new FileOutputStream(propertiesFile);
107: props.store(outStream, null);
108: outStream.close();
109: outStream = null;
110: } catch (Exception e) {
111: }
112:
113: String arguments = "-f" + space + new File(antfn).getPath()
114: + space + "-propertyfile" + space
115: + propertiesFile.getPath() + space + targetname;
116:
117: int exitValue = new ProcessExecutor().execute(asant + space
118: + arguments, new File(logFileName));
119:
120: if (exitValue == -1)
121: return false;
122:
123: return true;
124: }
125:
126: /**
127: * This method returns the JAVA_HOME for the JDK
128: * used by the appserver
129: */
130: public static String getAppserverJDK(String installDir,
131: boolean isWindows) {
132: BufferedReader bf = null;
133: InputStream in = null;
134: String javaHome = "";
135: try {
136: String asenv = "";
137: if (!isWindows)
138: asenv = installDir + File.separator + "config"
139: + File.separator + "asenv.conf";
140: else
141: asenv = installDir + File.separator + "config"
142: + File.separator + "asenv.bat";
143:
144: in = new FileInputStream(asenv);
145:
146: bf = new BufferedReader(new InputStreamReader(in));
147: String line = bf.readLine();
148: while (line != null) {
149: if (line.indexOf("AS_JAVA") != -1) {
150: int pos = line.indexOf("=");
151: if (pos > 0) {
152: String lhs = (line.substring(0, pos)).trim();
153: String rhs = (line.substring(pos + 1)).trim();
154:
155: if (isWindows) { //trim off the "set "
156: lhs = (lhs.substring(3)).trim();
157: }
158:
159: if (!isWindows) { // take the quotes out
160: pos = rhs.indexOf("\"");
161: if (pos != -1) {
162: rhs = (rhs.substring(pos + 1)).trim();
163: pos = rhs.indexOf("\"");
164: if (pos != -1)
165: rhs = (rhs.substring(0, pos))
166: .trim();
167: }
168:
169: }
170: javaHome = rhs;
171: break;
172: }
173: }
174: line = bf.readLine();
175: }
176: } catch (Exception e) {
177: //System.out.println("Default JDK of SDK");
178: return installDir + File.separator + "java";
179: }
180: return javaHome;
181: }
182:
183: }
|