use of org.apache.tomee.util.QuickServerXmlParser in project tomee by apache.
the class AbstractTomEEMojo method alignConfigOnServerXmlCurrentConfig.
private void alignConfigOnServerXmlCurrentConfig() {
final File sXml = new File(catalinaBase, "conf/server.xml");
if (sXml.isFile()) {
final QuickServerXmlParser quickServerXmlParser = QuickServerXmlParser.parse(sXml, false);
tomeeHttpPort = quickServerXmlParser.value("HTTP", null);
tomeeHttpsPort = quickServerXmlParser.value("HTTPS", null);
tomeeAjpPort = quickServerXmlParser.value("AJP", null);
tomeeShutdownPort = quickServerXmlParser.value("STOP", null);
final String host = quickServerXmlParser.value("host", null);
if (host != null) {
tomeeHost = host;
}
final String appBase = quickServerXmlParser.value("app-base", null);
if (appBase != null) {
webappDir = appBase;
}
}
if (webappDir == null) {
webappDir = "webapps";
}
}
use of org.apache.tomee.util.QuickServerXmlParser in project tomee by apache.
the class ExecRunner method main.
public static void main(final String[] rawArgs) throws Exception {
final String[] args;
if (rawArgs == null || rawArgs.length == 0) {
args = new String[] { "run" };
} else {
args = rawArgs;
}
final Properties config = new Properties();
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
final InputStream is = contextClassLoader.getResourceAsStream("configuration.properties");
if (is != null) {
config.load(new InputStreamReader(is, "UTF-8"));
is.close();
} else {
throw new IllegalArgumentException("Config not found");
}
final String distrib = config.getProperty("distribution");
final String workingDir = config.getProperty("workingDir");
final InputStream distribIs = contextClassLoader.getResourceAsStream(distrib);
final File distribOutput = new File(workingDir);
final File timestampFile = new File(distribOutput, "timestamp.txt");
final boolean forceDelete = Boolean.getBoolean("tomee.runner.force-delete");
if (forceDelete || !timestampFile.exists() || Long.parseLong(IO.slurp(timestampFile).replace(System.getProperty("line.separator"), "")) < Long.parseLong(config.getProperty("timestamp"))) {
if (forceDelete || timestampFile.exists()) {
System.out.println("Deleting " + distribOutput.getAbsolutePath());
Files.delete(distribOutput);
}
System.out.println("Extracting tomee to " + distribOutput.getAbsolutePath());
Zips.unzip(distribIs, distribOutput, false);
IO.writeString(timestampFile, config.getProperty("timestamp", Long.toString(System.currentTimeMillis())));
}
final File[] scripts = new File(distribOutput, "bin").listFiles();
if (scripts != null) {
// dont use filefilter to avoid dependency issue
for (final File f : scripts) {
setExecutable(f);
}
}
String cmd = config.getProperty("command");
if (cmd.endsWith(SH_BAT_AUTO)) {
final int lastSlash = cmd.lastIndexOf('/');
if (lastSlash > 0) {
final String dir = cmd.substring(0, lastSlash);
final boolean isWin = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("win");
final String script = cmd.substring(lastSlash + 1, cmd.length() - SH_BAT_AUTO.length()).replace('/', isWin ? '\\' : '/') + (isWin ? ".bat" : ".sh");
final File scriptFile = new File(distribOutput, dir + File.separator + script);
if (!scriptFile.exists()) {
throw new IllegalArgumentException("Can't find " + cmd);
}
cmd = scriptFile.getAbsolutePath();
// in case it is not in bin/
setExecutable(scriptFile);
}
}
final String additionalArgs = System.getProperty("additionalSystemProperties");
// build also post here to avoid surprises
final Map<?, ?> map = new HashMap<>();
final Collection<Runnable> preTasks = buildRunnables(config.getProperty("preTasks"), map);
final Collection<Runnable> postTasks = buildRunnables(config.getProperty("postTasks"), map);
final boolean doWait = Boolean.parseBoolean(config.getProperty("waitFor"));
if (!doWait && !postTasks.isEmpty()) {
throw new IllegalArgumentException("You can't use post task if you dont wait for the process.");
}
for (final Runnable r : preTasks) {
r.run();
}
try {
final Collection<String> params = new ArrayList<>();
if ("java".equals(cmd)) {
final File base = findBase(distribOutput);
final QuickServerXmlParser parser = QuickServerXmlParser.parse(new File(base, "conf/server.xml"));
System.setProperty("openejb.home", base.getAbsolutePath());
System.setProperty("server.shutdown.port", parser.stop());
System.setProperty("server.shutdown.command", config.getProperty("shutdownCommand"));
final RemoteServer server = new RemoteServer();
server.setPortStartup(Integer.parseInt(parser.http()));
if (config.containsKey("additionalClasspath")) {
server.setAdditionalClasspath(config.getProperty("additionalClasspath"));
}
final List<String> jvmArgs = new LinkedList<String>();
if (additionalArgs != null) {
Collections.addAll(jvmArgs, additionalArgs.split(" "));
}
for (final String k : config.stringPropertyNames()) {
if (k.startsWith("jvmArg.")) {
jvmArgs.add(config.getProperty(k));
}
}
final String userProps = String.class.cast(map.get("jvmArgs"));
if (userProps != null) {
Collections.addAll(jvmArgs, userProps.split(" "));
}
if ("run".equals(args[0])) {
args[0] = "start";
}
try {
server.start(jvmArgs, args[0], true);
} catch (final Exception e) {
server.destroy();
throw e;
}
if (doWait) {
server.getServer().waitFor();
}
} else {
// TODO: split cmd correctly to support multiple inlined segments in cmd
if (cmd.endsWith(".bat") && !cmd.startsWith("cmd.exe")) {
params.add("cmd.exe");
params.add("/c");
}
// else suppose the user knows what he does
params.add(cmd);
params.addAll(asList(args));
final ProcessBuilder builder = new ProcessBuilder(params.toArray(new String[params.size()])).inheritIO().directory(findBase(distribOutput));
final String existingOpts = System.getenv("CATALINA_OPTS");
final String catalinaOpts = config.getProperty("catalinaOpts");
if (catalinaOpts != null || existingOpts != null || additionalArgs != null) {
// inherit from existing env
builder.environment().put("CATALINA_OPTS", identityOrEmpty(catalinaOpts) + " " + identityOrEmpty(existingOpts) + " " + identityOrEmpty(additionalArgs) + " " + identityOrEmpty(String.class.cast(map.get("jvmArgs"))));
}
if (doWait) {
builder.start().waitFor();
}
}
System.out.flush();
System.err.flush();
System.out.println("Exited Successfully!");
} finally {
for (final Runnable r : postTasks) {
r.run();
}
}
}
use of org.apache.tomee.util.QuickServerXmlParser in project tomee by apache.
the class Container method start.
public void start() throws Exception {
if (base == null || !base.exists()) {
setup(configuration);
}
final Properties props = configuration.getProperties();
if (props != null) {
StrSubstitutor substitutor = null;
for (final String s : props.stringPropertyNames()) {
final String v = props.getProperty(s);
if (v != null && v.contains("${")) {
if (substitutor == null) {
final Map<String, String> placeHolders = new HashMap<>();
placeHolders.put("tomee.embedded.http", Integer.toString(configuration.getHttpPort()));
placeHolders.put("tomee.embedded.https", Integer.toString(configuration.getHttpsPort()));
placeHolders.put("tomee.embedded.stop", Integer.toString(configuration.getStopPort()));
substitutor = new StrSubstitutor(placeHolders);
}
props.put(s, substitutor.replace(v));
}
}
// inherit from system props
final Properties properties = new Properties(System.getProperties());
properties.putAll(configuration.getProperties());
Logger.configure(properties);
} else {
Logger.configure();
}
final File conf = new File(base, "conf");
final File webapps = new File(base, "webapps");
final String catalinaBase = base.getAbsolutePath();
// set the env before calling anoything on tomcat or Catalina!!
// TODO: save previous value and restore in stop
System.setProperty("catalina.base", catalinaBase);
System.setProperty("openejb.deployments.classpath", "false");
System.setProperty("catalina.home", catalinaBase);
System.setProperty("catalina.base", catalinaBase);
System.setProperty("openejb.home", catalinaBase);
System.setProperty("openejb.base", catalinaBase);
System.setProperty("openejb.servicemanager.enabled", "false");
copyFileTo(conf, "catalina.policy");
copyTemplateTo(conf, "catalina.properties");
copyFileTo(conf, "context.xml");
copyFileTo(conf, "openejb.xml");
copyFileTo(conf, "tomcat-users.xml");
copyFileTo(conf, "web.xml");
final boolean initialized;
if (configuration.hasServerXml()) {
final File file = new File(conf, "server.xml");
if (!file.equals(configuration.getServerXmlFile())) {
final FileOutputStream fos = new FileOutputStream(file);
try {
IO.copy(configuration.getServerXmlFile(), fos);
} finally {
IO.close(fos);
}
}
// respect config (host/port) of the Configuration
final QuickServerXmlParser ports = QuickServerXmlParser.parse(file);
if (configuration.isKeepServerXmlAsThis()) {
// force ports to be able to stop the server and get @ArquillianResource
configuration.setHttpPort(Integer.parseInt(ports.http()));
configuration.setStopPort(Integer.parseInt(ports.stop()));
} else {
final Map<String, String> replacements = new HashMap<String, String>();
replacements.put(ports.http(), String.valueOf(configuration.getHttpPort()));
replacements.put(ports.https(), String.valueOf(configuration.getHttpsPort()));
replacements.put(ports.stop(), String.valueOf(configuration.getStopPort()));
IO.copy(IO.slurp(new ReplaceStringsInputStream(IO.read(file), replacements)).getBytes(), file);
}
tomcat.server(createServer(file.getAbsolutePath()));
initialized = true;
} else {
copyFileTo(conf, "server.xml");
initialized = false;
}
if (props != null && !props.isEmpty()) {
final File file = new File(conf, "system.properties");
if (file.isFile()) {
final Properties existing = IO.readProperties(file);
for (final String key : existing.stringPropertyNames()) {
if (!props.containsKey(key)) {
props.put(key, existing.getProperty(key));
}
}
}
final FileWriter systemProperties = new FileWriter(file);
try {
props.store(systemProperties, "");
} finally {
IO.close(systemProperties);
}
}
// Need to use JULI so log messages from the tests are visible
// using openejb logging conf in embedded mode
/* if we use our config (Logger.configure()) don't override it
copyFileTo(conf, "logging.properties");
System.setProperty("java.util.logging.manager", "org.apache.juli.ClassLoaderLogManager");
final File logging = new File(conf, "logging.properties");
if (logging.exists()) {
System.setProperty("java.util.logging.config.file", logging.getAbsolutePath());
}
*/
// Trigger loading of catalina.properties
CatalinaProperties.getProperty("foo");
tomcat.setBaseDir(base.getAbsolutePath());
tomcat.setHostname(configuration.getHost());
if (!initialized) {
tomcat.getHost().setAppBase(webapps.getAbsolutePath());
tomcat.getEngine().setDefaultHost(configuration.getHost());
tomcat.setHostname(configuration.getHost());
}
if (configuration.getRealm() != null) {
tomcat.getEngine().setRealm(configuration.getRealm());
}
if (tomcat.getRawConnector() == null && !configuration.isSkipHttp()) {
final Connector connector = createConnector();
connector.setPort(configuration.getHttpPort());
if (connector.getAttribute("connectionTimeout") == null) {
connector.setAttribute("connectionTimeout", "3000");
}
if (configuration.isHttp2()) {
// would likely need SSLHostConfig programmatically
connector.addUpgradeProtocol(new Http2Protocol());
}
tomcat.getService().addConnector(connector);
tomcat.setConnector(connector);
}
// create https connector
if (configuration.isSsl()) {
final Connector httpsConnector = createConnector();
httpsConnector.setPort(configuration.getHttpsPort());
httpsConnector.setSecure(true);
httpsConnector.setProperty("SSLEnabled", "true");
httpsConnector.setProperty("sslProtocol", configuration.getSslProtocol());
if (configuration.getKeystoreFile() != null) {
httpsConnector.setAttribute("", configuration.getKeystoreFile());
}
if (configuration.getKeystorePass() != null) {
httpsConnector.setAttribute("keystorePass", configuration.getKeystorePass());
}
httpsConnector.setAttribute("keystoreType", configuration.getKeystoreType());
if (configuration.getClientAuth() != null) {
httpsConnector.setAttribute("clientAuth", configuration.getClientAuth());
}
if (configuration.getKeyAlias() != null) {
httpsConnector.setAttribute("keyAlias", configuration.getKeyAlias());
}
if (configuration.isHttp2()) {
// would likely need SSLHostConfig programmatically
httpsConnector.addUpgradeProtocol(new Http2Protocol());
}
tomcat.getService().addConnector(httpsConnector);
if (configuration.isSkipHttp()) {
tomcat.setConnector(httpsConnector);
}
}
for (final Connector c : configuration.getConnectors()) {
tomcat.getService().addConnector(c);
}
if (!configuration.isSkipHttp() && !configuration.isSsl() && !configuration.getConnectors().isEmpty()) {
tomcat.setConnector(configuration.getConnectors().iterator().next());
}
// Bootstrap Tomcat
// create it after Logger is configured
Logger.getInstance(LogCategory.OPENEJB_STARTUP, Container.class).info("Starting TomEE from: " + base.getAbsolutePath());
if (configuration.getUsers() != null) {
for (final Map.Entry<String, String> user : configuration.getUsers().entrySet()) {
tomcat.addUser(user.getKey(), user.getValue());
}
}
if (configuration.getRoles() != null) {
for (final Map.Entry<String, String> user : configuration.getRoles().entrySet()) {
for (final String role : user.getValue().split(" *, *")) {
tomcat.addRole(user.getKey(), role);
}
}
}
if (!initialized) {
tomcat.init();
}
tomcat.start();
// Bootstrap OpenEJB
final Properties properties = new Properties();
properties.setProperty("openejb.deployments.classpath", "false");
properties.setProperty("openejb.loader", "tomcat-system");
properties.setProperty("openejb.home", catalinaBase);
properties.setProperty("openejb.base", catalinaBase);
properties.setProperty("openejb.servicemanager.enabled", "false");
if (configuration.getProperties() != null) {
properties.putAll(configuration.getProperties());
}
if (properties.getProperty("openejb.system.apps") == null) {
// will make startup faster and it is rarely useful for embedded case
properties.setProperty("openejb.system.apps", "false");
}
if (configuration.isQuickSession()) {
properties.put("openejb.session.manager", QuickSessionManager.class.getName());
}
try {
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
final Properties tomcatServerInfo = IO.readProperties(classLoader.getResourceAsStream("org/apache/catalina/util/ServerInfo.properties"), new Properties());
String serverNumber = tomcatServerInfo.getProperty("server.number");
if (serverNumber == null) {
// Tomcat5 only has server.info
final String serverInfo = tomcatServerInfo.getProperty("server.info");
if (serverInfo != null) {
final int slash = serverInfo.indexOf('/');
serverNumber = serverInfo.substring(slash + 1);
}
}
if (serverNumber != null) {
System.setProperty("tomcat.version", serverNumber);
}
final String serverBuilt = tomcatServerInfo.getProperty("server.built");
if (serverBuilt != null) {
System.setProperty("tomcat.built", serverBuilt);
}
} catch (final Throwable e) {
// no-op
}
final TomcatLoader loader = new TomcatLoader();
loader.initDefaults(properties);
// need to add properties after having initialized defaults
// to properties passed to SystemInstance otherwise we loose some of them
final Properties initProps = new Properties();
initProps.putAll(System.getProperties());
initProps.putAll(properties);
if (SystemInstance.isInitialized()) {
SystemInstance.get().getProperties().putAll(initProps);
} else {
SystemInstance.init(initProps);
}
SystemInstance.get().setComponent(StandardServer.class, (StandardServer) tomcat.getServer());
// needed again cause of init()
SystemInstance.get().setComponent(Server.class, tomcat.getServer());
loader.initialize(properties);
assembler = SystemInstance.get().getComponent(Assembler.class);
configurationFactory = new ConfigurationFactory();
if (configuration.isWithEjbRemote()) {
tomcat.getHost().addChild(new TomEERemoteWebapp());
}
}
use of org.apache.tomee.util.QuickServerXmlParser in project tomee by apache.
the class Setup method updateServerXml.
public static void updateServerXml(final File tomeeHome, final TomEEConfiguration configuration) throws IOException {
final File serverXml = Files.path(new File(tomeeHome.getAbsolutePath()), "conf", "server.xml");
if (!serverXml.exists()) {
return;
}
final QuickServerXmlParser ports = QuickServerXmlParser.parse(serverXml);
if (configuration.getKeepServerXmlAsThis()) {
// force ports to be able to stop the server and get @ArquillianResource
configuration.setHttpPort(Integer.parseInt(ports.http()));
configuration.setStopPort(Integer.parseInt(ports.stop()));
// in this case we don't want to override the conf
return;
}
final Map<String, String> replacements = new HashMap<>();
replacements.put(ports.http(), String.valueOf(configuration.getHttpPort()));
replacements.put(ports.https(), String.valueOf(configuration.getHttpsPort()));
replacements.put(ports.stop(), String.valueOf(configuration.getStopPort()));
replacements.put(ports.ajp(), String.valueOf(ajpPort(configuration)));
if (configuration.isUnpackWars()) {
replacements.put("unpackWARs=\"false\"", "unpackWARs=\"true\"");
} else {
replacements.put("unpackWARs=\"true\"", "unpackWARs=\"false\"");
}
replace(replacements, serverXml, true);
}
use of org.apache.tomee.util.QuickServerXmlParser in project tomee by apache.
the class AbstractTomEEMojo method overrideServerXml.
private void overrideServerXml() {
final File serverXml = new File(catalinaBase, "conf/server.xml");
if (!serverXml.exists()) {
// openejb
return;
}
final QuickServerXmlParser parser = QuickServerXmlParser.parse(serverXml);
final String original = read(serverXml);
String value = original;
if (tomeeHttpsPort != null && tomeeHttpsPort.length() > 0 && parser.value("HTTPS", null) == null) {
String keystorePath = keystore != null ? keystore : parser.keystore();
if (keystorePath == null) {
final File conf = new File(catalinaBase, "conf");
if (conf.isDirectory()) {
final File[] jks = conf.listFiles(new FilenameFilter() {
@Override
public boolean accept(final File dir, final String name) {
return name.endsWith(".jks");
}
});
if (jks != null && jks.length == 1) {
keystorePath = "${catalina.base}/conf/" + jks[0].getName();
} else {
throw new IllegalArgumentException("Ambiguous jks in conf/,please use <keystore /> to force it.");
}
}
}
if (keystorePath == null) {
throw new IllegalArgumentException("No keystore specified, please use <keystore></keystore>");
}
// ensure connector is not commented
value = value.replace("<Service name=\"Catalina\">", "<Service name=\"Catalina\">\n" + " <Connector port=\"" + tomeeHttpsPort + "\" protocol=\"HTTP/1.1\" SSLEnabled=\"true\"\n" + " scheme=\"https\" secure=\"true\"\n" + " clientAuth=\"false\" sslProtocol=\"TLS\" keystoreFile=\"" + keystorePath + "\" />\n");
}
if (tomeeHttpPort != null) {
value = value.replace("\"" + parser.http() + "\"", "\"" + tomeeHttpPort + "\"");
}
if (tomeeHttpsPort != null) {
value = value.replace("\"" + parser.https() + "\"", "\"" + tomeeHttpsPort + "\"");
}
if (tomeeAjpPort != null) {
value = value.replace("\"" + parser.ajp() + "\"", "\"" + tomeeAjpPort + "\"");
}
if (tomeeShutdownPort != null) {
value = value.replace("\"" + parser.stop() + "\"", "\"" + tomeeShutdownPort + "\"");
}
if (webappDir != null) {
value = value.replace("\"" + parser.value("app-base", "webapps") + "\"", "\"" + webappDir + "\"");
}
if (tomeeHost != null) {
value = value.replace("\"" + parser.host() + "\"", "\"" + tomeeHost + "\"");
}
if (!original.equals(value)) {
FileWriter writer = null;
try {
writer = new FileWriter(serverXml);
writer.write(value);
} catch (final IOException e) {
throw new TomEEException(e.getMessage(), e);
} finally {
close(writer);
}
}
}
Aggregations