]> git.basschouten.com Git - openhab-addons.git/blob
09b697d961e8fe36a87f5c68309bba1637192063
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.openhab.binding.logreader.internal.filereader.api.FileReaderListener;
20 import org.openhab.binding.logreader.internal.filereader.api.LogFileReader;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Base class for LogFileReader implementations. Implements base functions which are same for all LogFileReaders.
26  *
27  * @author Pauli Anttila - Initial contribution
28  */
29 public abstract class AbstractLogFileReader implements LogFileReader {
30     private final Logger logger = LoggerFactory.getLogger(AbstractLogFileReader.class);
31
32     private List<FileReaderListener> fileReaderListeners = new CopyOnWriteArrayList<>();
33
34     @Override
35     public boolean registerListener(FileReaderListener fileReaderListener) {
36         Objects.requireNonNull(fileReaderListener, "It's not allowed to pass a null FileReaderListener.");
37         return fileReaderListeners.contains(fileReaderListener) ? false : fileReaderListeners.add(fileReaderListener);
38     }
39
40     @Override
41     public boolean unregisterListener(FileReaderListener fileReaderListener) {
42         Objects.requireNonNull(fileReaderListener, "It's not allowed to pass a null FileReaderListener.");
43         return fileReaderListeners.remove(fileReaderListener);
44     }
45
46     /**
47      * Send file not found event to all registered listeners.
48      *
49      */
50     public void sendFileNotFoundToListeners() {
51         for (FileReaderListener fileReaderListener : fileReaderListeners) {
52             try {
53                 fileReaderListener.fileNotFound();
54             } catch (Exception e) {
55                 // catch all exceptions give all handlers a fair chance of handling the messages
56                 logger.debug("An exception occurred while calling the FileReaderListener. ", e);
57             }
58         }
59     }
60
61     /**
62      * Send read log line to all registered listeners.
63      *
64      */
65     public void sendLineToListeners(String line) {
66         for (FileReaderListener fileReaderListener : fileReaderListeners) {
67             try {
68                 fileReaderListener.handle(line);
69             } catch (Exception e) {
70                 // catch all exceptions give all handlers a fair chance of handling the messages
71                 logger.debug("An exception occurred while calling the FileReaderListener. ", e);
72             }
73         }
74     }
75
76     /**
77      * Send file rotation event to all registered listeners.
78      *
79      */
80     public void sendFileRotationToListeners() {
81         for (FileReaderListener fileReaderListener : fileReaderListeners) {
82             try {
83                 fileReaderListener.fileRotated();
84             } catch (Exception e) {
85                 // catch all exceptions give all handlers a fair chance of handling the messages
86                 logger.debug("An exception occurred while calling the FileReaderListener. ", e);
87             }
88         }
89     }
90
91     /**
92      * Send exception event to all registered listeners.
93      *
94      */
95     public void sendExceptionToListeners(Exception e) {
96         for (FileReaderListener fileReaderListener : fileReaderListeners) {
97             try {
98                 fileReaderListener.handle(e);
99             } catch (Exception ex) {
100                 // catch all exceptions give all handlers a fair chance of handling the messages
101                 logger.debug("An exception occurred while calling the FileReaderListener. ", ex);
102             }
103         }
104     }
105 }