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;
20  
21  import javax.cache.CacheManager;
22  import javax.cache.configuration.OptionalFeature;
23  import javax.cache.spi.CachingProvider;
24  import java.net.URI;
25  import java.util.Map;
26  import java.util.Properties;
27  import java.util.concurrent.ConcurrentHashMap;
28  import java.util.concurrent.ConcurrentMap;
29  
30  public class JCSCachingProvider implements CachingProvider
31  {
32      public static final URI DEFAULT_URI = URI.create("jcs://jcache.ccf");
33  
34      private final ConcurrentMap<ClassLoader, ConcurrentMap<URI, CacheManager>> cacheManagersByLoader = new ConcurrentHashMap<ClassLoader, ConcurrentMap<URI, CacheManager>>();
35  
36      @Override
37      public CacheManager getCacheManager(final URI inUri, final ClassLoader inClassLoader, final Properties properties)
38      {
39          final URI uri = inUri != null ? inUri : getDefaultURI();
40          final ClassLoader classLoader = inClassLoader != null ? inClassLoader : getDefaultClassLoader();
41  
42          ConcurrentMap<URI, CacheManager> managers = cacheManagersByLoader.get(classLoader);
43          if (managers == null)
44          {
45              managers = new ConcurrentHashMap<URI, CacheManager>();
46              final ConcurrentMap<URI, CacheManager> existingManagers = cacheManagersByLoader.putIfAbsent(classLoader, managers);
47              if (existingManagers != null)
48              {
49                  managers = existingManagers;
50              }
51          }
52  
53          CacheManager mgr = managers.get(uri);
54          if (mgr == null)
55          {
56              mgr = new JCSCachingManager(this, uri, classLoader, properties);
57              final CacheManager existing = managers.putIfAbsent(uri, mgr);
58              if (existing != null)
59              {
60                  mgr = existing;
61              }
62          }
63  
64          return mgr;
65      }
66  
67      @Override
68      public URI getDefaultURI()
69      {
70          return DEFAULT_URI;
71      }
72  
73      @Override
74      public void close()
75      {
76          for (final Map<URI, CacheManager> v : cacheManagersByLoader.values())
77          {
78              for (final CacheManager m : v.values())
79              {
80                  m.close();
81              }
82              v.clear();
83          }
84          cacheManagersByLoader.clear();
85      }
86  
87      @Override
88      public void close(final ClassLoader classLoader)
89      {
90          final Map<URI, CacheManager> cacheManagers = cacheManagersByLoader.remove(classLoader);
91          if (cacheManagers != null)
92          {
93              for (final CacheManager mgr : cacheManagers.values())
94              {
95                  mgr.close();
96              }
97              cacheManagers.clear();
98          }
99      }
100 
101     @Override
102     public void close(final URI uri, final ClassLoader classLoader)
103     {
104         final Map<URI, CacheManager> cacheManagers = cacheManagersByLoader.remove(classLoader);
105         if (cacheManagers != null)
106         {
107             final CacheManager mgr = cacheManagers.remove(uri);
108             if (mgr != null)
109             {
110                 mgr.close();
111             }
112         }
113     }
114 
115     @Override
116     public CacheManager getCacheManager(final URI uri, final ClassLoader classLoader)
117     {
118         return getCacheManager(uri, classLoader, getDefaultProperties());
119     }
120 
121     @Override
122     public CacheManager getCacheManager()
123     {
124         return getCacheManager(getDefaultURI(), getDefaultClassLoader());
125     }
126 
127     @Override
128     public boolean isSupported(final OptionalFeature optionalFeature)
129     {
130         return optionalFeature == OptionalFeature.STORE_BY_REFERENCE;
131     }
132 
133     @Override
134     public ClassLoader getDefaultClassLoader()
135     {
136         return JCSCachingProvider.class.getClassLoader();
137     }
138 
139     @Override
140     public Properties getDefaultProperties()
141     {
142         return new Properties();
143     }
144 
145     void remove(final CacheManager mgr)
146     {
147         final ClassLoader classLoader = mgr.getClassLoader();
148         final Map<URI, CacheManager> mgrs = cacheManagersByLoader.get(classLoader);
149         if (mgrs != null)
150         {
151             mgrs.remove(mgr.getURI());
152             if (mgrs.isEmpty())
153             {
154                 cacheManagersByLoader.remove(classLoader);
155             }
156         }
157     }
158 }