metron-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattf-horton <...@git.apache.org>
Subject [GitHub] metron pull request #530: METRON-777 Metron Extension System and Parser Exte...
Date Mon, 14 Aug 2017 21:31:16 GMT
Github user mattf-horton commented on a diff in the pull request:

    https://github.com/apache/metron/pull/530#discussion_r133046812
  
    --- Diff: bundles-lib/src/main/java/org/apache/metron/bundles/BundleThreadContextClassLoader.java
---
    @@ -0,0 +1,213 @@
    +/*
    + * 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.metron.bundles;
    +
    +import java.io.IOException;
    +import java.io.InputStream;
    +import java.lang.reflect.Constructor;
    +import java.lang.reflect.InvocationTargetException;
    +import java.net.URL;
    +import java.net.URLClassLoader;
    +import java.util.ArrayList;
    +import java.util.Enumeration;
    +import java.util.List;
    +import java.util.concurrent.atomic.AtomicBoolean;
    +
    +import org.apache.metron.bundles.bundle.Bundle;
    +import org.apache.metron.bundles.util.BundleProperties;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +/**
    + * THREAD SAFE
    + */
    +public class BundleThreadContextClassLoader extends URLClassLoader {
    +    static final Logger LOG = LoggerFactory.getLogger(BundleThreadContextClassLoader.class);
    +    static final ContextSecurityManager contextSecurityManager = new ContextSecurityManager();
    +    private final ClassLoader forward = ClassLoader.getSystemClassLoader();
    +
    +    private BundleThreadContextClassLoader() {
    +        super(new URL[0]);
    +    }
    +
    +    @Override
    +    public void clearAssertionStatus() {
    +        lookupClassLoader().clearAssertionStatus();
    +    }
    +
    +    @Override
    +    public URL getResource(String name) {
    +        return lookupClassLoader().getResource(name);
    +    }
    +
    +    @Override
    +    public InputStream getResourceAsStream(String name) {
    +        return lookupClassLoader().getResourceAsStream(name);
    +    }
    +
    +    @Override
    +    public Enumeration<URL> getResources(String name) throws IOException {
    +        return lookupClassLoader().getResources(name);
    +    }
    +
    +    @Override
    +    public Class<?> loadClass(String name) throws ClassNotFoundException {
    +        return lookupClassLoader().loadClass(name);
    +    }
    +
    +    @Override
    +    public void setClassAssertionStatus(String className, boolean enabled) {
    +        lookupClassLoader().setClassAssertionStatus(className, enabled);
    +    }
    +
    +    @Override
    +    public void setDefaultAssertionStatus(boolean enabled) {
    +        lookupClassLoader().setDefaultAssertionStatus(enabled);
    +    }
    +
    +    @Override
    +    public void setPackageAssertionStatus(String packageName, boolean enabled) {
    +        lookupClassLoader().setPackageAssertionStatus(packageName, enabled);
    +    }
    +
    +    private ClassLoader lookupClassLoader() {
    +        final Class<?>[] classStack = contextSecurityManager.getExecutionStack();
    +
    +        for (Class<?> currentClass : classStack) {
    +            final Class<?> bundleClass = findBundleClass(currentClass);
    +            if (bundleClass != null) {
    +                final ClassLoader desiredClassLoader = bundleClass.getClassLoader();
    +
    +                // When new Threads are created, the new Thread inherits the ClassLoaderContext
of
    +                // the caller. However, the call stack of that new Thread may not trace
back to any app-specific
    +                // code. Therefore, the BundleThreadContextClassLoader will be unable
to find the appropriate Bundle
    +                // ClassLoader. As a result, we want to set the ContextClassLoader to
the Bundle ClassLoader that
    +                // contains the class or resource that we are looking for.
    +                // This locks the current Thread into the appropriate Bundle ClassLoader
Context. The framework will change
    +                // the ContextClassLoader back to the BundleThreadContextClassLoader
as appropriate via the
    +                //
    +                // TL;DR
    +                // We need to make sure the classloader for the thread is setup correctly
to use the bundle classloader
    +                // before we return the class.
    +                // Just looking the class up is not enough.
    +                // 
    +                // {@link FlowEngine.beforeExecute(Thread, Runnable)} and
    +                // {@link FlowEngine.afterExecute(Thread, Runnable)} methods.
    +                if (desiredClassLoader instanceof VFSBundleClassLoader) {
    +                    Thread.currentThread().setContextClassLoader(desiredClassLoader);
    +                }
    +                return desiredClassLoader;
    +            }
    +        }
    +        return forward;
    +    }
    +
    +    private Class<?> findBundleClass(final Class<?> cls) {
    +        try {
    +            for (final Class<?> bundleClass : ExtensionManager.getInstance()
    +                .getExtensionClasses()) {
    +                if (bundleClass.isAssignableFrom(cls)) {
    +                    return cls;
    +                } else if (cls.getEnclosingClass() != null) {
    +                    return findBundleClass(cls.getEnclosingClass());
    +                }
    +            }
    +        }catch(NotInitializedException e){
    +            LOG.error("ExtensionManager not initialized",e);
    +        }
    +
    +        return null;
    +    }
    +
    +    private static class SingletonHolder {
    +
    +        public static final BundleThreadContextClassLoader instance = new BundleThreadContextClassLoader();
    +    }
    +
    +    public static BundleThreadContextClassLoader getInstance() throws NotInitializedException
{
    --- End diff --
    
    It doesn't/can't throw NotInitializedException any more, can it?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message