1 37 package net.sourceforge.cruisecontrol; 38 39 import java.io.File ; 40 import java.io.IOException ; 41 import java.util.Properties ; 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 56 public final class Main { 57 58 private static final Logger LOG = Logger.getLogger(Main.class); 59 60 private Main() { } 61 62 66 public static void main(String [] args) { 67 Properties 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 configFile = 81 new File ( 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 [] 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 152 static String parseConfigFileName(String [] args, String 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 [] args) { 162 return MainArgs.argumentPresent(args, "jmxport") || MainArgs.argumentPresent(args, "rmiport") 163 || MainArgs.argumentPresent(args, "port"); 164 } 165 166 172 static int parseJMXHttpPort(String [] args) { 173 if (MainArgs.argumentPresent(args, "jmxport") && MainArgs.argumentPresent(args, "port")) { 174 throw new IllegalArgumentException ("'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 [] args) { 184 return MainArgs.parseInt(args, "rmiport", MainArgs.NOT_FOUND, 1099); 185 } 186 187 static String parseXslPath(String [] args) { 188 String xslpath = MainArgs.parseArgument(args, "xslpath", null, null); 189 if (xslpath != null) { 190 File directory = new File (xslpath); 191 if (!directory.isDirectory()) { 192 throw new IllegalArgumentException ( 193 "'xslpath' argument must specify an existing directory but was " + xslpath); 194 } 195 } 196 return xslpath; 197 } 198 199 205 static String parsePassword(String [] args) { 206 return MainArgs.parseArgument(args, "password", null, null); 207 } 208 209 215 static String parseUser(String [] args) { 216 return MainArgs.parseArgument(args, "user", null, null); 217 } 218 219 223 private static Properties getBuildVersionProperties() { 224 Properties props = new Properties (); 225 try { 226 props.load(Main.class.getResourceAsStream("/version.properties")); 227 } catch (IOException e) { 228 LOG.error("Error reading version properties", e); 229 } 230 return props; 231 } 232 233 236 private static void printVersion(Properties props) { 237 LOG.info("CruiseControl Version " + props.getProperty("version") 238 + " " + props.getProperty("version.info")); 239 } 240 241 static boolean shouldPrintUsage(String [] args) { 242 return MainArgs.findIndex(args, "?") != MainArgs.NOT_FOUND 243 || MainArgs.findIndex(args, "help") != MainArgs.NOT_FOUND; 244 } 245 } 246 | Popular Tags |