001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.wso2.esb.transport.tomcat;
020:
021: import org.apache.catalina.Context;
022: import org.apache.catalina.Engine;
023: import org.apache.catalina.Host;
024: import org.apache.catalina.LifecycleException;
025: import org.apache.catalina.connector.Connector;
026: import org.apache.catalina.realm.MemoryRealm;
027: import org.apache.catalina.startup.Embedded;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.tomcat.util.IntrospectionUtils;
031: import org.wso2.esb.ServiceBusConfiguration;
032: import org.wso2.esb.ServiceBusConstants;
033: import org.wso2.esb.ServiceBusException;
034: import org.wso2.esb.ServiceBusManager;
035: import org.wso2.esb.transport.WebServer;
036: import org.wso2.esb.util.XmlConfigurationFactory;
037: import org.wso2.utils.NetworkUtils;
038:
039: import java.io.File;
040: import java.net.InetAddress;
041:
042: /**
043: *
044: */
045:
046: public class TomcatServer implements WebServer {
047:
048: private static Log log = LogFactory.getLog(TomcatServer.class);
049: private Embedded embedded;
050:
051: public TomcatServer() {
052: embedded = new Embedded();
053: }
054:
055: public void start() throws Exception {
056: Engine engine;
057: ServiceBusManager manager = ServiceBusManager.getInstance();
058:
059: String esbHome = System
060: .getProperty(ServiceBusConstants.ESB_HOME);
061:
062: String catalinaHome = new File(esbHome).getAbsolutePath()
063: + File.separator + "tomcat";
064:
065: embedded.setCatalinaHome(catalinaHome);
066: String webappsDir = esbHome + File.separator + "webapp";
067: String docsDir = esbHome + File.separator + "docs";
068: String samplesDir = esbHome + File.separator + "samples";
069:
070: // set the memory realm
071: MemoryRealm memRealm = new MemoryRealm();
072: embedded.setRealm(memRealm);
073:
074: // Create an engine
075: String host = NetworkUtils.getLocalHostname();
076: manager.setHost(host);
077: engine = embedded.createEngine();
078: engine.setName("Catalina");
079: engine.setDefaultHost(manager.getHost());
080: embedded.addEngine(engine);
081:
082: // Create a default virtual host
083: Host defaultHost = embedded.createHost(host, webappsDir);
084: engine.addChild(defaultHost);
085: String esbContextPath = XmlConfigurationFactory
086: .getXmlConfiguration(
087: ServiceBusConstants.ESB_WEB_XML_KEY)
088: .getUniqueValue(
089: "//ns:Mapping[@name=\"admin\"]/ns:ContextPath");
090:
091: if (esbContextPath != null) {
092: if (esbContextPath.equals("/")) {
093: esbContextPath = "";
094: } else {
095: if (!esbContextPath.startsWith("/")) {
096: esbContextPath = "/" + esbContextPath;
097: }
098: }
099: } else {
100: esbContextPath = "/esb";
101: }
102:
103: Context esbContext = embedded.createContext(esbContextPath,
104: new File(webappsDir).getAbsolutePath());
105: defaultHost.addChild(esbContext);
106:
107: Context docsContext = embedded.createContext("/docs", new File(
108: docsDir).getAbsolutePath());
109: defaultHost.addChild(docsContext);
110:
111: Context samplesContext = embedded.createContext("/samples",
112: new File(samplesDir).getAbsolutePath());
113: defaultHost.addChild(samplesContext);
114:
115: defaultHost.setDeployOnStartup(true);
116: embedded.setUseNaming(true);
117:
118: // add the ssl listner for https transport - admin console
119: addSSLConnector();
120:
121: // Start the embedded server
122: try {
123: embedded.start();
124: log.info("Tomcat Server Started at " + "https://" + host
125: + ":" + manager.getHttpsPort() + esbContextPath);
126: } catch (Exception e) {
127: log.fatal("Error starting embedded tomcat server : " + e);
128: throw new ServiceBusException(e);
129: }
130:
131: }
132:
133: private void addSSLConnector() {
134: ServiceBusConfiguration esbConfiguration = ServiceBusConfiguration
135: .getInstance();
136: ServiceBusManager manager = ServiceBusManager.getInstance();
137: String portString = esbConfiguration
138: .getFirstProperty("AdminConsole.Port");
139: if (portString != null) {
140: int port = Integer.parseInt(portString);
141: if (port > 0) {
142: manager.setHttpsPort(port);
143: }
144: }
145: String ksLocation = esbConfiguration
146: .getFirstProperty("AdminConsole.KeyStore.Location");
147: String absKsLocation;
148: if (!new File(ksLocation).isAbsolute()) {
149: absKsLocation = new File(System
150: .getProperty(ServiceBusConstants.ESB_HOME)
151: + File.separator + ksLocation).getAbsolutePath();
152: } else {
153: absKsLocation = ksLocation;
154: }
155: String ksPassword = esbConfiguration
156: .getFirstProperty("AdminConsole.KeyStore.Password");
157:
158: String sslProtocol = esbConfiguration
159: .getFirstProperty("AdminConsole.SslProtocol");
160: String maxHttpHeaderSize = esbConfiguration
161: .getFirstProperty("AdminConsole.MaxHttpHeaderSize");
162: String maxThreads = esbConfiguration
163: .getFirstProperty("AdminConsole.MaxThreads");
164: String minSpareThreads = esbConfiguration
165: .getFirstProperty("AdminConsole.MinSpareThreads");
166: String maxSpareThreads = esbConfiguration
167: .getFirstProperty("AdminConsole.MaxSpareThreads");
168: String clientAuth = esbConfiguration
169: .getFirstProperty("AdminConsole.ClientAuth");
170: String disableUploadTimeout = esbConfiguration
171: .getFirstProperty("AdminConsole.DisableUploadTimeout");
172: String acceptCount = esbConfiguration
173: .getFirstProperty("AdminConsole.AcceptCount");
174: // SSLConnector
175: Connector sslConnector = embedded.createConnector(
176: (InetAddress) null, manager.getHttpsPort(), true);
177: sslConnector.setScheme(ServiceBusConstants.HTTPS_TRANSPORT);
178: if (ksPassword != null) {
179: IntrospectionUtils.setProperty(sslConnector, "keypass",
180: ksPassword);
181: }
182: if (ksLocation != null) {
183: IntrospectionUtils.setProperty(sslConnector, "keystore",
184: absKsLocation);
185: }
186: if (sslProtocol != null) {
187: IntrospectionUtils.setProperty(sslConnector, "sslProtocol",
188: sslProtocol);
189: }
190: if (maxHttpHeaderSize != null) {
191: IntrospectionUtils.setProperty(sslConnector,
192: "maxHttpHeaderSize", maxHttpHeaderSize);
193: }
194: if (maxThreads != null) {
195: IntrospectionUtils.setProperty(sslConnector, "maxThreads",
196: maxThreads);
197: }
198: if (maxSpareThreads != null) {
199: IntrospectionUtils.setProperty(sslConnector,
200: "maxSpareThreads", maxSpareThreads);
201: }
202: if (minSpareThreads != null) {
203: IntrospectionUtils.setProperty(sslConnector,
204: "minSpareThreads", minSpareThreads);
205: }
206: if (disableUploadTimeout != null) {
207: IntrospectionUtils.setProperty(sslConnector,
208: "disableUploadTimeout", disableUploadTimeout);
209: }
210: if (clientAuth != null) {
211: IntrospectionUtils.setProperty(sslConnector, "clientAuth",
212: clientAuth);
213: }
214: if (acceptCount != null) {
215: IntrospectionUtils.setProperty(sslConnector, "acceptCount",
216: acceptCount);
217: }
218: sslConnector.setEnableLookups(true);
219: sslConnector.setSecure(true);
220: embedded.addConnector(sslConnector);
221: }
222:
223: public void stop() throws Exception {
224:
225: try {
226: embedded.stop();
227: } catch (LifecycleException e) {
228: log
229: .error("Error occurred while stopping embedded tomcat server : "
230: + e);
231: }
232: }
233: }
|