blob: 7efcb12bfde570cd8a7f4d02432ec3c5cf8085ec [file] [log] [blame]
Richard S. Hallddf2e142009-09-30 17:03:45 +00001/*
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +00002 * 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. Hall44002cf2009-02-11 21:47:26 +000019package org.apache.felix.log;
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000020
21import java.util.Enumeration;
22import java.util.Iterator;
23import java.util.List;
24import java.util.Vector;
25
26import org.osgi.service.log.LogListener;
27import 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. Hallddf2e142009-09-30 17:03:45 +000039final class LogReaderServiceImpl implements LogReaderService
40{
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000041 /** The log implementation. */
42 private final Log m_log;
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000043 /** 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. Hallddf2e142009-09-30 17:03:45 +000050 LogReaderServiceImpl(final Log log)
51 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000052 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. Hallddf2e142009-09-30 17:03:45 +000069 public synchronized void addLogListener(final LogListener listener)
70 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000071 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. Hallddf2e142009-09-30 17:03:45 +000079 public synchronized void removeLogListener(final LogListener listener)
80 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000081 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. Hallddf2e142009-09-30 17:03:45 +000090 public Enumeration getLog()
91 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000092 return m_log.getEntries();
93 }
94
95 /**
96 * Remove all log listeners registered through this service.
97 */
Richard S. Hallddf2e142009-09-30 17:03:45 +000098 synchronized void removeAllLogListeners()
99 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +0000100 Iterator listenerIt = m_listeners.iterator();
Richard S. Hallddf2e142009-09-30 17:03:45 +0000101 while (listenerIt.hasNext())
102 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +0000103 LogListener listener = (LogListener) listenerIt.next();
104 m_log.removeListener(listener);
105 }
106 }
Richard S. Hallddf2e142009-09-30 17:03:45 +0000107}