commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1424883 - in /commons/sandbox/privilizer/trunk: maven-plugin/src/main/java/org/apache/commons/weaver/maven/ modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/ processor/src/main/java/org/apache/commons/weaver/spi...
Date Fri, 21 Dec 2012 11:52:12 GMT
Author: struberg
Date: Fri Dec 21 11:52:12 2012
New Revision: 1424883

URL: http://svn.apache.org/viewvc?rev=1424883&view=rev
Log:
add a simple java.util.logging to Mojo logging redirector

this allows the weavers to use standard logging and we still
pick the output out in maven

Added:
    commons/sandbox/privilizer/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/JavaLoggingToMojoLoggingRedirector.java
  (with props)
Modified:
    commons/sandbox/privilizer/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractWeaveMojo.java
    commons/sandbox/privilizer/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java
    commons/sandbox/privilizer/trunk/processor/src/main/java/org/apache/commons/weaver/spi/Weaver.java
    commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java

Modified: commons/sandbox/privilizer/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractWeaveMojo.java
URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractWeaveMojo.java?rev=1424883&r1=1424882&r2=1424883&view=diff
==============================================================================
--- commons/sandbox/privilizer/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractWeaveMojo.java
(original)
+++ commons/sandbox/privilizer/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractWeaveMojo.java
Fri Dec 21 11:52:12 2012
@@ -22,6 +22,7 @@ import java.util.Properties;
 import org.apache.commons.weaver.WeaveProcessor;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.plugins.annotations.Parameter;
 
 
