001: //========================================================================
002: //$Id: Jetty6PluginServer.java 860 2006-08-31 22:23:51Z janb $
003: //Copyright 2000-2004 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.jetty.plugin;
017:
018: import org.mortbay.jetty.Connector;
019: import org.mortbay.jetty.Handler;
020: import org.mortbay.jetty.RequestLog;
021: import org.mortbay.jetty.Server;
022: import org.mortbay.jetty.handler.ContextHandlerCollection;
023: import org.mortbay.jetty.handler.DefaultHandler;
024: import org.mortbay.jetty.handler.HandlerCollection;
025: import org.mortbay.jetty.handler.RequestLogHandler;
026: import org.mortbay.jetty.nio.SelectChannelConnector;
027: import org.mortbay.jetty.plugin.util.JettyPluginServer;
028: import org.mortbay.jetty.plugin.util.JettyPluginWebApplication;
029: import org.mortbay.jetty.plugin.util.PluginLog;
030: import org.mortbay.jetty.security.UserRealm;
031: import org.mortbay.resource.Resource;
032:
033: /**
034: * Jetty6PluginServer
035: *
036: * Jetty6 version of a wrapper for the Server class.
037: *
038: */
039: public class Jetty6PluginServer implements JettyPluginServer {
040: public static int DEFAULT_PORT = 8080;
041: public static int DEFAULT_MAX_IDLE_TIME = 30000;
042: private Server server;
043: private Connector[] connectors;
044: private UserRealm[] realms;
045: private ContextHandlerCollection contexts; //the list of ContextHandlers
046: HandlerCollection handlers; //the list of lists of Handlers
047: private RequestLogHandler requestLogHandler; //the request log handler
048: private DefaultHandler defaultHandler; //default handler
049:
050: private RequestLog requestLog; //the particular request log implementation
051:
052: /**
053: * @see org.mortbay.jetty.plugin.util.JettyPluginServer#create()
054: */
055: public Jetty6PluginServer() {
056: this .server = new Server();
057: this .server.setStopAtShutdown(true);
058: //make sure Jetty does not use URLConnection caches with the plugin
059: Resource.setDefaultUseCaches(false);
060: }
061:
062: /**
063: * @see org.mortbay.jetty.plugin.util.JettyPluginServer#setConnectorNames(org.mortbay.jetty.plugin.util.JettyPluginConnector[])
064: */
065: public void setConnectors(Object[] connectors) {
066: this .connectors = new Connector[connectors.length];
067: for (int i = 0; i < connectors.length; i++) {
068: this .connectors[i] = (Connector) connectors[i];
069: PluginLog.getLog().debug(
070: "Setting Connector: "
071: + this .connectors[i].getClass().getName()
072: + " on port "
073: + this .connectors[i].getPort());
074: }
075: this .server.setConnectors(this .connectors);
076: }
077:
078: /**
079: *
080: *
081: * @see org.mortbay.jetty.plugin.util.JettyPluginServer#getConnectors()
082: */
083: public Object[] getConnectors() {
084: return this .connectors;
085: }
086:
087: /**
088: *
089: *
090: * @see org.mortbay.jetty.plugin.JettyPluginServer#setUserRealms(org.mortbay.jetty.plugin.JettyPluginUserRealm[])
091: */
092: public void setUserRealms(Object[] realms) throws Exception {
093: if (realms == null)
094: this .realms = null;
095: else {
096: this .realms = new UserRealm[realms.length];
097: for (int i = 0; i < realms.length; i++)
098: this .realms[i] = (UserRealm) realms[i];
099: }
100:
101: this .server.setUserRealms(this .realms);
102: }
103:
104: /**
105: *
106: * @see org.mortbay.jetty.plugin.util.JettyPluginServer#getUserRealms()
107: */
108: public Object[] getUserRealms() {
109: return this .realms;
110: }
111:
112: public void setRequestLog(Object requestLog) {
113: this .requestLog = (RequestLog) requestLog;
114: }
115:
116: public Object getRequestLog() {
117: return this .requestLog;
118: }
119:
120: /**
121: * @see org.mortbay.jetty.plugin.util.JettyPluginServer#start()
122: */
123: public void start() throws Exception {
124: PluginLog.getLog().info(
125: "Starting jetty "
126: + this .server.getClass().getPackage()
127: .getImplementationVersion() + " ...");
128: this .server.start();
129: }
130:
131: /**
132: * @see org.mortbay.jetty.plugin.util.Proxy#getProxiedObject()
133: */
134: public Object getProxiedObject() {
135: return this .server;
136: }
137:
138: /**
139: * @see org.mortbay.jetty.plugin.util.JettyPluginServer#addWebApplication(java.lang.Object)
140: */
141: public void addWebApplication(JettyPluginWebApplication webapp)
142: throws Exception {
143: contexts.addHandler((Handler) webapp.getProxiedObject());
144: }
145:
146: /**
147: * Set up the handler structure to receive a webapp.
148: * Also put in a DefaultHandler so we get a nice page
149: * than a 404 if we hit the root and the webapp's
150: * context isn't at root.
151: * @throws Exception
152: */
153: public void configureHandlers() throws Exception {
154: this .defaultHandler = new DefaultHandler();
155: this .requestLogHandler = new RequestLogHandler();
156: if (this .requestLog != null)
157: this .requestLogHandler.setRequestLog(this .requestLog);
158:
159: this .contexts = (ContextHandlerCollection) server
160: .getChildHandlerByClass(ContextHandlerCollection.class);
161: if (this .contexts == null) {
162: this .contexts = new ContextHandlerCollection();
163: this .handlers = (HandlerCollection) server
164: .getChildHandlerByClass(HandlerCollection.class);
165: if (this .handlers == null) {
166: this .handlers = new HandlerCollection();
167: this .server.setHandler(handlers);
168: this .handlers.setHandlers(new Handler[] {
169: this .contexts, this .defaultHandler,
170: this .requestLogHandler });
171: } else {
172: this .handlers.addHandler(this .contexts);
173: }
174: }
175: }
176:
177: /**
178: * @see org.mortbay.jetty.plugin.JettyPluginServer#createDefaultConnector()
179: */
180: public Object createDefaultConnector(String portnum)
181: throws Exception {
182: SelectChannelConnector connector = new SelectChannelConnector();
183: connector = new SelectChannelConnector();
184: int port = ((portnum == null || portnum.equals("")) ? DEFAULT_PORT
185: : Integer.parseInt(portnum.trim()));
186: connector.setPort(port);
187: connector.setMaxIdleTime(DEFAULT_MAX_IDLE_TIME);
188:
189: return connector;
190: }
191:
192: /**
193: * @see org.mortbay.jetty.plugin.util.JettyPluginServer#createWebApplication()
194: */
195: public JettyPluginWebApplication createWebApplication()
196: throws Exception {
197: return new Jetty6PluginWebApplication();
198: }
199:
200: public void join() throws Exception {
201: this.server.getThreadPool().join();
202: }
203: }
|