View Javadoc
1   /**
2    * This file Copyright (c) 2017 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      @Override
56      public Cache getCache(String name) {
57          for (CacheFactory cacheFactory : delegateFactories) {
58              if (cacheFactory.getCache(name) != null) {
59                  return cacheFactory.getCache(name);
60              }
61          }
62          return new NullCache();
63      }
64  
65      @Override
66      public void start(boolean isRestart) {
67          for (CacheFactory cacheFactory : delegateFactories) {
68              cacheFactory.start(isRestart);
69          }
70      }
71  
72      @Override
73      public void stop(boolean isRestart) {
74          for (CacheFactory cacheFactory : delegateFactories) {
75              cacheFactory.stop(isRestart);
76          }
77      }
78  
79      @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      public void init() {
89          if (delegateFactories.isEmpty()) {
90              log.warn("No cache factory configured. Please install a cache implementation module");
91          }
92      }
93  
94      public List<CacheFactory> getDelegateFactories() {
95          return delegateFactories;
96      }
97  
98      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         @Override
108         public boolean hasElement(Object key) {
109             return false;
110         }
111 
112         @Override
113         public void put(Object key, Object value) {
114         }
115 
116         @Override
117         public void put(Object key, Object value, int timeToLiveInSeconds) {
118         }
119 
120         @Override
121         public Object get(Object key) {
122             return null;
123         }
124 
125         @Override
126         public Object getQuiet(Object key) {
127             return null;
128         }
129 
130         @Override
131         public void remove(Object key) {
132         }
133 
134         @Override
135         public void clear() {
136         }
137 
138         @Override
139         public String getName() {
140             return null;
141         }
142 
143         @Override
144         public int getSize() {
145             return 0;
146         }
147 
148         @Override
149         public Collection<Object> getKeys() {
150             return null;
151         }
152     }
153 }