aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1232387 - in /aries/trunk/util: util-r42/src/main/java/org/apache/aries/util/log/ util-r42/src/test/java/org/apache/aries/util/log/ util/
Date Tue, 17 Jan 2012 11:33:49 GMT
Author: davidb
Date: Tue Jan 17 11:33:48 2012
New Revision: 1232387

URL: http://svn.apache.org/viewvc?rev=1232387&view=rev
Log:
Logger: a utility class which makes it easier to work with the OSGi LogService. 
This class comes from aries-jmx. I've also added a test for it.

Added:
    aries/trunk/util/util-r42/src/main/java/org/apache/aries/util/log/
    aries/trunk/util/util-r42/src/main/java/org/apache/aries/util/log/Logger.java   (with
props)
    aries/trunk/util/util-r42/src/main/java/org/apache/aries/util/log/packageinfo
    aries/trunk/util/util-r42/src/test/java/org/apache/aries/util/log/
    aries/trunk/util/util-r42/src/test/java/org/apache/aries/util/log/LoggerTest.java   (with
props)
Modified:
    aries/trunk/util/util/pom.xml

Added: aries/trunk/util/util-r42/src/main/java/org/apache/aries/util/log/Logger.java
URL: http://svn.apache.org/viewvc/aries/trunk/util/util-r42/src/main/java/org/apache/aries/util/log/Logger.java?rev=1232387&view=auto
==============================================================================
--- aries/trunk/util/util-r42/src/main/java/org/apache/aries/util/log/Logger.java (added)
+++ aries/trunk/util/util-r42/src/main/java/org/apache/aries/util/log/Logger.java Tue Jan
17 11:33:48 2012
@@ -0,0 +1,85 @@
+/*
+ * 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.aries.util.log;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.log.LogService;
+import org.osgi.util.tracker.ServiceTracker;
+
+// Note this file originated in the JMX subproject.
+
+/**
+ * <p>This <tt>Logger</tt> class represents ServiceTracker for LogService.
+ * It provides a simple mechanism for interacting with the log service.
+ *
+ * @see org.osgi.service.log.LogService
+ * @see org.osgi.util.tracker.ServiceTracker
+ */
+public class Logger extends ServiceTracker implements LogService {
+    /**
+     * Constructs new Logger(ServiceTracker for LogService).
+     *
+     * @param context bundle context.
+     */
+    public Logger(BundleContext context) {
+        super(context, LogService.class.getName(), null);
+    }
+
+    /**
+     * @see org.osgi.service.log.LogService#log(int, java.lang.String)
+     */
+    public void log(int level, String message) {
+        LogService logService = (LogService) getService();
+        if (logService != null) {
+            logService.log(level, message);
+        }
+    }
+
+    /**
+     * @see org.osgi.service.log.LogService#log(int, java.lang.String, java.lang.Throwable)
+     */
+    public void log(int level, String message, Throwable exception) {
+        LogService logService = (LogService) getService();
+        if (logService != null) {
+            logService.log(level, message, exception);
+        }
+    }
+
+    /**
+     * @see org.osgi.service.log.LogService#log(org.osgi.framework.ServiceReference, int,
java.lang.String)
+     */
+    public void log(ServiceReference ref, int level, String message) {
+        LogService logService = (LogService) getService();
+        if (logService != null) {
+            logService.log(ref, level, message);
+        }
+    }
+
+    /**
+     * @see org.osgi.service.log.LogService#log(org.osgi.framework.ServiceReference, int,
java.lang.String,
+     *      java.lang.Throwable)
+     */
+    public void log(ServiceReference ref, int level, String message, Throwable exception)
{
+        LogService logService = (LogService) getService();
+        if (logService != null) {
+            logService.log(ref, level, message, exception);
+        }
+    }
+}

Propchange: aries/trunk/util/util-r42/src/main/java/org/apache/aries/util/log/Logger.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: aries/trunk/util/util-r42/src/main/java/org/apache/aries/util/log/packageinfo
URL: http://svn.apache.org/viewvc/aries/trunk/util/util-r42/src/main/java/org/apache/aries/util/log/packageinfo?rev=1232387&view=auto
==============================================================================
--- aries/trunk/util/util-r42/src/main/java/org/apache/aries/util/log/packageinfo (added)
+++ aries/trunk/util/util-r42/src/main/java/org/apache/aries/util/log/packageinfo Tue Jan
17 11:33:48 2012
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+version 0.1.0

