001: //========================================================================
002: // Parts Copyright 2006 Mort Bay Consulting Pty. Ltd.
003: //------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.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: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: //========================================================================
014:
015: package org.mortbay.jetty.grizzly;
016:
017: import java.io.IOException;
018: import java.net.InetAddress;
019:
020: import org.mortbay.io.EndPoint;
021: import org.mortbay.jetty.Connector;
022: import org.mortbay.jetty.Handler;
023: import org.mortbay.jetty.Request;
024: import org.mortbay.jetty.Server;
025: import org.mortbay.jetty.handler.ContextHandlerCollection;
026: import org.mortbay.jetty.handler.DefaultHandler;
027: import org.mortbay.jetty.handler.HandlerCollection;
028: import org.mortbay.jetty.nio.AbstractNIOConnector;
029: import org.mortbay.jetty.security.HashUserRealm;
030: import org.mortbay.jetty.security.UserRealm;
031: import org.mortbay.jetty.webapp.WebAppContext;
032: import org.mortbay.log.Log;
033: import org.mortbay.thread.BoundedThreadPool;
034: import org.mortbay.thread.ThreadPool;
035:
036: /* ------------------------------------------------------------------------------- */
037: /**
038: * @author gregw
039: *
040: */
041: public class GrizzlyConnector extends AbstractNIOConnector {
042: protected JettySelectorThread _selectorThread;
043:
044: /* ------------------------------------------------------------------------------- */
045: /**
046: * Constructor.
047: *
048: */
049: public GrizzlyConnector() {
050: _selectorThread = new JettySelectorThread();
051: }
052:
053: /* ------------------------------------------------------------ */
054: public Object getConnection() {
055: return _selectorThread.getServerSocketChannel();
056: }
057:
058: /* ------------------------------------------------------------ */
059: /*
060: * @see org.mortbay.jetty.AbstractConnector#doStart()
061: */
062: protected void doStart() throws Exception {
063: super .doStart();
064:
065: // TODO - is there a non-blocking way to do this?
066: new Thread() {
067: public void run() {
068: try {
069: _selectorThread.startEndpoint();
070: } catch (InstantiationException e) {
071: // TODO Auto-generated catch block
072: e.printStackTrace();
073: } catch (IOException e) {
074: // TODO Auto-generated catch block
075: e.printStackTrace();
076: }
077: }
078: }.start();
079: }
080:
081: /* ------------------------------------------------------------ */
082: /*
083: * @see org.mortbay.jetty.AbstractConnector#doStop()
084: */
085: protected void doStop() throws Exception {
086: super .doStop();
087: _selectorThread.stopEndpoint();
088: }
089:
090: /* ------------------------------------------------------------ */
091: public void open() throws IOException {
092: // TODO Open server socket
093: try {
094: _selectorThread.setPort(getPort());
095: _selectorThread.setGrizzlyConnector(this );
096:
097: ThreadPool threadPool = getServer().getThreadPool();
098: _selectorThread.setThreadPool(threadPool);
099:
100: //TO DO: Needs to find a way to get this information from the
101: // config file directly.
102: if (threadPool instanceof BoundedThreadPool) {
103: _selectorThread
104: .setMaxThreads(((BoundedThreadPool) threadPool)
105: .getMaxThreads());
106: _selectorThread
107: .setMinThreads(((BoundedThreadPool) threadPool)
108: .getMinThreads());
109: }
110: _selectorThread.setDisplayConfiguration(true);
111:
112: if (getHost() != null)
113: _selectorThread.setAddress(InetAddress
114: .getByName(getHost()));
115: _selectorThread.initEndpoint();
116: } catch (InstantiationException ex) {
117: throw new RuntimeException(ex);
118: }
119: }
120:
121: /* ------------------------------------------------------------ */
122: public void close() throws IOException {
123: // TODO Close server socket
124: // XXX Only supported when calling selectorThread.stopEndpoint();
125: }
126:
127: /* ------------------------------------------------------------ */
128: public void accept(int acceptorID) throws IOException {
129: try {
130: // TODO - this may not exactly be right. accept is called in a loop, so we
131: // may need to wait on the _selectorThread somehow?
132: // maybe we just set acceptors to zero and don't need to bother here as
133: // grizzly has it's own accepting threads.
134: _selectorThread.isAlive();
135: Thread.sleep(5000);
136:
137: } catch (Throwable e) {
138: // TODO Auto-generated catch block
139: Log.ignore(e);
140: }
141:
142: }
143:
144: /* ------------------------------------------------------------ */
145: public void stopAccept(int acceptorID) throws Exception {
146: // TODO
147: }
148:
149: /* ------------------------------------------------------------------------------- */
150: public void customize(EndPoint endpoint, Request request)
151: throws IOException {
152: super .customize(endpoint, request);
153: }
154:
155: /* ------------------------------------------------------------------------------- */
156: public int getLocalPort() {
157: // TODO return the actual port we are listening on
158: return _selectorThread.getPort();
159: }
160:
161: /* ------------------------------------------------------------------------------- */
162: /** temp main - just to help testing */
163: public static void main(String[] args) throws Exception {
164: Server server = new Server();
165: Connector connector = new GrizzlyConnector();
166: connector.setPort(8080);
167: server.setConnectors(new Connector[] { connector });
168:
169: HandlerCollection handlers = new HandlerCollection();
170: ContextHandlerCollection contexts = new ContextHandlerCollection();
171: handlers.setHandlers(new Handler[] { contexts,
172: new DefaultHandler() });
173: server.setHandler(handlers);
174:
175: // TODO add javadoc context to contexts
176:
177: WebAppContext.addWebApplications(server, "../../webapps",
178: "org/mortbay/jetty/webapp/webdefault.xml", true, false);
179:
180: HashUserRealm userRealm = new HashUserRealm();
181: userRealm.setName("Test Realm");
182: userRealm.setConfig("../../etc/realm.properties");
183: server.setUserRealms(new UserRealm[] { userRealm });
184:
185: server.start();
186: server.join();
187:
188: }
189: }
|