1*cdf0e10cSrcweir /*
2*cdf0e10cSrcweir  *************************************************************************
3*cdf0e10cSrcweir  *
4*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *
8*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
9*cdf0e10cSrcweir  *
10*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
11*cdf0e10cSrcweir  *
12*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
13*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
14*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
15*cdf0e10cSrcweir  *
16*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
17*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
20*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
21*cdf0e10cSrcweir  *
22*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
23*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
24*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
25*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
26*cdf0e10cSrcweir  *
27*cdf0e10cSrcweir  ************************************************************************/
28*cdf0e10cSrcweir package helper;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import java.io.FileWriter;
31*cdf0e10cSrcweir import java.io.IOException;
32*cdf0e10cSrcweir import java.util.ArrayList;
33*cdf0e10cSrcweir import lib.TestParameters;
34*cdf0e10cSrcweir import share.CwsDataExchange;
35*cdf0e10cSrcweir import share.LogWriter;
36*cdf0e10cSrcweir import util.PropertyName;
37*cdf0e10cSrcweir import util.utils;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir /**
40*cdf0e10cSrcweir  * Implementaion of the interface CwsDataExchange
41*cdf0e10cSrcweir  * @see share.CwsDataExchange
42*cdf0e10cSrcweir  */
43*cdf0e10cSrcweir public class CwsDataExchangeImpl implements CwsDataExchange
44*cdf0e10cSrcweir {
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir     private final String cwsName;
47*cdf0e10cSrcweir     private final TestParameters param;
48*cdf0e10cSrcweir     private final LogWriter log;
49*cdf0e10cSrcweir     private final BuildEnvTools bet;
50*cdf0e10cSrcweir     private final boolean mDebug;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir     public CwsDataExchangeImpl(String cwsName, TestParameters param, LogWriter log) throws ParameterNotFoundException
53*cdf0e10cSrcweir     {
54*cdf0e10cSrcweir         this.cwsName = cwsName;
55*cdf0e10cSrcweir         this.param = param;
56*cdf0e10cSrcweir         this.log = log;
57*cdf0e10cSrcweir         this.bet = new BuildEnvTools(param, log);
58*cdf0e10cSrcweir         mDebug = param.getBool(PropertyName.DEBUG_IS_ACTIVE);
59*cdf0e10cSrcweir     }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir     public ArrayList getModules()
62*cdf0e10cSrcweir     {
63*cdf0e10cSrcweir         // the cwstouched command send its version information to StdErr.
64*cdf0e10cSrcweir         // A piping from StdErr to SdtOut the tcsh does not support.
65*cdf0e10cSrcweir         // To find the output easily the echo command is used
66*cdf0e10cSrcweir         final String[] commands =
67*cdf0e10cSrcweir         {
68*cdf0e10cSrcweir             "echo cwstouched starts here",
69*cdf0e10cSrcweir             "cwstouched",
70*cdf0e10cSrcweir             "echo cwstouched ends here"
71*cdf0e10cSrcweir         };
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir         final ProcessHandler procHdl = bet.runCommandsInEnvironmentShell(commands, null, 20000);
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir         if (mDebug)
76*cdf0e10cSrcweir         {
77*cdf0e10cSrcweir             log.println("---> Output of getModules:");
78*cdf0e10cSrcweir             log.println(procHdl.getOutputText());
79*cdf0e10cSrcweir             log.println("<--- Output of getModules");
80*cdf0e10cSrcweir             log.println("---> Error output of getModules");
81*cdf0e10cSrcweir             log.println(procHdl.getErrorText());
82*cdf0e10cSrcweir             log.println("<--- Error output of getModules");
83*cdf0e10cSrcweir         }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir         final String[] outs = procHdl.getOutputText().split("\n");
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir         final ArrayList<String> moduleNames = new ArrayList<String>();
88*cdf0e10cSrcweir         boolean bStart = false;
89*cdf0e10cSrcweir         for (int i = 0; i < outs.length; i++)
90*cdf0e10cSrcweir         {
91*cdf0e10cSrcweir             final String line = outs[i];
92*cdf0e10cSrcweir             if (line.startsWith("cwstouched starts here"))
93*cdf0e10cSrcweir             {
94*cdf0e10cSrcweir                 bStart = true;
95*cdf0e10cSrcweir                 continue;
96*cdf0e10cSrcweir             }
97*cdf0e10cSrcweir             if (line.startsWith("cwstouched ends here"))
98*cdf0e10cSrcweir             {
99*cdf0e10cSrcweir                 bStart = false;
100*cdf0e10cSrcweir                 continue;
101*cdf0e10cSrcweir             }
102*cdf0e10cSrcweir             if (bStart && line.length() > 1)
103*cdf0e10cSrcweir             {
104*cdf0e10cSrcweir                 moduleNames.add(line);
105*cdf0e10cSrcweir             }
106*cdf0e10cSrcweir         }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir         return moduleNames;
109*cdf0e10cSrcweir     }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     public void setUnoApiCwsStatus(boolean status)
112*cdf0e10cSrcweir     {
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         FileWriter out = null;
115*cdf0e10cSrcweir         String statusFile = null;
116*cdf0e10cSrcweir         try
117*cdf0e10cSrcweir         {
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir             final String stat = status ? ".PASSED.OK" : ".PASSED.FAILED";
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir             statusFile = utils.getUsersTempDir() +
122*cdf0e10cSrcweir                     System.getProperty("file.separator") +
123*cdf0e10cSrcweir                     "UnoApiCwsStatus." +
124*cdf0e10cSrcweir                     (String) param.get(PropertyName.VERSION) +
125*cdf0e10cSrcweir                     "_" + param.get(PropertyName.OPERATING_SYSTEM) + stat + ".txt";
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir             out = new FileWriter(statusFile);
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir             out.write(stat);
130*cdf0e10cSrcweir             out.flush();
131*cdf0e10cSrcweir             out.close();
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir             final String[] commands =
134*cdf0e10cSrcweir             {
135*cdf0e10cSrcweir                 "cwsattach " + statusFile
136*cdf0e10cSrcweir             };
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir             bet.runCommandsInEnvironmentShell(commands, null, 5000);
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir         }
141*cdf0e10cSrcweir         catch (IOException ex)
142*cdf0e10cSrcweir         {
143*cdf0e10cSrcweir             System.out.println("ERROR: could not attach file '" + statusFile + "' to cws\n" + ex.toString());
144*cdf0e10cSrcweir         }
145*cdf0e10cSrcweir         finally
146*cdf0e10cSrcweir         {
147*cdf0e10cSrcweir             try
148*cdf0e10cSrcweir             {
149*cdf0e10cSrcweir                 out.close();
150*cdf0e10cSrcweir             }
151*cdf0e10cSrcweir             catch (IOException ex)
152*cdf0e10cSrcweir             {
153*cdf0e10cSrcweir                 ex.printStackTrace();
154*cdf0e10cSrcweir             }
155*cdf0e10cSrcweir         }
156*cdf0e10cSrcweir     }
157*cdf0e10cSrcweir }
158