Clover icon

Magnolia Module Cache 5.5.9

  1. Project Clover database Mon Nov 25 2019 16:46:50 CET
  2. Package info.magnolia.module.cache.factory

File DelegatingCacheFactory.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart2.png
71% of files have more coverage

Code metrics

4
9
9
2
153
90
11
1.22
1
4.5
1.22
48.8% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
DelegatingCacheFactory 49 6 55.6% 4 9
0.2525%
DelegatingCacheFactory.NullCache 105 3 37.5% 7 10
0.00%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /**
2    * This file Copyright (c) 2017-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.module.cache.factory;
35   
36    import info.magnolia.module.cache.Cache;
37    import info.magnolia.module.cache.CacheFactory;
38   
39    import java.util.ArrayList;
40    import java.util.Collection;
41    import java.util.List;
42   
43    import org.slf4j.Logger;
44    import org.slf4j.LoggerFactory;
45   
46    /**
47    * Delegating {@link info.magnolia.module.cache.CacheFactory} which delegates to another factories until the cache with requested name is found.
48    */
 
49    public class DelegatingCacheFactory implements CacheFactory {
50   
51    private final Logger log = LoggerFactory.getLogger(DelegatingCacheFactory.class);
52   
53    private List<CacheFactory> delegateFactories = new ArrayList<>();
54   
 
55  1 toggle @Override
56    public Cache getCache(String name) {
57  1 for (CacheFactory cacheFactory : delegateFactories) {
58  0 if (cacheFactory.getCache(name) != null) {
59  0 return cacheFactory.getCache(name);
60    }
61    }
62  1 return new NullCache();
63    }
64   
 
65    toggle @Override
66    public void start(boolean isRestart) {
67    for (CacheFactory cacheFactory : delegateFactories) {
68    cacheFactory.start(isRestart);
69    }
70    }
71   
 
72    toggle @Override
73    public void stop(boolean isRestart) {
74    for (CacheFactory cacheFactory : delegateFactories) {
75    cacheFactory.stop(isRestart);
76    }
77    }
78   
 
79    toggle @Override
80    public List<String> getCacheNames() {
81    List<String> cacheNames = new ArrayList<>();
82    for (CacheFactory cacheFactory : delegateFactories) {
83    cacheNames.addAll(cacheFactory.getCacheNames());
84    }
85    return cacheNames;
86    }
87   
 
88  0 toggle public void init() {
89  0 if (delegateFactories.isEmpty()) {
90  0 log.warn("No cache factory configured. Please install a cache implementation module");
91    }
92    }
93   
 
94    toggle public List<CacheFactory> getDelegateFactories() {
95    return delegateFactories;
96    }
97   
 
98    toggle public void setDelegateFactories(List<CacheFactory> delegateFactories) {
99    this.delegateFactories = delegateFactories;
100    }
101   
102    /**
103    * Fallback cache object.
104    */
 
105    class NullCache implements Cache {
106   
 
107  0 toggle @Override
108    public boolean hasElement(Object key) {
109  0 return false;
110    }
111   
 
112  0 toggle @Override
113    public void put(Object key, Object value) {
114    }
115   
 
116  0 toggle @Override
117    public void put(Object key, Object value, int timeToLiveInSeconds) {
118    }
119   
 
120  0 toggle @Override
121    public Object get(Object key) {
122  0 return null;
123    }
124   
 
125  0 toggle @Override
126    public Object getQuiet(Object key) {
127  0 return null;
128    }
129   
 
130  0 toggle @Override
131    public void remove(Object key) {
132    }
133   
 
134  0 toggle @Override
135    public void clear() {
136    }
137   
 
138    toggle @Override
139    public String getName() {
140    return null;
141    }
142   
 
143    toggle @Override
144    public int getSize() {
145    return 0;
146    }
147   
 
148    toggle @Override
149    public Collection<Object> getKeys() {
150    return null;
151    }
152    }
153    }