KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > bootstrappers > ClearCaseViewstrapper


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.bootstrappers;
38
39 import java.io.PrintWriter JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.StringTokenizer JavaDoc;
42
43 import net.sourceforge.cruisecontrol.Bootstrapper;
44 import net.sourceforge.cruisecontrol.CruiseControlException;
45 import net.sourceforge.cruisecontrol.util.Commandline;
46 import net.sourceforge.cruisecontrol.util.StreamPumper;
47 import net.sourceforge.cruisecontrol.util.ValidationHelper;
48
49 import org.apache.log4j.Logger;
50
51 /**
52  * This class allows you to start up ClearCase dynamic views and mount VOBs before
53  * you initiate your build. If your view has been stopped, a VOB unmounted or your
54  * machine rebooted, the likelihood is that your build will fail when using dynamic
55  * views. The class therefore allows you to specify a viewpath, from which it works
56  * out the view tag and starts it, optionally you can specify voblist, a comma
57  * separated list of VOBs to mount.
58  *
59  * The reason a viewpath is used rather than just the view path is that you can reuse
60  * a CruiseControl property which defines the source of your build. You should always
61  * specify the viewpath via the root location, i.e. M:\... on Windows or /view/... on Unix
62  *
63  * Usage:
64  *
65  * <clearcaseviewstrapper viewpath="M:\dynamic_view\some_vob\src"
66  * voblist="\SourceVOB,\ReleaseVOB"/%gt;
67  *
68  * @author <a HREF="mailto:kevin.lee@buildmeister.com">Kevin Lee</a>
69  */

70  public class ClearCaseViewstrapper implements Bootstrapper {
71
72     /** enable logging for this class */
73     private static Logger log = Logger.getLogger(ClearCaseViewstrapper.class);
74
75     private String JavaDoc viewpath;
76     private String JavaDoc voblist;
77
78     /**
79      * set the path to the view to be started
80      * @param path path to view to be started
81      */

82     public void setViewpath(String JavaDoc path) {
83         viewpath = path;
84     }
85     
86     /**
87      * set the list of VOBs to mount, the list is comma separated
88      * @param list comma separated list of VOBs to mount
89      */

90     public void setVoblist(String JavaDoc list) {
91         voblist = list;
92     }
93
94     /*
95      * start the specified view and VOBs.
96      */

97     public void bootstrap() {
98         
99         Commandline commandLine = buildStartViewCommand();
100         log.debug("Executing: " + commandLine);
101         try {
102             Process JavaDoc p = Runtime.getRuntime().exec(commandLine.getCommandline());
103             StreamPumper errorPumper =
104                 new StreamPumper(p.getErrorStream(), new PrintWriter JavaDoc(System.err, true));
105             new Thread JavaDoc(errorPumper).start();
106             p.waitFor();
107             p.getInputStream().close();
108             p.getOutputStream().close();
109             p.getErrorStream().close();
110         } catch (Exception JavaDoc e) {
111             log.error("Error executing ClearCase startview command", e);
112         }
113       
114         // have we got some VOBs to mount
115
if (voblist != null) {
116             String JavaDoc[] vobs = getVobsFromList(voblist);
117             for (int i = 0; i < vobs.length; i++) {
118                 commandLine = buildMountVOBCommand(vobs[i]);
119                 log.debug("Executing: " + commandLine);
120                 try {
121                     Process JavaDoc p = Runtime.getRuntime().exec(commandLine.getCommandline());
122                     StreamPumper errorPumper =
123                         new StreamPumper(p.getErrorStream(), new PrintWriter JavaDoc(System.err, true));
124                     new Thread JavaDoc(errorPumper).start();
125                     p.waitFor();
126                     p.getInputStream().close();
127                     p.getOutputStream().close();
128                     p.getErrorStream().close();
129                 } catch (Exception JavaDoc e) {
130                     log.error("Error executing ClearCase mount command", e);
131                 }
132             }
133         }
134     }
135
136     private String JavaDoc[] getVobsFromList(String JavaDoc voblist) {
137         // replacing voblist.split(","); for jdk 1.3 compatibility
138
ArrayList JavaDoc vobs = simpleSplitReplacement(voblist, ',');
139         return (String JavaDoc[]) vobs.toArray(new String JavaDoc[]{});
140     }
141
142     /*
143      * check whether the appropriate attributes have been set
144      */

145     public void validate() throws CruiseControlException {
146         ValidationHelper.assertIsSet(viewpath, "viewpath", this.getClass());
147     }
148
149     /*
150      * build a command line for starting the view
151      */

152     protected Commandline buildStartViewCommand() {
153         Commandline commandLine = new Commandline();
154         commandLine.setExecutable("cleartool");
155
156         commandLine.createArgument().setValue("startview");
157         commandLine.createArgument().setValue(getViewName());
158
159         return commandLine;
160     }
161     
162     /*
163      * build a command line for starting a VOB
164      */

165     protected Commandline buildMountVOBCommand(String JavaDoc vob) {
166         Commandline commandLine = new Commandline();
167         commandLine.setExecutable("cleartool");
168
169         commandLine.createArgument().setValue("mount");
170         commandLine.createArgument().setValue(vob);
171
172         return commandLine;
173     }
174
175     /*
176      * work out the view tag from the viewpath
177      */

178     private String JavaDoc getViewName() {
179         String JavaDoc viewname = "";
180         try {
181             if (isWindows()) {
182                 viewname = getWindowsViewname(viewpath);
183             } else {
184                 viewname = getUnixViewname(viewpath);
185             }
186         } catch (ArrayIndexOutOfBoundsException JavaDoc ex) {
187             
188         }
189         return viewname;
190     }
191
192     // second part after /view, i.e. /view/viewname
193
private String JavaDoc getUnixViewname(String JavaDoc viewpath) {
194 // replacing the following for jdk 1.3 compatibility
195
// String[] details = viewpath.split("/");
196
// viewname = details[2];
197
ArrayList JavaDoc parts = simpleSplitReplacement(viewpath, '/');
198        return (String JavaDoc) parts.get(1);
199     }
200
201     // first part after M: drive, i.e. M:\viewname
202
private String JavaDoc getWindowsViewname(String JavaDoc viewpath) {
203 // replacing the following for jdk 1.3 compatibility
204
// String[] details = viewpath.split("\\\\");
205
// viewname = details[1];
206
ArrayList JavaDoc parts = simpleSplitReplacement(viewpath, '\\');
207         return (String JavaDoc) parts.get(1);
208     }
209     
210     private ArrayList JavaDoc simpleSplitReplacement(String JavaDoc string, char tokenizeOn) {
211         ArrayList JavaDoc parts = new ArrayList JavaDoc();
212         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(string, String.valueOf(tokenizeOn));
213         while (tokenizer.hasMoreTokens()) {
214             parts.add(tokenizer.nextToken());
215         }
216         return parts;
217     }
218     
219     protected boolean isWindows() {
220         return getOsName().indexOf("Windows") >= 0;
221     }
222
223     protected String JavaDoc getOsName() {
224         return System.getProperty("os.name");
225     }
226
227     /** for testing */
228     public static void main(String JavaDoc[] args) {
229         ClearCaseViewstrapper bootstrapper = new ClearCaseViewstrapper();
230         bootstrapper.setViewpath("M:\\RatlBankModel_rel\\RatlBankSources");
231         bootstrapper.setVoblist("\\RatlBankSources,\\RatlBankReleases");
232         bootstrapper.bootstrap();
233     }
234
235 }
Popular Tags