Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 1 | /* |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Richard S. Hall | 44002cf | 2009-02-11 21:47:26 +0000 | [diff] [blame] | 19 | package org.apache.felix.log; |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 20 | |
| 21 | import java.util.Enumeration; |
| 22 | import java.util.Iterator; |
| 23 | import java.util.List; |
| 24 | import java.util.Vector; |
| 25 | |
| 26 | import org.osgi.service.log.LogListener; |
| 27 | import org.osgi.service.log.LogReaderService; |
| 28 | |
| 29 | /** |
| 30 | * Implementation of the OSGi {@link LogReaderService} interface. See section 101 |
| 31 | * of the OSGi service compendium. |
| 32 | * <p> |
| 33 | * The {@link LogReaderService} maintains a list of {@link org.osgi.service.log.LogEntry} |
| 34 | * objects called the <i>log</i>. The {@link LogReaderService} is a service that bundle |
| 35 | * developers can use to retrieve information contained in this log, and receive |
| 36 | * notifications about {@link org.osgi.service.log.LogEntry} objects when they are created |
| 37 | * through the {@link org.osgi.service.log.LogService}. |
| 38 | */ |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 39 | final class LogReaderServiceImpl implements LogReaderService |
| 40 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 41 | /** The log implementation. */ |
| 42 | private final Log m_log; |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 43 | /** The listeners associated with this service. */ |
| 44 | private final List m_listeners = new Vector(); |
| 45 | |
| 46 | /** |
| 47 | * Create a new instance. |
| 48 | * @param log the log implementation |
| 49 | */ |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 50 | LogReaderServiceImpl(final Log log) |
| 51 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 52 | this.m_log = log; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * This method is used to subscribe to the Log Reader Service in order to receive |
| 57 | * log messages as they occur. Unlike the previously recorded log entries, all |
| 58 | * log messages must be sent to subscribers of the Log Reader Service as they are |
| 59 | * recorded. |
| 60 | * <p> |
| 61 | * A subscriber to the Log Reader Service must implement the {@link LogListener} |
| 62 | * interface. |
| 63 | * <p> |
| 64 | * After a subscription of the Log Reader Service has been started, the subscriber's |
| 65 | * {@link LogListener#logged(LogEntry)} method must be called with a {@link LogEntry} |
| 66 | * object for the message each time a message is logged. |
| 67 | * @param listener the listener object to subscribe |
| 68 | */ |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 69 | public synchronized void addLogListener(final LogListener listener) |
| 70 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 71 | m_listeners.add(listener); |
| 72 | m_log.addListener(listener); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * This method is used to unsubscribe from the Log Reader Service. |
| 77 | * @param listener the listener object to unsubscribe |
| 78 | */ |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 79 | public synchronized void removeLogListener(final LogListener listener) |
| 80 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 81 | m_listeners.remove(listener); |
| 82 | m_log.removeListener(listener); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * This method retrieves past log entries as an enumeration with the most recent |
| 87 | * entry first. |
| 88 | * @return an enumeration of the {@link LogEntry} objects that have been stored |
| 89 | */ |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 90 | public Enumeration getLog() |
| 91 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 92 | return m_log.getEntries(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Remove all log listeners registered through this service. |
| 97 | */ |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 98 | synchronized void removeAllLogListeners() |
| 99 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 100 | Iterator listenerIt = m_listeners.iterator(); |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 101 | while (listenerIt.hasNext()) |
| 102 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 103 | LogListener listener = (LogListener) listenerIt.next(); |
| 104 | m_log.removeListener(listener); |
| 105 | } |
| 106 | } |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 107 | } |