Commit 552d40b5 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

commons module migrated from log4j to log4j2

parent eef024ad
Pipeline #10221 passed with stage
in 11 minutes and 11 seconds
......@@ -12,11 +12,25 @@
<description>Common classes and configuration files</description>
<dependencies>
<!-- Log4J version 1-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- Log4J2-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
......
......@@ -3,7 +3,8 @@ package lcsb.mapviewer.common;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import lcsb.mapviewer.common.exception.NotImplementedException;
......@@ -11,7 +12,7 @@ public abstract class Comparator<T extends Object> implements java.util.Comparat
/**
* Default class logger.
*/
private static Logger logger = Logger.getLogger(Comparator.class);
private static Logger logger = LogManager.getLogger(Comparator.class);
private Class<T> comparatorClazz;
private boolean exactClassMatch;
private List<Comparator<? extends T>> subClassComparatorList = new ArrayList<>();
......
......@@ -7,7 +7,8 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Basic configuration parameters of the system (these values cannot be modified
......@@ -30,7 +31,7 @@ public final class Configuration {
/**
* Default class logger.
*/
private static Logger logger = Logger.getLogger(Configuration.class);
private static Logger logger = LogManager.getLogger(Configuration.class);
/**
* How many elements should be visible in auto-complete lists.
......
......@@ -145,4 +145,4 @@ public class EventStorageLoggerAppender extends AppenderSkeleton {
public List<LoggingEvent> getFatals() {
return fatalEvents;
}
}
}
\ No newline at end of file
package lcsb.mapviewer.common;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.lf5.LogLevel;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.layout.PatternLayout;
/**
* Custom log4j {@link AbstractAppender}. This class is used to store logs in
* the memory. Such logs should be stored for short period of time. Only logs
* for the {@link Thread} that created the object will be stored.
*
* @author Piotr Gawron
*
*/
public class MinervaLoggerAppender extends AbstractAppender {
/**
* List of {@link LogLevel#DEBUG} logs.
*/
private List<LogEvent> debugEvents = new ArrayList<>();
/**
* List of {@link LogLevel#INFO} logs.
*/
private List<LogEvent> infoEvents = new ArrayList<>();
/**
* List of {@link LogLevel#WARN} logs.
*/
private List<LogEvent> warnEvents = new ArrayList<>();
/**
* List of {@link LogLevel#ERROR} logs.
*/
private List<LogEvent> errorEvents = new ArrayList<>();
/**
* List of {@link LogLevel#FATAL} logs.
*/
private List<LogEvent> fatalEvents = new ArrayList<>();
/**
* List of logs with unknown log level.
*/
private List<LogEvent> otherEvents = new ArrayList<>();
/**
* Identifier of {@link Thread} that created this object.
*/
private long threadId;
/**
* Flag that describe if we log only entries for current thread (
* <code>true</code>) or for all threads (<code>false</code>).
*/
private boolean currentThreadLogOnly = true;
/**
* Every logger must have different name. We use this counter to create new
* names.
*/
private static int loggerCounter = 0;
/**
* Private constructor preventing instantiation. Appender should be created
* using factory method: {@link MinervaLoggerAppender#createAppender()}.
*
* @param name
* @param filter
* @param layout
* @param ignoreExceptions
* @param currentThreadLogOnly
*/
private MinervaLoggerAppender(String name, Filter filter, Layout<? extends Serializable> layout,
final boolean ignoreExceptions, boolean currentThreadLogOnly) {
super(name, filter, layout, ignoreExceptions, new Property[0]);
this.threadId = Thread.currentThread().getId();
this.currentThreadLogOnly = currentThreadLogOnly;
}
/**
* Creates appender that will store logs in memory.
*
* @return appender
*/
public static MinervaLoggerAppender createAppender() {
return createAppender(true);
}
/**
* Creates appender that will store logs in memory.
*
* @param currentThreadLogOnly
* if <code>true</code> logs should be taken only from thread that
* created object, if <code>false</code> all logs will be stored
* @return appender
*/
public static MinervaLoggerAppender createAppender(boolean currentThreadLogOnly) {
LoggerContext context = LoggerContext.getContext(false);
org.apache.logging.log4j.core.config.Configuration config = context.getConfiguration();
PatternLayout layout = PatternLayout.createDefaultLayout(config);
if (layout == null) {
layout = PatternLayout.createDefaultLayout();
}
final Filter filter = null;
final MinervaLoggerAppender appender = new MinervaLoggerAppender("MinervaAppender" + loggerCounter++, filter,
layout, true, currentThreadLogOnly);
appender.start();
config.addAppender(appender);
final Level level = Level.DEBUG;
config.getRootLogger().addAppender(appender, level, filter);
// Configurator.setAllLevels(LogManager.getRootLogger().getName(), level);
return appender;
}
public static void unregisterLogEventStorage(MinervaLoggerAppender appender) {
if (appender != null) {
final LoggerContext context = LoggerContext.getContext(false);
final org.apache.logging.log4j.core.config.Configuration config = context.getConfiguration();
config.getRootLogger().removeAppender(appender.getName());
}
}
@Override
public void append(LogEvent event) {
// store information for all thread only if it is flagged by
// currentThreadLogOnly, if not only logs from current thread should be
// stored
if (!currentThreadLogOnly || threadId == Thread.currentThread().getId()) {
if (event.getLevel().equals(Level.DEBUG)) {
debugEvents.add(event);
} else if (event.getLevel().equals(Level.INFO)) {
infoEvents.add(event);
} else if (event.getLevel().equals(Level.WARN)) {
warnEvents.add(event);
} else if (event.getLevel().equals(Level.ERROR)) {
errorEvents.add(event);
} else if (event.getLevel().equals(Level.FATAL)) {
fatalEvents.add(event);
} else {
otherEvents.add(event);
}
}
}
/**
* Returns list of warning logs.
*
* @return list of warning logs
*/
public List<LogEvent> getWarnings() {
return warnEvents;
}
/**
* Returns list of error logs.
*
* @return list of error logs
*/
public List<LogEvent> getErrors() {
return errorEvents;
}
/**
* Returns list of fatal logs.
*
* @return list of fatal logs
*/
public List<LogEvent> getFatals() {
return fatalEvents;
}
/**
* Returns list of warning logs.
*
* @return list of warning logs
*/
public List<LogEvent> getInfos() {
return infoEvents;
}
public List<LogEvent> getDebugs() {
return debugEvents;
}
}
......@@ -7,7 +7,8 @@ import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Class allowing access to system clipboard.
......@@ -19,7 +20,7 @@ public class SystemClipboard implements ClipboardOwner {
/**
* Default class logger.
*/
private final Logger logger = Logger.getLogger(SystemClipboard.class);
private final Logger logger = LogManager.getLogger(SystemClipboard.class);
@Override
public void lostOwnership(Clipboard clipboard, Transferable contents) {
......
......@@ -28,7 +28,8 @@ import javax.xml.transform.stream.StreamSource;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
......@@ -51,7 +52,7 @@ final public class XmlParser {
/**
* Default class logger.
*/
private static Logger logger = Logger.getLogger(XmlParser.class);
private static Logger logger = LogManager.getLogger(XmlParser.class);
/**
* Base of the hex representation.
......
......@@ -3,7 +3,8 @@ package lcsb.mapviewer.common.comparator;
import java.awt.geom.Line2D;
import java.util.Comparator;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import lcsb.mapviewer.common.Configuration;
......@@ -15,7 +16,7 @@ import lcsb.mapviewer.common.Configuration;
*/
public class LineComparator implements Comparator<Line2D> {
private static Logger logger = Logger.getLogger(LineComparator.class);
private static Logger logger = LogManager.getLogger(LineComparator.class);
private PointComparator pointComparator;
......
......@@ -3,7 +3,9 @@ package lcsb.mapviewer.common.comparator;
import java.util.Comparator;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Comparator used for comparing lists of objects.
......@@ -15,7 +17,7 @@ public class ListComparator<T> implements Comparator<List<T>> {
/**
* Default class logger.
*/
private Logger logger = Logger.getLogger(ListComparator.class);
private Logger logger = LogManager.getLogger(ListComparator.class);
private Comparator<T> objectComparator;
......
......@@ -3,7 +3,9 @@ package lcsb.mapviewer.common.comparator;
import java.util.Comparator;
import java.util.Set;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Comparator used for comparing sets of strings.
......@@ -15,7 +17,7 @@ public class SetComparator<T> implements Comparator<Set<T>> {
/**
* Default class logger.
*/
private Logger logger = Logger.getLogger(SetComparator.class);
private Logger logger = LogManager.getLogger(SetComparator.class);
private Comparator<T> objectComparator;
......
......@@ -2,7 +2,9 @@ package lcsb.mapviewer.common.comparator;
import java.util.Comparator;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Comparator used for {@link String} class. It's null safe (it allows to
......@@ -16,7 +18,7 @@ public class StringComparator implements Comparator<String> {
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(StringComparator.class);
private Logger logger = LogManager.getLogger(StringComparator.class);
@Override
public int compare(String arg0, String arg1) {
......
......@@ -3,7 +3,8 @@ package lcsb.mapviewer.common.comparator;
import java.util.Comparator;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Comparator used for list of strings.
......@@ -12,45 +13,45 @@ import org.apache.log4j.Logger;
*
*/
public class StringListComparator implements Comparator<List<String>> {
/**
* Default class logger.
*/
private static Logger logger = Logger.getLogger(StringListComparator.class);
/**
* String comparator used for comparing strings.
*/
private StringComparator stringComparator = new StringComparator();
/**
* Integer comparator used for comparing integers.
*/
private IntegerComparator integerComparator = new IntegerComparator();
@Override
public int compare(List<String> arg0, List<String> arg1) {
if (arg0 == null) {
if (arg1 == null) {
return 0;
} else {
return 1;
}
} else if (arg1 == null) {
return -1;
}
if (arg0.size() != arg1.size()) {
logger.debug("String lists have different size: " + arg0.size() + ", " + arg1.size());
return integerComparator.compare(arg0.size(), arg1.size());
}
for (int i = 0; i < arg0.size(); i++) {
if (stringComparator.compare(arg0.get(i), arg1.get(i)) != 0) {
logger.debug("Strings in list different: \"" + arg0.get(i) + "\", \"" + arg1.get(i) + "\"");
return stringComparator.compare(arg0.get(i), arg1.get(i));
}
}
return 0;
}
/**
* Default class logger.
*/
private static Logger logger = LogManager.getLogger(StringListComparator.class);
/**
* String comparator used for comparing strings.
*/
private StringComparator stringComparator = new StringComparator();
/**
* Integer comparator used for comparing integers.
*/
private IntegerComparator integerComparator = new IntegerComparator();
@Override
public int compare(List<String> arg0, List<String> arg1) {
if (arg0 == null) {
if (arg1 == null) {
return 0;
} else {
return 1;
}
} else if (arg1 == null) {
return -1;
}
if (arg0.size() != arg1.size()) {
logger.debug("String lists have different size: " + arg0.size() + ", " + arg1.size());
return integerComparator.compare(arg0.size(), arg1.size());
}
for (int i = 0; i < arg0.size(); i++) {
if (stringComparator.compare(arg0.get(i), arg1.get(i)) != 0) {
logger.debug("Strings in list different: \"" + arg0.get(i) + "\", \"" + arg1.get(i) + "\"");
return stringComparator.compare(arg0.get(i), arg1.get(i));
}
}
return 0;
}
}
......@@ -3,7 +3,9 @@ package lcsb.mapviewer.common.comparator;
import java.util.Comparator;
import java.util.Set;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Comparator used for comparing sets of strings.
......@@ -15,7 +17,7 @@ public class StringSetComparator implements Comparator<Set<String>> {
/**
* Default class logger.
*/
private Logger logger = Logger.getLogger(StringSetComparator.class);
private Logger logger = LogManager.getLogger(StringSetComparator.class);
@Override
public int compare(Set<String> arg0, Set<String> arg1) {
......
......@@ -3,9 +3,11 @@ package lcsb.mapviewer.common.geometry;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import lcsb.mapviewer.common.Configuration;
import org.apache.log4j.Logger;
/**
* This class contains basic operators on ellipse used by converters.
......@@ -14,140 +16,142 @@ import org.apache.log4j.Logger;
*
*/
public class EllipseTransformation {
/**
* PI value.
*/
private static final double PI = Math.PI;
/**
* PI/2 value.
*/
private static final double PI_1_2 = Math.PI / 2;
/**
* 3/2 PI value.
*/
private static final double PI_3_2 = PI_1_2 * 3;
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private static Logger logger = Logger.getLogger(EllipseTransformation.class.getName());
/**
* Method returns a cross point between ellipse and a line (from center point
* with angle between the line and X axis).
*
* @param ellipse
* ellipse object on which we want to find point
* @param angle
* angle in degrees
* @return point on the ellipse that connect center of the ellipse with the
* line that cross X axis by angle
*/
public Point2D getPointOnEllipseByDegree(final Ellipse2D ellipse, final double angle) {
return getPointOnEllipseByRadian(ellipse, Math.toRadians(angle));
}
/**
* Method returns a cross point between ellipse and a line (from center point
* with angle between the line and X axis).
*
* @param x
* x coordinate of the ellipse
* @param y
* y coordinate of the ellipse
* @param width
* width of the ellipse
* @param height
* height of the ellipse
* @param angle
* angle in degrees
* @return point on the ellipse that connect center of the ellipse with the
* line that cross X axis by angle
*/
public Point2D getPointOnEllipseByDegree(final double x, final double y, final double width, final double height, final double angle) {
return getPointOnEllipseByRadian(x, y, width, height, Math.toRadians(angle));
}