1 17 package org.apache.servicemix.http; 18 19 import java.net.URI ; 20 import java.util.Map ; 21 22 import javax.jbi.messaging.MessageExchange; 23 import javax.jbi.servicedesc.ServiceEndpoint; 24 import javax.xml.namespace.QName ; 25 26 import org.apache.activemq.util.IntrospectionSupport; 27 import org.apache.activemq.util.URISupport; 28 import org.apache.commons.httpclient.HttpClient; 29 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 30 import org.apache.commons.httpclient.params.HttpConnectionManagerParams; 31 import org.apache.servicemix.common.BaseComponent; 32 import org.apache.servicemix.common.BaseLifeCycle; 33 import org.apache.servicemix.common.Endpoint; 34 import org.apache.servicemix.common.ServiceUnit; 35 import org.apache.servicemix.http.jetty.JettyContextManager; 36 import org.apache.servicemix.jbi.security.auth.AuthenticationService; 37 import org.apache.servicemix.jbi.security.auth.impl.JAASAuthenticationService; 38 import org.apache.servicemix.jbi.security.keystore.KeystoreManager; 39 40 public class HttpLifeCycle extends BaseLifeCycle { 41 42 protected ContextManager server; 43 protected HttpClient client; 44 protected MultiThreadedHttpConnectionManager connectionManager; 45 protected HttpConfiguration configuration = new HttpConfiguration(); 46 47 public HttpLifeCycle(BaseComponent component) { 48 super(component); 49 } 50 51 public ContextManager getServer() { 52 return server; 53 } 54 55 public void setServer(ContextManager server) { 56 this.server = server; 57 } 58 59 public HttpClient getClient() { 60 return client; 61 } 62 63 public void setClient(HttpClient client) { 64 this.client = client; 65 } 66 67 70 public HttpConfiguration getConfiguration() { 71 return configuration; 72 } 73 74 public void setConfiguration(HttpConfiguration configuration) { 75 this.configuration = configuration; 76 } 77 78 81 protected Object getExtensionMBean() throws Exception { 82 return configuration; 83 } 84 85 protected void doInit() throws Exception { 86 super.doInit(); 87 configuration.setRootDir(context.getWorkspaceRoot()); 89 configuration.load(); 90 if (configuration.getKeystoreManager() == null) { 92 try { 93 String name = configuration.getKeystoreManagerName(); 94 Object km = context.getNamingContext().lookup(name); 95 configuration.setKeystoreManager((KeystoreManager) km); 96 } catch (Throwable e) { 97 } 99 } 100 if (configuration.getAuthenticationService() == null) { 101 try { 102 String name = configuration.getAuthenticationServiceName(); 103 Object as = context.getNamingContext().lookup(name); 104 configuration.setAuthenticationService((AuthenticationService) as); 105 } catch (Throwable e) { 106 configuration.setAuthenticationService(new JAASAuthenticationService()); 107 } 108 } 109 if (client == null) { 111 connectionManager = new MultiThreadedHttpConnectionManager(); 112 HttpConnectionManagerParams params = new HttpConnectionManagerParams(); 113 params.setDefaultMaxConnectionsPerHost(configuration.getMaxConnectionsPerHost()); 114 params.setMaxTotalConnections(configuration.getMaxTotalConnections()); 115 connectionManager.setParams(params); 116 client = new HttpClient(connectionManager); 117 } 118 if (configuration.isManaged()) { 120 server = new ManagedContextManager(); 121 } else { 122 JettyContextManager server = new JettyContextManager(); 123 server.setMBeanServer(context.getMBeanServer()); 124 this.server = server; 125 } 126 server.setConfiguration(configuration); 127 server.init(); 128 } 129 130 protected void doShutDown() throws Exception { 131 super.doShutDown(); 132 if (server != null) { 133 ContextManager s = server; 134 server = null; 135 s.shutDown(); 136 } 137 if (connectionManager != null) { 138 connectionManager.shutdown(); 139 } 140 } 141 142 protected void doStart() throws Exception { 143 super.doStart(); 144 server.start(); 145 } 146 147 protected void doStop() throws Exception { 148 super.doStop(); 149 server.stop(); 150 } 151 152 protected QName getEPRServiceName() { 153 return HttpResolvedEndpoint.EPR_SERVICE; 154 } 155 156 protected Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception { 157 HttpEndpoint httpEp = new HttpEndpoint(); 160 httpEp.setServiceUnit(new ServiceUnit(component)); 161 httpEp.setService(ep.getServiceName()); 162 httpEp.setEndpoint(ep.getEndpointName()); 163 httpEp.setRole(MessageExchange.Role.PROVIDER); 164 URI uri = new URI (ep.getEndpointName()); 165 Map map = URISupport.parseQuery(uri.getQuery()); 166 if( IntrospectionSupport.setProperties(httpEp, map, "http.") ) { 167 uri = URISupport.createRemainingURI(uri, map); 168 } 169 if (httpEp.getLocationURI() == null) { 170 httpEp.setLocationURI(uri.toString()); 171 } 172 httpEp.activateDynamic(); 173 return httpEp; 174 } 175 176 179 public KeystoreManager getKeystoreManager() { 180 return configuration.getKeystoreManager(); 181 } 182 183 186 public void setKeystoreManager(KeystoreManager keystoreManager) { 187 this.configuration.setKeystoreManager(keystoreManager); 188 } 189 190 193 public AuthenticationService getAuthenticationService() { 194 return configuration.getAuthenticationService(); 195 } 196 197 200 public void setAuthenticationService(AuthenticationService authenticationService) { 201 this.configuration.setAuthenticationService(authenticationService); 202 } 203 204 210 public HttpProcessor getMainProcessor() { 211 return server.getMainProcessor(); 212 } 213 214 } 215 | Popular Tags |