001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.rice.web.jetty;
017:
018: import java.io.File;
019:
020: import org.kuali.rice.lifecycle.Lifecycle;
021: import org.mortbay.jetty.Server;
022: import org.mortbay.jetty.webapp.WebAppContext;
023:
024: public class JettyServer implements Lifecycle {
025:
026: private int port;
027: private String contextName;
028: private String relativeWebappRoot;
029: private Server server;
030:
031: public JettyServer() {
032: this (8080, null);
033: }
034:
035: public JettyServer(int port) {
036: this (port, null, null);
037: }
038:
039: public JettyServer(int port, String contextName) {
040: this (port, contextName, null);
041: }
042:
043: public JettyServer(int port, String contextName,
044: String relativeWebappRoot) {
045: this .port = port;
046: this .contextName = contextName;
047: this .relativeWebappRoot = relativeWebappRoot;
048: }
049:
050: public Server getServer() {
051: return server;
052: }
053:
054: public void start() throws Exception {
055: server = createServer();
056: server.start();
057: }
058:
059: public void stop() throws Exception {
060: server.stop();
061: }
062:
063: public boolean isStarted() {
064: return server.isStarted();
065: }
066:
067: protected Server createServer() {
068: Server server = new Server(getPort());
069: try {
070: setBaseDirSystemProperty();
071: WebAppContext context = new WebAppContext(System
072: .getProperty("basedir")
073: + getRelativeWebappRoot(), getContextName());
074: context.setTempDirectory(new File(System
075: .getProperty("basedir")
076: + "/jetty-tmp"));
077: server.addHandler(context);
078: } catch (Exception e) {
079: e.printStackTrace();
080: }
081: return server;
082: }
083:
084: protected void setBaseDirSystemProperty() {
085: if (System.getProperty("basedir") == null) {
086: System.setProperty("basedir", System
087: .getProperty("user.dir"));
088: }
089: }
090:
091: public String getRelativeWebappRoot() {
092: if (relativeWebappRoot == null) {
093: return "/sampleapp/web-root";
094: }
095: return relativeWebappRoot;
096: }
097:
098: public void setRelativeWebappRoot(String relativeWebappRoot) {
099: this .relativeWebappRoot = relativeWebappRoot;
100: }
101:
102: public String getContextName() {
103: if (contextName == null) {
104: return "/SampleRiceClient";
105: }
106: return contextName;
107: }
108:
109: public void setContextName(String contextName) {
110: this .contextName = contextName;
111: }
112:
113: public int getPort() {
114: return port;
115: }
116:
117: public void setPort(int port) {
118: this .port = port;
119: }
120:
121: public static void main(String[] args) {
122: int port = args.length > 0 ? Integer.parseInt(args[0]) : 8080;
123: String contextName = args.length > 1 ? args[1] : null;
124: String relativeWebappRoot = args.length > 2 ? args[2] : null;
125: try {
126: new JettyServer(port, contextName, relativeWebappRoot)
127: .start();
128: } catch (Exception e) {
129: e.printStackTrace();
130: }
131: }
132:
133: }
|