[ 
https://issues.apache.org/jira/browse/NIFI-3688?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16063824#comment-16063824
 ] 

ASF GitHub Bot commented on NIFI-3688:
--------------------------------------

Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/1662#discussion_r124123922
  
    --- Diff: 
nifi-nar-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/main/java/org/apache/nifi/processors/groovyx/ExecuteGroovyScript.java
 ---
    @@ -0,0 +1,468 @@
    +/*
    + * 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.nifi.processors.groovyx;
    +
    +import java.io.File;
    +import java.lang.reflect.Method;
    +import java.sql.SQLException;
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.Collection;
    +import java.util.HashMap;
    +import java.util.HashSet;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +
    +import org.apache.nifi.annotation.behavior.Restricted;
    +import org.apache.nifi.annotation.behavior.DynamicProperty;
    +import org.apache.nifi.annotation.behavior.EventDriven;
    +import org.apache.nifi.annotation.documentation.CapabilityDescription;
    +import org.apache.nifi.annotation.documentation.SeeAlso;
    +import org.apache.nifi.annotation.documentation.Tags;
    +import org.apache.nifi.annotation.lifecycle.OnScheduled;
    +import org.apache.nifi.annotation.lifecycle.OnStopped;
    +import org.apache.nifi.components.PropertyDescriptor;
    +import org.apache.nifi.controller.ControllerService;
    +import org.apache.nifi.dbcp.DBCPService;
    +import org.apache.nifi.flowfile.FlowFile;
    +import org.apache.nifi.processor.AbstractProcessor;
    +import org.apache.nifi.processor.ProcessContext;
    +import org.apache.nifi.processor.ProcessSession;
    +import org.apache.nifi.processor.ProcessorInitializationContext;
    +import org.apache.nifi.processor.Relationship;
    +import org.apache.nifi.processor.exception.ProcessException;
    +import org.apache.nifi.processor.util.StandardValidators;
    +import org.codehaus.groovy.control.CompilerConfiguration;
    +import org.codehaus.groovy.runtime.ResourceGroovyMethods;
    +import org.codehaus.groovy.runtime.StackTraceUtils;
    +
    +import org.apache.nifi.processors.groovyx.sql.OSql;
    +import org.apache.nifi.processors.groovyx.util.Files;
    +import org.apache.nifi.processors.groovyx.util.Validators;
    +import org.apache.nifi.processors.groovyx.flow.GroovyProcessSessionWrap;
    +
    +import groovy.lang.GroovyShell;
    +import groovy.lang.Script;
    +
    +import org.apache.nifi.components.ValidationResult;
    +import org.apache.nifi.components.ValidationContext;
    +
    +@EventDriven
    +@Tags({"script", "groovy", "groovyx"})
    +@CapabilityDescription(
    +        "Experimental Extended Groovy script processor. The script is 
responsible for "
    +        + "handling the incoming flow file (transfer to SUCCESS or remove, 
e.g.) as well as any flow files created by "
    +        + "the script. If the handling is incomplete or incorrect, the 
session will be rolled back.")
    +@Restricted("Provides operator the ability to execute arbitrary code 
assuming all permissions that NiFi has.")
    +@SeeAlso({})
    +@DynamicProperty(name = "A script engine property to update",
    +        value = "The value to set it to",
    +        supportsExpressionLanguage = true,
    +        description = "Updates a script engine property specified by the 
Dynamic Property's key with the value "
    +                + "specified by the Dynamic Property's value. Use `CTL.` 
to access any controller services.")
    +public class ExecuteGroovyScript extends AbstractProcessor {
    +    public static final String GROOVY_CLASSPATH = "${groovy.classes.path}";
    +
    +    private static final String PRELOADS = "import 
org.apache.nifi.components.*;" + "import org.apache.nifi.flowfile.FlowFile;" + 
"import org.apache.nifi.processor.*;"
    +            + "import 
org.apache.nifi.processor.FlowFileFilter.FlowFileFilterResult;" + "import 
org.apache.nifi.processor.exception.*;" + "import 
org.apache.nifi.processor.io.*;"
    +            + "import org.apache.nifi.processor.util.*;" + "import 
org.apache.nifi.processors.script.*;" + "import 
org.apache.nifi.logging.ComponentLog;";
    +
    +    public static final PropertyDescriptor SCRIPT_FILE = new 
PropertyDescriptor.Builder().name("Script File").required(false)
    +            .description("Path to script file to execute. Only one of 
Script File or Script Body may be 
used").addValidator(Validators.createFileExistsAndReadableValidator())
    +            .expressionLanguageSupported(true).build();
    +
    +    public static final PropertyDescriptor SCRIPT_BODY = new 
PropertyDescriptor.Builder().name("Script Body").required(false)
    +            .description("Body of script to execute. Only one of Script 
File or Script Body may be 
used").addValidator(StandardValidators.NON_EMPTY_VALIDATOR).expressionLanguageSupported(false)
    +            .build();
    +
    +    public static String[] VALID_BOOLEANS = {"true", "false"};
    +    public static final PropertyDescriptor REQUIRE_FLOW = new 
PropertyDescriptor.Builder().name("Requires flow file")
    --- End diff --
    
    I see the benefit of saving the user the trouble of the following 
boilerplate:
    
    ```
    def flowFile = session.get()
    if(!flowFile) return
    ```
    
    However there are other methods on ProcessSession that might be helpful to 
the user, for example getting a batch of flow files (if available): 
    
    ```
    def flowFiles = session.get(100)
    if(!flowFiles) return
    flowFiles.each { flowFile ->
      // do something
    }
    ```
    or applying a filter (I see GroovyProcessSessionWrap has a handy 
get(Closure) override but it is unused). For the small amount of boilerplate 
code, I'm not sure such a property is necessary. Thoughts?


> Create extended groovy scripting processor
> ------------------------------------------
>
>                 Key: NIFI-3688
>                 URL: https://issues.apache.org/jira/browse/NIFI-3688
>             Project: Apache NiFi
>          Issue Type: New Feature
>          Components: Extensions
>            Reporter: Dmitry Lukyanov
>            Priority: Minor
>
> The idea is to simplify groovy scripting.
> Main targets:
> - to be compatible with existing groovy scripting
> - simplify read/write attributes
> - simplify read/write content
> - avoid closure casting to nifi types like `StreamCallback`
> - simplify and provide visibility when accessing to controller services from 
> script



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to