| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.test.launch; |
| 17 | |
|
| 18 | |
import java.io.File; |
| 19 | |
import java.util.ArrayList; |
| 20 | |
import java.util.List; |
| 21 | |
import java.util.StringTokenizer; |
| 22 | |
|
| 23 | |
import javax.servlet.Servlet; |
| 24 | |
|
| 25 | |
import org.apache.commons.collections.CollectionUtils; |
| 26 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
| 27 | |
import org.eclipse.jetty.server.Server; |
| 28 | |
import org.eclipse.jetty.servlet.ServletContextHandler; |
| 29 | |
import org.eclipse.jetty.servlet.ServletHolder; |
| 30 | |
import org.eclipse.jetty.util.resource.ResourceCollection; |
| 31 | |
import org.eclipse.jetty.webapp.WebAppContext; |
| 32 | |
|
| 33 | |
public class JettyLauncher { |
| 34 | |
|
| 35 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(JettyLauncher.class); |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
public static final String JETTYSERVER_TESTMODE_ATTRIB = "JETTYSERVER_TESTMODE"; |
| 43 | |
|
| 44 | |
private int port; |
| 45 | |
private String contextName; |
| 46 | 0 | private List<String> relativeWebappRoots = new ArrayList<String>(); |
| 47 | |
private Class<? extends Servlet> servletClass; |
| 48 | |
private Server server; |
| 49 | |
private ServletContextHandler context; |
| 50 | |
private boolean failOnContextFailure; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 0 | private boolean testMode = false; |
| 56 | |
|
| 57 | |
public JettyLauncher() { |
| 58 | 0 | this(8080); |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
public JettyLauncher(int port) { |
| 62 | 0 | this(port, null, null, null); |
| 63 | 0 | } |
| 64 | |
|
| 65 | |
public JettyLauncher(int port, String contextName) { |
| 66 | 0 | this(port, contextName, null, null); |
| 67 | 0 | } |
| 68 | |
|
| 69 | |
public JettyLauncher(int port, String contextName, String relativeWebappRoot) { |
| 70 | 0 | this(port, contextName, relativeWebappRoot, null); |
| 71 | 0 | } |
| 72 | |
|
| 73 | |
public JettyLauncher(int port, String contextName, Class<? extends Servlet> servletClass) { |
| 74 | 0 | this(port, contextName, null, servletClass); |
| 75 | 0 | } |
| 76 | |
|
| 77 | 0 | public JettyLauncher(int port, String contextName, String relativeWebappRoots, Class<? extends Servlet> servletClass) { |
| 78 | 0 | this.port = port; |
| 79 | 0 | this.contextName = contextName; |
| 80 | 0 | StringTokenizer tokenizer = new StringTokenizer(relativeWebappRoots, ","); |
| 81 | 0 | while (tokenizer.hasMoreTokens()) { |
| 82 | 0 | String relativeWebappRoot = tokenizer.nextToken(); |
| 83 | 0 | this.relativeWebappRoots.add(relativeWebappRoot); |
| 84 | 0 | } |
| 85 | 0 | this.servletClass = servletClass; |
| 86 | 0 | } |
| 87 | |
|
| 88 | |
public void setTestMode(boolean t) { |
| 89 | 0 | this.testMode = t; |
| 90 | 0 | } |
| 91 | |
|
| 92 | |
public boolean isTestMode() { |
| 93 | 0 | return testMode; |
| 94 | |
} |
| 95 | |
|
| 96 | |
public Server getServer() { |
| 97 | 0 | return server; |
| 98 | |
} |
| 99 | |
|
| 100 | |
public ServletContextHandler getContext() { |
| 101 | 0 | return context; |
| 102 | |
} |
| 103 | |
|
| 104 | |
public void start() throws Exception { |
| 105 | 0 | server = createServer(); |
| 106 | 0 | server.start(); |
| 107 | 0 | if (isFailOnContextFailure() && contextStartupFailed()) { |
| 108 | |
try { |
| 109 | 0 | server.stop(); |
| 110 | 0 | } catch (Exception e) { |
| 111 | 0 | LOG.warn("Failed to stop server after web application startup failure."); |
| 112 | 0 | } |
| 113 | 0 | throw new Exception("Failed to startup web application context! Check logs for specific error."); |
| 114 | |
} |
| 115 | 0 | } |
| 116 | |
|
| 117 | |
public void stop() throws Exception { |
| 118 | 0 | server.stop(); |
| 119 | 0 | } |
| 120 | |
|
| 121 | |
public boolean isStarted() { |
| 122 | 0 | return server.isStarted(); |
| 123 | |
} |
| 124 | |
|
| 125 | |
protected Server createServer() { |
| 126 | 0 | Server server = new Server(getPort()); |
| 127 | 0 | setBaseDirSystemProperty(); |
| 128 | 0 | if (useWebAppContext()) { |
| 129 | 0 | File tmpDir = new File(System.getProperty("basedir") + "/target/jetty-tmp"); |
| 130 | 0 | tmpDir.mkdirs(); |
| 131 | 0 | WebAppContext webAppContext = new WebAppContext(); |
| 132 | 0 | webAppContext.setContextPath(getContextName()); |
| 133 | 0 | String[] fullRelativeWebappRoots = new String[this.relativeWebappRoots.size()]; |
| 134 | 0 | for (int i = 0; i < this.relativeWebappRoots.size(); i++) { |
| 135 | 0 | String fullRelativeWebappRoot = this.relativeWebappRoots.get(i); |
| 136 | 0 | fullRelativeWebappRoots[i] = System.getProperty("basedir") + fullRelativeWebappRoot; |
| 137 | 0 | if (LOG.isInfoEnabled()) { |
| 138 | 0 | LOG.info("WebAppRoot = " + fullRelativeWebappRoots[i]); |
| 139 | |
} |
| 140 | |
} |
| 141 | 0 | webAppContext.setBaseResource(new ResourceCollection(fullRelativeWebappRoots)); |
| 142 | 0 | webAppContext.setTempDirectory(tmpDir); |
| 143 | 0 | webAppContext.setAttribute(JETTYSERVER_TESTMODE_ATTRIB, String.valueOf(isTestMode())); |
| 144 | 0 | context = webAppContext; |
| 145 | 0 | server.setHandler(context); |
| 146 | 0 | } else { |
| 147 | 0 | ServletContextHandler root = new ServletContextHandler(server,"/",ServletContextHandler.SESSIONS); |
| 148 | 0 | root.addServlet(new ServletHolder(servletClass), getContextName()); |
| 149 | 0 | root.setAttribute(JETTYSERVER_TESTMODE_ATTRIB, String.valueOf(isTestMode())); |
| 150 | 0 | context = root; |
| 151 | |
} |
| 152 | 0 | return server; |
| 153 | |
} |
| 154 | |
|
| 155 | |
protected void setBaseDirSystemProperty() { |
| 156 | 0 | if (System.getProperty("basedir") == null) { |
| 157 | 0 | System.setProperty("basedir", System.getProperty("user.dir")); |
| 158 | |
} |
| 159 | 0 | } |
| 160 | |
|
| 161 | |
private boolean useWebAppContext() { |
| 162 | 0 | return CollectionUtils.isNotEmpty(this.relativeWebappRoots); |
| 163 | |
} |
| 164 | |
|
| 165 | |
protected boolean contextStartupFailed() throws Exception { |
| 166 | 0 | return !context.isAvailable(); |
| 167 | |
} |
| 168 | |
|
| 169 | |
public String getContextName() { |
| 170 | 0 | if (contextName == null) { |
| 171 | 0 | return "/SampleRiceClient"; |
| 172 | |
} |
| 173 | 0 | return contextName; |
| 174 | |
} |
| 175 | |
|
| 176 | |
public void setContextName(String contextName) { |
| 177 | 0 | this.contextName = contextName; |
| 178 | 0 | } |
| 179 | |
|
| 180 | |
public int getPort() { |
| 181 | 0 | return port; |
| 182 | |
} |
| 183 | |
|
| 184 | |
public void setPort(int port) { |
| 185 | 0 | this.port = port; |
| 186 | 0 | } |
| 187 | |
|
| 188 | |
|
| 189 | |
public boolean isFailOnContextFailure() { |
| 190 | 0 | return this.failOnContextFailure; |
| 191 | |
} |
| 192 | |
|
| 193 | |
public void setFailOnContextFailure(boolean failOnContextFailure) { |
| 194 | 0 | this.failOnContextFailure = failOnContextFailure; |
| 195 | 0 | } |
| 196 | |
|
| 197 | |
public String toString() { |
| 198 | 0 | return new ToStringBuilder(this).append("port", port) |
| 199 | |
.append("contextName", contextName) |
| 200 | |
.append("relativeWebappRoots", relativeWebappRoots) |
| 201 | |
.append("servletClass", servletClass) |
| 202 | |
.toString(); |
| 203 | |
} |
| 204 | |
|
| 205 | |
public static void main(String[] args) { |
| 206 | 0 | int port = args.length > 0 ? Integer.parseInt(args[0]) : 8080; |
| 207 | 0 | String contextName = args.length > 1 ? args[1] : null; |
| 208 | 0 | String relativeWebappRoot = args.length > 2 ? args[2] : null; |
| 209 | |
try { |
| 210 | 0 | new JettyLauncher(port, contextName, relativeWebappRoot).start(); |
| 211 | 0 | } catch (Exception e) { |
| 212 | 0 | e.printStackTrace(); |
| 213 | 0 | } |
| 214 | 0 | } |
| 215 | |
} |