Fixed SAT findings (#10722)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2021-05-20 20:24:46 +02:00 committed by GitHub
parent 0397fcb695
commit fe1c8fc21a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 78 additions and 47 deletions

View File

@ -14,10 +14,7 @@ package org.openhab.binding.logreader.internal;
import static org.openhab.binding.logreader.internal.LogReaderBindingConstants.THING_READER; import static org.openhab.binding.logreader.internal.LogReaderBindingConstants.THING_READER;
import java.util.Collections;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
@ -40,8 +37,7 @@ import org.osgi.service.component.annotations.Component;
@NonNullByDefault @NonNullByDefault
public class LogReaderHandlerFactory extends BaseThingHandlerFactory { public class LogReaderHandlerFactory extends BaseThingHandlerFactory {
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_READER);
.unmodifiableSet(Stream.of(THING_READER).collect(Collectors.toSet()));
@Override @Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) { public boolean supportsThingType(ThingTypeUID thingTypeUID) {

View File

@ -12,20 +12,24 @@
*/ */
package org.openhab.binding.logreader.internal.config; package org.openhab.binding.logreader.internal.config;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
/** /**
* Configuration class for {@link LogReaderBinding} binding. * Configuration class for {@link LogReaderBinding} binding.
* *
* @author Pauli Anttila - Initial contribution * @author Pauli Anttila - Initial contribution
*/ */
@NonNullByDefault
public class LogReaderConfiguration { public class LogReaderConfiguration {
public String filePath; public String filePath = "${OPENHAB_LOGDIR}/openhab.log";
public int refreshRate; public int refreshRate = 1000;
public String warningPatterns; public String warningPatterns = "WARN+";
public String warningBlacklistingPatterns; public @Nullable String warningBlacklistingPatterns;
public String errorPatterns; public String errorPatterns = "ERROR+";
public String errorBlacklistingPatterns; public @Nullable String errorBlacklistingPatterns;
public String customPatterns; public @Nullable String customPatterns;
public String customBlacklistingPatterns; public @Nullable String customBlacklistingPatterns;
@Override @Override
public String toString() { public String toString() {

View File

@ -16,6 +16,7 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.logreader.internal.filereader.api.FileReaderListener; import org.openhab.binding.logreader.internal.filereader.api.FileReaderListener;
import org.openhab.binding.logreader.internal.filereader.api.LogFileReader; import org.openhab.binding.logreader.internal.filereader.api.LogFileReader;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -26,6 +27,7 @@ import org.slf4j.LoggerFactory;
* *
* @author Pauli Anttila - Initial contribution * @author Pauli Anttila - Initial contribution
*/ */
@NonNullByDefault
public abstract class AbstractLogFileReader implements LogFileReader { public abstract class AbstractLogFileReader implements LogFileReader {
private final Logger logger = LoggerFactory.getLogger(AbstractLogFileReader.class); private final Logger logger = LoggerFactory.getLogger(AbstractLogFileReader.class);

View File

@ -19,6 +19,7 @@ import java.util.concurrent.Executors;
import org.apache.commons.io.input.Tailer; import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListener; import org.apache.commons.io.input.TailerListener;
import org.apache.commons.io.input.TailerListenerAdapter; import org.apache.commons.io.input.TailerListenerAdapter;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.logreader.internal.filereader.api.FileReaderException; import org.openhab.binding.logreader.internal.filereader.api.FileReaderException;
import org.openhab.binding.logreader.internal.filereader.api.LogFileReader; import org.openhab.binding.logreader.internal.filereader.api.LogFileReader;
@ -30,17 +31,21 @@ import org.slf4j.LoggerFactory;
* *
* @author Pauli Anttila - Initial contribution * @author Pauli Anttila - Initial contribution
*/ */
@NonNullByDefault
public class FileTailer extends AbstractLogFileReader implements LogFileReader { public class FileTailer extends AbstractLogFileReader implements LogFileReader {
private final Logger logger = LoggerFactory.getLogger(FileTailer.class); private final Logger logger = LoggerFactory.getLogger(FileTailer.class);
private Tailer tailer; private @Nullable Tailer tailer;
private ExecutorService executor; private @Nullable ExecutorService executor;
TailerListener logListener = new TailerListenerAdapter() { TailerListener logListener = new TailerListenerAdapter() {
@Override @Override
public void handle(@Nullable String line) { public void handle(@Nullable String line) {
if (line == null) {
return;
}
sendLineToListeners(line); sendLineToListeners(line);
} }
@ -51,6 +56,10 @@ public class FileTailer extends AbstractLogFileReader implements LogFileReader {
@Override @Override
public void handle(@Nullable Exception e) { public void handle(@Nullable Exception e) {
if (e == null) {
return;
}
sendExceptionToListeners(e); sendExceptionToListeners(e);
} }
@ -62,12 +71,13 @@ public class FileTailer extends AbstractLogFileReader implements LogFileReader {
@Override @Override
public void start(String filePath, long refreshRate) throws FileReaderException { public void start(String filePath, long refreshRate) throws FileReaderException {
tailer = new Tailer(new File(filePath), logListener, refreshRate, true, false, true); Tailer localTailer = new Tailer(new File(filePath), logListener, refreshRate, true, false, true);
executor = Executors.newSingleThreadExecutor(); executor = Executors.newSingleThreadExecutor();
try { try {
logger.debug("Start executor"); logger.debug("Start executor");
executor.execute(tailer); executor.execute(localTailer);
logger.debug("Executor started"); logger.debug("Executor started");
this.tailer = localTailer;
} catch (Exception e) { } catch (Exception e) {
throw new FileReaderException(e); throw new FileReaderException(e);
} }

View File

@ -12,11 +12,14 @@
*/ */
package org.openhab.binding.logreader.internal.filereader.api; package org.openhab.binding.logreader.internal.filereader.api;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* Exception for file reader errors. * Exception for file reader errors.
* *
* @author Pauli Anttila - Initial contribution * @author Pauli Anttila - Initial contribution
*/ */
@NonNullByDefault
public class FileReaderException extends Exception { public class FileReaderException extends Exception {
private static final long serialVersionUID = 1272957002073978608L; private static final long serialVersionUID = 1272957002073978608L;

View File

@ -12,11 +12,15 @@
*/ */
package org.openhab.binding.logreader.internal.filereader.api; package org.openhab.binding.logreader.internal.filereader.api;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
/** /**
* Interface for file reader listeners. * Interface for file reader listeners.
* *
* @author Pauli Anttila - Initial contribution * @author Pauli Anttila - Initial contribution
*/ */
@NonNullByDefault
public interface FileReaderListener { public interface FileReaderListener {
/** /**
@ -35,12 +39,12 @@ public interface FileReaderListener {
* *
* @param line the line. * @param line the line.
*/ */
void handle(String line); void handle(@Nullable String line);
/** /**
* This method is called when exception has occurred. * This method is called when exception has occurred.
* *
* @param ex the exception. * @param ex the exception.
*/ */
void handle(Exception ex); void handle(@Nullable Exception ex);
} }

View File

@ -12,11 +12,14 @@
*/ */
package org.openhab.binding.logreader.internal.filereader.api; package org.openhab.binding.logreader.internal.filereader.api;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* Interface for log file readers. * Interface for log file readers.
* *
* @author Pauli Anttila - Initial contribution * @author Pauli Anttila - Initial contribution
*/ */
@NonNullByDefault
public interface LogFileReader { public interface LogFileReader {
/** /**

View File

@ -17,6 +17,8 @@ import static org.openhab.binding.logreader.internal.LogReaderBindingConstants.*
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.regex.PatternSyntaxException; import java.util.regex.PatternSyntaxException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.logreader.internal.config.LogReaderConfiguration; import org.openhab.binding.logreader.internal.config.LogReaderConfiguration;
import org.openhab.binding.logreader.internal.filereader.api.FileReaderListener; import org.openhab.binding.logreader.internal.filereader.api.FileReaderListener;
import org.openhab.binding.logreader.internal.filereader.api.LogFileReader; import org.openhab.binding.logreader.internal.filereader.api.LogFileReader;
@ -42,16 +44,17 @@ import org.slf4j.LoggerFactory;
* @author Miika Jukka - Initial contribution * @author Miika Jukka - Initial contribution
* @author Pauli Anttila - Rewrite * @author Pauli Anttila - Rewrite
*/ */
@NonNullByDefault
public class LogHandler extends BaseThingHandler implements FileReaderListener { public class LogHandler extends BaseThingHandler implements FileReaderListener {
private final Logger logger = LoggerFactory.getLogger(LogHandler.class); private final Logger logger = LoggerFactory.getLogger(LogHandler.class);
private LogReaderConfiguration configuration; private final LogFileReader fileReader;
private LogFileReader fileReader; private @NonNullByDefault({}) LogReaderConfiguration configuration;
private SearchEngine errorEngine; private @Nullable SearchEngine errorEngine;
private SearchEngine warningEngine; private @Nullable SearchEngine warningEngine;
private SearchEngine customEngine; private @Nullable SearchEngine customEngine;
public LogHandler(Thing thing, LogFileReader fileReader) { public LogHandler(Thing thing, LogFileReader fileReader) {
super(thing); super(thing);
@ -97,8 +100,9 @@ public class LogHandler extends BaseThingHandler implements FileReaderListener {
try { try {
warningEngine = new SearchEngine(configuration.warningPatterns, configuration.warningBlacklistingPatterns); warningEngine = new SearchEngine(configuration.warningPatterns, configuration.warningBlacklistingPatterns);
errorEngine = new SearchEngine(configuration.errorPatterns, configuration.errorBlacklistingPatterns); errorEngine = new SearchEngine(configuration.errorPatterns, configuration.errorBlacklistingPatterns);
customEngine = new SearchEngine(configuration.customPatterns, configuration.customBlacklistingPatterns); String customPatterns = configuration.customPatterns;
customEngine = new SearchEngine(customPatterns != null ? customPatterns : "",
configuration.customBlacklistingPatterns);
} catch (PatternSyntaxException e) { } catch (PatternSyntaxException e) {
logger.debug("Illegal search pattern syntax '{}'. ", e.getMessage(), e); logger.debug("Illegal search pattern syntax '{}'. ", e.getMessage(), e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, e.getMessage()); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, e.getMessage());
@ -124,13 +128,17 @@ public class LogHandler extends BaseThingHandler implements FileReaderListener {
shutdown(); shutdown();
} }
private void updateChannel(ChannelUID channelUID, Command command, SearchEngine matcher) { private void updateChannel(ChannelUID channelUID, Command command, @Nullable SearchEngine matcher) {
if (command instanceof DecimalType) { if (matcher != null) {
matcher.setMatchCount(((DecimalType) command).longValue()); if (command instanceof DecimalType) {
} else if (command instanceof RefreshType) { matcher.setMatchCount(((DecimalType) command).longValue());
updateState(channelUID.getId(), new DecimalType(matcher.getMatchCount())); } else if (command instanceof RefreshType) {
updateState(channelUID.getId(), new DecimalType(matcher.getMatchCount()));
} else {
logger.debug("Unsupported command '{}' received for channel '{}'", command, channelUID);
}
} else { } else {
logger.debug("Unsupported command '{}' received for channel '{}'", command, channelUID); logger.debug("Cannot update channel as SearchEngine is null.");
} }
} }
@ -171,7 +179,7 @@ public class LogHandler extends BaseThingHandler implements FileReaderListener {
} }
@Override @Override
public void handle(String line) { public void handle(@Nullable String line) {
if (line == null) { if (line == null) {
return; return;
} }
@ -180,17 +188,17 @@ public class LogHandler extends BaseThingHandler implements FileReaderListener {
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
} }
if (errorEngine.isMatching(line)) { if (errorEngine != null && errorEngine.isMatching(line)) {
updateChannelIfLinked(CHANNEL_ERRORS, new DecimalType(errorEngine.getMatchCount())); updateChannelIfLinked(CHANNEL_ERRORS, new DecimalType(errorEngine.getMatchCount()));
updateChannelIfLinked(CHANNEL_LASTERROR, new StringType(line)); updateChannelIfLinked(CHANNEL_LASTERROR, new StringType(line));
triggerChannel(CHANNEL_NEWERROR, line); triggerChannel(CHANNEL_NEWERROR, line);
} }
if (warningEngine.isMatching(line)) { if (warningEngine != null && warningEngine.isMatching(line)) {
updateChannelIfLinked(CHANNEL_WARNINGS, new DecimalType(warningEngine.getMatchCount())); updateChannelIfLinked(CHANNEL_WARNINGS, new DecimalType(warningEngine.getMatchCount()));
updateChannelIfLinked(CHANNEL_LASTWARNING, new StringType(line)); updateChannelIfLinked(CHANNEL_LASTWARNING, new StringType(line));
triggerChannel(CHANNEL_NEWWARNING, line); triggerChannel(CHANNEL_NEWWARNING, line);
} }
if (customEngine.isMatching(line)) { if (customEngine != null && customEngine.isMatching(line)) {
updateChannelIfLinked(CHANNEL_CUSTOMEVENTS, new DecimalType(customEngine.getMatchCount())); updateChannelIfLinked(CHANNEL_CUSTOMEVENTS, new DecimalType(customEngine.getMatchCount()));
updateChannelIfLinked(CHANNEL_LASTCUSTOMEVENT, new StringType(line)); updateChannelIfLinked(CHANNEL_LASTCUSTOMEVENT, new StringType(line));
triggerChannel(CHANNEL_NEWCUSTOM, line); triggerChannel(CHANNEL_NEWCUSTOM, line);
@ -198,7 +206,7 @@ public class LogHandler extends BaseThingHandler implements FileReaderListener {
} }
@Override @Override
public void handle(Exception ex) { public void handle(@Nullable Exception ex) {
final String msg = ex != null ? ex.getMessage() : ""; final String msg = ex != null ? ex.getMessage() : "";
logger.debug("Error while trying to read log file: {}. ", msg, ex); logger.debug("Error while trying to read log file: {}. ", msg, ex);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, msg); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, msg);

View File

@ -18,6 +18,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException; import java.util.regex.PatternSyntaxException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
/** /**
@ -25,6 +26,7 @@ import org.eclipse.jdt.annotation.Nullable;
* *
* @author Pauli Anttila - Initial contribution * @author Pauli Anttila - Initial contribution
*/ */
@NonNullByDefault
public class SearchEngine { public class SearchEngine {
private List<Pattern> matchers; private List<Pattern> matchers;
@ -39,7 +41,7 @@ public class SearchEngine {
* @param blacklistingPatterns search patterns to bypass results which have found by the initial search patterns. * @param blacklistingPatterns search patterns to bypass results which have found by the initial search patterns.
* *
*/ */
public SearchEngine(String patterns, String blacklistingPatterns) throws PatternSyntaxException { public SearchEngine(String patterns, @Nullable String blacklistingPatterns) throws PatternSyntaxException {
matchers = compilePatterns(patterns); matchers = compilePatterns(patterns);
blacklistingMatchers = compilePatterns(blacklistingPatterns); blacklistingMatchers = compilePatterns(blacklistingPatterns);
} }
@ -80,7 +82,6 @@ public class SearchEngine {
*/ */
private List<Pattern> compilePatterns(@Nullable String patterns) throws PatternSyntaxException { private List<Pattern> compilePatterns(@Nullable String patterns) throws PatternSyntaxException {
List<Pattern> patternsList = new ArrayList<>(); List<Pattern> patternsList = new ArrayList<>();
if (patterns != null && !patterns.isEmpty()) { if (patterns != null && !patterns.isEmpty()) {
String list[] = patterns.split("\\|"); String list[] = patterns.split("\\|");
if (list.length > 0) { if (list.length > 0) {

View File

@ -29,34 +29,34 @@
<description>Path to log file. Empty will default to ${OPENHAB_LOGDIR}/openhab.log</description> <description>Path to log file. Empty will default to ${OPENHAB_LOGDIR}/openhab.log</description>
<default>${OPENHAB_LOGDIR}/openhab.log</default> <default>${OPENHAB_LOGDIR}/openhab.log</default>
</parameter> </parameter>
<parameter name="refreshRate" type="integer" required="false"> <parameter name="refreshRate" type="integer" unit="ms">
<label>Refresh Rate</label> <label>Refresh Rate</label>
<description>Refresh rate in milliseconds for reading logs</description> <description>Refresh rate in milliseconds for reading logs</description>
<default>1000</default> <default>1000</default>
</parameter> </parameter>
<parameter name="errorPatterns" type="text" required="false"> <parameter name="errorPatterns" type="text">
<label>Error Patterns</label> <label>Error Patterns</label>
<description>Search patterns separated by | character for error events. Empty will default to ERROR+</description> <description>Search patterns separated by | character for error events. Empty will default to ERROR+</description>
<default>ERROR+</default> <default>ERROR+</default>
</parameter> </parameter>
<parameter name="errorBlacklistingPatterns" type="text" required="false"> <parameter name="errorBlacklistingPatterns" type="text">
<label>Error Blacklisting Patterns</label> <label>Error Blacklisting Patterns</label>
<description>Search patterns for blacklisting unwanted error events separated by | character. </description> <description>Search patterns for blacklisting unwanted error events separated by | character. </description>
</parameter> </parameter>
<parameter name="warningPatterns" type="text" required="false"> <parameter name="warningPatterns" type="text">
<label>Warning Patterns</label> <label>Warning Patterns</label>
<description>Search patterns separated by | character for warning events. Empty will default to WARN+</description> <description>Search patterns separated by | character for warning events. Empty will default to WARN+</description>
<default>WARN+</default> <default>WARN+</default>
</parameter> </parameter>
<parameter name="warningBlacklistingPatterns" type="text" required="false"> <parameter name="warningBlacklistingPatterns" type="text">
<label>Warning Blacklisting Patterns</label> <label>Warning Blacklisting Patterns</label>
<description>Search patterns for blacklisting unwanted warning events separated by | character.</description> <description>Search patterns for blacklisting unwanted warning events separated by | character.</description>
</parameter> </parameter>
<parameter name="customPatterns" type="text" required="false"> <parameter name="customPatterns" type="text">
<label>Custom Patterns</label> <label>Custom Patterns</label>
<description>Search patterns separated by | character for custom events.</description> <description>Search patterns separated by | character for custom events.</description>
</parameter> </parameter>
<parameter name="customBlacklistingPatterns" type="text" required="false"> <parameter name="customBlacklistingPatterns" type="text">
<label>Custom Blacklisting Patterns</label> <label>Custom Blacklisting Patterns</label>
<description>Search patterns for blacklisting unwanted custom events separated by | character.</description> <description>Search patterns for blacklisting unwanted custom events separated by | character.</description>
</parameter> </parameter>