KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > Main


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, 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;
38
39 import java.io.File JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.util.Properties JavaDoc;
42
43 import net.sourceforge.cruisecontrol.jmx.CruiseControlControllerAgent;
44 import net.sourceforge.cruisecontrol.util.threadpool.ThreadQueueProperties;
45 import net.sourceforge.cruisecontrol.util.MainArgs;
46
47 import org.apache.log4j.Level;
48 import org.apache.log4j.Logger;
49
50 /**
51  * Command line entry point.
52  *
53  * @author alden almagro, ThoughtWorks, Inc. 2002
54  * @author <a HREF="mailto:jcyip@thoughtworks.com">Jason Yip</a>
55  */

56 public final class Main {
57     
58     private static final Logger LOG = Logger.getLogger(Main.class);
59
60     private Main() { }
61     
62     /**
63       * Print the version, configure the project with serialized build info
64       * and/or arguments and start the project build process.
65       */

66     public static void main(String JavaDoc[] args) {
67         Properties JavaDoc versionProperties = getBuildVersionProperties();
68         printVersion(versionProperties);
69         if (shouldPrintUsage(args)) {
70             printUsageAndExit();
71         }
72         try {
73             checkDeprecatedArguments(args, LOG);
74
75             if (MainArgs.findIndex(args, "debug") != MainArgs.NOT_FOUND) {
76                 Logger.getRootLogger().setLevel(Level.DEBUG);
77             }
78             CruiseControlController controller = new CruiseControlController();
79             controller.setVersionProperties(versionProperties);
80             File JavaDoc configFile =
81                 new File JavaDoc(
82                    parseConfigFileName(
83                        args,
84                        CruiseControlController.DEFAULT_CONFIG_FILE_NAME));
85             controller.setConfigFile(configFile);
86             ServerXMLHelper helper = new ServerXMLHelper(configFile);
87             ThreadQueueProperties.setMaxThreadCount(helper.getNumThreads());
88             if (shouldStartController(args)) {
89                 CruiseControlControllerAgent agent =
90                     new CruiseControlControllerAgent(
91                         controller,
92                         parseJMXHttpPort(args),
93                         parseRmiPort(args),
94                         parseUser(args),
95                         parsePassword(args),
96                         parseXslPath(args));
97                 agent.start();
98             }
99             controller.resume();
100         } catch (CruiseControlException e) {
101             LOG.fatal(e.getMessage());
102             printUsageAndExit();
103         }
104     }
105
106     protected static void checkDeprecatedArguments(String JavaDoc[] args, Logger logger) {
107         if (MainArgs.findIndex(args, "port") != MainArgs.NOT_FOUND) {
108             logger.warn("WARNING: The port argument is deprecated. Use jmxport instead.");
109         }
110     }
111
112     public static void printUsageAndExit() {
113         System.out.println("");
114         System.out.println("Usage:");
115         System.out.println("");
116         System.out.println("Starts a continuous integration loop");
117         System.out.println("");
118         System.out.println("java CruiseControl [options]");
119         System.out.println("java CruiseControlWithJetty [options]");
120         System.out.println("");
121         System.out.println("Build loop options are:");
122         System.out.println("");
123         System.out.println(" -configfile file configuration file; default config.xml");
124         System.out.println(" -debug set logging level to DEBUG");
125         System.out.println(" -? or -help print this usage message");
126         System.out.println("");
127         System.out.println("Options when using JMX");
128         System.out.println(" Note: JMX server only started if -jmxport and/or -rmiport specified");
129         System.out.println(" -jmxport [number] port of the JMX HttpAdapter; default 8000");
130         System.out.println(" -rmiport [number] RMI port of the Controller; default 1099");
131         System.out.println(" -user username username for HttpAdapter; default no login required");
132         System.out.println(" -password pwd password for HttpAdapter; default no login required");
133         System.out.println(" -xslpath directory location of jmx xsl files; default files in package");
134         System.out.println("");
135         System.out.println("Options when using embedded Jetty");
136         System.out.println(" -webport [number] port for the Reporting website; default 8080");
137         System.out.println(" -cchome directory location from which to start Cruise; default to .");
138         System.out.println(" -ccname name name for this Cruise instance; default to none");
139         System.out.println("");
140         System.exit(1);
141     }
142
143     /**
144      * Parse configfile from arguments and override any existing configfile value
145      * from reading serialized Project info.
146      *
147      * @param configFileName existing configfile value read from serialized Project
148      * info
149      * @return final value of configFileName; never null
150      * @throws CruiseControlException if final configfile value is null
151      */

152     static String JavaDoc parseConfigFileName(String JavaDoc[] args, String JavaDoc configFileName)
153         throws CruiseControlException {
154         configFileName = MainArgs.parseArgument(args, "configfile", configFileName, null);
155         if (configFileName == null) {
156             throw new CruiseControlException("'configfile' is a required argument to CruiseControl.");
157         }
158         return configFileName;
159     }
160
161     static boolean shouldStartController(String JavaDoc[] args) {
162         return MainArgs.argumentPresent(args, "jmxport") || MainArgs.argumentPresent(args, "rmiport")
163                 || MainArgs.argumentPresent(args, "port");
164     }
165
166     /**
167      * Parse port number from arguments.
168      *
169      * @return port number
170      * @throws IllegalArgumentException if port argument is invalid
171      */

172     static int parseJMXHttpPort(String JavaDoc[] args) {
173         if (MainArgs.argumentPresent(args, "jmxport") && MainArgs.argumentPresent(args, "port")) {
174             throw new IllegalArgumentException JavaDoc("'jmxport' and 'port' arguments are not valid together. Use"
175                     + " 'jmxport' instead.");
176         } else if (MainArgs.argumentPresent(args, "jmxport")) {
177             return MainArgs.parseInt(args, "jmxport", MainArgs.NOT_FOUND, 8000);
178         } else {
179             return MainArgs.parseInt(args, "port", MainArgs.NOT_FOUND, 8000);
180         }
181     }
182
183     static int parseRmiPort(String JavaDoc[] args) {
184         return MainArgs.parseInt(args, "rmiport", MainArgs.NOT_FOUND, 1099);
185     }
186
187     static String JavaDoc parseXslPath(String JavaDoc[] args) {
188         String JavaDoc xslpath = MainArgs.parseArgument(args, "xslpath", null, null);
189         if (xslpath != null) {
190             File JavaDoc directory = new File JavaDoc(xslpath);
191             if (!directory.isDirectory()) {
192                 throw new IllegalArgumentException JavaDoc(
193                         "'xslpath' argument must specify an existing directory but was " + xslpath);
194             }
195         }
196         return xslpath;
197     }
198
199     /**
200      * Parse password from arguments and override any existing password value
201      * from reading serialized Project info.
202      *
203      * @return final value of password.
204      */

205     static String JavaDoc parsePassword(String JavaDoc[] args) {
206         return MainArgs.parseArgument(args, "password", null, null);
207     }
208
209     /**
210      * Parse user from arguments and override any existing user value
211      * from reading serialized Project info.
212      *
213      * @return final value of user.
214      */

215     static String JavaDoc parseUser(String JavaDoc[] args) {
216         return MainArgs.parseArgument(args, "user", null, null);
217     }
218
219     /**
220      * Retrieves the current version information, as indicated in the
221      * version.properties file.
222      */

223     private static Properties JavaDoc getBuildVersionProperties() {
224         Properties JavaDoc props = new Properties JavaDoc();
225         try {
226             props.load(Main.class.getResourceAsStream("/version.properties"));
227         } catch (IOException JavaDoc e) {
228             LOG.error("Error reading version properties", e);
229         }
230         return props;
231      }
232
233     /**
234      * Writes the current version information to the logging information stream.
235      */

236     private static void printVersion(Properties JavaDoc props) {
237         LOG.info("CruiseControl Version " + props.getProperty("version")
238                  + " " + props.getProperty("version.info"));
239     }
240
241     static boolean shouldPrintUsage(String JavaDoc[] args) {
242         return MainArgs.findIndex(args, "?") != MainArgs.NOT_FOUND
243                 || MainArgs.findIndex(args, "help") != MainArgs.NOT_FOUND;
244     }
245 }
246
Popular Tags