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.axis2.context.ConfigurationContext;
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.log4j.PropertyConfigurator;
025: import org.wso2.adminui.AdminUIServletFilter;
026: import org.wso2.esb.ServiceBusConstants;
027: import org.wso2.esb.ServiceBusManager;
028:
029: import javax.servlet.ServletConfig;
030: import javax.servlet.ServletContext;
031: import javax.servlet.ServletException;
032: import javax.servlet.http.HttpServlet;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035: import java.io.File;
036: import java.io.IOException;
037: import java.net.MalformedURLException;
038: import java.net.URL;
039: import java.util.Map;
040:
041: /**
042: *
043: */
044:
045: public class StartUpServlet extends HttpServlet {
046:
047: private static Log log = LogFactory.getLog(StartUpServlet.class);
048: private final static String HAS_ALREADY_INIT = "hasAlreadyInit";
049: private final static String TRUE = "true";
050: private static final long serialVersionUID = 4517849288538613640L;
051:
052: public void init() throws ServletException {
053: super .init();
054: }
055:
056: public void init(ServletConfig servletConfig)
057: throws ServletException {
058:
059: ServletContext servletContext = servletConfig
060: .getServletContext();
061: if (TRUE.equals(servletContext.getAttribute(HAS_ALREADY_INIT))) {
062: return;
063: }
064:
065: String esbhome = resolveEsbHome(servletConfig);
066: if (esbhome != null || !"".equals(esbhome)) {
067: System.setProperty(ServiceBusConstants.ESB_HOME, esbhome);
068: ServiceBusManager.getInstance().initSystemProperties();
069: try {
070: URL url = new URL("file:" + esbhome + File.separator
071: + ServiceBusConstants.ESB_CONF_DIRECTORY
072: + "log4j.properties");
073: PropertyConfigurator.configure(url);
074: } catch (MalformedURLException ignore) {
075: }
076: } else {
077: String msg = "esb.home had not set : startup failed";
078: log.fatal(msg);
079: throw new ServletException(msg);
080: }
081: ServiceBusManager serviceBusManager = ServiceBusManager
082: .getInstance();
083: try {
084: serviceBusManager.startListeners();
085: } catch (Exception e) {
086: String msg = "WSO2 ESB failed to start ";
087: log.fatal(msg, e);
088: throw new ServletException(msg, e);
089: }
090: ConfigurationContext configctx = serviceBusManager
091: .getConfigurationContext();
092: Map fileContents = (Map) configctx
093: .getProperty(org.wso2.esb.ServiceBusConstants.GENERATED_PAGES);
094: if (fileContents != null) {
095: AdminUIServletFilter adminUIServletFilter = (AdminUIServletFilter) servletContext
096: .getAttribute(AdminUIServletFilter.class.getName());
097: if (adminUIServletFilter != null) {
098: adminUIServletFilter.init(fileContents, true,
099: serviceBusManager.getHttpsPort(),
100: serviceBusManager.getHttpPort(), configctx
101: .getServiceContextPath());
102: }
103: }
104: servletContext.setAttribute(HAS_ALREADY_INIT, TRUE);
105: }
106:
107: protected void doGet(HttpServletRequest request,
108: HttpServletResponse response) throws ServletException,
109: IOException {
110: }
111:
112: protected void doPost(HttpServletRequest request,
113: HttpServletResponse response) throws ServletException,
114: IOException {
115: }
116:
117: public void destroy() {
118: log.info("Shutting down WSO2 ESB ...");
119: try {
120: ServiceBusManager serviceBusManager = ServiceBusManager
121: .getInstance();
122: serviceBusManager.stopListeners();
123: log.info("Shutdown complete");
124: } catch (Exception e) {
125: log.error("Error occurred while Shutting down WSO2 ESB : "
126: + e);
127: }
128: }
129:
130: private String resolveEsbHome(ServletConfig servletConfig) {
131: // If esb.home has provided as init-param,the it will take as esb home
132: String esbHomeAsParam = servletConfig
133: .getInitParameter(ServiceBusConstants.ESB_HOME);
134: if (esbHomeAsParam != null) {
135: if (esbHomeAsParam.endsWith("/")) {
136: return esbHomeAsParam.substring(0, esbHomeAsParam
137: .lastIndexOf("/"));
138: }
139: }
140: //if esb.home has set as a system property , then use it
141: String esbHome = System
142: .getProperty(ServiceBusConstants.ESB_HOME);
143: //Setting the all required system properties
144: if (esbHome == null || "".equals(esbHome)) {
145: ServletContext servletContext = servletConfig
146: .getServletContext();
147: //if esb.home stil can not find ,then resolve it using real path of the WEB-INF
148: String webinfPath = servletContext.getRealPath("WEB-INF");
149: if (webinfPath != null) {
150: esbHome = webinfPath.substring(0, webinfPath
151: .lastIndexOf("webapp/WEB-INF"));
152: if (esbHome != null) {
153: if (esbHome.endsWith("/")) {
154: esbHome = esbHome.substring(0, esbHome
155: .lastIndexOf("/"));
156: }
157: }
158: }
159: }
160: return esbHome;
161: }
162: }
|