001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.util.collections;
018
019import java.util.Collections;
020import java.util.Map;
021import java.util.WeakHashMap;
022import java.util.concurrent.ConcurrentHashMap;
023
024/**
025 * This class wraps a WeakHashMap that holds one ConcurrentHashMap per ClassLoader. In the rare
026 * event of a previously unmapped ClassLoader, the WeakHashMap is replaced by a new one. This avoids
027 * any synchronization overhead, much like a {@link java.util.concurrent.CopyOnWriteArrayList}
028 * 
029 * @param <T>
030 *            type of objects stored in cache
031 */
032public class ClassMetaCache<T>
033{
034        private volatile Map<ClassLoader, ConcurrentHashMap<String, T>> cache = Collections.emptyMap();
035
036        /**
037         * Puts value into cache
038         * 
039         * @param key
040         *            the class that will be used as the value's key
041         * @param value
042         *            the value that should be stored in cache
043         * @return value previously stored in cache for this key, or {@code null} if none
044         */
045        public T put(final Class<?> key, final T value)
046        {
047                ConcurrentHashMap<String, T> container = getClassLoaderCache(key.getClassLoader(), true);
048                return container.put(key(key), value);
049        }
050
051        /**
052         * Gets value from cache or returns {@code null} if not in cache
053         * 
054         * @param key
055         *            the class that is the key for the value
056         * @return value stored in cache or {@code null} if none
057         */
058        public T get(final Class<?> key)
059        {
060                ConcurrentHashMap<String, T> container = getClassLoaderCache(key.getClassLoader(), false);
061                if (container == null)
062                {
063                        return null;
064                }
065                else
066                {
067                        return container.get(key(key));
068                }
069        }
070
071        /**
072         * @param classLoader
073         * @param create
074         * @return a {@link ConcurrentHashMap} mapping class names to injectable fields, never
075         *         <code>null</code>
076         */
077        private ConcurrentHashMap<String, T> getClassLoaderCache(final ClassLoader classLoader,
078                final boolean create)
079        {
080                ConcurrentHashMap<String, T> container = cache.get(classLoader);
081                if (container == null)
082                {
083                        if (!create)
084                        {
085                                return container;
086                        }
087
088                        // only lock in rare event of unknown ClassLoader
089                        synchronized (this)
090                        {
091                                // check again inside lock
092                                container = cache.get(classLoader);
093                                if (container == null)
094                                {
095                                        container = new ConcurrentHashMap<>();
096
097                                        /*
098                                         * don't write to current cache, copy instead
099                                         */
100                                        Map<ClassLoader, ConcurrentHashMap<String, T>> newCache = new WeakHashMap<>(cache);
101                                        newCache.put(classLoader, container);
102                                        cache = Collections.unmodifiableMap(newCache);
103                                }
104                        }
105                }
106                return container;
107        }
108
109        /**
110         * converts class into a key used by the cache
111         * 
112         * @param clazz
113         * 
114         * @return string representation of the clazz
115         */
116        private static String key(final Class<?> clazz)
117        {
118                return clazz.getName();
119        }
120}