KickJava   Java API By Example, From Geeks To Geeks.

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


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 net.sourceforge.cruisecontrol.Bootstrapper;
40 import net.sourceforge.cruisecontrol.CruiseControlException;
41 import net.sourceforge.cruisecontrol.sourcecontrols.VSSHelper;
42 import net.sourceforge.cruisecontrol.util.StreamPumper;
43 import net.sourceforge.cruisecontrol.util.ValidationHelper;
44
45 import org.apache.log4j.Logger;
46
47 import java.io.File JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.InputStream JavaDoc;
50 import java.io.PrintWriter JavaDoc;
51
52 public class VssBootstrapper implements Bootstrapper {
53
54     private static final Logger LOG = Logger.getLogger(VssBootstrapper.class);
55
56     private String JavaDoc ssDir;
57     private String JavaDoc serverPath;
58
59     private String JavaDoc vssPath;
60     private String JavaDoc localDirectory;
61     private String JavaDoc login;
62
63     public void bootstrap() throws CruiseControlException {
64         String JavaDoc commandLine = generateCommandLine();
65
66         try {
67             String JavaDoc[] env = VSSHelper.loadVSSEnvironment(serverPath);
68
69             Process JavaDoc p = Runtime.getRuntime().exec(commandLine, env);
70             InputStream JavaDoc errorIn = p.getErrorStream();
71             InputStream JavaDoc stdIn = p.getInputStream();
72             PrintWriter JavaDoc errorOut = new PrintWriter JavaDoc(System.err, true);
73             PrintWriter JavaDoc stdOut = new PrintWriter JavaDoc(System.out, true);
74             StreamPumper errorPumper = new StreamPumper(errorIn, errorOut);
75             StreamPumper stdPumper = new StreamPumper(stdIn, stdOut);
76             new Thread JavaDoc(errorPumper).start();
77             new Thread JavaDoc(stdPumper).start();
78             p.waitFor();
79             p.getInputStream().close();
80             p.getOutputStream().close();
81             p.getErrorStream().close();
82         } catch (IOException JavaDoc ex) {
83             LOG.debug("exception trying to exec ss.exe", ex);
84             throw new CruiseControlException(ex);
85         } catch (InterruptedException JavaDoc ex) {
86             LOG.debug("interrupted during get", ex);
87             throw new CruiseControlException(ex);
88         }
89     }
90
91     public void validate() throws CruiseControlException {
92         ValidationHelper.assertTrue(vssPath != null && localDirectory != null,
93                 "VssBootstrapper has required attributes vssPath and localDirectory");
94
95         File JavaDoc localDirForFile = new File JavaDoc(localDirectory);
96         ValidationHelper.assertTrue(localDirForFile.exists(),
97             "file path attribute value " + localDirectory + " must specify an existing directory.");
98
99         ValidationHelper.assertTrue(localDirForFile.isDirectory(),
100             "file path attribute value " + localDirectory + " must specify an existing directory, not a file.");
101
102         setLocalDirectory(localDirForFile.getAbsolutePath());
103     }
104
105     String JavaDoc generateCommandLine() {
106         StringBuffer JavaDoc commandLine = new StringBuffer JavaDoc();
107         final String JavaDoc backslash = "\\";
108         // optionally prefix the executable
109
if (ssDir != null) {
110             commandLine.append(ssDir).append(ssDir.endsWith(backslash) ? "" : backslash);
111         }
112         final String JavaDoc quote = "\"";
113         commandLine.append("ss.exe get ");
114         // check for leading "$", to be argument-compatible with other tasks
115
if (vssPath != null) {
116             String JavaDoc pathPrefix = vssPath.startsWith("$") ? "" : "$";
117             commandLine.append(quote).append(pathPrefix).append(vssPath).append(quote);
118         }
119         commandLine.append(" -GL");
120         commandLine.append(quote).append(localDirectory).append(quote);
121         commandLine.append(" -I-N");
122         if (login != null) {
123             commandLine.append(" -Y").append(login);
124         }
125
126         return commandLine.toString();
127     }
128
129     /**
130      * Required.
131      * @param vssPath fully qualified VSS path to the file ($/Project/subproject/filename.ext)
132      */

133     public void setVssPath(String JavaDoc vssPath) {
134         this.vssPath = vssPath;
135     }
136
137     /***
138      * Optional.
139      * @param ssDir Path to the directory containing ss.exe. Assumes that ss.exe is in the path by default.
140      */

141     public void setSsDir(String JavaDoc ssDir) {
142         this.ssDir = ssDir;
143     }
144
145     /**
146      * Optional.
147      * @param serverPath The path to the directory containing the srcsafe.ini file.
148      */

149     public void setServerPath(String JavaDoc serverPath) {
150         this.serverPath = serverPath;
151     }
152
153     /**
154      * Required.
155      * @param localDirectory fully qualified path for the destination directory (c:\directory\subdirectory\)
156      */

157     public void setLocalDirectory(String JavaDoc localDirectory) {
158         this.localDirectory = localDirectory;
159     }
160
161     /**
162      * Optional.
163      * @param login vss login information in the form username,password\
164      */

165     public void setLogin(String JavaDoc login) {
166         this.login = login;
167     }
168 }
Popular Tags