Added: aries/trunk/util/util-r42/src/test/java/org/apache/aries/util/log/LoggerTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/util/util-r42/src/test/java/org/apache/aries/util/log/LoggerTest.java?rev=1232387&view=auto
==============================================================================
--- aries/trunk/util/util-r42/src/test/java/org/apache/aries/util/log/LoggerTest.java (added)
+++ aries/trunk/util/util-r42/src/test/java/org/apache/aries/util/log/LoggerTest.java Tue
Jan 17 11:33:48 2012
@@ -0,0 +1,189 @@
+/*
+ * 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.aries.util.log;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.aries.mocks.BundleContextMock;
+import org.apache.aries.unittest.mocks.Skeleton;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.log.LogService;
+
+public class LoggerTest {
+    private BundleContext ctx;
+
+    @Before
+    public void setup() {
+        ctx = Skeleton.newMock(new BundleContextMock(), BundleContext.class);
+    }
+
+    @After
+    public void teardown() {
+        BundleContextMock.clear();
+    }
+
+    @Test
+    public void testLogger() {
+        Logger logger = new Logger(ctx);
+        logger.open();
+
+        logger.log(LogService.LOG_INFO, "This message will disappear");
+
+        TestLogService tls = new TestLogService();
+        ctx.registerService(LogService.class.getName(), tls, null);
+
+        Assert.assertEquals("Precondition", 0, tls.logData.size());
+        logger.log(LogService.LOG_INFO, "Hi");
+
+        Assert.assertEquals(1, tls.logData.size());
+        LogData ld = tls.logData.get(0);
+        Assert.assertEquals(LogService.LOG_INFO, ld.level);
+        Assert.assertEquals("Hi", ld.message);
+
+        logger.close();
+
+        logger.log(LogService.LOG_INFO, "This message will disappear too");
+        Assert.assertEquals("The logger was closed so log messages shouldn't appear in the
LogService any more",
+                1, tls.logData.size());
+    }
+
+    @Test
+    public void testLogger2() {
+        Logger logger = new Logger(ctx);
+        logger.open();
+        TestLogService tls = new TestLogService();
+        ctx.registerService(LogService.class.getName(), tls, null);
+
+        Assert.assertEquals("Precondition", 0, tls.logData.size());
+        Throwable th = new Throwable();
+        logger.log(LogService.LOG_INFO, "Hi", th);
+
+        Assert.assertEquals(1, tls.logData.size());
+        LogData ld = tls.logData.get(0);
+        Assert.assertEquals(LogService.LOG_INFO, ld.level);
+        Assert.assertEquals("Hi", ld.message);
+        Assert.assertEquals(th, ld.throwable);
+    }
+
+    @Test
+    public void testLogger3() {
+        Logger logger = new Logger(ctx);
+        logger.open();
+        TestLogService tls = new TestLogService();
+        ctx.registerService(LogService.class.getName(), tls, null);
+
+        Assert.assertEquals("Precondition", 0, tls.logData.size());
+        ServiceReference sr = new MockServiceReference();
+        logger.log(sr, LogService.LOG_INFO, "Hi");
+
+        Assert.assertEquals(1, tls.logData.size());
+        LogData ld = tls.logData.get(0);
+        Assert.assertSame(sr, ld.serviceReference);
+        Assert.assertEquals(LogService.LOG_INFO, ld.level);
+        Assert.assertEquals("Hi", ld.message);
+    }
+
+    @Test
+    public void testLogger4() {
+        Logger logger = new Logger(ctx);
+        logger.open();
+        TestLogService tls = new TestLogService();
+        ctx.registerService(LogService.class.getName(), tls, null);
+
+        Assert.assertEquals("Precondition", 0, tls.logData.size());
+        ServiceReference sr = new MockServiceReference();
+        Throwable th = new Throwable();
+        logger.log(sr, LogService.LOG_INFO, "Hi", th);
+
+        Assert.assertEquals(1, tls.logData.size());
+        LogData ld = tls.logData.get(0);
+        Assert.assertSame(sr, ld.serviceReference);
+        Assert.assertEquals(LogService.LOG_INFO, ld.level);
+        Assert.assertEquals("Hi", ld.message);
+        Assert.assertEquals(th, ld.throwable);
+    }
+
+    private class TestLogService implements LogService {
+        List<LogData> logData = Collections.synchronizedList(new ArrayList<LogData>());
+
+        public void log(int level, String message) {
+            logData.add(new LogData(null, level, message, null));
+        }
+
+        public void log(int level, String message, Throwable exception) {
+            logData.add(new LogData(null, level, message, exception));
+        }
+
+        public void log(ServiceReference sr, int level, String message) {
+            logData.add(new LogData(sr, level, message, null));
+        }
+
+        public void log(ServiceReference sr, int level, String message, Throwable exception)
{
+            logData.add(new LogData(sr, level, message, exception));
+        }
+    }
+
+    private static class LogData {
+        private final ServiceReference serviceReference;
+        private final int level;
+        private final String message;
+        private final Throwable throwable;
+
+        private LogData(ServiceReference sr, int level, String message, Throwable th) {
+            this.serviceReference = sr;
+            this.level = level;
+            this.message = message;
+            this.throwable = th;
+        }
+    }
+
+    private static class MockServiceReference implements ServiceReference {
+        public Object getProperty(String key) {
+            return null;
+        }
+
+        public String[] getPropertyKeys() {
+            return null;
+        }
+
+        public Bundle getBundle() {
+            return null;
+        }
+
+        public Bundle[] getUsingBundles() {
+            return null;
+        }
+
+        public boolean isAssignableTo(Bundle bundle, String className) {
+            return false;
+        }
+
+        public int compareTo(Object reference) {
+            return 0;
+        }
+    }
+}

Propchange: aries/trunk/util/util-r42/src/test/java/org/apache/aries/util/log/LoggerTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: aries/trunk/util/util/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/util/util/pom.xml?rev=1232387&r1=1232386&r2=1232387&view=diff
==============================================================================
--- aries/trunk/util/util/pom.xml (original)
+++ aries/trunk/util/util/pom.xml Tue Jan 17 11:33:48 2012
@@ -40,6 +40,7 @@
 
         <aries.osgi.export.pkg>
             org.apache.aries.util;
+            org.apache.aries.util.log;
             org.apache.aries.util.tracker;
             org.apache.aries.util.nls;
             org.apache.aries.util.io;
@@ -56,6 +57,7 @@
             org.eclipse.osgi.internal.loader;resolution:=optional,
             org.eclipse.osgi.framework.internal.core;resolution:=optional,
             org.eclipse.osgi.framework.adaptor;resolution:=optional,
+            org.osgi.service.log;resolution:=optional,
             *
         </aries.osgi.import.pkg>
         <aries.osgi.private.pkg>



Mime
View raw message