cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject cxf git commit: [CXF-6286] OSGi service for logging module
Date Sun, 22 Mar 2015 20:09:45 GMT
Repository: cxf
Updated Branches:
  refs/heads/master fe4deb5b6 -> d5ea460f2


[CXF-6286] OSGi service for logging module


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/d5ea460f
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/d5ea460f
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/d5ea460f

Branch: refs/heads/master
Commit: d5ea460f20d60c0496c70a2cf0b7f0484d48c806
Parents: fe4deb5
Author: Christian Schneider <chris@die-schneider.net>
Authored: Sun Mar 22 21:09:22 2015 +0100
Committer: Christian Schneider <chris@die-schneider.net>
Committed: Sun Mar 22 21:09:22 2015 +0100

----------------------------------------------------------------------
 rt/features/logging/pom.xml                     |  27 ++++-
 .../ext/logging/AbstractLoggingInterceptor.java |   3 +-
 .../apache/cxf/ext/logging/LoggingFeature.java  |  26 +++--
 .../cxf/ext/logging/LoggingInInterceptor.java   |   1 +
 .../ext/logging/event/PrettyLoggingFilter.java  |  77 ++++++++++++++
 .../apache/cxf/ext/logging/osgi/Activator.java  | 104 +++++++++++++++++++
 .../apache/cxf/ext/logging/SOAPLoggingTest.java |   1 +
 7 files changed, 223 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/d5ea460f/rt/features/logging/pom.xml
----------------------------------------------------------------------
diff --git a/rt/features/logging/pom.xml b/rt/features/logging/pom.xml
index 78c3333..e7a24c2 100644
--- a/rt/features/logging/pom.xml
+++ b/rt/features/logging/pom.xml
@@ -11,15 +11,31 @@
     </parent>
     <artifactId>cxf-rt-features-logging</artifactId>
     <packaging>bundle</packaging>
+    
+    <properties>
+        <cxf.version>3.1.0-SNAPSHOT</cxf.version>
+        <cxf.bundle.activator>org.apache.cxf.ext.logging.osgi.Activator</cxf.bundle.activator>
+    </properties>
 
     <name>Apache CXF Advanced Logging Feature</name>
     <description>Apache CXF Advanced Logging Feature</description>
 
     <dependencies>
         <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        
+        <dependency>
             <groupId>org.apache.cxf</groupId>
             <artifactId>cxf-core</artifactId>
-            <version>${project.version}</version>
+            <version>${cxf.version}</version>
         </dependency>
         <dependency>
             <groupId>org.slf4j</groupId>
@@ -45,26 +61,27 @@
         <dependency>
             <groupId>org.apache.cxf</groupId>
             <artifactId>cxf-rt-frontend-jaxws</artifactId>
-            <version>${project.version}</version>
+            <version>${cxf.version}</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.apache.cxf</groupId>
             <artifactId>cxf-rt-frontend-jaxrs</artifactId>
-            <version>${project.version}</version>
+            <version>${cxf.version}</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.apache.cxf</groupId>
             <artifactId>cxf-rt-rs-client</artifactId>
-            <version>${project.version}</version>
+            <version>${cxf.version}</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.apache.cxf</groupId>
             <artifactId>cxf-rt-transports-http-jetty</artifactId>
-            <version>${project.version}</version>
+            <version>${cxf.version}</version>
             <scope>test</scope>
         </dependency>
+
     </dependencies>
 </project>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cxf/blob/d5ea460f/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/AbstractLoggingInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/AbstractLoggingInterceptor.java
b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/AbstractLoggingInterceptor.java
index 03590fb..6dd2c7d 100644
--- a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/AbstractLoggingInterceptor.java
+++ b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/AbstractLoggingInterceptor.java
@@ -33,7 +33,7 @@ abstract class AbstractLoggingInterceptor extends AbstractPhaseInterceptor<Messa
     protected long threshold = -1;
 
     protected LogEventSender sender;
-    
+
     public AbstractLoggingInterceptor(String phase, LogEventSender sender) {
         super(phase);
         this.sender = sender;
@@ -63,4 +63,5 @@ abstract class AbstractLoggingInterceptor extends AbstractPhaseInterceptor<Messa
             exchange.put(LogEvent.KEY_EXCHANGE_ID, exchangeId);
         }
     }
