View Javadoc
1   /**
2    * This file Copyright (c) 2008-2015 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;
35  
36  import info.magnolia.cms.util.DeprecationUtil;
37  import info.magnolia.module.ModuleLifecycleContext;
38  import info.magnolia.module.ModuleManager;
39  import info.magnolia.module.cache.mbean.CacheMonitor;
40  
41  import java.util.Collections;
42  import java.util.HashMap;
43  import java.util.HashSet;
44  import java.util.Map;
45  import java.util.Set;
46  
47  import javax.inject.Inject;
48  
49  /**
50   * The CacheModule holds several named ContentCachingConfiguration instances and a CacheFactory.
51   *
52   * @see ContentCachingConfiguration
53   * @see CacheFactory
54   */
55  public class CacheModule extends AbstractCacheModule {
56  
57      private final Set<CacheModuleLifecycleListener> listeners = Collections.synchronizedSet(new HashSet<CacheModuleLifecycleListener>());
58      private CacheFactory cacheFactory;
59      private Map<String, ContentCachingConfiguration> contentCaching = new HashMap<String, ContentCachingConfiguration>();
60      private ContentCompression compression;
61  
62  
63      @Inject
64      public CacheModule(CacheMonitor cacheMonitor) {
65          super(cacheMonitor);
66      }
67  
68      /**
69       * @deprecated since 5.4. Use {@link #CacheModule(info.magnolia.module.cache.mbean.CacheMonitor)} intead.
70       */
71      @Deprecated
72      public CacheModule(ModuleManager moduleManager, CacheMonitor cacheMonitor) {
73          this(cacheMonitor);
74      }
75  
76      /**
77       * Inject {@link info.magnolia.module.cache.inject.CacheFactoryProvider}} instead.
78       */
79      @Override
80      public CacheFactory getCacheFactory() {
81          return cacheFactory;
82      }
83  
84      public void setCacheFactory(CacheFactory cacheFactory) {
85          this.cacheFactory = cacheFactory;
86      }
87  
88      public Map<String, ContentCachingConfiguration> getContentCaching() {
89          return contentCaching;
90      }
91  
92      /**
93       * @deprecated since 5.4 use {@link #setContentCaching(java.util.Map)}}
94       */
95      @Deprecated
96      public ContentCachingConfiguration addConfiguration(String name, CacheConfiguration cacheConfiguration) {
97          DeprecationUtil.isDeprecated("Use info.magnolia.module.cache.CacheModule#setContentCaching instead.");
98          return contentCaching.put(name, cacheConfiguration);
99      }
100 
101     public void setContentCaching(Map<String, ContentCachingConfiguration> contentCaching) {
102         this.contentCaching = contentCaching;
103     }
104 
105     public void setCompression(ContentCompression compression) {
106         this.compression = compression;
107     }
108 
109     public ContentCompression getCompression() {
110         return compression;
111     }
112 
113     public void register(CacheModuleLifecycleListener listener) {
114         listeners.add(listener);
115     }
116 
117     /**
118      * @deprecated since 5.4 use {@link #getContentCaching(String)}
119      */
120     @Deprecated
121     public ContentCachingConfiguration getConfiguration(String name) {
122         DeprecationUtil.isDeprecated("Use info.magnolia.module.cache.CacheModule#getContentCaching instead.");
123         return this.getContentCaching(name);
124     }
125 
126     @Override
127     public ContentCachingConfiguration getContentCaching(String name) {
128         return this.getContentCaching().get(name) != null ? this.getContentCaching().get(name) : this.getContentCaching().get(DEFAULT_CACHE_CONFIG);
129     }
130 
131     // TODO : i still feel like we should separate module config bean and lifecycle
132     @Override
133     public void start(ModuleLifecycleContext moduleLifecycleContext) {
134 
135         cacheFactory.start(ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase());
136         this.getCacheMonitor().countStart();
137 
138         // Monitor and start flush policies for every configured pageCache
139         for (ContentCachingConfiguration cfg : contentCaching.values()) {
140             final String name = cfg.getCacheName();
141             this.startCache(name);
142         }
143 
144         for (CacheModuleLifecycleListener listener : listeners) {
145             listener.onCacheModuleStart();
146         }
147     }
148 
149     @Override
150     public void stop(ModuleLifecycleContext moduleLifecycleContext) {
151         this.getCacheMonitor().stop();
152         for (ContentCachingConfiguration cfg : contentCaching.values()) {
153             final String name = cfg.getCacheName();
154             this.stopCache(name);
155         }
156         cacheFactory.stop(ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase());
157     }
158 }