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

    https://github.com/apache/nifi/pull/2335#discussion_r157231905
  
    --- Diff: 
nifi-nar-bundles/nifi-atlas-bundle/nifi-atlas-reporting-task/src/main/java/org/apache/nifi/atlas/provenance/NiFiProvenanceEventAnalyzerFactory.java
 ---
    @@ -0,0 +1,105 @@
    +/*
    + * 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.atlas.provenance;
    +
    +import org.apache.nifi.provenance.ProvenanceEventType;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.util.Map;
    +import java.util.ServiceLoader;
    +import java.util.concurrent.ConcurrentHashMap;
    +import java.util.regex.Pattern;
    +
    +public class NiFiProvenanceEventAnalyzerFactory {
    +
    +    private static final Logger logger = 
LoggerFactory.getLogger(NiFiProvenanceEventAnalyzerFactory.class);
    +    private static final Map<Pattern, NiFiProvenanceEventAnalyzer> 
analyzersForComponentType = new ConcurrentHashMap<>();
    +    private static final Map<Pattern, NiFiProvenanceEventAnalyzer> 
analyzersForTransitUri = new ConcurrentHashMap<>();
    +    private static final Map<ProvenanceEventType, 
NiFiProvenanceEventAnalyzer> analyzersForProvenanceEventType = new 
ConcurrentHashMap<>();
    +    private static boolean loaded = false;
    +
    +    private static void loadAnalyzers() {
    +        logger.debug("Loading NiFiProvenanceEventAnalyzer ...");
    +        final ServiceLoader<NiFiProvenanceEventAnalyzer> serviceLoader
    +                = ServiceLoader.load(NiFiProvenanceEventAnalyzer.class);
    +        serviceLoader.forEach(analyzer -> {
    +            addAnalyzer(analyzer.targetComponentTypePattern(), 
analyzersForComponentType, analyzer);
    +            addAnalyzer(analyzer.targetTransitUriPattern(), 
analyzersForTransitUri, analyzer);
    +            final ProvenanceEventType eventType = 
analyzer.targetProvenanceEventType();
    +            if (eventType != null) {
    +                if 
(analyzersForProvenanceEventType.containsKey(eventType)) {
    +                    logger.warn("Fo ProvenanceEventType {}, an Analyzer {} 
is already assigned." +
    +                            " Only one analyzer for a type can be 
registered. Ignoring {}",
    +                            eventType, 
analyzersForProvenanceEventType.get(eventType), analyzer);
    +                }
    +                analyzersForProvenanceEventType.put(eventType, analyzer);
    +            }
    +        });
    +        logger.info("Loaded NiFiProvenanceEventAnalyzers: 
componentTypes={}, transitUris={}", analyzersForComponentType, 
analyzersForTransitUri);
    +    }
    +
    +    private static void addAnalyzer(String patternStr, Map<Pattern, 
NiFiProvenanceEventAnalyzer> toAdd,
    +                                    NiFiProvenanceEventAnalyzer analyzer) {
    +        if (patternStr != null && !patternStr.isEmpty()) {
    +            Pattern pattern = Pattern.compile(patternStr.trim());
    +            toAdd.put(pattern, analyzer);
    +        }
    +    }
    +
    +    /**
    +     * Find and retrieve NiFiProvenanceEventAnalyzer implementation for 
the specified targets.
    +     * Pattern matching is performed by following order, and the one found 
at first is returned:
    +     * <ol>
    +     * <li>Component type name. Use an analyzer supporting the Component 
type with its {@link NiFiProvenanceEventAnalyzer#targetProvenanceEventType()}.
    +     * <li>TransitUri. Use an analyzer supporting the TransitUri with its 
{@link NiFiProvenanceEventAnalyzer#targetTransitUriPattern()}.
    +     * <li>Provenance Event Type. Use an analyzer supporting the 
Provenance Event Type with its {@link 
NiFiProvenanceEventAnalyzer#targetProvenanceEventType()}.
    +     * </ol>
    +     * @param typeName NiFi component type name.
    +     * @param transitUri Transit URI.
    +     * @param eventType Provenance event type.
    +     * @return Instance of NiFiProvenanceEventAnalyzer if one is found for 
the specified className, otherwise null.
    +     */
    +    public static NiFiProvenanceEventAnalyzer getAnalyzer(String typeName, 
String transitUri, ProvenanceEventType eventType) {
    +
    +        if (!loaded) {
    --- End diff --
    
    Thank you! I was referring an ancient blog post to implement this logic.. I 
will update it to use static lazy initializer.


---

Reply via email to