cshannon commented on code in PR #3136: URL: https://github.com/apache/accumulo/pull/3136#discussion_r1105882736
########## core/src/main/java/org/apache/accumulo/core/classloader/URLContextClassLoaderFactory.java: ########## @@ -0,0 +1,81 @@ +/* + * 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 + * + * https://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.accumulo.core.classloader; + +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Arrays; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; + +import org.apache.accumulo.core.spi.common.ContextClassLoaderFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; + +/** + * The default implementation of ContextClassLoaderFactory. This classloader returns a + * URLClassLoader based on the given context value which is a CSV list of URLs. For example, + * file://path/one/jar1.jar,file://path/two/jar2.jar + */ +public class URLContextClassLoaderFactory implements ContextClassLoaderFactory { + + private static final AtomicBoolean isInstantiated = new AtomicBoolean(false); + private static final Logger LOG = LoggerFactory.getLogger(URLContextClassLoaderFactory.class); + private static final String className = URLContextClassLoaderFactory.class.getName(); + + // Cache the class loaders for re-use + // WeakReferences are used so that the class loaders can be cleaned up when no longer needed + // Classes that are loaded contain a reference to the class loader used to load them + // so the class loader will be garbage collected when no more classes are loaded that reference it + private final Cache<String,URLClassLoader> classloaders = + CacheBuilder.newBuilder().weakValues().build(); + + public URLContextClassLoaderFactory() { + if (!isInstantiated.compareAndSet(false, true)) { + throw new IllegalStateException("Can only instantiate " + className + " once"); + } + } + + @Override + public ClassLoader getClassLoader(String context) { + if (context == null) { + throw new IllegalArgumentException("Unknown context"); + } + + try { + return classloaders.get(context, () -> { Review Comment: It's using Guava in this case but it works the same as Caffeine. (I plan to create a follow on issue to replace Guava cache with Caffeine everywhere for consistency). In regards to your question the Cache won't return null. Yes the garbage collector will collect the ClassLoader if there are no more classes using it but the cache guarantees to not return null in that case. If the cache is hit the value is [checked](https://github.com/google/guava/blob/master/guava/src/com/google/common/cache/LocalCache.java#L2143) for null first and if it's null due to having been cleaned up then the Cache will call the compute function to create a new class loader and cache it before returning. So even if the GC cleaned up the reference you won't get null back, it will just create a brand new ClassLoader instead. The only way it would be null is if the function that computes the value returns null which won't be the case here. I need to double check but I believe if the compute function throws an Exception it will propagate back up as a runtime exception so it still wouldn't be null but an exception would be seen. Also, keys will be [cleaned up](https://github.com/google/guava/blob/master/guava/src/com/google/common/cache/LocalCache.java#L2477) as well during the clean up/maintenance phase. The cache periodically runs clean up tasks and one of the things that it will do is clean up keys for entries that had their values removed due to being a weak/soft reference. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
