001/* 002 * Copyright 2002-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.core.type.classreading; 018 019import java.io.IOException; 020import java.util.LinkedHashMap; 021import java.util.Map; 022 023import org.springframework.core.io.Resource; 024import org.springframework.core.io.ResourceLoader; 025 026/** 027 * Caching implementation of the {@link MetadataReaderFactory} interface, 028 * caching a {@link MetadataReader} instance per Spring {@link Resource} handle 029 * (i.e. per ".class" file). 030 * 031 * @author Juergen Hoeller 032 * @author Costin Leau 033 * @since 2.5 034 */ 035public class CachingMetadataReaderFactory extends SimpleMetadataReaderFactory { 036 037 /** Default maximum number of entries for the MetadataReader cache: 256 */ 038 public static final int DEFAULT_CACHE_LIMIT = 256; 039 040 041 private volatile int cacheLimit = DEFAULT_CACHE_LIMIT; 042 043 @SuppressWarnings("serial") 044 private final Map<Resource, MetadataReader> metadataReaderCache = 045 new LinkedHashMap<Resource, MetadataReader>(DEFAULT_CACHE_LIMIT, 0.75f, true) { 046 @Override 047 protected boolean removeEldestEntry(Map.Entry<Resource, MetadataReader> eldest) { 048 return size() > getCacheLimit(); 049 } 050 }; 051 052 053 /** 054 * Create a new CachingMetadataReaderFactory for the default class loader. 055 */ 056 public CachingMetadataReaderFactory() { 057 super(); 058 } 059 060 /** 061 * Create a new CachingMetadataReaderFactory for the given resource loader. 062 * @param resourceLoader the Spring ResourceLoader to use 063 * (also determines the ClassLoader to use) 064 */ 065 public CachingMetadataReaderFactory(ResourceLoader resourceLoader) { 066 super(resourceLoader); 067 } 068 069 /** 070 * Create a new CachingMetadataReaderFactory for the given class loader. 071 * @param classLoader the ClassLoader to use 072 */ 073 public CachingMetadataReaderFactory(ClassLoader classLoader) { 074 super(classLoader); 075 } 076 077 078 /** 079 * Specify the maximum number of entries for the MetadataReader cache. 080 * <p>Default is 256. 081 */ 082 public void setCacheLimit(int cacheLimit) { 083 this.cacheLimit = cacheLimit; 084 } 085 086 /** 087 * Return the maximum number of entries for the MetadataReader cache. 088 */ 089 public int getCacheLimit() { 090 return this.cacheLimit; 091 } 092 093 094 @Override 095 public MetadataReader getMetadataReader(Resource resource) throws IOException { 096 if (getCacheLimit() <= 0) { 097 return super.getMetadataReader(resource); 098 } 099 synchronized (this.metadataReaderCache) { 100 MetadataReader metadataReader = this.metadataReaderCache.get(resource); 101 if (metadataReader == null) { 102 metadataReader = super.getMetadataReader(resource); 103 this.metadataReaderCache.put(resource, metadataReader); 104 } 105 return metadataReader; 106 } 107 } 108 109 /** 110 * Clear the entire MetadataReader cache, removing all cached class metadata. 111 */ 112 public void clearCache() { 113 synchronized (this.metadataReaderCache) { 114 this.metadataReaderCache.clear(); 115 } 116 } 117 118}