001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse;
004:
005: import fitnesse.authentication.*;
006: import fitnesse.components.*;
007: import fitnesse.html.HtmlPageFactory;
008: import fitnesse.responders.*;
009: import fitnesse.socketservice.SocketService;
010: import fitnesse.updates.Updater;
011: import fitnesse.wiki.*;
012:
013: import java.io.File;
014: import java.net.BindException;
015:
016: public class FitNesse {
017: public static final String VERSION = "20070619";
018:
019: private FitNesseContext context = new FitNesseContext();
020: private SocketService theService;
021: private static String extraOutput;
022:
023: public static void main(String[] args) throws Exception {
024: Arguments arguments = parseCommandLine(args);
025: if (arguments != null) {
026: FitNesseContext context = loadContext(arguments);
027: PageVersionPruner.daysTillVersionsExpire = arguments
028: .getDaysTillVersionsExpire();
029: FitNesse fitnesse = new FitNesse(context);
030: if (!arguments.isOmittingUpdates())
031: fitnesse.applyUpdates();
032: boolean started = fitnesse.start();
033: if (started)
034: printStartMessage(arguments, context);
035: } else {
036: printUsage();
037: System.exit(1);
038: }
039: }
040:
041: private static FitNesseContext loadContext(Arguments arguments)
042: throws Exception {
043: FitNesseContext context = new FitNesseContext();
044: ComponentFactory componentFactory = new ComponentFactory(
045: context.rootPath);
046: context.port = arguments.getPort();
047: context.rootPath = arguments.getRootPath();
048: context.rootPageName = arguments.getRootDirectory();
049: context.rootPagePath = context.rootPath + "/"
050: + context.rootPageName;
051: context.root = componentFactory.getRootPage(FileSystemPage
052: .makeRoot(context.rootPath, context.rootPageName));
053: context.responderFactory = new ResponderFactory(
054: context.rootPagePath);
055: context.logger = makeLogger(arguments);
056: context.authenticator = makeAuthenticator(arguments
057: .getUserpass(), componentFactory);
058: context.htmlPageFactory = componentFactory
059: .getHtmlPageFactory(new HtmlPageFactory());
060:
061: extraOutput = componentFactory
062: .loadResponderPlugins(context.responderFactory);
063: extraOutput += componentFactory.loadWikiWidgetPlugins();
064: extraOutput += componentFactory.loadWikiWidgetInterceptors();
065: extraOutput += componentFactory.loadContentFilter();
066:
067: WikiImportTestEventListener.register();
068:
069: return context;
070: }
071:
072: public static Arguments parseCommandLine(String[] args) {
073: CommandLine commandLine = new CommandLine(
074: "[-p port][-d dir][-r root][-l logDir][-e days][-o][-a userpass]");
075: Arguments arguments = null;
076: if (commandLine.parse(args)) {
077: arguments = new Arguments();
078: if (commandLine.hasOption("p"))
079: arguments.setPort(commandLine.getOptionArgument("p",
080: "port"));
081: if (commandLine.hasOption("d"))
082: arguments.setRootPath(commandLine.getOptionArgument(
083: "d", "dir"));
084: if (commandLine.hasOption("r"))
085: arguments.setRootDirectory(commandLine
086: .getOptionArgument("r", "root"));
087: if (commandLine.hasOption("l"))
088: arguments.setLogDirectory(commandLine
089: .getOptionArgument("l", "logDir"));
090: if (commandLine.hasOption("e"))
091: arguments.setDaysTillVersionsExpire(commandLine
092: .getOptionArgument("e", "days"));
093: if (commandLine.hasOption("a"))
094: arguments.setUserpass(commandLine.getOptionArgument(
095: "a", "userpass"));
096: arguments.setOmitUpdates(commandLine.hasOption("o"));
097: }
098: return arguments;
099: }
100:
101: private static Logger makeLogger(Arguments arguments) {
102: String logDirectory = arguments.getLogDirectory();
103: return logDirectory != null ? new Logger(logDirectory) : null;
104: }
105:
106: public static Authenticator makeAuthenticator(
107: String authenticationParameter,
108: ComponentFactory componentFactory) throws Exception {
109: Authenticator authenticator = new PromiscuousAuthenticator();
110: if (authenticationParameter != null) {
111: if (new File(authenticationParameter).exists())
112: authenticator = new MultiUserAuthenticator(
113: authenticationParameter);
114: else {
115: String[] values = authenticationParameter.split(":");
116: authenticator = new OneUserAuthenticator(values[0],
117: values[1]);
118: }
119: }
120:
121: return componentFactory.getAuthenticator(authenticator);
122: }
123:
124: private static void printUsage() {
125: System.err.println("Usage: java fitnesse.FitNesse [-pdrleoa]");
126: System.err.println("\t-p <port number> {"
127: + Arguments.DEFAULT_PORT + "}");
128: System.err.println("\t-d <working directory> {"
129: + Arguments.DEFAULT_PATH + "}");
130: System.err.println("\t-r <page root directory> {"
131: + Arguments.DEFAULT_ROOT + "}");
132: System.err.println("\t-l <log directory> {no logging}");
133: System.err.println("\t-e <days> {"
134: + Arguments.DEFAULT_VERSION_DAYS
135: + "} Number of days before page versions expire");
136: System.err.println("\t-o omit updates");
137: System.err
138: .println("\t-a {user:pwd | user-file-name} enable authentication.");
139: }
140:
141: private static void printStartMessage(Arguments args,
142: FitNesseContext context) {
143: System.out.println("FitNesse (" + VERSION + ") Started...");
144: System.out.print(context.toString());
145: System.out.println("\tpage version expiration set to "
146: + args.getDaysTillVersionsExpire() + " days.");
147: System.out.print(extraOutput);
148: }
149:
150: private static void printBadPortMessage(int port) {
151: System.err.println("FitNesse cannot be started...");
152: System.err.println("Port " + port + " is already in use.");
153: System.err
154: .println("Use the -p <port#> command line argument to use a different port.");
155: }
156:
157: private static void establishDirectory(String path) {
158: File filesDir = new File(path);
159: if (!filesDir.exists())
160: filesDir.mkdir();
161: }
162:
163: public FitNesse(FitNesseContext context) throws Exception {
164: this (context, true);
165: }
166:
167: //TODO MdM. This boolean agument is annoying... please fix.
168: public FitNesse(FitNesseContext context, boolean makeDirs) {
169: this .context = context;
170: context.fitnesse = this ;
171: if (makeDirs)
172: establishRequiredDirectories();
173: }
174:
175: public boolean start() {
176: try {
177: theService = new SocketService(context.port,
178: new FitNesseServer(context));
179: return true;
180: } catch (BindException e) {
181: printBadPortMessage(context.port);
182: } catch (Exception e) {
183: e.printStackTrace();
184: }
185: return false;
186: }
187:
188: public void stop() throws Exception {
189: if (theService != null) {
190: theService.close();
191: theService = null;
192: }
193: }
194:
195: private void establishRequiredDirectories() {
196: establishDirectory(context.rootPagePath);
197: establishDirectory(context.rootPagePath + "/files");
198: }
199:
200: public void applyUpdates() throws Exception {
201: Updater updater = new Updater(context);
202: updater.update();
203: }
204:
205: public boolean isRunning() {
206: return theService != null;
207: }
208: }
|