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