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.jcs.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 Cache<Object, Object> cache = manager.getOrCreateCache(OPENJPA_PREFIX, cls.getName()); 053 if (cache == null) 054 { 055 return null; 056 } 057 else 058 { 059 result = cache.get(oid); 060 } 061 } 062 else 063 { 064 final CacheManager cacheManager = manager.getCacheManager(); 065 for (final String cacheName : cacheManager.getCacheNames()) 066 { 067 if (!cacheName.startsWith(OPENJPA_PREFIX)) 068 { 069 continue; 070 } 071 072 result = cacheManager.getCache(cacheName).get(oid); 073 if (result != null) 074 { 075 break; 076 } 077 } 078 } 079 if (result == null) 080 { 081 return null; 082 } 083 return DataCachePCData.class.cast(result); 084 } 085 086 @Override 087 protected DataCachePCData putInternal(final Object oid, final DataCachePCData pc) 088 { 089 manager.getOrCreateCache(OPENJPA_PREFIX, pc.getType().getName()).put(oid, pc); 090 return pc; 091 } 092 093 @Override 094 protected DataCachePCData removeInternal(final Object oid) 095 { 096 if (OpenJPAId.class.isInstance(oid)) 097 { 098 final Object remove = manager.getOrCreateCache(OPENJPA_PREFIX, OpenJPAId.class.cast(oid).getType().getName()).getAndRemove(oid); 099 if (remove == null) 100 { 101 return null; 102 } 103 return DataCachePCData.class.cast(remove); 104 } 105 return null; 106 } 107 108 @Override 109 protected void removeAllInternal(final Class<?> cls, final boolean subclasses) 110 { 111 final String name; 112 if (subclasses) 113 { 114 name = cls.getSuperclass().getName(); 115 } 116 else 117 { 118 name = cls.getName(); 119 } 120 manager.getOrCreateCache(OPENJPA_PREFIX, name).removeAll(); 121 } 122 123 @Override 124 protected void clearInternal() 125 { 126 final CacheManager cacheManager = manager.getCacheManager(); 127 for (final String cacheName : cacheManager.getCacheNames()) 128 { 129 if (!cacheName.startsWith(OPENJPA_PREFIX)) 130 { 131 continue; 132 } 133 cacheManager.getCache(cacheName).clear(); 134 } 135 } 136 137 @Override 138 protected boolean pinInternal(final Object oid) 139 { 140 throw new UnsupportedOperationException(); 141 } 142 143 @Override 144 protected boolean unpinInternal(final Object oid) 145 { 146 throw new UnsupportedOperationException(); 147 } 148 149 @Override 150 public void writeLock() 151 { 152 lock.lock(); 153 } 154 155 @Override 156 public void writeUnlock() 157 { 158 lock.unlock(); 159 } 160}