KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > http > HttpLifeCycle


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 package org.apache.servicemix.http;
18
19 import java.net.URI JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.jbi.messaging.MessageExchange;
23 import javax.jbi.servicedesc.ServiceEndpoint;
24 import javax.xml.namespace.QName JavaDoc;
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     /**
68      * @return Returns the configuration.
69      */

70     public HttpConfiguration getConfiguration() {
71         return configuration;
72     }
73     
74     public void setConfiguration(HttpConfiguration configuration) {
75         this.configuration = configuration;
76     }
77
78     /* (non-Javadoc)
79      * @see org.servicemix.common.BaseComponentLifeCycle#getExtensionMBean()
80      */

81     protected Object JavaDoc getExtensionMBean() throws Exception JavaDoc {
82         return configuration;
83     }
84
85     protected void doInit() throws Exception JavaDoc {
86         super.doInit();
87         // Load configuration
88
configuration.setRootDir(context.getWorkspaceRoot());
89         configuration.load();
90         // Lookup keystoreManager and authenticationService
91
if (configuration.getKeystoreManager() == null) {
92             try {
93                 String JavaDoc name = configuration.getKeystoreManagerName();
94                 Object JavaDoc km = context.getNamingContext().lookup(name);
95                 configuration.setKeystoreManager((KeystoreManager) km);
96             } catch (Throwable JavaDoc e) {
97                 // ignore
98
}
99         }
100         if (configuration.getAuthenticationService() == null) {
101             try {
102                 String JavaDoc name = configuration.getAuthenticationServiceName();
103                 Object JavaDoc as = context.getNamingContext().lookup(name);
104                 configuration.setAuthenticationService((AuthenticationService) as);
105             } catch (Throwable JavaDoc e) {
106                 configuration.setAuthenticationService(new JAASAuthenticationService());
107             }
108         }
109         // Create client
110
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         // Create serverManager
119
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 JavaDoc {
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 JavaDoc {
143         super.doStart();
144         server.start();
145     }
146
147     protected void doStop() throws Exception JavaDoc {
148         super.doStop();
149         server.stop();
150     }
151     
152     protected QName JavaDoc getEPRServiceName() {
153         return HttpResolvedEndpoint.EPR_SERVICE;
154     }
155     
156     protected Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception JavaDoc {
157         // We receive an exchange for an EPR that has not been used yet.
158
// Register a provider endpoint and restart processing.
159
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 JavaDoc uri = new URI JavaDoc(ep.getEndpointName());
165         Map JavaDoc 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     /**
177      * @return the keystoreManager
178      */

179     public KeystoreManager getKeystoreManager() {
180         return configuration.getKeystoreManager();
181     }
182
183     /**
184      * @param keystoreManager the keystoreManager to set
185      */

186     public void setKeystoreManager(KeystoreManager keystoreManager) {
187         this.configuration.setKeystoreManager(keystoreManager);
188     }
189
190     /**
191      * @return the authenticationService
192      */

193     public AuthenticationService getAuthenticationService() {
194         return configuration.getAuthenticationService();
195     }
196
197     /**
198      * @param authenticationService the authenticationService to set
199      */

200     public void setAuthenticationService(AuthenticationService authenticationService) {
201         this.configuration.setAuthenticationService(authenticationService);
202     }
203
204     /**
205      * When servicemix-http is embedded inside a web application and configured
206      * to reuse the existing servlet container, this method will create and
207      * return the HTTPProcessor which will handle all servlet calls
208      * @param mappings
209      */

210     public HttpProcessor getMainProcessor() {
211         return server.getMainProcessor();
212     }
213
214 }
215
Popular Tags