KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
40
41 import net.sourceforge.cruisecontrol.Bootstrapper;
42 import net.sourceforge.cruisecontrol.CruiseControlException;
43 import net.sourceforge.cruisecontrol.util.Commandline;
44 import net.sourceforge.cruisecontrol.util.StreamPumper;
45 import net.sourceforge.cruisecontrol.util.ValidationHelper;
46
47 import org.apache.log4j.Logger;
48
49 /**
50  * Bootstrapper for Perforce. Accepts one view that we sync.
51  *
52  * @author <a HREF="mailto:mroberts@thoughtworks.com">Mike Roberts</a>
53  * @author <a HREF="mailto:cstevenson@thoughtworks.com">Chris Stevenson</a>
54  * @author J D Glanville
55  */

56 public class P4Bootstrapper implements Bootstrapper {
57     private static final Logger LOG = Logger.getLogger(P4Bootstrapper.class);
58     private String JavaDoc view;
59     private String JavaDoc port;
60     private String JavaDoc client;
61     private String JavaDoc user;
62     private String JavaDoc passwd;
63
64     public void setPort(String JavaDoc port) {
65         this.port = port;
66     }
67
68     public void setClient(String JavaDoc client) {
69         this.client = client;
70     }
71
72     public void setUser(String JavaDoc user) {
73         this.user = user;
74     }
75
76     public void setView(String JavaDoc view) {
77         this.view = view;
78     }
79
80     public void setPasswd(String JavaDoc passwd) {
81         this.passwd = passwd;
82     }
83
84     /**
85      * @deprecated Use <code>setView</code> instead
86      */

87     public void setPath(String JavaDoc path) {
88         LOG.warn("The path attribute is deprecated, please use view attribute instead.");
89         this.view = path;
90     }
91
92     /**
93      * @deprecated Use <code>setPort</code> instead
94      */

95     public void setP4Port(String JavaDoc p4Port) {
96         LOG.warn("The p4Port attribute is deprecated, please use port attribute instead.");
97         this.port = p4Port;
98     }
99
100     /**
101      * @deprecated Use <code>setClient</code> instead
102      */

103     public void setP4Client(String JavaDoc p4Client) {
104         LOG.warn("The p4Client attribute is deprecated, please use client attribute instead.");
105         this.client = p4Client;
106     }
107
108     /**
109      * @deprecated Use <code>setUser</code> instead
110      */

111     public void setP4User(String JavaDoc p4User) {
112         LOG.warn("The p4User attribute is deprecated, please use user attribute instead.");
113         this.user = p4User;
114     }
115
116     public void validate() throws CruiseControlException {
117         ValidationHelper.assertIsSet(view, "view", this.getClass());
118         ValidationHelper.assertNotEmpty(view, "view", this.getClass());
119         ValidationHelper.assertNotEmpty(port, "P4Port", this.getClass());
120         ValidationHelper.assertNotEmpty(client, "P4Client", this.getClass());
121         ValidationHelper.assertNotEmpty(user, "P4User", this.getClass());
122         ValidationHelper.assertNotEmpty(passwd, "P4Passwd", this.getClass());
123     }
124
125     public void bootstrap() throws CruiseControlException {
126         Commandline commandline = createCommandline();
127         LOG.debug("Executing commandline [" + commandline + "]");
128         executeCommandLine(commandline);
129     }
130
131     public Commandline createCommandline() throws CruiseControlException {
132         validate();
133         Commandline cmd = new Commandline();
134         cmd.setExecutable("p4");
135         cmd.createArgument().setValue("-s");
136         if (port != null) {
137             cmd.createArgument().setValue("-p");
138             cmd.createArgument().setValue(port);
139         }
140         if (client != null) {
141             cmd.createArgument().setValue("-c");
142             cmd.createArgument().setValue(client);
143         }
144         if (user != null) {
145             cmd.createArgument().setValue("-u");
146             cmd.createArgument().setValue(user);
147         }
148         if (passwd != null) {
149             cmd.createArgument().setValue("-P");
150             cmd.createArgument().setValue(passwd);
151         }
152         cmd.createArgument().setValue("sync");
153         cmd.createArgument().setValue(view);
154
155         return cmd;
156     }
157
158     // TODO: Refactor this into a class. Then we can mock it and unit test bootstrap()
159
private void executeCommandLine(Commandline commandline) throws CruiseControlException {
160         try {
161             LOG.info(commandline.toString());
162             Process JavaDoc p = Runtime.getRuntime().exec(commandline.getCommandline());
163
164             new Thread JavaDoc(new StreamPumper(p.getInputStream())).start();
165             new Thread JavaDoc(new StreamPumper(p.getErrorStream())).start();
166             p.waitFor();
167
168         } catch (IOException JavaDoc e) {
169             throw new CruiseControlException("Problem trying to execute command line process", e);
170         } catch (InterruptedException JavaDoc e) {
171             throw new CruiseControlException("Problem trying to execute command line process", e);
172         }
173     }
174 }
175
Popular Tags