From f4f1b05f8d5211476ae073d8a3c2dbd10cbcbe8a Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 29 Jan 2023 09:33:16 -0800 Subject: Upgrade JXLS library --- build.gradle | 5 +++-- .../traccar/handler/ComputedAttributesHandler.java | 24 ++++++++++++---------- .../traccar/handler/ComputedAttributesTest.java | 2 +- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/build.gradle b/build.gradle index 0ac074f20..1a904d661 100644 --- a/build.gradle +++ b/build.gradle @@ -15,6 +15,7 @@ ext { jerseyVersion = "2.37" // jersey 3 javax to jakarta jacksonVersion = "2.13.4" // same version as jersey-media-json-jackson dependency protobufVersion = "3.21.9" + jxlsVersion = "2.12.0" } sourceCompatibility = "11" @@ -67,8 +68,8 @@ dependencies { implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr353:$jacksonVersion" implementation "org.liquibase:liquibase-core:4.17.2" implementation "com.sun.mail:jakarta.mail:1.6.7" - implementation "org.jxls:jxls:2.4.7" // needs upgrade (wait for jexl 4) - implementation "org.jxls:jxls-poi:1.0.16" // needs upgrade (wait for jexl 4) + implementation "org.jxls:jxls:$jxlsVersion" + implementation "org.jxls:jxls-poi:$jxlsVersion" implementation 'org.apache.velocity:velocity-engine-core:2.3' implementation 'org.apache.velocity.tools:velocity-tools-generic:3.1' implementation "org.apache.commons:commons-collections4:4.4" diff --git a/src/main/java/org/traccar/handler/ComputedAttributesHandler.java b/src/main/java/org/traccar/handler/ComputedAttributesHandler.java index 620852502..ca6c0fc74 100644 --- a/src/main/java/org/traccar/handler/ComputedAttributesHandler.java +++ b/src/main/java/org/traccar/handler/ComputedAttributesHandler.java @@ -26,9 +26,10 @@ import java.util.Map; import java.util.Set; import io.netty.channel.ChannelHandler; -import org.apache.commons.jexl2.JexlEngine; -import org.apache.commons.jexl2.JexlException; -import org.apache.commons.jexl2.MapContext; +import org.apache.commons.jexl3.JexlBuilder; +import org.apache.commons.jexl3.JexlEngine; +import org.apache.commons.jexl3.JexlException; +import org.apache.commons.jexl3.MapContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.BaseDataHandler; @@ -57,9 +58,10 @@ public class ComputedAttributesHandler extends BaseDataHandler { @Inject public ComputedAttributesHandler(Config config, CacheManager cacheManager) { this.cacheManager = cacheManager; - engine = new JexlEngine(); - engine.setStrict(true); - engine.setFunctions(Collections.singletonMap("math", Math.class)); + engine = new JexlBuilder() + .strict(true) + .namespaces(Collections.singletonMap("math", Math.class)) + .create(); includeDeviceAttributes = config.getBoolean(Keys.PROCESSING_COMPUTED_ATTRIBUTES_DEVICE_ATTRIBUTES); } @@ -68,13 +70,13 @@ public class ComputedAttributesHandler extends BaseDataHandler { if (includeDeviceAttributes) { Device device = cacheManager.getObject(Device.class, position.getDeviceId()); if (device != null) { - for (Object key : device.getAttributes().keySet()) { - result.set((String) key, device.getAttributes().get(key)); + for (String key : device.getAttributes().keySet()) { + result.set(key, device.getAttributes().get(key)); } } } Set methods = new HashSet<>(Arrays.asList(position.getClass().getMethods())); - methods.removeAll(Arrays.asList(Object.class.getMethods())); + Arrays.asList(Object.class.getMethods()).forEach(methods::remove); for (Method method : methods) { if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) { String name = Character.toLowerCase(method.getName().charAt(3)) + method.getName().substring(4); @@ -83,8 +85,8 @@ public class ComputedAttributesHandler extends BaseDataHandler { if (!method.getReturnType().equals(Map.class)) { result.set(name, method.invoke(position)); } else { - for (Object key : ((Map) method.invoke(position)).keySet()) { - result.set((String) key, ((Map) method.invoke(position)).get(key)); + for (Object key : ((Map) method.invoke(position)).keySet()) { + result.set((String) key, ((Map) method.invoke(position)).get(key)); } } } catch (IllegalAccessException | InvocationTargetException error) { diff --git a/src/test/java/org/traccar/handler/ComputedAttributesTest.java b/src/test/java/org/traccar/handler/ComputedAttributesTest.java index 2668c4f14..0beef9c57 100644 --- a/src/test/java/org/traccar/handler/ComputedAttributesTest.java +++ b/src/test/java/org/traccar/handler/ComputedAttributesTest.java @@ -41,7 +41,7 @@ public class ComputedAttributesTest { attribute.setExpression("(bitFlag & 4) != 0"); assertEquals(true, handler.computeAttribute(attribute, position)); - attribute.setExpression("if (event == 42) \"lowBattery\""); + attribute.setExpression("event == 42 ? \"lowBattery\" : null"); assertEquals("lowBattery", handler.computeAttribute(attribute, position)); attribute.setExpression("speed > 5 && valid"); -- cgit v1.2.3