KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty6 > JettyContainerImpl


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.jetty6;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.management.j2ee.statistics.Stats JavaDoc;
24
25 import org.apache.geronimo.gbean.GBeanInfo;
26 import org.apache.geronimo.gbean.GBeanInfoBuilder;
27 import org.apache.geronimo.gbean.GBeanLifecycle;
28 import org.apache.geronimo.management.StatisticsProvider;
29 import org.apache.geronimo.management.geronimo.NetworkConnector;
30 import org.apache.geronimo.management.geronimo.WebManager;
31 import org.apache.geronimo.webservices.SoapHandler;
32 import org.apache.geronimo.webservices.WebServiceContainer;
33 import org.mortbay.jetty.Connector;
34 import org.mortbay.jetty.Handler;
35 import org.mortbay.jetty.RequestLog;
36 import org.mortbay.jetty.Server;
37 import org.mortbay.jetty.handler.ContextHandler;
38 import org.mortbay.jetty.handler.ContextHandlerCollection;
39 import org.mortbay.jetty.handler.DefaultHandler;
40 import org.mortbay.jetty.handler.HandlerCollection;
41 import org.mortbay.jetty.handler.RequestLogHandler;
42
43 /**
44  * @version $Rev: 487669 $ $Date: 2006-12-15 16:22:23 -0500 (Fri, 15 Dec 2006) $
45  */

