--- /dev/null
+package com.itmill.toolkit.launcher;\r
+\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.mortbay.jetty.Connector;\r
+import org.mortbay.jetty.Server;\r
+import org.mortbay.jetty.nio.SelectChannelConnector;\r
+import org.mortbay.jetty.webapp.WebAppContext;\r
+import org.mortbay.thread.BoundedThreadPool;\r
+\r
+/**\r
+ * Class for running Jetty servlet container within Eclipse project.\r
+ * \r
+ */\r
+public class ITMillWebModeLaunch {\r
+\r
+ private final static String serverPort = "8080";\r
+\r
+ /**\r
+ * Main function for running Jetty.\r
+ * \r
+ * Command line Arguments are passed through to Jetty, see runServer method\r
+ * for options.\r
+ * \r
+ * @param args\r
+ */\r
+ public static void main(String[] args) {\r
+\r
+ // Pass-through of arguments for Jetty\r
+ Map serverArgs = parseArguments(args);\r
+\r
+ String url = runServer(serverArgs);\r
+\r
+ // Open browser into application URL\r
+ if (url != null) {\r
+ BrowserLauncher.openBrowser(url);\r
+ }\r
+\r
+ }\r
+\r
+ /**\r
+ * Run the server with specified arguments.\r
+ * \r
+ * @param serverArgs\r
+ * @return\r
+ */\r
+ protected static String runServer(Map serverArgs) {\r
+\r
+ // Add help for System.out\r
+ System.out\r
+ .println("-------------------------------------------------\n"\r
+ + "Starting IT Mill Toolkit examples.\n"\r
+ + "Please go to http://localhost:"\r
+ + serverPort\r
+ + "\nif your web browser is not automatically started."\r
+ + "\n-------------------------------------------------\n");\r
+\r
+ // Assign default values for some arguments\r
+ assignDefault(serverArgs, "webroot", "WebContent");\r
+ assignDefault(serverArgs, "httpPort", serverPort);\r
+\r
+ try {\r
+ long started = System.currentTimeMillis();\r
+\r
+ Server server = new Server();\r
+\r
+ // String threadPoolName =\r
+ // System.getProperty("jetty.threadpool.name",\r
+ // "Jetty thread");\r
+ // int maxIdleTimeMs = Integer.getInteger(\r
+ // "jetty.threadpool.maxIdleTimeMs", 60000);\r
+ // int maxThreads =\r
+ // Integer.getInteger("jetty.threadpool.maxThreads",\r
+ // 100);\r
+ // int minThreads =\r
+ // Integer.getInteger("jetty.threadpool.minThreads",\r
+ // 1);\r
+ // int lowThreads = Integer.getInteger(\r
+ // "jetty.threadpool.maxIdleTimeMs", 25);\r
+ // BoundedThreadPool threadPool = new BoundedThreadPool();\r
+ // threadPool.setName(threadPoolName);\r
+ // threadPool.setMaxIdleTimeMs(maxIdleTimeMs);\r
+ // threadPool.setMaxThreads(maxThreads);\r
+ // threadPool.setMinThreads(minThreads);\r
+ // threadPool.setLowThreads(lowThreads);\r
+ // server.setThreadPool(threadPool);\r
+\r
+ Connector connector = new SelectChannelConnector();\r
+ // FIXME httpPort hardcoded to 8080\r
+ // connector.setPort(Integer.valueOf(serverArgs.get("httpPort")\r
+ // .toString()));\r
+ connector.setPort(8080);\r
+ server.setConnectors(new Connector[] { connector });\r
+\r
+ WebAppContext webappcontext = new WebAppContext();\r
+ webappcontext.setContextPath("");\r
+ webappcontext.setWar(serverArgs.get("webroot").toString());\r
+\r
+ server.setHandler(webappcontext);\r
+\r
+ server.start();\r
+ System.err.println("Started Jetty in "\r
+ + (System.currentTimeMillis() - started) + "ms.");\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ return null;\r
+ }\r
+\r
+ return "http://localhost:" + serverArgs.get("httpPort");\r
+ }\r
+\r
+ /**\r
+ * Assign default value for given key.\r
+ * \r
+ * @param map\r
+ * @param key\r
+ * @param value\r
+ */\r
+ private static void assignDefault(Map map, String key, String value) {\r
+ if (!map.containsKey(key)) {\r
+ map.put(key, value);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Parse all command line arguments into a map.\r
+ * \r
+ * Arguments format "key=value" are put into map.\r
+ * \r
+ * @param args\r
+ * @return map of arguments key value pairs.\r
+ */\r
+ protected static Map parseArguments(String[] args) {\r
+ Map map = new HashMap();\r
+ for (int i = 0; i < args.length; i++) {\r
+ int d = args[i].indexOf("=");\r
+ if (d > 0 && d < args[i].length() && args[i].startsWith("--")) {\r
+ String name = args[i].substring(2, d);\r
+ String value = args[i].substring(d + 1);\r
+ map.put(name, value);\r
+ }\r
+ }\r
+ return map;\r
+ }\r
+\r
+}\r