ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1582291 - in /ace/trunk/org.apache.ace.agent: src/org/apache/ace/agent/ src/org/apache/ace/agent/impl/ test/org/apache/ace/agent/impl/
Date Thu, 27 Mar 2014 13:13:46 GMT
Author: jawi
Date: Thu Mar 27 13:13:45 2014
New Revision: 1582291

URL: http://svn.apache.org/r1582291
Log:
Improved logging in agent:

- when there is an external log service present we can use, use it instead
  of our "internal" log service. If no log service is present, we can always
  log to our internal service instead;
- when no valid configuration property is given for the log handler, do not
  fall back on the global default, but use the current setting instead. This
  way, we can avoid unwanted toggling of log levels when updating the agent's
  configuration.


Modified:
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/InstallationFailedException.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/LoggingHandler.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DeploymentHandlerImpl.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LoggingHandlerImpl.java
    ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandlerTest.java

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/InstallationFailedException.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/InstallationFailedException.java?rev=1582291&r1=1582290&r2=1582291&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/InstallationFailedException.java
(original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/InstallationFailedException.java
Thu Mar 27 13:13:45 2014
@@ -54,12 +54,14 @@ public class InstallationFailedException
     private static final long serialVersionUID = 1L;
 
     private final int m_code;
+    private final String m_origMsg;
 
     /**
      * Creates a new {@link InstallationFailedException} instance.
      */
     public InstallationFailedException(String msg, DeploymentException cause) {
         super(msg, cause.getCause());
+        m_origMsg = cause.getMessage();
         m_code = cause.getCode();
     }
 
@@ -71,6 +73,13 @@ public class InstallationFailedException
     }
 
     /**
+     * @return the original message of the exception that caused this exception.
+     */
+    public String getOriginalMessage() {
+        return m_origMsg;
+    }
+
+    /**
      * @return a string representation as to why the installation failed, never <code>null</code>.
      */
     public String getReason() {
@@ -108,8 +117,9 @@ public class InstallationFailedException
             case CODE_TIMEOUT:
                 return "Installation of deployment package timed out";
             case CODE_OTHER_ERROR:
+                return m_origMsg + " (" + m_code + ")";
             default:
-                return "Unknown/other error condition";
+                return "Unknown error condition: " + m_origMsg + " (" + m_code + ")";
         }
     }
 }

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/LoggingHandler.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/LoggingHandler.java?rev=1582291&r1=1582290&r2=1582291&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/LoggingHandler.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/LoggingHandler.java Thu Mar 27
13:13:45 2014
@@ -20,6 +20,8 @@ package org.apache.ace.agent;
 
 import java.util.Formatter;
 
+import org.osgi.service.log.LogService;
+
 import aQute.bnd.annotation.ConsumerType;
 
 /**
@@ -29,17 +31,41 @@ import aQute.bnd.annotation.ConsumerType
 public interface LoggingHandler {
 
     enum Levels {
-        DEBUG, INFO, WARNING, ERROR;
+        /** Lowest log level. Used to log debugging/internal information. */
+        DEBUG(LogService.LOG_DEBUG),
+        /** Used to log useful information. */
+        INFO(LogService.LOG_INFO),
+        /** Used to report warnings. */
+        WARNING(LogService.LOG_WARNING),
+        /** Highest log level, used to report errors. */
+        ERROR(LogService.LOG_ERROR);
+
+        private final int m_logLevel;
+
+        private Levels(int logLevel) {
+            m_logLevel = logLevel;
+        }
+
+        /**
+         * @return the OSGi LogService log level corresponding to this log level.
+         */
+        public int getLogLevel() {
+            return m_logLevel;
+        }
     }
 
     /**
      * Log an debug message. If <code>args</code> are provided the message will
be processed as a format using the
      * standard {@link Formatter}.
      * 
-     * @param component The component identifier, not <code>null</code>
-     * @param message The log message or format, not <code>null</code>
-     * @param cause The cause, may be <code>null</code>
-     * @param args The optional formatter arguments
+     * @param component
+     *            The component identifier, not <code>null</code>
+     * @param message
+     *            The log message or format, not <code>null</code>
+     * @param cause
+     *            The cause, may be <code>null</code>
+     * @param args
+     *            The optional formatter arguments
      */
     void logDebug(String component, String message, Throwable exception, Object... args);
 
