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 samples.util;
020:
021: import org.apache.axis2.context.ConfigurationContext;
022: import org.apache.axis2.context.ConfigurationContextFactory;
023: import org.apache.axis2.description.TransportInDescription;
024: import org.apache.axis2.engine.ListenerManager;
025: import org.apache.axis2.util.CommandLineOption;
026: import org.apache.axis2.util.CommandLineOptionParser;
027: import org.apache.axis2.util.OptionsValidator;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import java.io.File;
032: import java.util.List;
033: import java.util.Map;
034:
035: public class SampleAxis2ServerManager {
036:
037: private static final Log log = LogFactory
038: .getLog(SampleAxis2ServerManager.class);
039:
040: private static SampleAxis2ServerManager ourInstance = new SampleAxis2ServerManager();
041:
042: public static int DEFAULT_PORT = 9000;
043:
044: private ConfigurationContext configctx;
045:
046: private ListenerManager listenerManager;
047:
048: public static SampleAxis2ServerManager getInstance() {
049: return ourInstance;
050: }
051:
052: private SampleAxis2ServerManager() {
053: }
054:
055: public void start(String[] args) throws Exception {
056: String repoLocation = null;
057: String confLocation = null;
058:
059: CommandLineOptionParser optionsParser = new CommandLineOptionParser(
060: args);
061: List invalidOptionsList = optionsParser
062: .getInvalidOptions(new OptionsValidator() {
063: public boolean isInvalid(CommandLineOption option) {
064: String optionType = option.getOptionType();
065: return !("repo".equalsIgnoreCase(optionType) || "conf"
066: .equalsIgnoreCase(optionType));
067: }
068: });
069:
070: if ((invalidOptionsList.size() > 0) || (args.length > 4)) {
071: printUsage();
072: }
073:
074: Map optionsMap = optionsParser.getAllOptions();
075:
076: CommandLineOption repoOption = (CommandLineOption) optionsMap
077: .get("repo");
078: CommandLineOption confOption = (CommandLineOption) optionsMap
079: .get("conf");
080:
081: log.info("[SimpleAxisServer] Starting");
082: if (repoOption != null) {
083: repoLocation = repoOption.getOptionValue();
084: System.out
085: .println("[SimpleAxisServer] Using the Axis2 Repository : "
086: + new File(repoLocation).getAbsolutePath());
087: }
088: if (confOption != null) {
089: confLocation = confOption.getOptionValue();
090: System.out
091: .println("[SimpleAxisServer] Using the Axis2 Configuration File : "
092: + new File(confLocation).getAbsolutePath());
093: }
094: try {
095: configctx = ConfigurationContextFactory
096: .createConfigurationContextFromFileSystem(
097: repoLocation, confLocation);
098:
099: configurePort(configctx);
100:
101: listenerManager = new ListenerManager();
102: listenerManager.init(configctx);
103: listenerManager.start();
104: log.info("[SimpleAxisServer] Started");
105: } catch (Throwable t) {
106: log
107: .fatal(
108: "[SimpleAxisServer] Shutting down. Error starting SimpleAxisServer",
109: t);
110: System.exit(1); // must stop application
111: }
112: }
113:
114: public void stop() throws Exception {
115: try {
116: if (listenerManager != null) {
117: listenerManager.stop();
118: listenerManager.destroy();
119: }
120: //we need to call this method to clean the team fils we created.
121: if (configctx != null) {
122: configctx.terminate();
123: }
124: } catch (Exception ignored) {
125: }
126: }
127:
128: private void configurePort(ConfigurationContext configCtx) {
129:
130: TransportInDescription trsIn = (TransportInDescription) configCtx
131: .getAxisConfiguration().getTransportsIn().get("http");
132:
133: if (trsIn != null) {
134: String port = System.getProperty("http_port");
135: if (port != null) {
136: try {
137: new Integer(port);
138: trsIn.getParameter("port").setValue(port);
139: } catch (NumberFormatException e) {
140: log
141: .error("Given port is not a valid integer. Using 9000 for port.");
142: trsIn.getParameter("port").setValue("9000");
143: }
144: } else {
145: trsIn.getParameter("port").setValue("9000");
146: }
147: }
148:
149: TransportInDescription httpsTrsIn = (TransportInDescription) configCtx
150: .getAxisConfiguration().getTransportsIn().get("https");
151:
152: if (httpsTrsIn != null) {
153: String port = System.getProperty("https_port");
154: if (port != null) {
155: try {
156: new Integer(port);
157: httpsTrsIn.getParameter("port").setValue(port);
158: } catch (NumberFormatException e) {
159: log
160: .error("Given port is not a valid integer. Using 9000 for port.");
161: httpsTrsIn.getParameter("port").setValue("9002");
162: }
163: } else {
164: httpsTrsIn.getParameter("port").setValue("9002");
165: }
166: }
167: }
168:
169: public static void printUsage() {
170: System.out
171: .println("Usage: SampleAxisServer -repo <repository> -conf <axis2 configuration file>");
172: System.out.println();
173: System.exit(1);
174: }
175: }
|