46 public class JettyContainerImpl implements JettyContainer, SoapHandler, GBeanLifecycle, StatisticsProvider {
47     private final Server server;
48     private final Map JavaDoc webServices = new HashMap JavaDoc();
49     private final String JavaDoc objectName;
50     private final WebManager manager;
51     private JettyWebContainerStatsImpl stats;
52     private final Map JavaDoc realms = new HashMap JavaDoc();
53     private HandlerCollection handlerCollection = new HandlerCollection();
54     private ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection();
55     private DefaultHandler defaultHandler = new DefaultHandler();
56     private RequestLogHandler requestLogHandler = new RequestLogHandler();
57
58     public JettyContainerImpl(String JavaDoc objectName, WebManager manager) {
59         this.objectName = objectName;
60         server = new JettyServer();
61
62         //set up the new jetty6 handler structure which is to have a HandlerCollection,
63
//each element of which is always tried on each request.
64
//The first element of the HandlerCollection is a
65
//ContextHandlerCollection, which is itself is a collection
66
//of Handlers. It's special property is that only of it's
67
//handlers will respond to a request.
68
//The second element of the HandlerCollection is a DefaultHandler
69
//which is responsible for serving static content or anything not
70
//handled by a Handler in the ContextHandlerCollection.
71
//The third element is the RequestLogHandler, which requires
72
//a RequestLog impl to be set.
73
Handler[] handlers = new Handler[3];
74         handlers[0] = contextHandlerCollection;
75         handlers[1] = defaultHandler;
76         handlers[2] = requestLogHandler;
77         handlerCollection.setHandlers(handlers);
78         server.setHandler(handlerCollection);
79
80         stats = new JettyWebContainerStatsImpl();
81         this.manager = manager;
82     }
83
84     public String JavaDoc getObjectName() {
85         return objectName;
86     }
87
88     public boolean isStateManageable() {
89         return true;
90     }
91
92     public boolean isStatisticsProvider() {
93         return true;
94     }
95
96     public boolean isEventProvider() {
97         return true;
98     }
99
100     public NetworkConnector[] getConnectors() {
101         return manager.getConnectorsForContainer(this);
102     }
103
104     public NetworkConnector[] getConnectors(String JavaDoc protocol) {
105         return manager.getConnectorsForContainer(this, protocol);
106     }
107
108     public void resetStatistics() {
109         //TODO: for jetty6
110
}
111
112     public void setCollectStatistics(boolean on) {
113         //TODO: for jetty6
114
}
115
116     public boolean getCollectStatistics() {
117         //TODO: for jetty6
118
return false;
119     }
120
121     public long getCollectStatisticsStarted() {
122         //TODO: for jetty6
123
return 0L;
124     }
125
126     public Stats JavaDoc getStats() {
127         if (getCollectStatistics()) {
128
129             /* set active request count */
130 // stats.getTotalRequestCountImpl().setCount(server.getRequests());
131

132             /* set total connection count */
133 // stats.getTotalConnectionCountImpl().setCount(server.getConnections());
134

135             /* set total error count */
136 // stats.getTotalErrorCountImpl().setCount(server.getErrors());
137

138             /* set active request range values */
139 // stats.getActiveRequestCountImpl().setCurrent(server.getRequestsActive());
140
// stats.getActiveRequestCountImpl().setLowWaterMark(server.getRequestsActiveMin());
141
// stats.getActiveRequestCountImpl().setHighWaterMark(server.getRequestsActiveMax());
142

143             /* set connection requests range values */
144 // stats.getConnectionRequestCountImpl().setCurrent(server.getConnectionsRequestsCurrent()); // temporarily removed until added by jetty6
145
// stats.getConnectionRequestCountImpl().setCurrent(server.getConnectionsOpen());
146
// stats.getConnectionRequestCountImpl().setLowWaterMark(server.getConnectionsRequestsMin());
147
// stats.getConnectionRequestCountImpl().setHighWaterMark(server.getConnectionsRequestsMax());
148

149             /* set open connection range values */
150 // stats.getOpenConnectionCountImpl().setCurrent(server.getConnectionsOpen());
151
// stats.getOpenConnectionCountImpl().setLowWaterMark(server.getConnectionsOpenMin());
152
// stats.getOpenConnectionCountImpl().setHighWaterMark(server.getConnectionsOpenMax());
153

154             /* set request duration time values */
155 // stats.getRequestDurationImpl().setMinTime(server.getRequestsDurationMin());
156
// stats.getRequestDurationImpl().setMaxTime(server.getRequestsDurationMax());
157
// stats.getRequestDurationImpl().setCount(server.getRequestsDurationCount()); // temporarily removed until added by jetty6
158
stats.getRequestDurationImpl().setCount(stats.getTotalRequestCount().getCount());
159 // stats.getRequestDurationImpl().setTotalTime(server.getRequestsDurationTotal());
160

161             /* set connection duration Time values */
162 // stats.getConnectionDurationImpl().setMinTime(server.getConnectionsDurationMin());
163
// stats.getConnectionDurationImpl().setMaxTime(server.getConnectionsDurationMax());
164
// stats.getConnectionDurationImpl().setCount(server.getConnectionsDurationCount()); // temporarily removed until added by jetty6
165
stats.getConnectionDurationImpl().setCount(stats.getTotalConnectionCount().getCount());
166 // stats.getConnectionDurationImpl().setTotalTime(server.getConnectionsDurationTotal());
167

168         } else {
169             // should probably set the stats object to all zero/null values to avoid unpredicable results
170
}
171         return stats;
172     }
173
174     public void addListener(Connector listener) {
175         server.addConnector(listener);
176     }
177
178     public void removeListener(Connector listener) {
179         server.removeConnector(listener);
180     }
181
182     public void addContext(ContextHandler context) {
183         contextHandlerCollection.addHandler(context);
184     }
185
186     public void removeContext(ContextHandler context) {
187         contextHandlerCollection.removeHandler(context);
188     }
189
190     public InternalJAASJettyRealm addRealm(String JavaDoc realmName) {
191         InternalJAASJettyRealm realm = (InternalJAASJettyRealm) realms.get(realmName);
192         if (realm == null) {
193             realm = new InternalJAASJettyRealm(realmName);
194             realms.put(realmName, realm);
195         } else {
196             realm.addUse();
197         }
198         return realm;
199     }
200
201     public void removeRealm(String JavaDoc realmName) {
202         InternalJAASJettyRealm realm = (InternalJAASJettyRealm) realms.get(realmName);
203         if (realm != null) {
204             if (realm.removeUse() == 0) {
205                 realms.remove(realmName);
206             }
207         }
208     }
209
210     public void addWebService(String JavaDoc contextPath, String JavaDoc[] virtualHosts, WebServiceContainer webServiceContainer, String JavaDoc securityRealmName, String JavaDoc realmName, String JavaDoc transportGuarantee, String JavaDoc authMethod, ClassLoader JavaDoc classLoader) throws Exception JavaDoc {
211         InternalJAASJettyRealm internalJAASJettyRealm = securityRealmName == null ? null : addRealm(securityRealmName);
212         JettyEJBWebServiceContext webServiceContext = new JettyEJBWebServiceContext(contextPath, webServiceContainer, internalJAASJettyRealm, realmName, transportGuarantee, authMethod, classLoader);
213         webServiceContext.setVirtualHosts(virtualHosts);
214         addContext(webServiceContext);
215         webServiceContext.start();
216         webServices.put(contextPath, webServiceContext);
217     }
218
219     public void removeWebService(String JavaDoc contextPath) {
220         JettyEJBWebServiceContext webServiceContext = (JettyEJBWebServiceContext) webServices.remove(contextPath);
221         String JavaDoc securityRealmName = webServiceContext.getSecurityRealmName();
222         if (securityRealmName != null) {
223             removeRealm(securityRealmName);
224         }
225         try {
226             removeContext(webServiceContext);
227         } catch (Exception JavaDoc e) {
228             throw new IllegalStateException JavaDoc(e.getMessage());
229         }
230     }
231
232     public void setRequestLog(RequestLog log) {
233         this.requestLogHandler.setRequestLog(log);
234     }
235
236     /* ------------------------------------------------------------ */
237     public RequestLog getRequestLog() {
238         return this.requestLogHandler.getRequestLog();
239     }
240
241     public void doStart() throws Exception JavaDoc {
242         server.start();
243     }
244
245     public void doStop() {
246         try {
247             server.stop();
248         } catch (Exception JavaDoc e) {
249         }
250     }
251
252     public void doFail() {
253         try {
254             server.stop();
255         } catch (Exception JavaDoc e) {
256             // continue
257
}
258     }
259
260     public static final GBeanInfo GBEAN_INFO;
261
262     static {
263         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic("Jetty Web Container", JettyContainerImpl.class);
264         infoBuilder.addAttribute("collectStatistics", Boolean.TYPE, true);
265         infoBuilder.addAttribute("collectStatisticsStarted", Long.TYPE, false);
266         infoBuilder.addOperation("resetStatistics");
267
268         infoBuilder.addAttribute("requestLog", RequestLog.class, false, false);
269
270         infoBuilder.addOperation("addListener", new Class JavaDoc[]{Connector.class});
271         infoBuilder.addOperation("removeListener", new Class JavaDoc[]{Connector.class});
272         infoBuilder.addOperation("addContext", new Class JavaDoc[]{ContextHandler.class});
273         infoBuilder.addOperation("removeContext", new Class JavaDoc[]{ContextHandler.class});
274         infoBuilder.addOperation("addRealm", new Class JavaDoc[]{String JavaDoc.class});
275         infoBuilder.addOperation("removeRealm", new Class JavaDoc[]{String JavaDoc.class});
276
277         infoBuilder.addAttribute("objectName", String JavaDoc.class, false);
278         infoBuilder.addReference("WebManager", WebManager.class);
279
280         infoBuilder.addInterface(SoapHandler.class);
281         infoBuilder.addInterface(JettyContainer.class);
282         infoBuilder.addInterface(StatisticsProvider.class);
283         infoBuilder.setConstructor(new String JavaDoc[]{"objectName", "WebManager"});
284
285         GBEAN_INFO = infoBuilder.getBeanInfo();
286     }
287
288     public static GBeanInfo getGBeanInfo() {
289         return GBEAN_INFO;
290     }
291
292 }
293
Popular Tags