1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: BuildEnvTools.java,v $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
34 import java
.io
.PrintWriter
;
35 import lib
.TestParameters
;
36 import share
.LogWriter
;
40 * This class support you to execute some shell commands in a buld environment. At ervery call of commands
41 * a build environment was created and the commands will be executed.
44 public class BuildEnvTools
{
46 private final TestParameters param
;
47 private final LogWriter log
;
48 private final boolean mDebug
;
49 private final String mPlatform
;
50 private final String mShell
;
51 private boolean mCygwin
;
54 * This constructor creates an instance of BuildEncTools. It is verifying for all neccesarry
55 * parameters in <CODE>TestParameters</CODE> This must be:
57 * <li>OperatingSystem: Fill this parameter with an operating system like unxsols, unxsoli, unxlngi or wntmsci.
59 * <li> Shell: Fill this parameter with a shell f.e '/bin/tcsh'
60 * or 'c:\\myShell\\myShell.exe'
64 * @throws helper.ParameterNotFoundException
66 public BuildEnvTools(TestParameters param
, LogWriter log
) throws ParameterNotFoundException
{
69 mDebug
= param
.getBool(PropertyName
.DEBUG_IS_ACTIVE
);
71 boolean error
= false;
73 String msg
= "\nERROR: the following parameter must be set before executing the test:\n\n";
75 mPlatform
= (String
) param
.get(PropertyName
.OPERATING_SYSTEM
);
77 log
.println("### " + mPlatform
);
79 if (mPlatform
== null){
80 msg
+= PropertyName
.OPERATING_SYSTEM
+ "\nFill this parameter with an operating system like unxsols," +
81 " unxsoli, unxlngi, unxmacxi or wntmsci. \n\n";
84 (!mPlatform
.equalsIgnoreCase(PropertyName
.UNXSOLS
)) &&
85 (!mPlatform
.equalsIgnoreCase(PropertyName
.UNXSOLI
)) &&
86 (!mPlatform
.equalsIgnoreCase(PropertyName
.UNXLNGI
)) &&
87 (!mPlatform
.equalsIgnoreCase(PropertyName
.UNXMACXI
))&&
88 (!mPlatform
.equalsIgnoreCase(PropertyName
.WNTMSCI
)) ){
90 msg
+= PropertyName
.OPERATING_SYSTEM
+ ":" + mPlatform
+ "\nFill this parameter with an operating system like unxsols," +
91 " unxsoli, unxlngi, unxmacxi or wntmsci. \n\n";
95 mShell
= (String
) param
.get(PropertyName
.SHELL
);
97 msg
+= PropertyName
.SHELL
+ "\nFill this parameter with a shell" +
98 "\n\t/bin/tcsh c:\\myShell\\myShell.exe\n\n";
102 mCygwin
= (param
.getBool(PropertyName
.CYGWIN
));
105 throw new ParameterNotFoundException(msg
);
110 * Executes the given commands in OOo-Environment shell.
114 * @return the processHandler of the commands
115 * @see helper.ProcessHandler
117 public ProcessHandler
runCommandsInEnvironmentShell(String
[] commands
, File workDir
, int shortWait
) {
119 final String
[] cmdLines
= getCmdLinesWithCommand(commands
);
120 final ProcessHandler pHdl
= new ProcessHandler(cmdLines
, (PrintWriter
) log
, workDir
, shortWait
, param
);
125 public String
getSrcRoot() {
127 String sSrcRoot
= (String
) param
.get(PropertyName
.SRC_ROOT
);
129 if (sSrcRoot
== null) {
130 String
[] cmdLines
= null;
131 if (mPlatform
.equals(PropertyName
.WNTMSCI
) && ! mCygwin
) {
132 cmdLines
= new String
[]{mShell
, "/C", "echo SRC_ROOT=%SRC_ROOT"};
134 cmdLines
= new String
[]{mShell
, "--login ", "-c ", "echo \"SRC_ROOT=$SRC_ROOT\""};
137 final ProcessHandler procHdl
= new ProcessHandler(cmdLines
, (PrintWriter
) log
, null, 5000, param
);
138 procHdl
.runCommand();
141 log
.println("---> Output of command:");
142 log
.println(procHdl
.getOutputText());
143 log
.println("<--- Output of command:");
144 log
.println("---> Error output of command");
145 log
.println(procHdl
.getErrorText());
146 log
.println("<--- Error output of command");
148 final String output
= procHdl
.getOutputText();
149 final String
[] outs
= output
.split("\n");
151 for (int i
= 0; i
< outs
.length
; i
++) {
152 final String line
= outs
[i
];
153 if (line
.startsWith("SRC_ROOT")) {
154 sSrcRoot
= getEnvValue(line
);
161 private String
[] getCmdLinesWithCommand(String
[] commands
) {
162 String
[] cmdLines
= null;
163 log
.println("prepare command for platform " + mPlatform
);
165 String seperator
= "";
166 if (mPlatform
.equals(PropertyName
.WNTMSCI
)) {
167 seperator
= mCygwin ?
";" : "^";
173 for (int i
= 0; i
< commands
.length
; i
++) {
175 command
+= seperator
;
177 command
+= commands
[i
];
180 if (mPlatform
.equals(PropertyName
.WNTMSCI
)){
182 String srcRoot
= (String
) param
.get(PropertyName
.SRC_ROOT
);
183 String envSet
= "export cyg_src_root=`cygpath '" + srcRoot
.replaceAll("\\\\", "\\\\\\\\")+ "'`; source $cyg_src_root/winenv.set.sh;";
184 command
= envSet
+ command
;
185 cmdLines
= new String
[]{mShell
, "--login", "-c", "\"" + command
+ "\""};
187 cmdLines
= new String
[]{mShell
, "/C", "\"" + command
+ "\""};
190 cmdLines
= new String
[]{mShell
, "-c", command
};
195 private String
getEnvValue(String line
) {
196 final String
[] split
= line
.split("=");