Added: felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogNode.java URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogNode.java?rev=743848&view=auto ============================================================================== --- felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogNode.java (added) +++ felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogNode.java Thu Feb 12 19:36:18 2009 @@ -0,0 +1,83 @@ +/* + * 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.felix.log; + +import org.osgi.service.log.LogEntry; + +/** + * The class used as a doubly linked list node in the log. + */ +final class LogNode { + /** The previous node. */ + private LogNode m_previous; + + /** The next node. */ + private LogNode m_next; + + /** The log entry. */ + private final LogEntry m_entry; + + /** + * Create a new instance. + * @param entry the log entry. + */ + LogNode(final LogEntry entry) { + m_entry = entry; + } + + /** + * Returns the associated entry. + * @return the associated entry + */ + LogEntry getEntry() { + return m_entry; + } + + /** + * Get the next node. + * @return the next node + */ + LogNode getNextNode() { + return m_next; + } + + /** + * Set the next node. + * @param next the next node + */ + void setNextNode(final LogNode next) { + m_next = next; + } + + /** + * Get the previous node. + * @return the previous node + */ + LogNode getPreviousNode() { + return m_previous; + } + + /** + * Set the previous node. + * @param previous the previous node + */ + void setPreviousNode(final LogNode previous) { + m_previous = previous; + } +}
Added: felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogNodeEnumeration.java URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogNodeEnumeration.java?rev=743848&view=auto ============================================================================== --- felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogNodeEnumeration.java (added) +++ felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogNodeEnumeration.java Thu Feb 12 19:36:18 2009 @@ -0,0 +1,71 @@ +/* + * 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.felix.log; + +import java.util.Enumeration; + +import org.osgi.service.log.LogEntry; + +/** + * Implementation of the {...@link Enumeration} interface for a linked list of + * {...@link LogNode} entries. + */ +final class LogNodeEnumeration implements Enumeration { + /** The next node. */ + private LogNode m_next; + + /** The last node. */ + private final LogNode m_last; + + /** + * Creates a new instance. + * @param start the first node to return + * @param end the last node to return + */ + LogNodeEnumeration(final LogNode start, final LogNode end) { + m_next = start; + m_last = end; + } + + /** + * Determines whether there are any more elements to return. + * @return <code>true</code> if there are more elements; <code>false</code> otherwise + */ + public boolean hasMoreElements() { + return m_next != null; + } + + /** + * Returns the current element and moves onto the next element. + * @return the current element + */ + public Object nextElement() { + LogEntry result = null; + + if (m_next == m_last) { + result = m_next.getEntry(); + m_next = null; + } else if (m_next != null) { + result = m_next.getEntry(); + m_next = m_next.getNextNode(); + } + + return result; + } +} Added: felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogReaderServiceFactory.java URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogReaderServiceFactory.java?rev=743848&view=auto ============================================================================== --- felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogReaderServiceFactory.java (added) +++ felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogReaderServiceFactory.java Thu Feb 12 19:36:18 2009 @@ -0,0 +1,64 @@ +/* + * 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.felix.log; + +import org.osgi.framework.Bundle; +import org.osgi.framework.ServiceFactory; +import org.osgi.framework.ServiceRegistration; + +/** + * {...@link ServiceFactory} implementation for {...@link LogReaderService}. Associates + * an individual {...@link LogReaderService} with a {...@link Bundle}. + */ +final class LogReaderServiceFactory implements ServiceFactory { + /** The log to associate the service implementations with. */ + private final Log m_log; + + /** + * Create a new instance. + * @param log the log to associate the service implementations with., + */ + LogReaderServiceFactory(final Log log) { + m_log = log; + } + + /** + * Get the service to use for the specified bundle. + * @param bundle the bundle requesting the service + * @param registration the service registration + * @return the log reader service implementation for the specified bundle + */ + public Object getService(final Bundle bundle, + final ServiceRegistration registration) { + return new LogReaderServiceImpl(m_log); + } + + /** + * Release the service previously obtained through + * {...@link #getService(Bundle, ServiceRegistration)}. + * @param bundle the bundle that originally requested the service + * @param registration the service registration + * @param service the service to release + */ + public void ungetService(final Bundle bundle, + final ServiceRegistration registration, + final Object service) { + ((LogReaderServiceImpl) service).removeAllLogListeners(); + } +} Added: felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogReaderServiceImpl.java URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogReaderServiceImpl.java?rev=743848&view=auto ============================================================================== --- felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogReaderServiceImpl.java (added) +++ felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogReaderServiceImpl.java Thu Feb 12 19:36:18 2009 @@ -0,0 +1,102 @@ +/* + * 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.felix.log; + +import java.util.Enumeration; +import java.util.Iterator; +import java.util.List; +import java.util.Vector; + +import org.osgi.service.log.LogListener; +import org.osgi.service.log.LogReaderService; + +/** + * Implementation of the OSGi {...@link LogReaderService} interface. See section 101 + * of the OSGi service compendium. + * <p> + * The {...@link LogReaderService} maintains a list of {...@link org.osgi.service.log.LogEntry} + * objects called the <i>log</i>. The {...@link LogReaderService} is a service that bundle + * developers can use to retrieve information contained in this log, and receive + * notifications about {...@link org.osgi.service.log.LogEntry} objects when they are created + * through the {...@link org.osgi.service.log.LogService}. + */ +final class LogReaderServiceImpl implements LogReaderService { + + /** The log implementation. */ + private final Log m_log; + + /** The listeners associated with this service. */ + private final List m_listeners = new Vector(); + + /** + * Create a new instance. + * @param log the log implementation + */ + LogReaderServiceImpl(final Log log) { + this.m_log = log; + } + + /** + * This method is used to subscribe to the Log Reader Service in order to receive + * log messages as they occur. Unlike the previously recorded log entries, all + * log messages must be sent to subscribers of the Log Reader Service as they are + * recorded. + * <p> + * A subscriber to the Log Reader Service must implement the {...@link LogListener} + * interface. + * <p> + * After a subscription of the Log Reader Service has been started, the subscriber's + * {...@link LogListener#logged(LogEntry)} method must be called with a {...@link LogEntry} + * object for the message each time a message is logged. + * @param listener the listener object to subscribe + */ + public synchronized void addLogListener(final LogListener listener) { + m_listeners.add(listener); + m_log.addListener(listener); + } + + /** + * This method is used to unsubscribe from the Log Reader Service. + * @param listener the listener object to unsubscribe + */ + public synchronized void removeLogListener(final LogListener listener) { + m_listeners.remove(listener); + m_log.removeListener(listener); + } + + /** + * This method retrieves past log entries as an enumeration with the most recent + * entry first. + * @return an enumeration of the {...@link LogEntry} objects that have been stored + */ + public Enumeration getLog() { + return m_log.getEntries(); + } + + /** + * Remove all log listeners registered through this service. + */ + synchronized void removeAllLogListeners() { + Iterator listenerIt = m_listeners.iterator(); + while (listenerIt.hasNext()) { + LogListener listener = (LogListener) listenerIt.next(); + m_log.removeListener(listener); + } + } +} Added: felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogServiceFactory.java URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogServiceFactory.java?rev=743848&view=auto ============================================================================== --- felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogServiceFactory.java (added) +++ felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogServiceFactory.java Thu Feb 12 19:36:18 2009 @@ -0,0 +1,64 @@ +/* + * 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.felix.log; + +import org.osgi.framework.Bundle; +import org.osgi.framework.ServiceFactory; +import org.osgi.framework.ServiceRegistration; + +/** + * {...@link ServiceFactory} implementation for {...@link LogService}. Associates + * an individual {...@link LogService} with a {...@link Bundle}. + */ +final class LogServiceFactory implements ServiceFactory { + /** The log to associate the service implementations with. */ + private final Log m_log; + + /** + * Create a new instance. + * @param log the log to associate the service implementations with., + */ + LogServiceFactory(final Log log) { + m_log = log; + } + + /** + * Get the service to use for the specified bundle. + * @param bundle the bundle requesting the service + * @param registration the service registration + * @return the log service implementation for the specified bundle + */ + public Object getService(final Bundle bundle, + final ServiceRegistration registration) { + return new LogServiceImpl(m_log, bundle); + } + + /** + * Release the service previously obtained through + * {...@link #getService(Bundle, ServiceRegistration)}. + * @param bundle the bundle that originally requested the service + * @param registration the service registration + * @param service the service to release + */ + public void ungetService(final Bundle bundle, + final ServiceRegistration registration, + final Object service) { + // do nothing + } +} Added: felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogServiceImpl.java URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogServiceImpl.java?rev=743848&view=auto ============================================================================== --- felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogServiceImpl.java (added) +++ felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/org/apache/felix/log/LogServiceImpl.java Thu Feb 12 19:36:18 2009 @@ -0,0 +1,99 @@ +/* + * 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.felix.log; + +import org.osgi.framework.Bundle; +import org.osgi.framework.ServiceReference; +import org.osgi.service.log.LogService; + +/** + * Implementation of the OSGi {...@link LogService}. + */ +final class LogServiceImpl implements LogService { + + /** The log implementation. */ + private final Log m_log; + + /** The bundle associated with this implementation. */ + private final Bundle m_bundle; + + /** + * Create a new instance. + * @param log the log implementation + * @param bundle the bundle associated with this implementation + */ + LogServiceImpl(final Log log, final Bundle bundle) { + this.m_log = log; + this.m_bundle = bundle; + } + + /** + * Log the specified message at the specified level. + * @param level the level to log the message at + * @param message the message to log + */ + public void log(final int level, final String message) { + log(null, level, message, null); + } + + /** + * Log the specified message along with the specified exception at the + * specified level. + * @param level the level to log the message and exception at + * @param message the message to log + * @param exception the exception to log + */ + public void log(final int level, + final String message, + final Throwable exception) { + log(null, level, message, exception); + } + + /** + * Log the specified message along with the speicified service reference + * at the specified level. + * @param sr the service reference of the service that produced the message + * @param level the level to log the message at + * @param message the message to log + */ + public void log(final ServiceReference sr, + final int level, + final String message) { + log(sr, level, message, null); + } + + /** + * Log the specified message along with the specified exception and + * service reference at the specified level. + * @param sr the service reference of the service that produced the message + * @param level the level to log the message at + * @param message the message to log + * @param exception the exception to log + */ + public void log(final ServiceReference sr, + final int level, + final String message, + final Throwable exception) { + m_log.addEntry(new LogEntryImpl((sr != null) ? sr.getBundle() : m_bundle, + sr, + level, + message, + exception)); + } +} Added: felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/test/TestLog.java URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/test/TestLog.java?rev=743848&view=auto ============================================================================== --- felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/test/TestLog.java (added) +++ felix/sandbox/rickhall/bnd-test/org.apache.felix.log/src/test/TestLog.java Thu Feb 12 19:36:18 2009 @@ -0,0 +1,101 @@ +/* + * 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 test; + +import java.util.Enumeration; +import org.osgi.framework.*; +import org.osgi.service.log.*; +import junit.framework.TestCase; + +public class TestLog extends TestCase +{ + private volatile BundleContext m_context; + + public void setBundleContext(BundleContext context) + { + m_context = context; + } + + public void testLog() throws Exception + { + final String msg = "This is a test!"; + try + { + ServiceReference ref; + + // Get LogReaderService. + ref = m_context.getServiceReference( + LogReaderService.class.getName()); + assertNotNull(ref); + LogReaderService reader = (LogReaderService) m_context.getService(ref); + assertNotNull(reader); + TestLogListener listener = new TestLogListener(); + reader.addLogListener(listener); + + // Get LogService. + ref = m_context.getServiceReference( + LogService.class.getName()); + assertNotNull(ref); + LogService log = (LogService) m_context.getService(ref); + assertNotNull(log); + + // Log a message and test whether it is recorded in the + // log and received by the log listener. + log.log(LogService.LOG_INFO, msg); + Thread.sleep(500); + LogEntry entry = listener.getEntry(); + assertEquals(LogService.LOG_INFO, entry.getLevel()); + assertEquals(msg, entry.getMessage()); + assertNull(entry.getException()); + Enumeration e = reader.getLog(); + assertNotNull(e); + entry = (LogEntry) e.nextElement(); + assertEquals(LogService.LOG_INFO, entry.getLevel()); + assertEquals(msg, entry.getMessage()); + assertNull(entry.getException()); + + // Remove log listener and make sure we no longer are + // notified. + reader.removeLogListener(listener); + log.log(LogService.LOG_INFO, msg); + Thread.sleep(500); + assertNull(listener.getEntry()); + } + finally + { + } + } + + static class TestLogListener implements LogListener + { + private volatile LogEntry m_entry = null; + + public void logged(LogEntry entry) + { + m_entry = entry; + } + + public LogEntry getEntry() + { + LogEntry entry = m_entry; + m_entry = null; + return entry; + } + } +} Added: felix/sandbox/rickhall/bnd-test/pom.xml URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/bnd-test/pom.xml?rev=743848&view=auto ============================================================================== --- felix/sandbox/rickhall/bnd-test/pom.xml (added) +++ felix/sandbox/rickhall/bnd-test/pom.xml Thu Feb 12 19:36:18 2009 @@ -0,0 +1,83 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <parent> + <groupId>org.apache</groupId> + <artifactId>apache</artifactId> + <version>4</version> + </parent> + + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.felix</groupId> + <artifactId>build.dependencies</artifactId> + <version>1.0</version> + + <name>Apache Felix Build Dependencies</name> + + <repositories> + <repository> + <id>aQute</id> + <url>http://www.aqute.biz/repo</url> + </repository> + </repositories> + + <dependencies> + <dependency> + <groupId>biz.aQute</groupId> + <artifactId>bnd</artifactId> + <version>0.0.311</version> + </dependency> + <dependency> + <groupId>${pom.groupId}</groupId> + <artifactId>org.apache.felix.framework</artifactId> + <version>1.5.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>${pom.groupId}</groupId> + <artifactId>org.osgi.core</artifactId> + <version>1.2.0</version> + </dependency> + <dependency> + <groupId>${pom.groupId}</groupId> + <artifactId>org.osgi.foundation</artifactId> + <version>1.2.0</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>3.8</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <executions> + <execution> + <id>copy</id> + <phase>install</phase> + <goals> + <goal>copy</goal> + </goals> + <configuration> + <artifactItems> + <artifactItem> + <groupId>biz.aQute</groupId> + <artifactId>bnd</artifactId> + <version>0.0.311</version> + <type>jar</type> + <overWrite>true</overWrite> + <outputDirectory>${basedir}/cnf/repo/biz.aQute.bnd/</outputDirectory> + <destFileName>biz.aQute.bnd-latest.jar</destFileName> + </artifactItem> + </artifactItems> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + +</project>
