]> git.basschouten.com Git - openhab-addons.git/blob
51bc153d4c3681c608deff20d670e3b9af64120b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.logreader.internal.filereader;
14
15 import java.util.List;
16 import java.util.Objects;
17 import java.util.concurrent.CopyOnWriteArrayList;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.logreader.internal.filereader.api.FileReaderListener;
21 import org.openhab.binding.logreader.internal.filereader.api.LogFileReader;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Base class for LogFileReader implementations. Implements base functions which are same for all LogFileReaders.
27  *
28  * @author Pauli Anttila - Initial contribution
29  */
30 @NonNullByDefault
31 public abstract class AbstractLogFileReader implements LogFileReader {
32     private final Logger logger = LoggerFactory.getLogger(AbstractLogFileReader.class);
33
34     private List<FileReaderListener> fileReaderListeners = new CopyOnWriteArrayList<>();
35
36     @Override
37     public boolean registerListener(FileReaderListener fileReaderListener) {
38         Objects.requireNonNull(fileReaderListener, "It's not allowed to pass a null FileReaderListener.");
39         return fileReaderListeners.contains(fileReaderListener) ? false : fileReaderListeners.add(fileReaderListener);
40     }
41
42     @Override
43     public boolean unregisterListener(FileReaderListener fileReaderListener) {
44         Objects.requireNonNull(fileReaderListener, "It's not allowed to pass a null FileReaderListener.");
45         return fileReaderListeners.remove(fileReaderListener);
46     }
47
48     /**
49      * Send file not found event to all registered listeners.
50      *
51      */
52     public void sendFileNotFoundToListeners() {
53         for (FileReaderListener fileReaderListener : fileReaderListeners) {
54             try {
55                 fileReaderListener.fileNotFound();
56             } catch (Exception e) {
57                 // catch all exceptions give all handlers a fair chance of handling the messages
58                 logger.debug("An exception occurred while calling the FileReaderListener. ", e);
59             }
60         }
61     }
62
63     /**
64      * Send read log line to all registered listeners.
65      *
66      */
67     public void sendLineToListeners(String line) {
68         for (FileReaderListener fileReaderListener : fileReaderListeners) {
69             try {
70                 fileReaderListener.handle(line);
71             } catch (Exception e) {
72                 // catch all exceptions give all handlers a fair chance of handling the messages
73                 logger.debug("An exception occurred while calling the FileReaderListener. ", e);
74             }
75         }
76     }
77
78     /**
79      * Send file rotation event to all registered listeners.
80      *
81      */
82     public void sendFileRotationToListeners() {
83         for (FileReaderListener fileReaderListener : fileReaderListeners) {
84             try {
85                 fileReaderListener.fileRotated();
86             } catch (Exception e) {
87                 // catch all exceptions give all handlers a fair chance of handling the messages
88                 logger.debug("An exception occurred while calling the FileReaderListener. ", e);
89             }
90         }
91     }
92
93     /**
94      * Send exception event to all registered listeners.
95      *
96      */
97     public void sendExceptionToListeners(Exception e) {
98         for (FileReaderListener fileReaderListener : fileReaderListeners) {
99             try {
100                 fileReaderListener.handle(e);
101             } catch (Exception ex) {
102                 // catch all exceptions give all handlers a fair chance of handling the messages
103                 logger.debug("An exception occurred while calling the FileReaderListener. ", ex);
104             }
105         }
106     }
107 }