/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.server;
import org.jboss.as.process.CommandLineConstants;
import org.jboss.logmanager.Level;
import org.jboss.logmanager.Logger;
import org.jboss.logmanager.log4j.BridgeRepositorySelector;
import org.jboss.modules.Module;
import org.jboss.msc.service.ServiceActivator;
import org.jboss.stdio.LoggingOutputStream;
import org.jboss.stdio.NullInputStream;
import org.jboss.stdio.SimpleStdioContextSelector;
import org.jboss.stdio.StdioContext;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
/**
* The main-class entry point for standalone server instances.
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
* @author John Bailey
* @author Brian Stansberry
*/
public final class Main {
private Main() {
}
/**
* The main method.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
SecurityActions.setSystemProperty("log4j.defaultInitOverride", "true");
new BridgeRepositorySelector().start();
// Install JBoss Stdio to avoid any nasty crosstalk.
StdioContext.install();
final StdioContext context = StdioContext.create(
new NullInputStream(),
new LoggingOutputStream(Logger.getLogger("stdout"), Level.INFO),
new LoggingOutputStream(Logger.getLogger("stderr"), Level.ERROR)
);
StdioContext.setStdioContextSelector(new SimpleStdioContextSelector(context));
try {
ServerEnvironment serverEnvironment = determineEnvironment(args, new Properties(SecurityActions.getSystemProperties()), SecurityActions.getSystemEnvironment());
if (serverEnvironment == null) {
abort(null);
} else {
final Bootstrap bootstrap = Bootstrap.Factory.newInstance();
final Bootstrap.Configuration configuration = new Bootstrap.Configuration();
configuration.setServerEnvironment(serverEnvironment);
configuration.setModuleLoader(Module.getBootModuleLoader());
bootstrap.bootstrap(configuration, Collections.<ServiceActivator>emptyList()).get();
return;
}
} catch (Throwable t) {
abort(t);
}
}
private static void abort(Throwable t) {
if (t != null) {
t.printStackTrace(System.err);
}
try {
// Inform the process controller that we are shutting down on purpose
// so it doesn't try to respawn us
// FIXME implement shutdown()
throw new UnsupportedOperationException("implement me");
} finally {
SystemExiter.exit(1);
}
}
public static ServerEnvironment determineEnvironment(String[] args, Properties systemProperties, Map<String, String> systemEnvironment) {
final int argsLength = args.length;
String serverConfig = null;
for (int i = 0; i < argsLength; i++) {
final String arg = args[i];
try {
if (CommandLineConstants.SERVER_CONFIG.equals(arg)) {
serverConfig = args[++i];
} else if (CommandLineConstants.PROPERTIES.equals(arg) || "-P".equals(arg)) {
// Set system properties from url/file
URL url = null;
try {
url = makeURL(args[++i]);
Properties props = System.getProperties();
props.load(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
System.err.printf("Malformed URL provided for option %s\n", arg);
return null;
} catch (IOException e) {
System.err.printf("Unable to load properties from URL %s\n", url);
return null;
}
} else if (arg.startsWith("-D")) {
// set a system property
String name, value;
int idx = arg.indexOf("=");
if (idx == -1) {
name = arg.substring(2);
value = "true";
} else {
name = arg.substring(2, idx);
value = arg.substring(idx + 1, arg.length());
}
systemProperties.setProperty(name, value);
SecurityActions.setSystemProperty(name, value);
} else {
System.err.printf("Invalid option '%s'\n", arg);
return null;
}
} catch (IndexOutOfBoundsException e) {
System.err.printf("Argument expected for option %s\n", arg);
return null;
}
}
return new ServerEnvironment(systemProperties, systemEnvironment, serverConfig, true);
}
private static URL makeURL(String urlspec) throws MalformedURLException {
urlspec = urlspec.trim();
URL url;
try {
url = new URL(urlspec);
if (url.getProtocol().equals("file")) {
// make sure the file is absolute & canonical file url
File file = new File(url.getFile()).getCanonicalFile();
url = file.toURI().toURL();
}
} catch (Exception e) {
// make sure we have a absolute & canonical file url
try {
File file = new File(urlspec).getCanonicalFile();
url = file.toURI().toURL();
} catch (Exception n) {
throw new MalformedURLException(n.toString());
}
}
return url;
}
}