001/* 002 * Copyright 2012-2017 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 * http://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.boot.type.classreading; 018 019import java.io.IOException; 020import java.util.Map; 021 022import org.springframework.core.io.Resource; 023import org.springframework.core.io.ResourceLoader; 024import org.springframework.core.type.classreading.CachingMetadataReaderFactory; 025import org.springframework.core.type.classreading.MetadataReader; 026import org.springframework.core.type.classreading.MetadataReaderFactory; 027import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; 028import org.springframework.util.ConcurrentReferenceHashMap; 029 030/** 031 * Caching implementation of the {@link MetadataReaderFactory} interface backed by a 032 * {@link ConcurrentReferenceHashMap}, caching {@link MetadataReader} per Spring 033 * {@link Resource} handle (i.e. per ".class" file). 034 * 035 * @author Phillip Webb 036 * @since 1.4.0 037 * @see CachingMetadataReaderFactory 038 */ 039public class ConcurrentReferenceCachingMetadataReaderFactory 040 extends SimpleMetadataReaderFactory { 041 042 private final Map<Resource, MetadataReader> cache = new ConcurrentReferenceHashMap<>(); 043 044 /** 045 * Create a new {@link ConcurrentReferenceCachingMetadataReaderFactory} instance for 046 * the default class loader. 047 */ 048 public ConcurrentReferenceCachingMetadataReaderFactory() { 049 } 050 051 /** 052 * Create a new {@link ConcurrentReferenceCachingMetadataReaderFactory} instance for 053 * the given resource loader. 054 * @param resourceLoader the Spring ResourceLoader to use (also determines the 055 * ClassLoader to use) 056 */ 057 public ConcurrentReferenceCachingMetadataReaderFactory( 058 ResourceLoader resourceLoader) { 059 super(resourceLoader); 060 } 061 062 /** 063 * Create a new {@link ConcurrentReferenceCachingMetadataReaderFactory} instance for 064 * the given class loader. 065 * @param classLoader the ClassLoader to use 066 */ 067 public ConcurrentReferenceCachingMetadataReaderFactory(ClassLoader classLoader) { 068 super(classLoader); 069 } 070 071 @Override 072 public MetadataReader getMetadataReader(Resource resource) throws IOException { 073 MetadataReader metadataReader = this.cache.get(resource); 074 if (metadataReader == null) { 075 metadataReader = createMetadataReader(resource); 076 this.cache.put(resource, metadataReader); 077 } 078 return metadataReader; 079 } 080 081 /** 082 * Create the meta-data reader. 083 * @param resource the source resource. 084 * @return the meta-data reader 085 * @throws IOException on error 086 */ 087 protected MetadataReader createMetadataReader(Resource resource) throws IOException { 088 return super.getMetadataReader(resource); 089 } 090 091 /** 092 * Clear the entire MetadataReader cache, removing all cached class metadata. 093 */ 094 public void clearCache() { 095 this.cache.clear(); 096 } 097 098}