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