View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.proxy2.impl;
19  
20  import java.lang.ref.Reference;
21  import java.lang.ref.WeakReference;
22  import java.util.Arrays;
23  import java.util.HashMap;
24  import java.util.HashSet;
25  import java.util.Map;
26  import java.util.Set;
27  import java.util.WeakHashMap;
28  
29  /**
30   * A cache for storing implementation classes for proxies based on a specific type of {@link ProxyClassGenerator}. A
31   * proxy class cache ensures that there is only one class for every {@link ProxyClassGenerator}/{@link ClassLoader}
32   * /proxy class array combination.
33   * 
34   * @since 1.0
35   */
36  public class ProxyClassCache
37  {
38  
39      //******************************************************************************************************************
40      // Fields
41      //******************************************************************************************************************
42  
43      private final Map<ClassLoader, Map<Set<Class<?>>, WeakReference<Class<?>>>> loaderToClassCache
44          = new WeakHashMap<ClassLoader, Map<Set<Class<?>>, WeakReference<Class<?>>>>();
45      private final ProxyClassGenerator proxyClassGenerator;
46  
47      //******************************************************************************************************************
48      // Constructors
49      //******************************************************************************************************************
50  
51      /**
52       * Create a new ProxyClassCache instance.
53       * 
54       * @param proxyClassGenerator
55       */
56      public ProxyClassCache(ProxyClassGenerator proxyClassGenerator)
57      {
58          this.proxyClassGenerator = proxyClassGenerator;
59      }
60  
61      //******************************************************************************************************************
62      // Other Methods
63      //******************************************************************************************************************
64  
65      private Map<Set<Class<?>>, WeakReference<Class<?>>> getClassCache(ClassLoader classLoader)
66      {
67          Map<Set<Class<?>>, WeakReference<Class<?>>> cache = loaderToClassCache.get(classLoader);
68          if (cache == null)
69          {
70              cache = new HashMap<Set<Class<?>>, WeakReference<Class<?>>>();
71              loaderToClassCache.put(classLoader, cache);
72          }
73          return cache;
74      }
75  
76      private Set<Class<?>> toClassCacheKey(Class<?>[] proxyClasses)
77      {
78          return new HashSet<Class<?>>(Arrays.asList(proxyClasses));
79      }
80  
81      /**
82       * Returns the proxy class generated by the {@link ProxyClassGenerator} using the specified {@link ClassLoader} and
83       * array of proxy classes.
84       * 
85       * @param classLoader
86       *            the classloader
87       * @param proxyClasses
88       *            the proxy classes
89       * @return the proxy class generated by the {@link ProxyClassGenerator} using the specified {@link ClassLoader} and
90       *         array of proxy classes
91       */
92      public synchronized Class<?> getProxyClass(ClassLoader classLoader, Class<?>[] proxyClasses)
93      {
94          final Map<Set<Class<?>>, WeakReference<Class<?>>> classCache = getClassCache(classLoader);
95          final Set<Class<?>> key = toClassCacheKey(proxyClasses);
96          Class<?> proxyClass;
97          Reference<Class<?>> proxyClassReference = classCache.get(key);
98          if (proxyClassReference == null)
99          {
100             proxyClass = proxyClassGenerator.generateProxyClass(classLoader, proxyClasses);
101             classCache.put(key, new WeakReference<Class<?>>(proxyClass));
102         }
103         else
104         {
105             synchronized (proxyClassReference)
106             {
107                 proxyClass = proxyClassReference.get();
108                 if (proxyClass == null)
109                 {
110                     proxyClass = proxyClassGenerator.generateProxyClass(classLoader, proxyClasses);
111                     classCache.put(key, new WeakReference<Class<?>>(proxyClass));
112                 }
113             }
114         }
115         return proxyClass;
116     }
117 }