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

    https://github.com/apache/nifi/pull/271#discussion_r56106464
  
    --- Diff: 
nifi-nar-bundles/nifi-spring-bundle/nifi-spring-processors/src/main/java/org/apache/nifi/spring/SpringContextFactory.java
 ---
    @@ -0,0 +1,139 @@
    +/*
    + * 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.spring;
    +
    +import java.io.File;
    +import java.io.InputStream;
    +import java.lang.reflect.Constructor;
    +import java.net.URL;
    +import java.net.URLClassLoader;
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.List;
    +
    +import org.apache.commons.io.IOUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +/**
    + * Helper class which provides factory method to create and initialize 
Spring
    + * Application Context while scoping it within the dedicated Class Loader.
    + */
    +final class SpringContextFactory {
    +
    +    private static final Logger logger = 
LoggerFactory.getLogger(SpringContextFactory.class);
    +
    +    private static final String SC_DELEGATE_NAME = 
"org.apache.nifi.spring.bootstrap.SpringContextDelegate";
    +
    +    /**
    +     * Creates and instance of Spring Application Context scoped within a
    +     * dedicated Class Loader
    +     */
    +    static SpringDataExchanger createSpringContextDelegate(String 
classpath, String config) {
    +        System.out.println(SpringContextFactory.class.getClassLoader());
    +        URL[] urls = gatherAdditionalClassPathUrls(classpath);
    +        SpringContextClassLoader contextCl = new 
SpringContextClassLoader(urls,
    +                SpringContextFactory.class.getClassLoader());
    +        try {
    +            InputStream delegateStream = 
contextCl.getResourceAsStream(SC_DELEGATE_NAME.replace('.', '/') + ".class");
    +            byte[] delegateBytes = IOUtils.toByteArray(delegateStream);
    +            Class<?> clazz = contextCl.doDefineClass(SC_DELEGATE_NAME, 
delegateBytes, 0, delegateBytes.length);
    +            Constructor<?> ctr = 
clazz.getDeclaredConstructor(String.class);
    +            ctr.setAccessible(true);
    +            SpringDataExchanger springDelegate = (SpringDataExchanger) 
ctr.newInstance(config);
    +            if (logger.isInfoEnabled()) {
    +                logger.info("Successfully instantiated Spring Application 
Context from '" + config + "'");
    +            }
    +            return springDelegate;
    +        } catch (Exception e) {
    +            try {
    +                contextCl.close();
    +            } catch (Exception e2) {
    +                // ignore
    +            }
    +            throw new IllegalStateException("Failed to instantiate Spring 
Application Context. Config path: '" + config
    +                    + "'; Classpath: " + Arrays.asList(urls), e);
    +        }
    +    }
    +
    +    /**
    +     *
    +     */
    +    private static URL[] gatherAdditionalClassPathUrls(String path) {
    +        if (logger.isDebugEnabled()) {
    +            logger.debug("Adding additional resources from '" + path + "' 
to the classpath.");
    +        }
    +        File libraryDir = new File(path);
    +        if (libraryDir.exists() && libraryDir.isDirectory()) {
    +            String[] cpResourceNames = libraryDir.list();
    +            try {
    +                URLClassLoader thisCl = (URLClassLoader) 
SpringContextFactory.class.getClassLoader();
    +                List<URL> urls = new ArrayList<>();
    +                for (int i = 0; i < cpResourceNames.length; i++) {
    +                    if (!isDuplicate(thisCl.getURLs(), 
cpResourceNames[i])) {
    +                        URL url = new File(libraryDir, 
cpResourceNames[i]).toURI().toURL();
    +                        urls.add(url);
    +                        if (logger.isDebugEnabled()) {
    +                            logger.debug("Identifying additional resource 
to the classpath: " + url);
    +                        }
    +                    }
    +                }
    +                return urls.toArray(new URL[] {});
    +            } catch (Exception e) {
    +                throw new IllegalStateException(
    +                        "Failed to parse user libraries from '" + 
libraryDir.getAbsolutePath() + "'", e);
    +            }
    +        } else {
    +            throw new IllegalArgumentException("Path '" + 
libraryDir.getAbsolutePath()
    +                    + "' is not valid because it doesn't exist or does not 
point to a directory.");
    +        }
    +    }
    +
    +    /**
    +     *
    +     */
    +    static boolean isDuplicate(URL[] currentURLs, String resourceName) {
    +        if (resourceName.startsWith("spring")) {
    +            resourceName = resourceName.substring(0, 
resourceName.lastIndexOf("-"));
    --- End diff --
    
    Let me review this. This whole method will probably go since this NAR no 
longer packages any Spring JARs. Basically it's now one of the thinnest NARs in 
NiFi and only comes with 4-5 JARs


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to