@@ -84,13 +85,21 @@ public abstract class AbstractWeaveMojo 
     @Override
     public void execute() throws MojoExecutionException
     {
+        Log mojoLog = getLog();
+        JavaLoggingToMojoLoggingRedirector logRedirector = new JavaLoggingToMojoLoggingRedirector(mojoLog);
+        logRedirector.activate();
+
         try {
+
             WeaveProcessor wp = WeaveProcessor.getInstance();
             configure(wp);
             wp.weave();
         } catch (Exception e) {
             throw new MojoExecutionException("weaving failed", e);
         }
+        finally {
+            logRedirector.deactivate();
+        }
     }
 
     protected void configure(WeaveProcessor wp) {

Added: commons/sandbox/privilizer/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/JavaLoggingToMojoLoggingRedirector.java
URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/JavaLoggingToMojoLoggingRedirector.java?rev=1424883&view=auto
==============================================================================
--- commons/sandbox/privilizer/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/JavaLoggingToMojoLoggingRedirector.java
(added)
+++ commons/sandbox/privilizer/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/JavaLoggingToMojoLoggingRedirector.java
Fri Dec 21 11:52:12 2012
@@ -0,0 +1,151 @@
+/*
+ * 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.commons.weaver.maven;
+
+
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.ResourceBundle;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogManager;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.logging.Log;
+
+
+/**
+ * This class redirects calls to java.util.Logging to Mojo logging.
+ */
+public class JavaLoggingToMojoLoggingRedirector
+{
+    private JDKLogHandler activeHandler;
+    private List<Handler> removedHandlers = new ArrayList<Handler>();
+
+    /**
+     * The Maven mojo logger to delegate messages to.
+     */
+    private Log mojoLogger;
+
+    /**
+     * @param mojoLogger the Maven mojo logger to delegate messages to.
+     */
+    public JavaLoggingToMojoLoggingRedirector(Log mojoLogger)
+    {
+        this.mojoLogger = mojoLogger;
+    }
+
+    /**
+     * Activates this feature.
+     */
+    public void activate() throws MojoExecutionException {
+        try {
+            Logger rootLogger = LogManager.getLogManager().getLogger("");
+            // remove old handlers
+            for (Handler handler : rootLogger.getHandlers()) {
+                rootLogger.removeHandler(handler);
+                removedHandlers.add(handler);
+            }
+            if (removedHandlers.size() == 0) {
+                throw new MojoExecutionException("could not remove any handler. aborting.");
+            }
+
+            // add our own
+            activeHandler = new JDKLogHandler();
+            activeHandler.setLevel(Level.ALL);
+            rootLogger.setLevel(Level.ALL);
+
+            rootLogger.addHandler(activeHandler);
+        } catch (Exception exc) {
+            throw new MojoExecutionException("failed to activate the jul logging redirector",
exc);
+        }
+    }
+
+    /**
+     * deactivate the redirection and put the original Handlers back in place again.
+     */
+    public void deactivate() {
+        Logger rootLogger = LogManager.getLogManager().getLogger("");
+        // remove old handlers
+        for (Handler handler : rootLogger.getHandlers()) {
+            if (handler == activeHandler) {
+                rootLogger.removeHandler(handler);
+            }
+        }
+
+        for (Handler oldHandler : removedHandlers) {
+            rootLogger.addHandler(oldHandler);
+        }
+    }
+
+
+    private class JDKLogHandler extends Handler {
+
+        @Override
+        public void publish(LogRecord record) {
+            Throwable exception = record.getThrown();
+            Level level = record.getLevel();
+            if (level == Level.SEVERE && mojoLogger.isErrorEnabled()) {
+                mojoLogger.error(getMessage(record), exception);
+            }
+            else if (level == Level.WARNING && mojoLogger.isWarnEnabled()) {
+                mojoLogger.warn(getMessage(record), exception);
+            }
+            else if (level == Level.INFO && mojoLogger.isInfoEnabled()) {
+                mojoLogger.info(getMessage(record), exception);
+            }
+            else if (level == Level.CONFIG && mojoLogger.isDebugEnabled()) {
+                mojoLogger.debug(getMessage(record), exception);
+            }
+            else if (mojoLogger.isDebugEnabled()) {
+                mojoLogger.debug(getMessage(record), exception);
+            }
+        }
+
+        private String getMessage(LogRecord record) {
+            String message = record.getMessage();
+            ResourceBundle bundle = record.getResourceBundle();
+            Object params[] = record.getParameters();
+            if (bundle != null && bundle.containsKey(message)) {
+                // todo: cannot enforce Locale.ENGLISH here
+                message = bundle.getString(message);
+            }
+            if (params != null && params.length > 0) {
+                MessageFormat format = new MessageFormat(message);
+                message = format.format(params);
+            }
+            return message;
+        }
+
+        @Override
+        public void flush() {
+            // nothing to do
+        }
+
+        @Override
+        public void close() {
+            // nothing to do
+        }
+
+    }
+
+}

Propchange: commons/sandbox/privilizer/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/JavaLoggingToMojoLoggingRedirector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/privilizer/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java
URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java?rev=1424883&r1=1424882&r2=1424883&view=diff
==============================================================================
--- commons/sandbox/privilizer/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java
(original)
+++ commons/sandbox/privilizer/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java
Fri Dec 21 11:52:12 2012
@@ -24,7 +24,7 @@ import org.apache.commons.weaver.utils.U
  */
 public class PrivilizerWeaver implements Weaver
 {
-    private Logger logger = Logger.getLogger(PrivilizerWeaver.class.getName());
+    private static final Logger LOG = Logger.getLogger(PrivilizerWeaver.class.getName());
 
     private FilesystemPrivilizer privilizer;
 
@@ -33,14 +33,9 @@ public class PrivilizerWeaver implements
     private AccessLevel targetAccessLevel;
 
     @Override
-    public void setLogger(Logger customLogger)
-    {
-        this.logger = customLogger;
-    }
-
-    @Override
     public void configure(List<String> classPath, File target, Map<String, Object>
config)
     {
+        LOG.info("");
         privilizer = new FilesystemPrivilizer(policy, new URLClassLoader(URLArray.fromPaths(classPath)),
target) {
             @Override
             protected boolean permitMethodWeaving(final AccessLevel accessLevel) {

Modified: commons/sandbox/privilizer/trunk/processor/src/main/java/org/apache/commons/weaver/spi/Weaver.java
URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/trunk/processor/src/main/java/org/apache/commons/weaver/spi/Weaver.java?rev=1424883&r1=1424882&r2=1424883&view=diff
==============================================================================
--- commons/sandbox/privilizer/trunk/processor/src/main/java/org/apache/commons/weaver/spi/Weaver.java
(original)
+++ commons/sandbox/privilizer/trunk/processor/src/main/java/org/apache/commons/weaver/spi/Weaver.java
Fri Dec 21 11:52:12 2012
@@ -23,7 +23,6 @@ import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
 import java.util.List;
 import java.util.Map;
-import java.util.logging.Logger;
 
 /**
  * An implementation of a 'Weaver' takes care about
@@ -45,12 +44,6 @@ public interface Weaver
     void configure(List<String> classPath, File target, Map<String, Object> config);
 
     /**
-     * This method can be used to set a custom logger
-     * @param customLogger
-     */
-    void setLogger(Logger customLogger);
-
-    /**
      * A Weaver must return a List of Annotations he is interested in.
      */
     List<Class<? extends Annotation>> getInterest();

Modified: commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java
URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java?rev=1424883&r1=1424882&r2=1424883&view=diff
==============================================================================
--- commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java
(original)
+++ commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java
Fri Dec 21 11:52:12 2012
@@ -41,11 +41,6 @@ public class TestWeaver implements Weave
     public static List<Class> wovenClasses = new ArrayList<Class>();
 
     @Override
-    public void setLogger(Logger customLogger) {
-        // do nothing
-    }
-
-    @Override
     public void configure(List<String> classPath, File target, Map<String, Object>
config)
     {
         Assert.assertNotNull(config);



Mime
View raw message