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;
020:
021: import org.apache.axis2.description.TransportInDescription;
022: import org.apache.axis2.transport.TransportListener;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.synapse.transport.base.ManagementSupport;
026: import org.wso2.esb.util.XmlConfigurationFactory;
027: import org.wso2.utils.ServerException;
028:
029: import java.io.File;
030: import java.util.HashMap;
031: import java.util.Iterator;
032:
033: /**
034: *
035: */
036:
037: public class ServiceBus {
038:
039: private static Log log = LogFactory.getLog(ServiceBus.class);
040:
041: //ESB lifecycle commads
042: public static final String COMMAND_START = "START";
043: public static final String COMMAND_RUN = "RUN";
044: public static final String COMMAND_STOP = "STOP";
045: public static final String COMMAND_STATUS = "STATUS";
046: public static final String COMMAND_HELP = "HELP";
047:
048: public ServiceBus() {
049: if (System.getProperty(ServiceBusConstants.ESB_HOME) == null) {
050: System.setProperty(ServiceBusConstants.ESB_HOME, ".");
051: }
052: ServiceBusConfiguration.getInstance();
053: try {
054: String server_web_xml = System
055: .getProperty(ServiceBusConstants.ESB_SERVER_WEB_XML);
056: if (server_web_xml == null || "".equals(server_web_xml)) {
057: server_web_xml = ServiceBusConstants.ESB_CONF_DIRECTORY
058: + File.separator
059: + ServiceBusConstants.ESB_SERVER_WEB_XML;
060: }
061: XmlConfigurationFactory.init(
062: ServiceBusConstants.ESB_WEB_XML_KEY,
063: server_web_xml,
064: ServiceBusConstants.ESB_XML_NAMESPACE);
065: } catch (ServerException e) {
066: log.error("Error loading server-web.xml", e);
067: }
068: }
069:
070: public static void main(String[] args) {
071: ServiceBus esb = new ServiceBus();
072: esb.start();
073: }
074:
075: public void shutdown() throws Exception {
076: log.info("[ESB] Shutting down WSO2 ESB ...");
077: ServiceBusManager manager = ServiceBusManager.getInstance();
078: manager.stop();
079: }
080:
081: public void shutdownGracefully(long millis) throws Exception {
082: log.info("[ESB] Maintenence Shutting down WSO2 ESB...");
083: ServiceBusManager manager = ServiceBusManager.getInstance();
084: HashMap transIns = manager.getConfigurationContext()
085: .getAxisConfiguration().getTransportsIn();
086: for (Iterator iter = transIns.values().iterator(); iter
087: .hasNext();) {
088: TransportInDescription tinDesc = (TransportInDescription) iter
089: .next();
090: TransportListener listener = tinDesc.getReceiver();
091: if (listener instanceof ManagementSupport) {
092: ((ManagementSupport) listener)
093: .maintenenceShutdown(millis);
094: }
095: }
096: try {
097: Thread.sleep(millis);
098: } catch (InterruptedException ignored) {
099:
100: }
101: manager.stop();
102: }
103:
104: public void startMaintenance() throws Exception {
105: log.info("Starting to switch to maintenance mode...");
106: ServiceBusManager manager = ServiceBusManager.getInstance();
107: HashMap transIns = manager.getConfigurationContext()
108: .getAxisConfiguration().getTransportsIn();
109: for (Iterator iter = transIns.values().iterator(); iter
110: .hasNext();) {
111: TransportInDescription tinDesc = (TransportInDescription) iter
112: .next();
113: TransportListener listener = tinDesc.getReceiver();
114: if (listener instanceof ManagementSupport) {
115: ((ManagementSupport) listener).pause();
116: }
117: }
118: log.info("Switched to maintenance mode");
119: }
120:
121: public void endMaintenance() throws Exception {
122: log.info("Switching to normal mode...");
123: ServiceBusManager manager = ServiceBusManager.getInstance();
124: HashMap transIns = manager.getConfigurationContext()
125: .getAxisConfiguration().getTransportsIn();
126: for (Iterator iter = transIns.values().iterator(); iter
127: .hasNext();) {
128: TransportInDescription tinDesc = (TransportInDescription) iter
129: .next();
130: TransportListener listener = tinDesc.getReceiver();
131: if (listener instanceof ManagementSupport) {
132: ((ManagementSupport) listener).resume();
133: }
134: }
135: log.info("Switched to normal mode");
136: }
137:
138: public void start() {
139:
140: log.info("[ESB] Starting WSO2 ESB ...");
141: long before = System.currentTimeMillis();
142: try {
143: ServiceBusManager manager = ServiceBusManager.getInstance();
144: manager.init();
145: manager.start();
146: } catch (ServiceBusException e) {
147: log.fatal(
148: "Error starting the WSO2 ESB : " + e.getMessage(),
149: e);
150: System.exit(1); // must stop application
151: }
152: log.info("[ESB] WSO2 ESB started in "
153: + (System.currentTimeMillis() - before) + " ms");
154: }
155:
156: }
|