1 17 18 package org.apache.geronimo.jetty6; 19 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import javax.management.j2ee.statistics.Stats ; 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 46 public class JettyContainerImpl implements JettyContainer, SoapHandler, GBeanLifecycle, StatisticsProvider { 47 private final Server server; 48 private final Map webServices = new HashMap (); 49 private final String objectName; 50 private final WebManager manager; 51 private JettyWebContainerStatsImpl stats; 52 private final Map realms = new HashMap (); 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 objectName, WebManager manager) { 59 this.objectName = objectName; 60 server = new JettyServer(); 61 62 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 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 protocol) { 105 return manager.getConnectorsForContainer(this, protocol); 106 } 107 108 public void resetStatistics() { 109 } 111 112 public void setCollectStatistics(boolean on) { 113 } 115 116 public boolean getCollectStatistics() { 117 return false; 119 } 120 121 public long getCollectStatisticsStarted() { 122 return 0L; 124 } 125 126 public Stats getStats() { 127 if (getCollectStatistics()) { 128 129 130 132 133 135 136 138 139 143 144 149 150 154 155 stats.getRequestDurationImpl().setCount(stats.getTotalRequestCount().getCount()); 159 161 162 stats.getConnectionDurationImpl().setCount(stats.getTotalConnectionCount().getCount()); 166 168 } else { 169 } 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 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 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 contextPath, String [] virtualHosts, WebServiceContainer webServiceContainer, String securityRealmName, String realmName, String transportGuarantee, String authMethod, ClassLoader classLoader) throws Exception { 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 contextPath) { 220 JettyEJBWebServiceContext webServiceContext = (JettyEJBWebServiceContext) webServices.remove(contextPath); 221 String securityRealmName = webServiceContext.getSecurityRealmName(); 222 if (securityRealmName != null) { 223 removeRealm(securityRealmName); 224 } 225 try { 226 removeContext(webServiceContext); 227 } catch (Exception e) { 228 throw new IllegalStateException (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 { 242 server.start(); 243 } 244 245 public void doStop() { 246 try { 247 server.stop(); 248 } catch (Exception e) { 249 } 250 } 251 252 public void doFail() { 253 try { 254 server.stop(); 255 } catch (Exception e) { 256 } 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 []{Connector.class}); 271 infoBuilder.addOperation("removeListener", new Class []{Connector.class}); 272 infoBuilder.addOperation("addContext", new Class []{ContextHandler.class}); 273 infoBuilder.addOperation("removeContext", new Class []{ContextHandler.class}); 274 infoBuilder.addOperation("addRealm", new Class []{String .class}); 275 infoBuilder.addOperation("removeRealm", new Class []{String .class}); 276 277 infoBuilder.addAttribute("objectName", String .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 []{"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 |