Repository: tomee Updated Branches: refs/heads/master 01ba71b2e -> c3a5f3965
TOMEE-1746 TOMEE-1745 TOMEE-1744 tomeexml adn serverxml Main options for tomee embedded, allowing to configure tomee embedded connector through properties and ensuring it uses tomcat default connector type (nio) Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/c3a5f396 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/c3a5f396 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/c3a5f396 Branch: refs/heads/master Commit: c3a5f396502029342909568305dbbe3ab81379c9 Parents: 01ba71b Author: Romain manni-Bucau <[email protected]> Authored: Sun Mar 20 15:15:15 2016 +0100 Committer: Romain manni-Bucau <[email protected]> Committed: Sun Mar 20 15:15:15 2016 +0100 ---------------------------------------------------------------------- .../org/apache/tomee/embedded/Container.java | 66 +++++++++++++++----- .../java/org/apache/tomee/embedded/Main.java | 22 +++++++ .../tomee/embedded/ConnectorConfigTest.java | 58 +++++++++++++++++ 3 files changed, 130 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/c3a5f396/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java ---------------------------------------------------------------------- diff --git a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java index 0f09858..c650a7c 100644 --- a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java +++ b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java @@ -5,14 +5,14 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package org.apache.tomee.embedded; @@ -30,7 +30,6 @@ import org.apache.catalina.startup.Catalina; import org.apache.catalina.startup.CatalinaProperties; import org.apache.catalina.startup.Tomcat; import org.apache.commons.lang3.text.StrSubstitutor; -import org.apache.coyote.http11.Http11Protocol; import org.apache.openejb.AppContext; import org.apache.openejb.BeanContext; import org.apache.openejb.Injector; @@ -83,8 +82,11 @@ import org.apache.velocity.runtime.log.NullLogChute; import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; import org.apache.xbean.finder.UrlSet; import org.apache.xbean.finder.filter.Filters; +import org.apache.xbean.recipe.ObjectRecipe; import org.codehaus.swizzle.stream.ReplaceStringsInputStream; +import javax.naming.Context; +import javax.naming.NamingException; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; @@ -102,8 +104,6 @@ import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.concurrent.CountDownLatch; -import javax.naming.Context; -import javax.naming.NamingException; import static java.util.Arrays.asList; @@ -479,16 +479,19 @@ public class Container implements AutoCloseable { } if (tomcat.getRawConnector() == null && !configuration.isSkipHttp()) { - final Connector connector = new Connector(Http11Protocol.class.getName()); + final Connector connector = createConnector(); connector.setPort(configuration.getHttpPort()); - connector.setAttribute("connectionTimeout", "3000"); + if (connector.getAttribute("connectionTimeout") == null) { + connector.setAttribute("connectionTimeout", "3000"); + } + tomcat.getService().addConnector(connector); tomcat.setConnector(connector); } // create https connector if (configuration.isSsl()) { - final Connector httpsConnector = new Connector(Http11Protocol.class.getName()); + final Connector httpsConnector = createConnector(); httpsConnector.setPort(configuration.getHttpsPort()); httpsConnector.setSecure(true); httpsConnector.setProperty("SSLEnabled", "true"); @@ -501,8 +504,12 @@ public class Container implements AutoCloseable { httpsConnector.setAttribute("keystorePass", configuration.getKeystorePass()); } httpsConnector.setAttribute("keystoreType", configuration.getKeystoreType()); - httpsConnector.setAttribute("clientAuth", configuration.getClientAuth()); - httpsConnector.setAttribute("keyAlias", configuration.getKeyAlias()); + if (configuration.getClientAuth() != null) { + httpsConnector.setAttribute("clientAuth", configuration.getClientAuth()); + } + if (configuration.getKeyAlias() != null) { + httpsConnector.setAttribute("keyAlias", configuration.getKeyAlias()); + } tomcat.getService().addConnector(httpsConnector); @@ -599,6 +606,33 @@ public class Container implements AutoCloseable { } } + protected Connector createConnector() { + final Connector connector; + final Properties properties = configuration.getProperties(); + if (properties != null) { + final Map<String, String> attributes = new HashMap<>(); + final ObjectRecipe recipe = new ObjectRecipe(Connector.class); + for (final String key : properties.stringPropertyNames()) { + if (!key.startsWith("connector.")) { + continue; + } + final String substring = key.substring("connector.".length()); + if (!substring.startsWith("attributes.")) { + recipe.setProperty(substring, properties.getProperty(key)); + } else { + attributes.put(substring.substring("attributes.".length()), properties.getProperty(key)); + } + } + connector = recipe.getProperties().isEmpty() ? new Connector() : Connector.class.cast(recipe.create()); + for (final Map.Entry<String, String> attr : attributes.entrySet()) { + connector.setAttribute(attr.getKey(), attr.getValue()); + } + } else { + connector = new Connector(); + } + return connector; + } + private static Server createServer(final String serverXml) { final Catalina catalina = new Catalina() { // skip few init we don't need *here* http://git-wip-us.apache.org/repos/asf/tomee/blob/c3a5f396/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Main.java ---------------------------------------------------------------------- diff --git a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Main.java b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Main.java index 44306ff..2eb7ea9 100644 --- a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Main.java +++ b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Main.java @@ -38,6 +38,9 @@ public class Main { public static final String DOC_BASE = "doc-base"; public static final String AS_WAR = "as-war"; public static final String RENAMING = "renaming"; + public static final String SERVER_XML = "serverxml"; + public static final String TOMEE_XML = "tomeexml"; + public static final String PROPERTY = "property"; public static void main(final String[] args) { final CommandLineParser parser = new PosixParser(); @@ -121,6 +124,9 @@ public class Main { options.addOption("c", AS_WAR, false, "deploy classpath as war"); options.addOption("b", DOC_BASE, true, "when deploy classpath as war, the doc base"); options.addOption(null, RENAMING, true, "for fat war only, is renaming of the context supported"); + options.addOption(null, SERVER_XML, true, "the server.xml path"); + options.addOption(null, TOMEE_XML, true, "the tomee.xml path"); + options.addOption(null, PROPERTY, true, "some container properties"); return options; } @@ -130,6 +136,22 @@ public class Main { config.setHttpPort(Integer.parseInt(args.getOptionValue(PORT, "8080"))); config.setStopPort(Integer.parseInt(args.getOptionValue(SHUTDOWN, "8005"))); config.setDir(args.getOptionValue(DIRECTORY, new File(new File("."), "apache-tomee").getAbsolutePath())); + if (args.hasOption(SERVER_XML)) { + config.setServerXml(args.getOptionValue(SERVER_XML)); + } + if (args.hasOption(TOMEE_XML)) { + config.property("openejb.conf.file", args.getOptionValue(TOMEE_XML)); + } + if (args.hasOption(PROPERTY)) { + for (final String opt : args.getOptionValues(PROPERTY)) { + final int sep = opt.indexOf('='); + if (sep > 0) { + config.property(opt.substring(0, sep), opt.substring(sep + 1)); + } else { + config.property(opt, "true"); + } + } + } return config; } http://git-wip-us.apache.org/repos/asf/tomee/blob/c3a5f396/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/ConnectorConfigTest.java ---------------------------------------------------------------------- diff --git a/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/ConnectorConfigTest.java b/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/ConnectorConfigTest.java new file mode 100644 index 0000000..6b2dfb9 --- /dev/null +++ b/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/ConnectorConfigTest.java @@ -0,0 +1,58 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tomee.embedded; + +import org.apache.catalina.connector.Connector; +import org.apache.coyote.http11.AbstractHttp11Protocol; +import org.junit.Test; + +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class ConnectorConfigTest { + @Test + public void createConnector() { + final AtomicReference<Connector> connector = new AtomicReference<>(); + try { + new Container(new Configuration() + .property("connector.xpoweredBy", "true") + .property("connector.attributes.compression", "true") + .property("connector.attributes.maxHeaderCount", "2016")) { + + @Override + protected Connector createConnector() { + final Connector connector1 = super.createConnector(); + connector.set(connector1); + throw new RuntimeException("end"); + } + }; + fail("we throw an exception to prevent startup"); + } catch (final Exception re) { + assertEquals(re.getMessage(), "java.lang.RuntimeException: end", re.getMessage()); + + final Connector c = connector.get(); + assertNotNull(c); + assertTrue(c.getXpoweredBy()); + assertEquals(2016, AbstractHttp11Protocol.class.cast(c.getProtocolHandler()).getMaxHeaderCount()); + assertEquals("true", AbstractHttp11Protocol.class.cast(c.getProtocolHandler()).getCompression()); + } + } +}
