View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.jcs3.jcache.openjpa;
20  
21  import org.apache.openjpa.datacache.AbstractDataCache;
22  import org.apache.openjpa.datacache.DataCacheManager;
23  import org.apache.openjpa.datacache.DataCachePCData;
24  import org.apache.openjpa.util.OpenJPAId;
25  
26  import javax.cache.Cache;
27  import javax.cache.CacheManager;
28  import java.util.concurrent.locks.Lock;
29  import java.util.concurrent.locks.ReentrantLock;
30  
31  public class OpenJPAJCacheDataCache extends AbstractDataCache
32  {
33      private static final String OPENJPA_PREFIX = "openjpa.datacache.";
34  
35      private final Lock lock = new ReentrantLock();
36      private OpenJPAJCacheDataCacheManager manager;
37  
38      @Override
39      public void initialize(final DataCacheManager manager)
40      {
41          super.initialize(manager);
42          this.manager = OpenJPAJCacheDataCacheManager.class.cast(manager);
43      }
44  
45      @Override
46      protected DataCachePCData getInternal(final Object oid)
47      {
48          Object result = null;
49          if (OpenJPAId.class.isInstance(oid))
50          {
51              final Class<?> cls = OpenJPAId.class.cast(oid).getType();
52              final Cache<Object, Object> cache = manager.getOrCreateCache(OPENJPA_PREFIX, cls.getName());
53              if (cache == null)
54              {
55                  return null;
56              }
57              result = cache.get(oid);
58          }
59          else
60          {
61              final CacheManager cacheManager = manager.getCacheManager();
62              for (final String cacheName : cacheManager.getCacheNames())
63              {
64                  if (!cacheName.startsWith(OPENJPA_PREFIX))
65                  {
66                      continue;
67                  }
68  
69                  result = cacheManager.getCache(cacheName).get(oid);
70                  if (result != null)
71                  {
72                      break;
73                  }
74              }
75          }
76          if (result == null)
77          {
78              return null;
79          }
80          return DataCachePCData.class.cast(result);
81      }
82  
83      @Override
84      protected DataCachePCData putInternal(final Object oid, final DataCachePCData pc)
85      {
86          manager.getOrCreateCache(OPENJPA_PREFIX, pc.getType().getName()).put(oid, pc);
87          return pc;
88      }
89  
90      @Override
91      protected DataCachePCData removeInternal(final Object oid)
92      {
93          if (OpenJPAId.class.isInstance(oid))
94          {
95              final Object remove = manager.getOrCreateCache(OPENJPA_PREFIX, OpenJPAId.class.cast(oid).getType().getName()).getAndRemove(oid);
96              if (remove == null)
97              {
98                  return null;
99              }
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 }