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 Fri, 11 Aug 2017 23:50:28 GMT
Github user mattf-horton commented on a diff in the pull request:

    https://github.com/apache/metron/pull/530#discussion_r132801000
  
    --- Diff: bundles-lib/src/main/java/org/apache/metron/bundles/BundleThreadContextClassLoader.java
---
    @@ -0,0 +1,221 @@
    +/*
    + * 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;
    +
    +/**
    + * THREAD SAFE
    + */
    +public class BundleThreadContextClassLoader extends URLClassLoader {
    +
    +    static final ContextSecurityManager contextSecurityManager = new ContextSecurityManager();
    +    private final ClassLoader forward = ClassLoader.getSystemClassLoader();
    +    private static final List<Class<?>> bundleSpecificClasses = new ArrayList<>();
    +    private static AtomicBoolean inited = new AtomicBoolean(false);
    +    // should initialize class definitions
    +    public static void initClasses(final List<Class> classes){
    +        bundleSpecificClasses.clear();
    +        if(classes != null){
    +            for( Class clazz : classes){
    +                bundleSpecificClasses.add(clazz);
    +            }
    +        }
    +        inited.set(true);
    +    }
    +
    +    public static void resetClasses(){
    +        bundleSpecificClasses.clear();
    +        inited.set(false);
    +    }
    +
    +    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 Bundel 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
    +                // OPF NEEDS CLARIFICATION
    --- End diff --
    
    "NEEDS CLARIFICATION" -- what a good idea :-)


---
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