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.AbstractDataCache; 022import org.apache.openjpa.datacache.DataCacheManager; 023import org.apache.openjpa.datacache.DataCachePCData; 024import org.apache.openjpa.util.OpenJPAId; 025 026import javax.cache.Cache; 027import javax.cache.CacheManager; 028import java.util.concurrent.locks.Lock; 029import java.util.concurrent.locks.ReentrantLock; 030 031public class OpenJPAJCacheDataCache extends AbstractDataCache 032{ 033 private static final String OPENJPA_PREFIX = "openjpa.datacache."; 034 035 private final Lock lock = new ReentrantLock(); 036 private OpenJPAJCacheDataCacheManager manager; 037 038 @Override 039 public void initialize(final DataCacheManager manager) 040 { 041 super.initialize(manager); 042 this.manager = OpenJPAJCacheDataCacheManager.class.cast(manager); 043 } 044 045 @Override 046 protected DataCachePCData getInternal(final Object oid) 047 { 048 Object result = null; 049 if (OpenJPAId.class.isInstance(oid)) 050 { 051 final Class<?> cls = OpenJPAId.class.cast(oid).getType(); 052 final Cache<Object, Object> cache = manager.getOrCreateCache(OPENJPA_PREFIX, cls.getName()); 053 if (cache == null) 054 { 055 return null; 056 } 057 result = cache.get(oid); 058 } 059 else 060 { 061 final CacheManager cacheManager = manager.getCacheManager(); 062 for (final String cacheName : cacheManager.getCacheNames()) 063 { 064 if (!cacheName.startsWith(OPENJPA_PREFIX)) 065 { 066 continue; 067 } 068 069 result = cacheManager.getCache(cacheName).get(oid); 070 if (result != null) 071 { 072 break; 073 } 074 } 075 } 076 if (result == null) 077 { 078 return null; 079 } 080 return DataCachePCData.class.cast(result); 081 } 082 083 @Override 084 protected DataCachePCData putInternal(final Object oid, final DataCachePCData pc) 085 { 086 manager.getOrCreateCache(OPENJPA_PREFIX, pc.getType().getName()).put(oid, pc); 087 return pc; 088 } 089 090 @Override 091 protected DataCachePCData removeInternal(final Object oid) 092 { 093 if (OpenJPAId.class.isInstance(oid)) 094 { 095 final Object remove = manager.getOrCreateCache(OPENJPA_PREFIX, OpenJPAId.class.cast(oid).getType().getName()).getAndRemove(oid); 096 if (remove == null) 097 { 098 return null; 099 } 100 return DataCachePCData.class.cast(remove); 101 } 102 return null; 103 } 104 105 @Override 106 protected void removeAllInternal(final Class<?> cls, final boolean subclasses) 107 { 108 final String name; 109 if (subclasses) 110 { 111 name = cls.getSuperclass().getName(); 112 } 113 else 114 { 115 name = cls.getName(); 116 } 117 manager.getOrCreateCache(OPENJPA_PREFIX, name).removeAll(); 118 } 119 120 @Override 121 protected void clearInternal() 122 { 123 final CacheManager cacheManager = manager.getCacheManager(); 124 for (final String cacheName : cacheManager.getCacheNames()) 125 { 126 if (!cacheName.startsWith(OPENJPA_PREFIX)) 127 { 128 continue; 129 } 130 cacheManager.getCache(cacheName).clear(); 131 } 132 } 133 134 @Override 135 protected boolean pinInternal(final Object oid) 136 { 137 throw new UnsupportedOperationException(); 138 } 139 140 @Override 141 protected boolean unpinInternal(final Object oid) 142 { 143 throw new UnsupportedOperationException(); 144 } 145 146 @Override 147 public void writeLock() 148 { 149 lock.lock(); 150 } 151 152 @Override 153 public void writeUnlock() 154 { 155 lock.unlock(); 156 } 157}