Skip to content

Commit

Permalink
Update handling of hte @logging annotation to use the new logging cap…
Browse files Browse the repository at this point in the history
…abilities if available
  • Loading branch information
dkulp committed Mar 10, 2017
1 parent f22e465 commit 54e7c58
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Class<?>, Object> extensions;
Expand All @@ -109,11 +85,6 @@ public ExtensionManagerBus(Map<Class<?>, Object> extensions, Map<String, Object>
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);
}
Expand Down Expand Up @@ -354,11 +325,6 @@ public Collection<Feature> getFeatures() {
public synchronized void setFeatures(Collection<? extends Feature> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class FactoryBeanListenerManager {

public FactoryBeanListenerManager() {
listeners.add(new AnnotationsFactoryBeanListener());
listeners.add(new OldLoggingFactoryBeanListener());
}
public FactoryBeanListenerManager(Bus b) {
this();
Expand All @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}


}
Original file line number Diff line number Diff line change
@@ -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() {
}

}
Original file line number Diff line number Diff line change
@@ -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 ("<stdout>".equals(annotation.outLocation())) {
lf.setOutSender(new PrintWriterEventSender(System.out));
} else if ("<stderr>".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);
}
}

}
Loading

0 comments on commit 54e7c58

Please sign in to comment.