From 54e7c58d5449d5f91879674598fe1b1ee339a272 Mon Sep 17 00:00:00 2001 From: Daniel Kulp Date: Fri, 10 Mar 2017 16:22:12 -0500 Subject: [PATCH] Update handling of hte @Logging annotation to use the new logging capabilities if available --- .../bus/extension/ExtensionManagerBus.java | 36 +----- .../AnnotationsFactoryBeanListener.java | 11 -- .../factory/FactoryBeanListenerManager.java | 3 +- .../OldLoggingFactoryBeanListener.java | 77 +++++++++++++ .../logging/LoggingBusLifecycleListener.java | 83 ++++++++++++++ .../logging/LoggingFactoryBeanListener.java | 106 ++++++++++++++++++ .../cxf/ext/logging/LoggingFeature.java | 28 +++-- .../logging/event/PrintWriterEventSender.java | 4 + .../resources/META-INF/cxf/bus-extensions.txt | 2 + 9 files changed, 293 insertions(+), 57 deletions(-) create mode 100644 core/src/main/java/org/apache/cxf/service/factory/OldLoggingFactoryBeanListener.java create mode 100644 rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingBusLifecycleListener.java create mode 100644 rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingFactoryBeanListener.java create mode 100644 rt/features/logging/src/main/resources/META-INF/cxf/bus-extensions.txt diff --git a/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerBus.java b/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerBus.java index 008a57cb2bc..e2409988d6a 100644 --- a/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerBus.java +++ b/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerBus.java @@ -40,7 +40,6 @@ import org.apache.cxf.configuration.Configurer; import org.apache.cxf.configuration.NullConfigurer; import org.apache.cxf.feature.Feature; -import org.apache.cxf.feature.LoggingFeature; import org.apache.cxf.interceptor.AbstractBasicInterceptorProvider; import org.apache.cxf.resource.DefaultResourceManager; import org.apache.cxf.resource.ObjectTypeResolver; @@ -59,30 +58,7 @@ */ public class ExtensionManagerBus extends AbstractBasicInterceptorProvider implements Bus { public static final String BUS_PROPERTY_NAME = "bus"; - static final boolean FORCE_LOGGING; - static final boolean FORCE_PRETTY; - static { - boolean b = false; - boolean pretty = false; - try { - String prop = System.getProperty("org.apache.cxf.logging.enabled", "false"); - if ("pretty".equals(prop)) { - b = true; - pretty = true; - } else { - b = Boolean.parseBoolean(prop); - //treat these all the same - b |= Boolean.getBoolean("com.sun.xml.ws.transport.local.LocalTransportPipe.dump"); - b |= Boolean.getBoolean("com.sun.xml.ws.util.pipe.StandaloneTubeAssembler.dump"); - b |= Boolean.getBoolean("com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump"); - b |= Boolean.getBoolean("com.sun.xml.ws.transport.http.HttpAdapter.dump"); - } - } catch (Throwable t) { - //ignore - } - FORCE_LOGGING = b; - FORCE_PRETTY = pretty; - } + private static final String BUS_ID_PROPERTY_NAME = "org.apache.cxf.bus.id"; protected final Map, Object> extensions; @@ -109,11 +85,6 @@ public ExtensionManagerBus(Map, Object> extensions, Map state = BusState.INITIAL; CXFBusFactory.possiblySetDefaultBus(this); - if (FORCE_LOGGING) { - LoggingFeature feature = new LoggingFeature(); - feature.setPrettyLogging(FORCE_PRETTY); - features.add(feature); - } if (null != props) { properties.putAll(props); } @@ -354,11 +325,6 @@ public Collection getFeatures() { public synchronized void setFeatures(Collection features) { this.features.clear(); this.features.addAll(features); - if (FORCE_LOGGING) { - LoggingFeature feature = new LoggingFeature(); - feature.setPrettyLogging(FORCE_PRETTY); - this.features.add(feature); - } if (state == BusState.RUNNING) { initializeFeatures(); } diff --git a/core/src/main/java/org/apache/cxf/service/factory/AnnotationsFactoryBeanListener.java b/core/src/main/java/org/apache/cxf/service/factory/AnnotationsFactoryBeanListener.java index cfb45ff2196..90dca4f3ae0 100644 --- a/core/src/main/java/org/apache/cxf/service/factory/AnnotationsFactoryBeanListener.java +++ b/core/src/main/java/org/apache/cxf/service/factory/AnnotationsFactoryBeanListener.java @@ -30,7 +30,6 @@ import org.apache.cxf.annotations.FactoryType; import org.apache.cxf.annotations.FastInfoset; import org.apache.cxf.annotations.GZIP; -import org.apache.cxf.annotations.Logging; import org.apache.cxf.annotations.SchemaValidation; import org.apache.cxf.annotations.WSDLDocumentation; import org.apache.cxf.annotations.WSDLDocumentation.Placement; @@ -39,7 +38,6 @@ import org.apache.cxf.configuration.ConfiguredBeanLocator; import org.apache.cxf.endpoint.Endpoint; import org.apache.cxf.endpoint.Server; -import org.apache.cxf.feature.LoggingFeature; import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.interceptor.FIStaxInInterceptor; import org.apache.cxf.interceptor.FIStaxOutInterceptor; @@ -97,7 +95,6 @@ public void handleEvent(Event ev, AbstractServiceFactoryBean factory, Object... addSchemaValidationSupport(ep, cls.getAnnotation(SchemaValidation.class)); addFastInfosetSupport(ep, cls.getAnnotation(FastInfoset.class)); addGZipSupport(ep, bus, cls.getAnnotation(GZIP.class)); - addLoggingSupport(ep, bus, cls.getAnnotation(Logging.class)); addEndpointProperties(ep, bus, cls.getAnnotation(EndpointProperty.class)); EndpointProperties props = cls.getAnnotation(EndpointProperties.class); if (props != null) { @@ -151,7 +148,6 @@ public void handleEvent(Event ev, AbstractServiceFactoryBean factory, Object... addGZipSupport(server.getEndpoint(), bus, cls.getAnnotation(GZIP.class)); addSchemaValidationSupport(server.getEndpoint(), cls.getAnnotation(SchemaValidation.class)); addFastInfosetSupport(server.getEndpoint(), cls.getAnnotation(FastInfoset.class)); - addLoggingSupport(server.getEndpoint(), bus, cls.getAnnotation(Logging.class)); addEndpointProperties(server.getEndpoint(), bus, cls.getAnnotation(EndpointProperty.class)); EndpointProperties props = cls.getAnnotation(EndpointProperties.class); if (props != null) { @@ -276,13 +272,6 @@ private void setDataBinding(AbstractServiceFactoryBean factory, } } - private void addLoggingSupport(Endpoint endpoint, Bus bus, Logging annotation) { - if (annotation != null) { - LoggingFeature lf = new LoggingFeature(annotation); - lf.initialize(endpoint, bus); - } - } - private void addGZipSupport(Endpoint ep, Bus bus, GZIP annotation) { if (annotation != null) { try { diff --git a/core/src/main/java/org/apache/cxf/service/factory/FactoryBeanListenerManager.java b/core/src/main/java/org/apache/cxf/service/factory/FactoryBeanListenerManager.java index af07871ac30..e8ef8301237 100644 --- a/core/src/main/java/org/apache/cxf/service/factory/FactoryBeanListenerManager.java +++ b/core/src/main/java/org/apache/cxf/service/factory/FactoryBeanListenerManager.java @@ -40,6 +40,7 @@ public class FactoryBeanListenerManager { public FactoryBeanListenerManager() { listeners.add(new AnnotationsFactoryBeanListener()); + listeners.add(new OldLoggingFactoryBeanListener()); } public FactoryBeanListenerManager(Bus b) { this(); @@ -55,7 +56,7 @@ public final void setBus(Bus bus) { ConfiguredBeanLocator loc = bus.getExtension(ConfiguredBeanLocator.class); if (loc != null) { for (FactoryBeanListener f : loc.getBeansOfType(FactoryBeanListener.class)) { - listeners.addIfAbsent(f); + listeners.add(0, f); } } } diff --git a/core/src/main/java/org/apache/cxf/service/factory/OldLoggingFactoryBeanListener.java b/core/src/main/java/org/apache/cxf/service/factory/OldLoggingFactoryBeanListener.java new file mode 100644 index 00000000000..bef3c3814ee --- /dev/null +++ b/core/src/main/java/org/apache/cxf/service/factory/OldLoggingFactoryBeanListener.java @@ -0,0 +1,77 @@ +/** + * 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.cxf.service.factory; + + +import java.util.logging.Logger; + +import org.apache.cxf.Bus; +import org.apache.cxf.annotations.Logging; +import org.apache.cxf.common.logging.LogUtils; +import org.apache.cxf.endpoint.Endpoint; +import org.apache.cxf.endpoint.Server; + +/** + * + */ +public class OldLoggingFactoryBeanListener implements FactoryBeanListener { + static final Logger LOG = LogUtils.getL7dLogger(OldLoggingFactoryBeanListener.class); + + /** {@inheritDoc}*/ + public void handleEvent(Event ev, AbstractServiceFactoryBean factory, Object... args) { + switch (ev) { + case ENDPOINT_SELECTED: { + Class cls = (Class)args[2]; + Endpoint ep = (Endpoint)args[1]; + Bus bus = factory.getBus(); + // To avoid the NPE + if (cls == null) { + return; + } + addLoggingSupport(ep, bus, cls.getAnnotation(Logging.class)); + break; + } + case SERVER_CREATED: { + Class cls = (Class)args[2]; + if (cls == null) { + return; + } + Server server = (Server)args[0]; + Bus bus = factory.getBus(); + addLoggingSupport(server.getEndpoint(), bus, cls.getAnnotation(Logging.class)); + break; + } + default: + //do nothing + } + } + + + private void addLoggingSupport(Endpoint endpoint, Bus bus, Logging annotation) { + if (annotation != null) { + @SuppressWarnings("deprecation") + org.apache.cxf.feature.LoggingFeature lf = new org.apache.cxf.feature.LoggingFeature(annotation); + LOG.warning("Deprecated logging interceptors being used, switch to cxf-rt-ext-logging based logging."); + lf.initialize(endpoint, bus); + } + } + + +} diff --git a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingBusLifecycleListener.java b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingBusLifecycleListener.java new file mode 100644 index 00000000000..98f1e746936 --- /dev/null +++ b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingBusLifecycleListener.java @@ -0,0 +1,83 @@ +/** + * 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.cxf.ext.logging; + +import org.apache.cxf.Bus; +import org.apache.cxf.buslifecycle.BusLifeCycleListener; +import org.apache.cxf.buslifecycle.BusLifeCycleManager; + +/** + * + */ +public class LoggingBusLifecycleListener implements BusLifeCycleListener { + + static final boolean FORCE_LOGGING; + static final boolean FORCE_PRETTY; + static { + boolean b = false; + boolean pretty = false; + try { + String prop = System.getProperty("org.apache.cxf.logging.enabled", "false"); + if ("pretty".equals(prop)) { + b = true; + pretty = true; + } else { + b = Boolean.parseBoolean(prop); + //treat these all the same + b |= Boolean.getBoolean("com.sun.xml.ws.transport.local.LocalTransportPipe.dump"); + b |= Boolean.getBoolean("com.sun.xml.ws.util.pipe.StandaloneTubeAssembler.dump"); + b |= Boolean.getBoolean("com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump"); + b |= Boolean.getBoolean("com.sun.xml.ws.transport.http.HttpAdapter.dump"); + } + } catch (Throwable t) { + //ignore + } + FORCE_LOGGING = b; + FORCE_PRETTY = pretty; + } + + private final Bus bus; + public LoggingBusLifecycleListener(Bus b) { + bus = b; + bus.getExtension(BusLifeCycleManager.class).registerLifeCycleListener(this); + } + + /** {@inheritDoc}*/ + @Override + public void initComplete() { + if (FORCE_LOGGING) { + LoggingFeature feature = new LoggingFeature(); + feature.setPrettyLogging(FORCE_PRETTY); + bus.getFeatures().add(feature); + feature.initialize(bus); + } + } + + /** {@inheritDoc}*/ + @Override + public void preShutdown() { + } + + /** {@inheritDoc}*/ + @Override + public void postShutdown() { + } + +} diff --git a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingFactoryBeanListener.java b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingFactoryBeanListener.java new file mode 100644 index 00000000000..f4f31d3ccad --- /dev/null +++ b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingFactoryBeanListener.java @@ -0,0 +1,106 @@ +/** + * 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.cxf.ext.logging; + +import java.io.File; +import java.io.FileWriter; +import java.io.PrintWriter; +import java.net.URI; + +import org.apache.cxf.Bus; + +import org.apache.cxf.annotations.Logging; + +import org.apache.cxf.endpoint.Endpoint; +import org.apache.cxf.endpoint.Server; +import org.apache.cxf.ext.logging.event.PrintWriterEventSender; +import org.apache.cxf.service.factory.AbstractServiceFactoryBean; +import org.apache.cxf.service.factory.FactoryBeanListener; +import org.apache.cxf.service.factory.FactoryBeanListenerManager; + +/** + * + */ +public class LoggingFactoryBeanListener implements FactoryBeanListener { + + public LoggingFactoryBeanListener(Bus b) { + FactoryBeanListenerManager m = b.getExtension(FactoryBeanListenerManager.class); + for (FactoryBeanListener f : m.getListeners()) { + if (f.getClass().getSimpleName().equals("OldLoggingFactoryBeanListener")) { + m.removeListener(f); + } + } + } + + /** {@inheritDoc}*/ + public void handleEvent(Event ev, AbstractServiceFactoryBean factory, Object... args) { + switch (ev) { + case ENDPOINT_SELECTED: { + Class cls = (Class)args[2]; + Endpoint ep = (Endpoint)args[1]; + Bus bus = factory.getBus(); + // To avoid the NPE + if (cls == null) { + return; + } + addLoggingSupport(ep, bus, cls.getAnnotation(Logging.class)); + break; + } + case SERVER_CREATED: { + Class cls = (Class)args[2]; + if (cls == null) { + return; + } + Server server = (Server)args[0]; + Bus bus = factory.getBus(); + addLoggingSupport(server.getEndpoint(), bus, cls.getAnnotation(Logging.class)); + break; + } + default: + //do nothing + } + } + + private void addLoggingSupport(Endpoint endpoint, Bus bus, Logging annotation) { + if (annotation != null) { + LoggingFeature lf = new LoggingFeature(); + lf.setPrettyLogging(annotation.pretty()); + lf.setLimit(annotation.limit()); + lf.setLogBinary(annotation.showBinary()); + + if ("".equals(annotation.outLocation())) { + lf.setOutSender(new PrintWriterEventSender(System.out)); + } else if ("".equals(annotation.outLocation())) { + lf.setOutSender(new PrintWriterEventSender(System.err)); + } else if (annotation.outLocation().startsWith("file:")) { + try { + URI uri = new URI(annotation.outLocation()); + File file = new File(uri); + PrintWriter writer = new PrintWriter(new FileWriter(file, true), true); + lf.setOutSender(new PrintWriterEventSender(writer)); + } catch (Exception ex) { + //stick with default + } + } + lf.initialize(endpoint, bus); + } + } + +} diff --git a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingFeature.java b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingFeature.java index 51d33e93f8b..1518752dcb1 100644 --- a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingFeature.java +++ b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingFeature.java @@ -47,16 +47,17 @@ @NoJSR250Annotations @Provider(value = Type.Feature) public class LoggingFeature extends AbstractFeature { - private LogEventSender sender; private LoggingInInterceptor in; private LoggingOutInterceptor out; - private PrettyLoggingFilter prettyFilter; + private PrettyLoggingFilter inPrettyFilter; + private PrettyLoggingFilter outPrettyFilter; public LoggingFeature() { - this.sender = new Slf4jVerboseEventSender(); - prettyFilter = new PrettyLoggingFilter(sender); - in = new LoggingInInterceptor(prettyFilter); - out = new LoggingOutInterceptor(prettyFilter); + LogEventSender sender = new Slf4jVerboseEventSender(); + inPrettyFilter = new PrettyLoggingFilter(sender); + outPrettyFilter = new PrettyLoggingFilter(sender); + in = new LoggingInInterceptor(inPrettyFilter); + out = new LoggingOutInterceptor(outPrettyFilter); } @Override @@ -80,11 +81,19 @@ public void setInMemThreshold(long inMemThreshold) { } public void setSender(LogEventSender sender) { - this.prettyFilter.setNext(sender); + this.inPrettyFilter.setNext(sender); + this.outPrettyFilter.setNext(sender); + } + public void setInSender(LogEventSender s) { + this.inPrettyFilter.setNext(s); + } + public void setOutSender(LogEventSender s) { + this.outPrettyFilter.setNext(s); } public void setPrettyLogging(boolean prettyLogging) { - this.prettyFilter.setPrettyLogging(prettyLogging); + this.inPrettyFilter.setPrettyLogging(prettyLogging); + this.outPrettyFilter.setPrettyLogging(prettyLogging); } /** @@ -106,7 +115,6 @@ public void setLogMultipart(boolean logMultipart) { } public void setVerbose(boolean verbose) { - this.sender = verbose ? new Slf4jVerboseEventSender() : new Slf4jEventSender(); - this.prettyFilter.setNext(sender); + setSender(verbose ? new Slf4jVerboseEventSender() : new Slf4jEventSender()); } } diff --git a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/PrintWriterEventSender.java b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/PrintWriterEventSender.java index daa657eb570..aedf3f7acea 100644 --- a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/PrintWriterEventSender.java +++ b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/PrintWriterEventSender.java @@ -19,6 +19,7 @@ package org.apache.cxf.ext.logging.event; +import java.io.PrintStream; import java.io.PrintWriter; import java.time.Instant; @@ -31,6 +32,9 @@ public class PrintWriterEventSender implements LogEventSender { public PrintWriterEventSender(PrintWriter writer) { this.writer = writer; } + public PrintWriterEventSender(PrintStream ps) { + this.writer = new PrintWriter(ps); + } /** {@inheritDoc}*/ @Override diff --git a/rt/features/logging/src/main/resources/META-INF/cxf/bus-extensions.txt b/rt/features/logging/src/main/resources/META-INF/cxf/bus-extensions.txt new file mode 100644 index 00000000000..cf0d294b92a --- /dev/null +++ b/rt/features/logging/src/main/resources/META-INF/cxf/bus-extensions.txt @@ -0,0 +1,2 @@ +org.apache.cxf.ext.logging.LoggingFactoryBeanListener::true +org.apache.cxf.ext.logging.LoggingBusLifecycleListener::true