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.jcs.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              Cache<Object, Object> cache = manager.getOrCreateCache(OPENJPA_PREFIX, cls.getName());
53              if (cache == null)
54              {
55                  return null;
56              }
57              else
58              {
59                  result = cache.get(oid);
60              }
61          }
62          else
63          {
64              final CacheManager cacheManager = manager.getCacheManager();
65              for (final String cacheName : cacheManager.getCacheNames())
66              {
67                  if (!cacheName.startsWith(OPENJPA_PREFIX))
68                  {
69                      continue;
70                  }
71  
72                  result = cacheManager.getCache(cacheName).get(oid);
73                  if (result != null)
74                  {
75                      break;
76                  }
77              }
78          }
79          if (result == null)
80          {
81              return null;
82          }
83          return DataCachePCData.class.cast(result);
84      }
85  
86      @Override
87      protected DataCachePCData putInternal(final Object oid, final DataCachePCData pc)
88      {
89          manager.getOrCreateCache(OPENJPA_PREFIX, pc.getType().getName()).put(oid, pc);
90          return pc;
91      }
92  
93      @Override
94      protected DataCachePCData removeInternal(final Object oid)
95      {
96          if (OpenJPAId.class.isInstance(oid))
97          {
98              final Object remove = manager.getOrCreateCache(OPENJPA_PREFIX, OpenJPAId.class.cast(oid).getType().getName()).getAndRemove(oid);
99              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 }