+
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/d5ea460f/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingFeature.java
----------------------------------------------------------------------
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 8906f0b..c42b51e 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
@@ -21,6 +21,7 @@ package org.apache.cxf.ext.logging;
 import org.apache.cxf.Bus;
 import org.apache.cxf.common.injection.NoJSR250Annotations;
 import org.apache.cxf.ext.logging.event.LogEventSender;
+import org.apache.cxf.ext.logging.event.PrettyLoggingFilter;
 import org.apache.cxf.ext.logging.slf4j.Slf4jEventSender;
 import org.apache.cxf.feature.AbstractFeature;
 import org.apache.cxf.interceptor.InterceptorProvider;
@@ -45,20 +46,19 @@ public class LoggingFeature extends AbstractFeature {
     private int limit = AbstractLoggingInterceptor.DEFAULT_LIMIT;
     private long inMemThreshold;
     private LogEventSender sender;
+    private LoggingInInterceptor in;
+    private LoggingOutInterceptor out;
+    private PrettyLoggingFilter prettyFilter;
     
     public LoggingFeature() {
         this.sender = new Slf4jEventSender();
+        prettyFilter = new PrettyLoggingFilter(sender);
+        in = new LoggingInInterceptor(prettyFilter);
+        out = new LoggingOutInterceptor(prettyFilter);
     }
     
     @Override
     protected void initializeProvider(InterceptorProvider provider, Bus bus) {
-        LoggingInInterceptor in = new LoggingInInterceptor(sender);
-        in.setLimit(limit);
-        in.setInMemThreshold(inMemThreshold);
-        LoggingOutInterceptor out = new LoggingOutInterceptor(sender);
-        out.setLimit(limit);
-        out.setInMemThreshold(inMemThreshold);
-        
         WireTapIn wireTapIn = new WireTapIn(inMemThreshold, limit);
         provider.getInInterceptors().add(wireTapIn);
         provider.getInInterceptors().add(in);
@@ -69,14 +69,20 @@ public class LoggingFeature extends AbstractFeature {
     }
 
     public void setLimit(int lim) {
-        limit = lim;
+        in.setLimit(limit);
+        out.setLimit(limit);
     }
     
     public void setInMemThreshold(long inMemThreshold) {
-        this.inMemThreshold = inMemThreshold;
+        in.setInMemThreshold(inMemThreshold);
+        out.setInMemThreshold(inMemThreshold);
     }
     
     public void setSender(LogEventSender sender) {
-        this.sender = sender;
+        this.prettyFilter.setNext(sender);
+    }
+
+    public void setPrettyLogging(boolean prettyLogging) {
+        this.prettyFilter.setPrettyLogging(prettyLogging);
     }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/d5ea460f/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingInInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingInInterceptor.java
b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingInInterceptor.java
index 7388eef..aeb851d 100644
--- a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingInInterceptor.java
+++ b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingInInterceptor.java
@@ -84,4 +84,5 @@ public class LoggingInInterceptor extends AbstractLoggingInterceptor {
         event.setFullContentFile(writer.getTempFile());
     }
 
+
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/d5ea460f/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/PrettyLoggingFilter.java
----------------------------------------------------------------------
diff --git a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/PrettyLoggingFilter.java
b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/PrettyLoggingFilter.java
new file mode 100644
index 0000000..719c162
--- /dev/null
+++ b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/PrettyLoggingFilter.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.ext.logging.event;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+public class PrettyLoggingFilter implements LogEventSender {
+    private LogEventSender next;
+    private boolean prettyLogging;
+
+    public PrettyLoggingFilter(LogEventSender next) {
+        this.next = next;
+        this.prettyLogging = false;
+    }
+
+    @Override
+    public void send(LogEvent event) {
+        if (shouldPrettyPrint(event)) {
+            event.setPayload(getPrettyMessage(event.getPayload()));
+        }
+        next.send(event);
+    }
+
+    private boolean shouldPrettyPrint(LogEvent event) {
+        return prettyLogging 
+            && event.getContentType() != null 
+            && event.getContentType().indexOf("xml") >= 0 
+            && event.getPayload().length() > 0;
+    }
+
+    public String getPrettyMessage(String message) {
+        try {
+            Transformer serializer = TransformerFactory.newInstance().newTransformer();
+            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
+            serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
+
+            StringWriter swriter = new StringWriter();
+            serializer.transform(new StreamSource(new StringReader(message)), new StreamResult(swriter));
+            return swriter.toString();
+        } catch (TransformerException e) {
+            return message;
+        }
+    }
+
+    public void setNext(LogEventSender next) {
+        this.next = next;
+    }
+    
+    public void setPrettyLogging(boolean prettyLogging) {
+        this.prettyLogging = prettyLogging;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/d5ea460f/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/osgi/Activator.java
----------------------------------------------------------------------
diff --git a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/osgi/Activator.java
b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/osgi/Activator.java
new file mode 100644
index 0000000..c7a2b80
--- /dev/null
+++ b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/osgi/Activator.java
@@ -0,0 +1,104 @@
+/**
+ * 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.osgi;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.cxf.ext.logging.LoggingFeature;
+import org.apache.cxf.feature.Feature;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class Activator implements BundleActivator {
+    private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
+    private static final String CONFIG_PID = "org.apache.cxf.features.logging";
+
+    @Override
+    public void start(final BundleContext bundleContext) {
+        Dictionary<String, String> properties = new Hashtable<>();
+        properties.put(Constants.SERVICE_PID, CONFIG_PID);
+        bundleContext.registerService(ManagedService.class.getName(), new ConfigUpdater(bundleContext),
properties);
+    }
+
+    @Override
+    public void stop(BundleContext context) throws Exception {
+
+    }
+    
+    private final class ConfigUpdater implements ManagedService {
+        private BundleContext bundleContext;
+        private ServiceRegistration serviceReg;
+        private LoggingFeature logging;
+
+        public ConfigUpdater(BundleContext bundleContext) {
+            this.logging = new LoggingFeature();
+            this.bundleContext = bundleContext;
+        }
+
+        @SuppressWarnings("rawtypes")
+        @Override
+        public void updated(Dictionary config) throws ConfigurationException {
+            boolean enabled = Boolean.valueOf(getValue(config, "enabled", "false"));
+            LOG.info("CXF message logging feature " + (enabled ? "enabled" : "disabled"));
+            if (enabled) {
+                Integer limit = new Integer(getValue(config, "limit", "65536"));
+                Boolean pretty = Boolean.valueOf(getValue(config, "pretty", "false"));
+                Long inMemThreshold = new Long(getValue(config, "inMemThresHold", "-1"));
+
+                if (limit != null) {
+                    logging.setLimit(limit);
+                }
+                if (inMemThreshold != null) {
+                    logging.setInMemThreshold(inMemThreshold);
+                }
+                if (pretty != null) {
+                    logging.setPrettyLogging(pretty);
+                }
+                Dictionary<String, Object> properties = new Hashtable<>();
+                properties.put("name", "logging");
+                if (serviceReg == null) {
+                    serviceReg =  bundleContext.registerService(Feature.class.getName(),
logging, properties);
+                }
+            } else {
+                if (serviceReg != null) {
+                    serviceReg.unregister();
+                    serviceReg = null;
+                }
+            }
+        }
+        
+        @SuppressWarnings("rawtypes")
+        private String getValue(Dictionary config, String key, String defaultValue) {
+            if (config == null) {
+                return defaultValue;
+            }
+            String value = (String)config.get(key);
+            return value != null ? value : defaultValue;
+        }
+
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/d5ea460f/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/SOAPLoggingTest.java
----------------------------------------------------------------------
diff --git a/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/SOAPLoggingTest.java
b/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/SOAPLoggingTest.java
index 79033da..17f0c37 100644
--- a/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/SOAPLoggingTest.java
+++ b/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/SOAPLoggingTest.java
@@ -48,6 +48,7 @@ public class SOAPLoggingTest {
     public void testSlf4j() throws MalformedURLException {
         TestService serviceImpl = new TestServiceImplementation();
         LoggingFeature loggingFeature = new LoggingFeature();
+        loggingFeature.setPrettyLogging(true);
         Endpoint ep = Endpoint.publish(SERVICE_URI, serviceImpl, loggingFeature);
         TestService client = createTestClient(loggingFeature);
         client.echo("test");


Mime
View raw message