001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.jcs3.jcache.openjpa; 020 021import org.apache.openjpa.datacache.AbstractQueryCache; 022import org.apache.openjpa.datacache.DataCacheManager; 023import org.apache.openjpa.datacache.QueryKey; 024import org.apache.openjpa.datacache.QueryResult; 025 026import javax.cache.Cache; 027import javax.cache.CacheManager; 028import java.util.Collection; 029import java.util.LinkedList; 030import java.util.concurrent.locks.Lock; 031import java.util.concurrent.locks.ReentrantLock; 032 033public class OpenJPAJCacheQueryCache extends AbstractQueryCache 034{ 035 private static final String OPENJPA_PREFIX = "openjpa.querycache."; 036 private static final String QUERY_CACHE_NAME = "query"; 037 038 private final Lock lock = new ReentrantLock(); 039 private OpenJPAJCacheDataCacheManager manager; 040 041 @Override 042 public void initialize(final DataCacheManager manager) 043 { 044 super.initialize(manager); 045 this.manager = OpenJPAJCacheDataCacheManager.class.cast(manager); 046 } 047 048 @Override 049 protected void clearInternal() 050 { 051 final CacheManager cacheManager = manager.getCacheManager(); 052 for (final String cacheName : cacheManager.getCacheNames()) 053 { 054 if (!cacheName.startsWith(OPENJPA_PREFIX)) 055 { 056 continue; 057 } 058 cacheManager.getCache(cacheName).clear(); 059 } 060 } 061 062 @Override 063 protected Collection keySet() 064 { 065 final Collection<QueryKey> keys = new LinkedList<>(); 066 for (final Cache.Entry<Object, Object> entry : queryCache()) 067 { 068 keys.add(QueryKey.class.cast(entry.getKey())); 069 } 070 return keys; 071 } 072 073 @Override 074 protected QueryResult getInternal(final QueryKey qk) 075 { 076 return QueryResult.class.cast(queryCache().get(qk)); 077 } 078 079 private Cache<Object, Object> queryCache() 080 { 081 return manager.getOrCreateCache(OPENJPA_PREFIX, QUERY_CACHE_NAME); 082 } 083 084 @Override 085 protected QueryResult putInternal(final QueryKey qk, final QueryResult oids) 086 { 087 queryCache().put(qk, oids); 088 return oids; 089 } 090 091 @Override 092 protected QueryResult removeInternal(final QueryKey qk) 093 { 094 final Object remove = queryCache().getAndRemove(qk); 095 if (remove == null) 096 { 097 return null; 098 } 099 return QueryResult.class.cast(remove); 100 } 101 102 @Override 103 protected boolean pinInternal(final QueryKey qk) 104 { 105 throw new UnsupportedOperationException(); 106 } 107 108 @Override 109 protected boolean unpinInternal(final QueryKey qk) 110 { 111 throw new UnsupportedOperationException(); 112 } 113 114 @Override 115 public void writeLock() 116 { 117 lock.lock(); 118 } 119 120 @Override 121 public void writeUnlock() 122 { 123 lock.unlock(); 124 } 125}