TOMEE-1642 patch from soro, logging when a jaxrs application configuration is used
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/dc459341 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/dc459341 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/dc459341 Branch: refs/heads/tomee-7.0.0-M1 Commit: dc4593418bbb350b74cbf271a77e5f16dad74010 Parents: 7b7e630 Author: Romain Manni-Bucau <[email protected]> Authored: Fri Oct 23 08:05:23 2015 +0200 Committer: Romain Manni-Bucau <[email protected]> Committed: Fri Oct 23 08:05:23 2015 +0200 ---------------------------------------------------------------------- .../cxf/rs/logging/LoggingJAXRSCommons.java | 156 +++++++++++++++++++ .../ejbmodule/LoggingJAXRSEJBModuleTest.java | 50 ++++++ ...gJAXRSEJBModuleWithoutConfigurationTest.java | 50 ++++++ ...JAXRSWebAppModuleClassConfigurationTest.java | 55 +++++++ .../LoggingJAXRSWebAppModuleTest.java | 55 +++++++ ...XRSWebAppModuleWithoutConfigurationTest.java | 52 +++++++ .../apache/openejb/server/rest/RESTService.java | 4 + 7 files changed, 422 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/dc459341/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/LoggingJAXRSCommons.java ---------------------------------------------------------------------- diff --git a/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/LoggingJAXRSCommons.java b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/LoggingJAXRSCommons.java new file mode 100644 index 0000000..64bbe77 --- /dev/null +++ b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/LoggingJAXRSCommons.java @@ -0,0 +1,156 @@ +/* + * 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 + * + * 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. + */ +package org.apache.openejb.server.cxf.rs.logging; + +import org.apache.cxf.jaxrs.provider.JAXBElementProvider; +import org.apache.openejb.config.EjbModule; +import org.apache.openejb.config.sys.Resources; +import org.apache.openejb.config.sys.Service; +import org.apache.openejb.jee.EjbJar; +import org.apache.openejb.jee.EnterpriseBean; +import org.apache.openejb.jee.SingletonBean; +import org.apache.openejb.jee.WebApp; +import org.apache.openejb.jee.oejb3.OpenejbJar; +import org.apache.openejb.jee.oejb3.PojoDeployment; +import org.apache.openejb.log.LoggerCreator; +import org.apache.openejb.server.rest.RESTService; +import org.apache.openejb.util.JuliLogStream; +import org.apache.openejb.util.LogCategory; +import org.apache.openejb.util.LogStreamAsync; +import org.apache.openejb.util.Logger; +import org.apache.openejb.util.NetworkUtil; +import org.apache.openejb.util.reflection.Reflections; + +import javax.ejb.Singleton; +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.core.Application; +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.logging.Handler; +import java.util.logging.LogRecord; + +public class LoggingJAXRSCommons { + + protected static int port = -1; + protected Collection<String> msgs; + + protected boolean assertJAXRSConfiguration() { + final Iterator<String> iterator = msgs.iterator(); + while (iterator.hasNext()) { + if (iterator.next().contains("Registered JAX-RS Configuration:")) { + return true; + } + } + return false; + } + + protected java.util.logging.Logger getLooger() throws Exception { + final Logger logger = Logger.getInstance(LogCategory.OPENEJB_RS, RESTService.class); + final LogStreamAsync stream = LogStreamAsync.class.cast(Reflections.get(logger, "logStream")); + final JuliLogStream ls = JuliLogStream.class.cast(Reflections.get(stream, "ls")); + final LoggerCreator julCreator = LoggerCreator.class.cast(Reflections.get(ls, "logger")); + return julCreator.call(); + } + + protected EjbModule getEjbModule(String pojoDeploymentClassName, String ejbModuleId) throws Exception { + final EjbModule module = new EjbModule(new EjbJar(), new OpenejbJar()); + + if (ejbModuleId != null) { + module.setModuleId(ejbModuleId); + } + + final EnterpriseBean bean = new SingletonBean(LogginTestBean.class).localBean(); + module.getEjbJar().addEnterpriseBean(bean); + + final Resources resources = new Resources(); + + final Service feature = new Service("xml", null); + feature.setClassName(JAXBElementProvider.class.getName()); + feature.getProperties().put("eventHandler", "$handler"); + + resources.getService().add(feature); + module.initResources(resources); + + + if (pojoDeploymentClassName != null) { + final PojoDeployment e = new PojoDeployment(); + e.setClassName(pojoDeploymentClassName); + e.getProperties().setProperty("cxf.jaxrs.providers", "xml"); + module.getOpenejbJar().getPojoDeployment().add(e); + } + + return module; + } + + protected EjbModule getEjbModule(String ejbModuleId) throws Exception { + return getEjbModule(null, ejbModuleId); + } + + protected EjbModule getEjbModule() throws Exception { + return getEjbModule(null, null); + } + + protected WebApp getWebApp() throws Exception { + return new WebApp(); + } + + protected static void configurePort() { + port = NetworkUtil.getNextAvailablePort(); + } + + protected void configureLoggin() throws Exception { + msgs = new LinkedList<>(); + final java.util.logging.Logger looger = getLooger(); + looger.addHandler(new Handler() { + @Override + public void publish(final LogRecord record) { + msgs.add(record.getMessage()); + } + + @Override + public void flush() { + // no-op + } + + @Override + public void close() throws SecurityException { + // no-op + } + + public Collection getMsg() { + return msgs; + } + }); + } + + @Singleton + @Path("loggin-test-bean") + public static class LogginTestBean { + + @PUT + public void test() { + + } + } + + @ApplicationPath("/api") + public static class LogginTestApplication extends Application { + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/dc459341/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/ejbmodule/LoggingJAXRSEJBModuleTest.java ---------------------------------------------------------------------- diff --git a/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/ejbmodule/LoggingJAXRSEJBModuleTest.java b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/ejbmodule/LoggingJAXRSEJBModuleTest.java new file mode 100644 index 0000000..3eb6340 --- /dev/null +++ b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/ejbmodule/LoggingJAXRSEJBModuleTest.java @@ -0,0 +1,50 @@ +/* + * 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 + * + * 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. + */ +package org.apache.openejb.server.cxf.rs.logging.ejbmodule; + +import org.apache.openejb.config.EjbModule; +import org.apache.openejb.junit.ApplicationComposer; +import org.apache.openejb.server.cxf.rs.logging.LoggingJAXRSCommons; +import org.apache.openejb.testing.EnableServices; +import org.apache.openejb.testing.Module; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertTrue; + + +@EnableServices("jax-rs") +@RunWith(ApplicationComposer.class) +public class LoggingJAXRSEJBModuleTest extends LoggingJAXRSCommons { + + @BeforeClass + public static void beforeClass() throws Exception { + configurePort(); + } + + @Module + public EjbModule service() throws Exception { + configureLoggin(); + return getEjbModule("jaxrs-application", null); + } + + @Test + public void checkLogger() throws Exception { + assertTrue(assertJAXRSConfiguration()); + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/dc459341/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/ejbmodule/LoggingJAXRSEJBModuleWithoutConfigurationTest.java ---------------------------------------------------------------------- diff --git a/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/ejbmodule/LoggingJAXRSEJBModuleWithoutConfigurationTest.java b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/ejbmodule/LoggingJAXRSEJBModuleWithoutConfigurationTest.java new file mode 100644 index 0000000..859c229 --- /dev/null +++ b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/ejbmodule/LoggingJAXRSEJBModuleWithoutConfigurationTest.java @@ -0,0 +1,50 @@ +/* + * 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 + * + * 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. + */ +package org.apache.openejb.server.cxf.rs.logging.ejbmodule; + +import org.apache.openejb.config.EjbModule; +import org.apache.openejb.junit.ApplicationComposer; +import org.apache.openejb.server.cxf.rs.logging.LoggingJAXRSCommons; +import org.apache.openejb.testing.EnableServices; +import org.apache.openejb.testing.Module; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertFalse; + + +@EnableServices("jax-rs") +@RunWith(ApplicationComposer.class) +public class LoggingJAXRSEJBModuleWithoutConfigurationTest extends LoggingJAXRSCommons { + + @BeforeClass + public static void beforeClass() throws Exception { + configurePort(); + } + + @Module + public EjbModule service() throws Exception { + configureLoggin(); + return getEjbModule(); + } + + @Test + public void checkLogger() throws Exception { + assertFalse(assertJAXRSConfiguration()); + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/dc459341/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/webappmodule/LoggingJAXRSWebAppModuleClassConfigurationTest.java ---------------------------------------------------------------------- diff --git a/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/webappmodule/LoggingJAXRSWebAppModuleClassConfigurationTest.java b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/webappmodule/LoggingJAXRSWebAppModuleClassConfigurationTest.java new file mode 100644 index 0000000..6a3853a --- /dev/null +++ b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/webappmodule/LoggingJAXRSWebAppModuleClassConfigurationTest.java @@ -0,0 +1,55 @@ +/* + * 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 + * + * 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. + */ +package org.apache.openejb.server.cxf.rs.logging.webappmodule; + +import org.apache.openejb.config.AppModule; +import org.apache.openejb.config.WebModule; +import org.apache.openejb.junit.ApplicationComposer; +import org.apache.openejb.server.cxf.rs.logging.LoggingJAXRSCommons; +import org.apache.openejb.testing.EnableServices; +import org.apache.openejb.testing.Module; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertTrue; + + +@EnableServices("jax-rs") +@RunWith(ApplicationComposer.class) +public class LoggingJAXRSWebAppModuleClassConfigurationTest extends LoggingJAXRSCommons { + + @BeforeClass + public static void beforeClass() throws Exception { + configurePort(); + } + + @Module + public AppModule service() throws Exception { + final WebModule war = new WebModule(getWebApp(), "/test", Thread.currentThread().getContextClassLoader(), "", "test"); + war.getRestApplications().add(LogginTestApplication.class.getName()); + final AppModule appModule = new AppModule(getEjbModule(LogginTestApplication.class.getName(), "test"), war); + + configureLoggin(); + return appModule; + } + + @Test + public void checkLogger() throws Exception { + assertTrue(assertJAXRSConfiguration()); + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/dc459341/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/webappmodule/LoggingJAXRSWebAppModuleTest.java ---------------------------------------------------------------------- diff --git a/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/webappmodule/LoggingJAXRSWebAppModuleTest.java b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/webappmodule/LoggingJAXRSWebAppModuleTest.java new file mode 100644 index 0000000..c51c1a1 --- /dev/null +++ b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/webappmodule/LoggingJAXRSWebAppModuleTest.java @@ -0,0 +1,55 @@ +/* + * 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 + * + * 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. + */ +package org.apache.openejb.server.cxf.rs.logging.webappmodule; + +import org.apache.openejb.config.AppModule; +import org.apache.openejb.config.WebModule; +import org.apache.openejb.junit.ApplicationComposer; +import org.apache.openejb.server.cxf.rs.logging.LoggingJAXRSCommons; +import org.apache.openejb.testing.EnableServices; +import org.apache.openejb.testing.Module; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertTrue; + + +@EnableServices("jax-rs") +@RunWith(ApplicationComposer.class) +public class LoggingJAXRSWebAppModuleTest extends LoggingJAXRSCommons { + + @BeforeClass + public static void beforeClass() throws Exception { + configurePort(); + } + + @Module + public AppModule service() throws Exception { + final WebModule war = new WebModule(getWebApp(), "/test", Thread.currentThread().getContextClassLoader(), "", "test"); + war.getRestApplications().add(LogginTestApplication.class.getName()); + final AppModule appModule = new AppModule(getEjbModule("jaxrs-application", "test"), war); + + configureLoggin(); + return appModule; + } + + @Test + public void checkLogger() throws Exception { + assertTrue(assertJAXRSConfiguration()); + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/dc459341/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/webappmodule/LoggingJAXRSWebAppModuleWithoutConfigurationTest.java ---------------------------------------------------------------------- diff --git a/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/webappmodule/LoggingJAXRSWebAppModuleWithoutConfigurationTest.java b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/webappmodule/LoggingJAXRSWebAppModuleWithoutConfigurationTest.java new file mode 100644 index 0000000..319b319 --- /dev/null +++ b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/logging/webappmodule/LoggingJAXRSWebAppModuleWithoutConfigurationTest.java @@ -0,0 +1,52 @@ +/* + * 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 + * + * 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. + */ +package org.apache.openejb.server.cxf.rs.logging.webappmodule; + +import org.apache.openejb.jee.WebApp; +import org.apache.openejb.junit.ApplicationComposer; +import org.apache.openejb.server.cxf.rs.logging.LoggingJAXRSCommons; +import org.apache.openejb.testing.Classes; +import org.apache.openejb.testing.EnableServices; +import org.apache.openejb.testing.Module; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertFalse; + + +@EnableServices("jax-rs") +@RunWith(ApplicationComposer.class) +public class LoggingJAXRSWebAppModuleWithoutConfigurationTest extends LoggingJAXRSCommons { + + @BeforeClass + public static void beforeClass() throws Exception { + configurePort(); + } + + @Module + @Classes(value = {LogginTestApplication.class, LogginTestBean.class}) + public WebApp service() throws Exception { + configureLoggin(); + return getWebApp(); + } + + @Test + public void checkLogger() throws Exception { + assertFalse(assertJAXRSConfiguration()); + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/dc459341/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java ---------------------------------------------------------------------- diff --git a/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java b/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java index df2eadb..fc28001 100644 --- a/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java +++ b/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java @@ -448,6 +448,10 @@ public abstract class RESTService implements ServerService, SelfManaging { configuration = PojoUtil.findConfiguration(pojoConfigurations, "jaxrs-application"); } + if (configuration != null) { + LOGGER.info("Registered JAX-RS Configuration: " + configuration); + } + final String base = getAddress(contextRoot); final String nopath; if (base.endsWith("/") && prefix.startsWith("/")) {
