This is an automated email from the ASF dual-hosted git repository. jkevan pushed a commit to branch fixYauaaLibUpgrade in repository https://gitbox.apache.org/repos/asf/unomi.git
commit 18ad9dd9790a75395a9bd97c8ea2e52090ebf2a3 Author: Kevan <ke...@jahia.com> AuthorDate: Wed Nov 16 20:51:18 2022 +0100 UNOMI-710: Fix Yauaa upgrade to 7.8.0, clean dependencies and fix loading of Yauaa rules in OSGI context --- plugins/request/pom.xml | 144 ++++----------------- .../useragent/UserAgentDetectorServiceImpl.java | 83 ++++++------ .../request/actions/UserAgentDetectorTest.java | 12 +- 3 files changed, 80 insertions(+), 159 deletions(-) diff --git a/plugins/request/pom.xml b/plugins/request/pom.xml index f6cf6bea1..a7eb910cd 100644 --- a/plugins/request/pom.xml +++ b/plugins/request/pom.xml @@ -30,14 +30,6 @@ <description>Request reading actions plugin for the Apache Unomi Context Server</description> <packaging>bundle</packaging> - <properties> - <yauaa.version>7.8.0</yauaa.version> - <kryo.version>2.24.0</kryo.version> - <minlog.version>1.3.1</minlog.version> - <prefixmap>1.1</prefixmap> - <objenesis.version>2.1</objenesis.version> - </properties> - <dependencies> <dependency> <groupId>javax.servlet</groupId> @@ -45,118 +37,40 @@ <scope>provided</scope> </dependency> - <dependency> - <groupId>org.glassfish</groupId> - <artifactId>javax.json</artifactId> - </dependency> - - <dependency> - <groupId>net.sf.qualitycheck</groupId> - <artifactId>quality-check</artifactId> - <version>1.3</version> - </dependency> - <dependency> <groupId>com.maxmind.geoip2</groupId> <artifactId>geoip2</artifactId> <version>0.9.0</version> </dependency> - <!-- dependencies of GeoIP 2 --> - - <dependency> - <groupId>com.maxmind.db</groupId> - <artifactId>maxmind-db</artifactId> - <version>0.3.4</version> - </dependency> - - <dependency> - <groupId>com.google.http-client</groupId> - <artifactId>google-http-client</artifactId> - <version>1.18.0-rc</version> - </dependency> - - <dependency> - <groupId>org.apache.httpcomponents</groupId> - <artifactId>httpclient-osgi</artifactId> - <scope>provided</scope> - </dependency> - - <dependency> - <groupId>org.apache.httpcomponents</groupId> - <artifactId>httpcore-osgi</artifactId> - <scope>provided</scope> - </dependency> - - <dependency> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - <version>1.1.1</version> - </dependency> - - <dependency> - <groupId>commons-codec</groupId> - <artifactId>commons-codec</artifactId> - <version>1.3</version> - </dependency> - <dependency> <groupId>nl.basjes.parse.useragent</groupId> <artifactId>yauaa</artifactId> - <version>${yauaa.version}</version> + <version>7.8.0</version> </dependency> <dependency> - <groupId>com.esotericsoftware.kryo</groupId> - <artifactId>kryo</artifactId> - <version>${kryo.version}</version> - </dependency> - - <dependency> - <groupId>com.esotericsoftware</groupId> - <artifactId>minlog</artifactId> - <version>${minlog.version}</version> - </dependency> - - <dependency> - <groupId>nl.basjes.collections</groupId> - <artifactId>prefixmap</artifactId> - <version>1.1</version> - </dependency> - - <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-text</artifactId> - <version>1.6</version> - </dependency> - - <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-collections4</artifactId> - <version>4.3</version> - </dependency> - - <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-lang3</artifactId> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.11</version> + <scope>test</scope> </dependency> - <dependency> - <groupId>org.objenesis</groupId> - <artifactId>objenesis</artifactId> - <version>${objenesis.version}</version> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + <version>2.19.0</version> + <scope>test</scope> </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>4.11</version> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <version>2.19.0</version> <scope>test</scope> </dependency> <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-simple</artifactId> - <version>1.6.6</version> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-slf4j-impl</artifactId> + <version>2.19.0</version> <scope>test</scope> </dependency> @@ -165,13 +79,6 @@ <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> </dependency> - - <dependency> - <groupId>org.ops4j.pax.logging</groupId> - <artifactId>pax-logging-api</artifactId> - <version>1.11.13</version> - <scope>test</scope> - </dependency> </dependencies> <build> @@ -182,7 +89,15 @@ <extensions>true</extensions> <configuration> <instructions> + <!-- + _noee is used to avoid BND to set JAVAEE requirement to Java 11 + (due to Yauaa internal dependency: "Caffeine" being a Java 11 library), + We explicitly know the differences between Java 8 and Java 11, + and they are already handled in the implementation + --> + <_noee>true</_noee> <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency> + <Embed-Transitive>true</Embed-Transitive> <Import-Package> COM.newmonics.PercClassLoader;resolution:=optional, android.os;resolution:=optional, @@ -190,14 +105,13 @@ org.apache.log;resolution:=optional, org.apache.log4j;resolution:=optional, sun.misc;resolution:=optional, - sun.nio.ch;resolution:=optional, jrockit.vm;resolution:=optional, - kotlin.reflect;resolution:=optional, - kotlin.reflect.jvm;resolution:=optional, - nl.basjes.shaded.com.google.errorprone.annotations;resolution:=optional, - nl.basjes.shaded.com.google.errorprone.annotations.concurrent;resolution:=optional, - org.checkerframework.checker.nullness.qual;resolution:=optional, sun.reflect;resolution:=optional, + com.esotericsoftware.kryo;resolution:=optional, + com.esotericsoftware.kryo.*;resolution:=optional, + kotlin;resolution:=optional, + kotlin.*;resolution:=optional, + org.conscrypt;resolution:=optional, * </Import-Package> </instructions> @@ -229,6 +143,4 @@ </plugin> </plugins> </build> - - </project> diff --git a/plugins/request/src/main/java/org/apache/unomi/plugins/request/useragent/UserAgentDetectorServiceImpl.java b/plugins/request/src/main/java/org/apache/unomi/plugins/request/useragent/UserAgentDetectorServiceImpl.java index 1097fca63..fc379bd7b 100644 --- a/plugins/request/src/main/java/org/apache/unomi/plugins/request/useragent/UserAgentDetectorServiceImpl.java +++ b/plugins/request/src/main/java/org/apache/unomi/plugins/request/useragent/UserAgentDetectorServiceImpl.java @@ -17,7 +17,9 @@ package org.apache.unomi.plugins.request.useragent; +import nl.basjes.parse.useragent.PackagedRules; import nl.basjes.parse.useragent.UserAgentAnalyzer; +import nl.basjes.parse.useragent.config.ConfigLoader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,52 +30,59 @@ public class UserAgentDetectorServiceImpl { private static final Logger logger = LoggerFactory.getLogger(UserAgentDetectorServiceImpl.class.getName()); - private final static int JDK11 = 11; - private final static String JDK_VERSION = "java.version"; - private UserAgentAnalyzer userAgentAnalyzer; public void postConstruct() { - ClassLoader tccl = Thread.currentThread().getContextClassLoader(); - try { - Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); - final UserAgentAnalyzer.UserAgentAnalyzerBuilder userAgentAnalyzerBuilder = UserAgentAnalyzer - .newBuilder() - .hideMatcherLoadStats() - .immediateInitialization(); - // Check JDK Version - // Versions prior to 10 are named 1.x - String[] versionElements = System.getProperty(JDK_VERSION).split("\\."); - int discard = Integer.parseInt(versionElements[0]); - int currentJDK; - if (discard == 1) { - currentJDK = Integer.parseInt(versionElements[1]); - } else { - currentJDK = discard; - } - if (currentJDK < JDK11) { - // Use custom cache for jdk8 compatibility - logger.info("Use JDK8 compliant version of the agent analyzer caching"); - userAgentAnalyzerBuilder.useJava8CompatibleCaching(); + final UserAgentAnalyzer.UserAgentAnalyzerBuilder userAgentAnalyzerBuilder = UserAgentAnalyzer + .newBuilder() + .hideMatcherLoadStats() + .immediateInitialization(); + + // We bypass the default resources lookup that is using some Spring class lookup in pattern: + // "classpath*:UserAgents/**/*.yaml" and it's not working on Felix OSGI env + userAgentAnalyzerBuilder.dropDefaultResources(); + for (String ruleFileName : PackagedRules.getRuleFileNames()) { + // We don't want test rules + if (!ConfigLoader.isTestRulesOnlyFile(ruleFileName)) { + userAgentAnalyzerBuilder.addResources("classpath*:" + ruleFileName); } - this.userAgentAnalyzer = userAgentAnalyzerBuilder.withCache(10000) - .withField(nl.basjes.parse.useragent.UserAgent.OPERATING_SYSTEM_CLASS) - .withField(nl.basjes.parse.useragent.UserAgent.OPERATING_SYSTEM_NAME) - .withField(nl.basjes.parse.useragent.UserAgent.AGENT_NAME) - .withField(nl.basjes.parse.useragent.UserAgent.AGENT_VERSION) - .withField(nl.basjes.parse.useragent.UserAgent.DEVICE_CLASS) - .withField(nl.basjes.parse.useragent.UserAgent.DEVICE_NAME) - .withField(nl.basjes.parse.useragent.UserAgent.DEVICE_BRAND) - .build(); - this.userAgentAnalyzer.initializeMatchers(); - } finally { - Thread.currentThread().setContextClassLoader(tccl); } + + // Use custom cache for jdk8 compatibility + if (getCurrentJVMMajorVersion() < 11) { + logger.info("Use JVM 8 compliant version of the agent analyzer caching"); + userAgentAnalyzerBuilder.useJava8CompatibleCaching(); + } + + this.userAgentAnalyzer = userAgentAnalyzerBuilder.withCache(10000) + .withField(nl.basjes.parse.useragent.UserAgent.OPERATING_SYSTEM_CLASS) + .withField(nl.basjes.parse.useragent.UserAgent.OPERATING_SYSTEM_NAME) + .withField(nl.basjes.parse.useragent.UserAgent.AGENT_NAME) + .withField(nl.basjes.parse.useragent.UserAgent.AGENT_VERSION) + .withField(nl.basjes.parse.useragent.UserAgent.DEVICE_CLASS) + .withField(nl.basjes.parse.useragent.UserAgent.DEVICE_NAME) + .withField(nl.basjes.parse.useragent.UserAgent.DEVICE_BRAND) + .build(); + this.userAgentAnalyzer.initializeMatchers(); logger.info("UserAgentDetector service initialized."); } + private int getCurrentJVMMajorVersion() { + String[] versionElements = System.getProperty("java.version").split("\\."); + int discard = Integer.parseInt(versionElements[0]); + // Versions prior to 10 are named 1.x + if (discard == 1) { + return Integer.parseInt(versionElements[1]); + } else { + return discard; + } + } + public void preDestroy() { - userAgentAnalyzer = null; + if (userAgentAnalyzer != null) { + userAgentAnalyzer.destroy(); + userAgentAnalyzer = null; + } logger.info("UserAgentDetector service shutdown."); } diff --git a/plugins/request/src/test/java/org/apache/unomi/plugins/request/actions/UserAgentDetectorTest.java b/plugins/request/src/test/java/org/apache/unomi/plugins/request/actions/UserAgentDetectorTest.java index 4733c4ee0..3c9c7aed2 100644 --- a/plugins/request/src/test/java/org/apache/unomi/plugins/request/actions/UserAgentDetectorTest.java +++ b/plugins/request/src/test/java/org/apache/unomi/plugins/request/actions/UserAgentDetectorTest.java @@ -21,6 +21,8 @@ import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.logging.Logger; + import org.apache.unomi.plugins.request.useragent.UserAgent; import org.apache.unomi.plugins.request.useragent.UserAgentDetectorServiceImpl; import org.junit.After; @@ -28,13 +30,11 @@ import org.junit.Before; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class UserAgentDetectorTest { - - private static final Logger logger = LoggerFactory.getLogger(UserAgentDetectorTest.class); + + private static final Logger logger = Logger.getLogger(UserAgentDetectorTest.class.getName()); private UserAgentDetectorServiceImpl userAgentDetectorService; @@ -44,7 +44,7 @@ public class UserAgentDetectorTest { this.userAgentDetectorService = new UserAgentDetectorServiceImpl(); this.userAgentDetectorService.postConstruct(); long end = System.currentTimeMillis(); - logger.info("Duration starting user agent (in msec) > {}", (end - start)); + logger.info("Duration starting user agent (in msec) > " + (end - start)); } @After @@ -59,7 +59,7 @@ public class UserAgentDetectorTest { long start = System.currentTimeMillis(); UserAgent agent = this.userAgentDetectorService.parseUserAgent(header); long end = System.currentTimeMillis(); - logger.info("Duration user agent parsing (in msec) > {}", (end - start)); + logger.info("Duration user agent parsing (in msec) > " + (end - start)); logger.info(agent.toString()); }