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