1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package helper;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import java.io.FileWriter;
26cdf0e10cSrcweir import java.io.IOException;
27cdf0e10cSrcweir import java.util.ArrayList;
28cdf0e10cSrcweir import lib.TestParameters;
29cdf0e10cSrcweir import share.CwsDataExchange;
30cdf0e10cSrcweir import share.LogWriter;
31cdf0e10cSrcweir import util.PropertyName;
32cdf0e10cSrcweir import util.utils;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir /**
35cdf0e10cSrcweir  * Implementaion of the interface CwsDataExchange
36cdf0e10cSrcweir  * @see share.CwsDataExchange
37cdf0e10cSrcweir  */
38cdf0e10cSrcweir public class CwsDataExchangeImpl implements CwsDataExchange
39cdf0e10cSrcweir {
40cdf0e10cSrcweir 
41cdf0e10cSrcweir     private final String cwsName;
42cdf0e10cSrcweir     private final TestParameters param;
43cdf0e10cSrcweir     private final LogWriter log;
44cdf0e10cSrcweir     private final BuildEnvTools bet;
45cdf0e10cSrcweir     private final boolean mDebug;
46cdf0e10cSrcweir 
CwsDataExchangeImpl(String cwsName, TestParameters param, LogWriter log)47cdf0e10cSrcweir     public CwsDataExchangeImpl(String cwsName, TestParameters param, LogWriter log) throws ParameterNotFoundException
48cdf0e10cSrcweir     {
49cdf0e10cSrcweir         this.cwsName = cwsName;
50cdf0e10cSrcweir         this.param = param;
51cdf0e10cSrcweir         this.log = log;
52cdf0e10cSrcweir         this.bet = new BuildEnvTools(param, log);
53cdf0e10cSrcweir         mDebug = param.getBool(PropertyName.DEBUG_IS_ACTIVE);
54cdf0e10cSrcweir     }
55cdf0e10cSrcweir 
getModules()56cdf0e10cSrcweir     public ArrayList getModules()
57cdf0e10cSrcweir     {
58cdf0e10cSrcweir         // the cwstouched command send its version information to StdErr.
59cdf0e10cSrcweir         // A piping from StdErr to SdtOut the tcsh does not support.
60cdf0e10cSrcweir         // To find the output easily the echo command is used
61cdf0e10cSrcweir         final String[] commands =
62cdf0e10cSrcweir         {
63cdf0e10cSrcweir             "echo cwstouched starts here",
64cdf0e10cSrcweir             "cwstouched",
65cdf0e10cSrcweir             "echo cwstouched ends here"
66cdf0e10cSrcweir         };
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         final ProcessHandler procHdl = bet.runCommandsInEnvironmentShell(commands, null, 20000);
69cdf0e10cSrcweir 
70cdf0e10cSrcweir         if (mDebug)
71cdf0e10cSrcweir         {
72cdf0e10cSrcweir             log.println("---> Output of getModules:");
73cdf0e10cSrcweir             log.println(procHdl.getOutputText());
74cdf0e10cSrcweir             log.println("<--- Output of getModules");
75cdf0e10cSrcweir             log.println("---> Error output of getModules");
76cdf0e10cSrcweir             log.println(procHdl.getErrorText());
77cdf0e10cSrcweir             log.println("<--- Error output of getModules");
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir         final String[] outs = procHdl.getOutputText().split("\n");
81cdf0e10cSrcweir 
82cdf0e10cSrcweir         final ArrayList<String> moduleNames = new ArrayList<String>();
83cdf0e10cSrcweir         boolean bStart = false;
84cdf0e10cSrcweir         for (int i = 0; i < outs.length; i++)
85cdf0e10cSrcweir         {
86cdf0e10cSrcweir             final String line = outs[i];
87cdf0e10cSrcweir             if (line.startsWith("cwstouched starts here"))
88cdf0e10cSrcweir             {
89cdf0e10cSrcweir                 bStart = true;
90cdf0e10cSrcweir                 continue;
91cdf0e10cSrcweir             }
92cdf0e10cSrcweir             if (line.startsWith("cwstouched ends here"))
93cdf0e10cSrcweir             {
94cdf0e10cSrcweir                 bStart = false;
95cdf0e10cSrcweir                 continue;
96cdf0e10cSrcweir             }
97cdf0e10cSrcweir             if (bStart && line.length() > 1)
98cdf0e10cSrcweir             {
99cdf0e10cSrcweir                 moduleNames.add(line);
100cdf0e10cSrcweir             }
101cdf0e10cSrcweir         }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir         return moduleNames;
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir 
setUnoApiCwsStatus(boolean status)106cdf0e10cSrcweir     public void setUnoApiCwsStatus(boolean status)
107cdf0e10cSrcweir     {
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         FileWriter out = null;
110cdf0e10cSrcweir         String statusFile = null;
111cdf0e10cSrcweir         try
112cdf0e10cSrcweir         {
113cdf0e10cSrcweir 
114cdf0e10cSrcweir             final String stat = status ? ".PASSED.OK" : ".PASSED.FAILED";
115cdf0e10cSrcweir 
116cdf0e10cSrcweir             statusFile = utils.getUsersTempDir() +
117cdf0e10cSrcweir                     System.getProperty("file.separator") +
118cdf0e10cSrcweir                     "UnoApiCwsStatus." +
119cdf0e10cSrcweir                     (String) param.get(PropertyName.VERSION) +
120cdf0e10cSrcweir                     "_" + param.get(PropertyName.OPERATING_SYSTEM) + stat + ".txt";
121cdf0e10cSrcweir 
122cdf0e10cSrcweir             out = new FileWriter(statusFile);
123cdf0e10cSrcweir 
124cdf0e10cSrcweir             out.write(stat);
125cdf0e10cSrcweir             out.flush();
126cdf0e10cSrcweir             out.close();
127cdf0e10cSrcweir 
128cdf0e10cSrcweir             final String[] commands =
129cdf0e10cSrcweir             {
130cdf0e10cSrcweir                 "cwsattach " + statusFile
131cdf0e10cSrcweir             };
132cdf0e10cSrcweir 
133cdf0e10cSrcweir             bet.runCommandsInEnvironmentShell(commands, null, 5000);
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         }
136cdf0e10cSrcweir         catch (IOException ex)
137cdf0e10cSrcweir         {
138cdf0e10cSrcweir             System.out.println("ERROR: could not attach file '" + statusFile + "' to cws\n" + ex.toString());
139cdf0e10cSrcweir         }
140cdf0e10cSrcweir         finally
141cdf0e10cSrcweir         {
142cdf0e10cSrcweir             try
143cdf0e10cSrcweir             {
144cdf0e10cSrcweir                 out.close();
145cdf0e10cSrcweir             }
146cdf0e10cSrcweir             catch (IOException ex)
147cdf0e10cSrcweir             {
148cdf0e10cSrcweir                 ex.printStackTrace();
149cdf0e10cSrcweir             }
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir }
153