1 /* 2 * ModeShape (http://www.modeshape.org) 3 * See the COPYRIGHT.txt file distributed with this work for information 4 * regarding copyright ownership. Some portions may be licensed 5 * to Red Hat, Inc. under one or more contributor license agreements. 6 * See the AUTHORS.txt file in the distribution for a full listing of 7 * individual contributors. 8 * 9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape 10 * is licensed to you under the terms of the GNU Lesser General Public License as 11 * published by the Free Software Foundation; either version 2.1 of 12 * the License, or (at your option) any later version. 13 * 14 * ModeShape is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * Lesser General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this software; if not, write to the Free 21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 23 */ 24 package org.modeshape.graph.connector.path.cache; 25 26 import java.util.concurrent.ConcurrentHashMap; 27 import java.util.concurrent.ConcurrentMap; 28 import net.jcip.annotations.ThreadSafe; 29 30 /** 31 * The repository-source level cache of workspace names to {@link WorkspaceCache workspace caches}. 32 * <p> 33 * This object gets created for each repository whenever the {@link PathCachePolicy cache policy} is set. When the cache policy is 34 * modified, the old {@code PathRepositoryCache} is {@link #close() closed} after the new path repository cache is created. 35 * </p> 36 */ 37 @ThreadSafe 38 public class PathRepositoryCache { 39 40 private final PathCachePolicy policy; 41 private final ConcurrentMap<String, WorkspaceCache> cachesByName = new ConcurrentHashMap<String, WorkspaceCache>(); 42 43 public PathRepositoryCache( PathCachePolicy policy ) { 44 this.policy = policy; 45 } 46 47 public void close() { 48 for (WorkspaceCache cache : cachesByName.values()) { 49 cache.close(); 50 } 51 } 52 53 /** 54 * Gets the cache for the named workspace, creating a cache if necessary. Subsequent calls to this method with the same 55 * workspace name must return the exact same cache instance. 56 * 57 * @param workspaceName the name of the workspace for which the cache should be returned. 58 * @return the cache instance associated with the workspace; never null 59 * @throws IllegalStateException if no cache exists for this workspace and an instance of the 60 * {@link PathCachePolicy#getCacheClass() cache class from the policy} cannot be created. 61 */ 62 public WorkspaceCache getCache( String workspaceName ) { 63 WorkspaceCache cache = cachesByName.get(workspaceName); 64 if (cache != null) return cache; 65 66 cache = newCache(workspaceName); 67 cachesByName.putIfAbsent(workspaceName, cache); 68 69 return cachesByName.get(workspaceName); 70 } 71 72 private final WorkspaceCache newCache( String workspaceName ) { 73 WorkspaceCache cache = null; 74 75 try { 76 cache = policy.getCacheClass().newInstance(); 77 cache.initialize(policy, workspaceName); 78 } catch (IllegalAccessException iae) { 79 throw new IllegalStateException(iae); 80 } catch (InstantiationException ie) { 81 throw new IllegalStateException(ie); 82 } 83 return cache; 84 } 85 86 }