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.AbstractQueryCache;
22  import org.apache.openjpa.datacache.DataCacheManager;
23  import org.apache.openjpa.datacache.QueryKey;
24  import org.apache.openjpa.datacache.QueryResult;
25  
26  import javax.cache.Cache;
27  import javax.cache.CacheManager;
28  import java.util.Collection;
29  import java.util.LinkedList;
30  import java.util.concurrent.locks.Lock;
31  import java.util.concurrent.locks.ReentrantLock;
32  
33  public class OpenJPAJCacheQueryCache extends AbstractQueryCache
34  {
35      private static final String OPENJPA_PREFIX = "openjpa.querycache.";
36      private static final String QUERY_CACHE_NAME = "query";
37  
38      private final Lock lock = new ReentrantLock();
39      private OpenJPAJCacheDataCacheManager manager;
40  
41      @Override
42      public void initialize(final DataCacheManager manager)
43      {
44          super.initialize(manager);
45          this.manager = OpenJPAJCacheDataCacheManager.class.cast(manager);
46      }
47  
48      @Override
49      protected void clearInternal()
50      {
51          final CacheManager cacheManager = manager.getCacheManager();
52          for (final String cacheName : cacheManager.getCacheNames())
53          {
54              if (!cacheName.startsWith(OPENJPA_PREFIX))
55              {
56                  continue;
57              }
58              cacheManager.getCache(cacheName).clear();
59          }
60      }
61  
62      @Override
63      protected Collection keySet()
64      {
65          final Collection<QueryKey> keys = new LinkedList<QueryKey>();
66          for (final Cache.Entry<Object, Object> entry : queryCache())
67          {
68              keys.add(QueryKey.class.cast(entry.getKey()));
69          }
70          return keys;
71      }
72  
73      @Override
74      protected QueryResult getInternal(final QueryKey qk)
75      {
76          return QueryResult.class.cast(queryCache().get(qk));
77      }
78  
79      private Cache<Object, Object> queryCache()
80      {
81          return manager.getOrCreateCache(OPENJPA_PREFIX, QUERY_CACHE_NAME);
82      }
83  
84      @Override
85      protected QueryResult putInternal(final QueryKey qk, final QueryResult oids)
86      {
87          queryCache().put(qk, oids);
88          return oids;
89      }
90  
91      @Override
92      protected QueryResult removeInternal(final QueryKey qk)
93      {
94          final Object remove = queryCache().getAndRemove(qk);
95          if (remove == null)
96          {
97              return null;
98          }
99          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 }