This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-logservice.git
commit 8968823d00f89df724374de5874a4d8a7b42a70c Author: Carsten Ziegeler <[email protected]> AuthorDate: Wed Apr 8 09:54:32 2015 +0000 SLING-4563 : Log start level number on STARTLEVEL CHANGED event git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1672051 13f79535-47bb-0310-9956-ffa450edef68 --- .../sling/commons/logservice/internal/Activator.java | 13 ++++++++++++- .../sling/commons/logservice/internal/LogSupport.java | 16 +++++++--------- .../commons/logservice/internal/LogSupportTest.java | 10 +++++++--- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/apache/sling/commons/logservice/internal/Activator.java b/src/main/java/org/apache/sling/commons/logservice/internal/Activator.java index 60766b5..60a4755 100644 --- a/src/main/java/org/apache/sling/commons/logservice/internal/Activator.java +++ b/src/main/java/org/apache/sling/commons/logservice/internal/Activator.java @@ -22,8 +22,10 @@ import java.util.Hashtable; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; +import org.osgi.framework.ServiceReference; import org.osgi.service.log.LogReaderService; import org.osgi.service.log.LogService; +import org.osgi.service.startlevel.StartLevel; /** * The <code>Activator</code> class is the <code>BundleActivator</code> for the @@ -36,11 +38,17 @@ public class Activator implements BundleActivator { private LogSupport logSupport; + /** Reference to the start level service. */ + private ServiceReference startLevelRef; + /** * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ public void start(final BundleContext context) throws Exception { - logSupport = new LogSupport(); + // get start level service, it's always there (required by the spec) + startLevelRef = context.getServiceReference(StartLevel.class.getName()); + + logSupport = new LogSupport((StartLevel)context.getService(startLevelRef)); context.addBundleListener(logSupport); context.addFrameworkListener(logSupport); context.addServiceListener(logSupport); @@ -66,6 +74,9 @@ public class Activator implements BundleActivator { * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ public void stop(final BundleContext context) throws Exception { + if ( startLevelRef != null ) { + context.ungetService(startLevelRef); + } if (logSupport != null) { context.removeBundleListener(logSupport); context.removeFrameworkListener(logSupport); diff --git a/src/main/java/org/apache/sling/commons/logservice/internal/LogSupport.java b/src/main/java/org/apache/sling/commons/logservice/internal/LogSupport.java index 126e654..00be9c3 100644 --- a/src/main/java/org/apache/sling/commons/logservice/internal/LogSupport.java +++ b/src/main/java/org/apache/sling/commons/logservice/internal/LogSupport.java @@ -70,7 +70,7 @@ public class LogSupport implements SynchronousBundleListener, ServiceListener, // The loggers by bundle id used for logging messages originated from // specific bundles @SuppressWarnings("serial") - private Map<Long, Logger> loggers = new LinkedHashMap<Long, Logger>(16, + private final Map<Long, Logger> loggers = new LinkedHashMap<Long, Logger>(16, 0.75f, true) { private static final int MAX_SIZE = 50; @@ -81,11 +81,14 @@ public class LogSupport implements SynchronousBundleListener, ServiceListener, }; // the worker thread actually sending LogEvents to LogListeners - private LogEntryDispatcher logEntryDispatcher; + private final LogEntryDispatcher logEntryDispatcher; - /* package */LogSupport() { + private final StartLevel startLevelService; + + /* package */LogSupport(final StartLevel startLevelService) { logEntryDispatcher = new LogEntryDispatcher(this); logEntryDispatcher.start(); + this.startLevelService = startLevelService; } /* package */void shutdown() { @@ -348,12 +351,7 @@ public class LogSupport implements SynchronousBundleListener, ServiceListener, message = "FrameworkEvent PACKAGES REFRESHED"; break; case FrameworkEvent.STARTLEVEL_CHANGED: - // bundle must be the system bundle - final Bundle bundle = event.getBundle(); - // StartLevel service is always there - final ServiceReference slRef = bundle.getBundleContext().getServiceReference(StartLevel.class.getName()); - final StartLevel sl = (StartLevel) bundle.getBundleContext().getService(slRef); - message = "FrameworkEvent STARTLEVEL CHANGED to " + sl.getStartLevel(); + message = "FrameworkEvent STARTLEVEL CHANGED to " + this.startLevelService.getStartLevel(); break; case FrameworkEvent.WARNING: message = "FrameworkEvent WARNING"; diff --git a/src/test/java/org/apache/sling/commons/logservice/internal/LogSupportTest.java b/src/test/java/org/apache/sling/commons/logservice/internal/LogSupportTest.java index 8c19d32..269ff49 100644 --- a/src/test/java/org/apache/sling/commons/logservice/internal/LogSupportTest.java +++ b/src/test/java/org/apache/sling/commons/logservice/internal/LogSupportTest.java @@ -32,12 +32,14 @@ import org.osgi.framework.ServiceEvent; import org.osgi.framework.ServiceReference; import org.osgi.service.log.LogEntry; import org.osgi.service.log.LogService; +import org.osgi.service.startlevel.StartLevel; import org.slf4j.Logger; public class LogSupportTest { @Test @SuppressWarnings("unchecked") public void testServiceEvent() throws Exception { - LogSupport ls = new LogSupport(); + StartLevel startLevel = Mockito.mock(StartLevel.class); + LogSupport ls = new LogSupport(startLevel); Field lf = LogSupport.class.getDeclaredField("loggers"); lf.setAccessible(true); Map<Long, Logger> loggers = (Map<Long, Logger>) lf.get(ls); @@ -77,7 +79,8 @@ public class LogSupportTest { ServiceReference sr = Mockito.mock(ServiceReference.class); LogEntry le = new LogEntryImpl(b, sr, LogService.LOG_DEBUG, "test", null); - LogSupport ls = new LogSupport(); + StartLevel startLevel = Mockito.mock(StartLevel.class); + LogSupport ls = new LogSupport(startLevel); Field lf = LogSupport.class.getDeclaredField("loggers"); lf.setAccessible(true); Map<Long, Logger> loggers = (Map<Long, Logger>) lf.get(ls); @@ -107,7 +110,8 @@ public class LogSupportTest { Exception e = new Exception(); LogEntry le = new LogEntryImpl(b, null, LogService.LOG_ERROR, "my-error-msg", e); - LogSupport ls = new LogSupport(); + StartLevel startLevel = Mockito.mock(StartLevel.class); + LogSupport ls = new LogSupport(startLevel); Field lf = LogSupport.class.getDeclaredField("loggers"); lf.setAccessible(true); Map<Long, Logger> loggers = (Map<Long, Logger>) lf.get(ls); -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
