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.proxy.factory.util;
19  
20  import java.lang.ref.WeakReference;
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.WeakHashMap;
24  
25  /**
26   * A cache for storing implementation classes for proxies based on a specific type of {@link ProxyClassGenerator}.  A
27   * proxy class cache ensures that there is only one class for every
28   * {@link ProxyClassGenerator}/{@link ClassLoader}/proxy class array combination.
29   *
30   * @author James Carman
31   * @since 1.0
32   */
33  public class ProxyClassCache
34  {
35  //----------------------------------------------------------------------------------------------------------------------
36  // Fields
37  //----------------------------------------------------------------------------------------------------------------------
38  
39      private final Map loaderToClassCache = new WeakHashMap();
40      private final ProxyClassGenerator proxyClassGenerator;
41  
42  //----------------------------------------------------------------------------------------------------------------------
43  // Constructors
44  //----------------------------------------------------------------------------------------------------------------------
45  
46      public ProxyClassCache( ProxyClassGenerator proxyClassGenerator )
47      {
48          this.proxyClassGenerator = proxyClassGenerator;
49      }
50  
51  //----------------------------------------------------------------------------------------------------------------------
52  // Other Methods
53  //----------------------------------------------------------------------------------------------------------------------
54  
55      /**
56       * Returns the proxy class generated by the {@link ProxyClassGenerator} using the specified {@link ClassLoader} and
57       * array of proxy classes.
58       * 
59       * @param classLoader the classloader
60       * @param proxyClasses the proxy classes
61       * @return the proxy class generated by the {@link ProxyClassGenerator} using the specified {@link ClassLoader} and
62       * array of proxy classes
63       */
64      public synchronized Class getProxyClass( ClassLoader classLoader, Class[] proxyClasses )
65      {
66          final Map classCache = getClassCache( classLoader );
67          final String key = toClassCacheKey( proxyClasses );
68          Class proxyClass;
69          WeakReference proxyClassReference = ( WeakReference )classCache.get( key );
70          if( proxyClassReference == null )
71          {
72              proxyClass = proxyClassGenerator.generateProxyClass( classLoader, proxyClasses );
73              classCache.put( key, new WeakReference( proxyClass ) );
74          }
75          else
76          {
77              synchronized( proxyClassReference )
78              {
79                  proxyClass = ( Class )proxyClassReference.get();
80                  if( proxyClass == null )
81                  {
82                      proxyClass = proxyClassGenerator.generateProxyClass( classLoader, proxyClasses );
83                      classCache.put( key, new WeakReference( proxyClass ) );
84                  }
85              }
86          }
87          return proxyClass;
88      }
89  
90      private Map getClassCache( ClassLoader classLoader )
91      {
92          Map cache = ( Map )loaderToClassCache.get( classLoader );
93          if( cache == null )
94          {
95              cache = new HashMap();
96              loaderToClassCache.put( classLoader, cache );
97          }
98          return cache;
99      }
100 
101     private String toClassCacheKey( Class[] proxyClasses )
102     {
103         final StringBuffer sb = new StringBuffer();
104         for( int i = 0; i < proxyClasses.length; i++ )
105         {
106             Class proxyInterface = proxyClasses[i];
107             sb.append( proxyInterface.getName() );
108             if( i != proxyClasses.length - 1 )
109             {
110                 sb.append( "," );
111             }
112         }
113         return sb.toString();
114     }
115 }
116