Copilot commented on code in PR #720: URL: https://github.com/apache/unomi/pull/720#discussion_r2212914807
########## extensions/groovy-actions/services/src/main/java/org/apache/unomi/groovy/actions/GroovyActionDispatcher.java: ########## @@ -54,30 +57,52 @@ public void setGroovyActionsService(GroovyActionsService groovyActionsService) { this.groovyActionsService = groovyActionsService; } + @Reference + public void setDefinitionsService(DefinitionsService definitionsService) { + this.definitionsService = definitionsService; + } + + @Reference + public void setActionExecutorDispatcher(ActionExecutorDispatcher actionExecutorDispatcher) { + this.actionExecutorDispatcher = actionExecutorDispatcher; + } + public String getPrefix() { return GROOVY_PREFIX; } public Integer execute(Action action, Event event, String actionName) { - GroovyCodeSource groovyCodeSource = groovyActionsService.getGroovyCodeSource(actionName); - if (groovyCodeSource == null) { - LOGGER.warn("Couldn't find a Groovy action with name {}, action will not execute !", actionName); - } else { - GroovyShell groovyShell = groovyActionsService.getGroovyShell(); - groovyShell.setVariable("action", action); - groovyShell.setVariable("event", event); - Script script = groovyShell.parse(groovyCodeSource); - try { - return new MetricAdapter<Integer>(metricsService, this.getClass().getName() + ".action.groovy." + actionName) { - @Override - public Integer execute(Object... args) throws Exception { - return (Integer) script.invokeMethod("execute", null); - } - }.runWithTimer(); - } catch (Exception e) { - LOGGER.error("Error executing Groovy action with key={}", actionName, e); - } + Class<? extends Script> scriptClass = groovyActionsService.getCompiledScript(actionName); + if (scriptClass == null) { + LOGGER.warn("Couldn't find a Groovy action with name {}, action will not execute!", actionName); + return 0; + } + + try { + Script script = scriptClass.newInstance(); Review Comment: Class.newInstance() is deprecated and can mask underlying exceptions. Consider using scriptClass.getDeclaredConstructor().newInstance() for clearer exception handling. ```suggestion Script script = scriptClass.getDeclaredConstructor().newInstance(); ``` ########## extensions/groovy-actions/services/src/main/java/org/apache/unomi/groovy/actions/ScriptMetadata.java: ########## @@ -0,0 +1,156 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.unomi.groovy.actions; + +import groovy.lang.Script; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +/** + * Metadata container for compiled Groovy scripts with hash-based change detection. + * <p> + * This class encapsulates all metadata associated with a compiled Groovy script, + * including content hash for efficient change detection and the compiled class + * for direct execution without recompilation. + * </p> + * + * <p> + * Thread Safety: This class is immutable and thread-safe. All fields are final + * and the class provides no methods to modify its state after construction. + * </p> + * + * @since 2.7.0 + */ +public final class ScriptMetadata { + private final String actionName; + private final String scriptContent; + private final String contentHash; + private final long lastModified; Review Comment: [nitpick] The field 'lastModified' captures the metadata creation time, not script modification time. Consider renaming it to 'creationTime' or updating the Javadoc to avoid confusion. ```suggestion private final long creationTime; ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@unomi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org