1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 package org.apache.hadoop.hbase.util; 19 20 import java.net.URL; 21 import java.net.URLClassLoader; 22 import java.util.concurrent.ConcurrentHashMap; 23 24 import org.apache.hadoop.hbase.classification.InterfaceAudience; 25 26 import com.google.common.base.Preconditions; 27 28 /** 29 * Base class loader that defines couple shared constants used 30 * by sub-classes. It also defined method getClassLoadingLock for parallel 31 * class loading and JDK 1.6 support. This method (getClassLoadingLock) 32 * is similar to the same method in the base class Java ClassLoader 33 * introduced in JDK 1.7, but not in JDK 1.6. 34 */ 35 @InterfaceAudience.Private 36 public class ClassLoaderBase extends URLClassLoader { 37 38 // Maps class name to the corresponding lock object 39 private final ConcurrentHashMap<String, Object> parallelLockMap 40 = new ConcurrentHashMap<String, Object>(); 41 42 protected static final String DEFAULT_LOCAL_DIR = "/tmp/hbase-local-dir"; 43 protected static final String LOCAL_DIR_KEY = "hbase.local.dir"; 44 45 /** 46 * Parent class loader. 47 */ 48 protected final ClassLoader parent; 49 50 /** 51 * Creates a DynamicClassLoader that can load classes dynamically 52 * from jar files under a specific folder. 53 * 54 * @param parent the parent ClassLoader to set. 55 */ 56 public ClassLoaderBase(final ClassLoader parent) { 57 super(new URL[]{}, parent); 58 Preconditions.checkNotNull(parent, "No parent classloader!"); 59 this.parent = parent; 60 } 61 62 /** 63 * Returns the lock object for class loading operations. 64 */ 65 protected Object getClassLoadingLock(String className) { 66 Object lock = parallelLockMap.get(className); 67 if (lock != null) { 68 return lock; 69 } 70 71 Object newLock = new Object(); 72 lock = parallelLockMap.putIfAbsent(className, newLock); 73 if (lock == null) { 74 lock = newLock; 75 } 76 return lock; 77 } 78 }