@@ -47,10 +73,14 @@ public interface LoggingHandler {
      * Log an info message. If <code>args</code> are provided the message will
be processed as a format using the
      * standard {@link Formatter}.
      * 
-     * @param component The component identifier, not <code>null</code>
-     * @param message The log message or format, not <code>null</code>
-     * @param cause The cause, may be <code>null</code>
-     * @param args The optional formatter arguments
+     * @param component
+     *            The component identifier, not <code>null</code>
+     * @param message
+     *            The log message or format, not <code>null</code>
+     * @param cause
+     *            The cause, may be <code>null</code>
+     * @param args
+     *            The optional formatter arguments
      */
     void logInfo(String component, String message, Throwable exception, Object... args);
 
@@ -58,10 +88,14 @@ public interface LoggingHandler {
      * Log an warning message. If <code>args</code> are provided the message
will be processed as a format using the
      * standard {@link Formatter}.
      * 
-     * @param component The component identifier, not <code>null</code>
-     * @param message The log message or format, not <code>null</code>
-     * @param cause The cause, may be <code>null</code>
-     * @param args The optional formatter arguments
+     * @param component
+     *            The component identifier, not <code>null</code>
+     * @param message
+     *            The log message or format, not <code>null</code>
+     * @param cause
+     *            The cause, may be <code>null</code>
+     * @param args
+     *            The optional formatter arguments
      */
     void logWarning(String component, String message, Throwable exception, Object... args);
 
@@ -69,10 +103,14 @@ public interface LoggingHandler {
      * Log an error message. If <code>args</code> are provided the message will
be processed as a format using the
      * standard {@link Formatter}.
      * 
-     * @param component The component identifier, not <code>null</code>
-     * @param message The log message or format, not <code>null</code>
-     * @param cause The cause, may be <code>null</code>
-     * @param args The optional formatter arguments
+     * @param component
+     *            The component identifier, not <code>null</code>
+     * @param message
+     *            The log message or format, not <code>null</code>
+     * @param cause
+     *            The cause, may be <code>null</code>
+     * @param args
+     *            The optional formatter arguments
      */
     void logError(String component, String message, Throwable exception, Object... args);
 }

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java?rev=1582291&r1=1582290&r2=1582291&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java Thu Mar 27
13:13:45 2014
@@ -114,7 +114,7 @@ public class Activator implements Bundle
 
         m_agentContext = new AgentContextImpl(bundleDataArea);
 
-        m_agentContext.setHandler(LoggingHandler.class, new LoggingHandlerImpl());
+        m_agentContext.setHandler(LoggingHandler.class, new LoggingHandlerImpl(context));
         m_agentContext.setHandler(ConfigurationHandler.class, new ConfigurationHandlerImpl(context));
         m_agentContext.setHandler(EventsHandler.class, new EventsHandlerImpl(context));
         m_agentContext.setHandler(ScheduledExecutorService.class, m_executorService);

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DeploymentHandlerImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DeploymentHandlerImpl.java?rev=1582291&r1=1582290&r2=1582291&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DeploymentHandlerImpl.java
(original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DeploymentHandlerImpl.java
Thu Mar 27 13:13:45 2014
@@ -68,13 +68,13 @@ public class DeploymentHandlerImpl exten
         @Override
         public void postEvent(Event event) {
             getEventsHandler().postEvent(event.getTopic(), getPayload(event));
-            eventAdminInvoke("postEvent", event);
+            invokeExternalEventAdmin("postEvent", event);
         }
 
         @Override
         public void sendEvent(Event event) {
             getEventsHandler().sendEvent(event.getTopic(), getPayload(event));
-            eventAdminInvoke("sendEvent", event);
+            invokeExternalEventAdmin("sendEvent", event);
         }
 
         /**
@@ -82,7 +82,7 @@ public class DeploymentHandlerImpl exten
          * dependency on the (external!) EventAdmin API we cannot always call like we normally
would do for
          * OSGi-services. Instead, we need to do some advanced reflection trickery in order
to call an EventAdmin.
          */
-        private void eventAdminInvoke(String method, Event event) {
+        private void invokeExternalEventAdmin(String method, Event event) {
             try {
                 // try to find an EventAdmin service
                 ServiceReference[] refs = m_context.getAllServiceReferences(EventAdmin.class.getName(),
null);
@@ -137,7 +137,24 @@ public class DeploymentHandlerImpl exten
 
         @Override
         public void log(int level, String message, Throwable exception) {
+            invokeInternalLogService(level, message, exception);
+        }
+
+        @Override
+        public void log(ServiceReference sr, int level, String message) {
+            log(level, message, null);
+        }
+
+        @Override
+        public void log(ServiceReference sr, int level, String message, Throwable exception)
{
+            log(level, message, exception);
+        }
+
+        private void invokeInternalLogService(int level, String message, Throwable exception)
{
             switch (level) {
+                case LogService.LOG_ERROR:
+                    logError(message, exception);
+                    break;
                 case LogService.LOG_WARNING:
                     logWarning(message, exception);
                     break;
@@ -145,23 +162,11 @@ public class DeploymentHandlerImpl exten
                     logInfo(message, exception);
                     break;
                 case LogService.LOG_DEBUG:
-                    logDebug(message, exception);
-                    break;
                 default:
-                    logError(message, exception);
+                    logDebug(message, exception);
                     break;
             }
         }
-
-        @Override
-        public void log(ServiceReference sr, int level, String message) {
-            log(level, message, null);
-        }
-
-        @Override
-        public void log(ServiceReference sr, int level, String message, Throwable exception)
{
-            log(level, message, exception);
-        }
     }
 
     private final DeploymentAdmin m_deploymentAdmin;

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LoggingHandlerImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LoggingHandlerImpl.java?rev=1582291&r1=1582290&r2=1582291&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LoggingHandlerImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LoggingHandlerImpl.java Thu
Mar 27 13:13:45 2014
@@ -21,11 +21,15 @@ package org.apache.ace.agent.impl;
 import static org.apache.ace.agent.AgentConstants.EVENT_AGENT_CONFIG_CHANGED;
 import static org.apache.ace.agent.AgentConstants.CONFIG_LOGGING_LEVEL;
 
+import java.lang.reflect.Method;
 import java.util.Date;
 import java.util.Map;
 
 import org.apache.ace.agent.EventListener;
 import org.apache.ace.agent.LoggingHandler;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.log.LogService;
 
 /**
  * Default thread-safe {@link LoggingHandler} implementation that logs messages to {@link
System.out} .
@@ -33,14 +37,17 @@ import org.apache.ace.agent.LoggingHandl
 public class LoggingHandlerImpl extends ComponentBase implements LoggingHandler, EventListener
{
     private static final Levels DEFAULT_LEVEL = Levels.WARNING;
 
+    private final BundleContext m_context;
+
     private volatile Levels m_logLevel;
 
-    public LoggingHandlerImpl() {
-        this(DEFAULT_LEVEL);
+    public LoggingHandlerImpl(BundleContext context) {
+        this(context, fromName(context.getProperty(CONFIG_LOGGING_LEVEL), DEFAULT_LEVEL));
     }
 
-    public LoggingHandlerImpl(Levels defaultLevel) {
+    public LoggingHandlerImpl(BundleContext context, Levels defaultLevel) {
         super("logging");
+        m_context = context;
         m_logLevel = defaultLevel;
     }
 
@@ -49,7 +56,7 @@ public class LoggingHandlerImpl extends 
         if (EVENT_AGENT_CONFIG_CHANGED.equals(topic)) {
             String newValue = payload.get(CONFIG_LOGGING_LEVEL);
 
-            m_logLevel = fromName(newValue);
+            m_logLevel = fromName(newValue, m_logLevel);
         }
     }
 
@@ -91,22 +98,67 @@ public class LoggingHandlerImpl extends 
         if (args.length > 0) {
             message = String.format(message, args);
         }
-        System.out.printf("[%s] %TT (%s) %s%n", logLevel, new Date(), component, message);
 
+        if (!invokeExternalLogService(logLevel, message, exception)) {
+            invokeInternalLogService(logLevel, component, message, exception);
+        }
+    }
+
+    private boolean invokeInternalLogService(Levels logLevel, String component, String message,
Throwable exception) {
+        System.out.printf("[%s] %TT (%s) %s%n", logLevel, new Date(), component, message);
         if (exception != null) {
             exception.printStackTrace(System.out);
         }
+        return true;
     }
 
-    private static Levels fromName(String name) {
-        if (name == null) {
-            return DEFAULT_LEVEL;
+    /**
+     * Bridges events from out local event-handling methods to the first (external) LogService
implementation. As we do
+     * not have a dependency on the (external!) LogService API we cannot always call like
we normally would do for
+     * OSGi-services. Instead, we need to do some advanced reflection trickery in order to
call the first found
+     * LogService.
+     * 
+     * @return <code>true</code> if an external log service was successfully
called, <code>false</code> otherwise.
+     */
+    private boolean invokeExternalLogService(Levels logLevel, String message, Throwable exception)
{
+        try {
+            ServiceReference[] refs = m_context.getAllServiceReferences(LogService.class.getName(),
null);
+            if (refs != null && refs.length > 0) {
+                // if we've found one (or more) we pick the first match
+                Object svc = m_context.getService(refs[0]);
+                if (svc != null) {
+                    try {
+                        Method m = svc.getClass().getMethod("log", Integer.TYPE, String.class,
Throwable.class);
+                        m.setAccessible(true); // Not entirely sure why this is needed for
a public method...
+                        m.invoke(svc, logLevel.getLogLevel(), message, exception);
+                        // Success!
+                        return true;
+                    }
+                    finally {
+                        // make sure we always unget our service reference
+                        m_context.ungetService(refs[0]);
+                    }
+                }
+            }
+        }
+        catch (Exception e) {
+            // there is a lot that can go wrong, but not much we can do at this point
+            // beyond logging the error message to our default logging implementation...
+            invokeInternalLogService(Levels.ERROR, "logging", "Failed to invoke external
LogService: " + e.getMessage(), e);
         }
+
+        return false;
+    }
+
+    private static Levels fromName(String name, Levels defaultLevel) {
         try {
-            return Levels.valueOf(name.toUpperCase().trim());
+            if (name != null) {
+                return Levels.valueOf(name.toUpperCase().trim());
+            }
         }
         catch (Exception e) {
-            return DEFAULT_LEVEL;
+            // Fall through...
         }
+        return defaultLevel;
     }
 }

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandlerTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandlerTest.java?rev=1582291&r1=1582290&r2=1582291&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandlerTest.java
(original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandlerTest.java
Thu Mar 27 13:13:45 2014
@@ -56,6 +56,7 @@ import org.apache.ace.agent.LoggingHandl
 import org.apache.ace.agent.RetryAfterException;
 import org.apache.ace.agent.testutil.BaseAgentTest;
 import org.apache.ace.agent.testutil.TestWebServer;
+import org.osgi.framework.BundleContext;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
@@ -122,9 +123,11 @@ public class DownloadHandlerTest extends
         m_agentContextImpl = mockAgentContext();
         m_agentContext = m_agentContextImpl;
 
-        m_agentContextImpl.setHandler(EventsHandler.class, new EventsHandlerImpl(mockBundleContext()));
+        BundleContext bc = mockBundleContext();
+        
+        m_agentContextImpl.setHandler(EventsHandler.class, new EventsHandlerImpl(bc));
         m_agentContextImpl.setHandler(ConnectionHandler.class, new ConnectionHandlerImpl());
-        m_agentContextImpl.setHandler(LoggingHandler.class, new LoggingHandlerImpl(Levels.DEBUG));
+        m_agentContextImpl.setHandler(LoggingHandler.class, new LoggingHandlerImpl(bc, Levels.DEBUG));
         m_agentContextImpl.setHandler(DownloadHandler.class, new DownloadHandlerImpl(dataLocation));
 
         m_agentContextImpl.start();



Mime
View raw message