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.event.EventBus;
37  import info.magnolia.event.SystemEventBus;
38  import info.magnolia.module.ModuleLifecycleContext;
39  import info.magnolia.module.ModulesStartedEvent;
40  import info.magnolia.module.cache.factory.DelegatingCacheFactory;
41  import info.magnolia.module.cache.mbean.CacheMonitor;
42  
43  import java.util.Collections;
44  import java.util.HashMap;
45  import java.util.HashSet;
46  import java.util.Map;
47  import java.util.Set;
48  
49  import javax.inject.Inject;
50  import javax.inject.Named;
51  
52  /**
53   * The CacheModule holds several named ContentCachingConfiguration instances and a CacheFactory.
54   *
55   * @see ContentCachingConfiguration
56   * @see CacheFactory
57   */
58  public class CacheModule extends AbstractCacheModule {
59  
60      private final Set<CacheModuleLifecycleListener> listeners = Collections.synchronizedSet(new HashSet<CacheModuleLifecycleListener>());
61      private CacheFactory cacheFactory = new DelegatingCacheFactory();
62      private Map<String, ContentCachingConfiguration> contentCaching = new HashMap<String, ContentCachingConfiguration>();
63      private ContentCompression compression;
64  
65      @Inject
66      public CacheModule(CacheMonitor cacheMonitor, @Named(SystemEventBus.NAME) EventBus systemEventBus) {
67          super(cacheMonitor);
68          systemEventBus.addHandler(ModulesStartedEvent.class, new ModulesStartedEvent.Handler() {
69              @Override
70              public void onModuleStartupCompleted(ModulesStartedEvent event) {
71                  start();
72              }
73          });
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      public void setContentCaching(Map<String, ContentCachingConfiguration> contentCaching) {
93          this.contentCaching = contentCaching;
94      }
95  
96      public void setCompression(ContentCompression compression) {
97          this.compression = compression;
98      }
99  
100     public ContentCompression getCompression() {
101         return compression;
102     }
103 
104     public void register(CacheModuleLifecycleListener listener) {
105         listeners.add(listener);
106     }
107 
108     @Override
109     public ContentCachingConfiguration getContentCaching(String name) {
110         return this.getContentCaching().get(name) != null ? this.getContentCaching().get(name) : this.getContentCaching().get(DEFAULT_CACHE_CONFIG);
111     }
112 
113     @Override
114     public void start(ModuleLifecycleContext moduleLifecycleContext) {
115         cacheFactory.start(ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase());
116         this.getCacheMonitor().countStart();
117         if (ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase()) {
118             start();
119             for (CacheModuleLifecycleListener listener : listeners) {
120                 listener.onCacheModuleStart();
121             }
122         }
123     }
124 
125     @Override
126     public void stop(ModuleLifecycleContext moduleLifecycleContext) {
127         this.getCacheMonitor().stop();
128         for (ContentCachingConfiguration cfg : contentCaching.values()) {
129             final String name = cfg.getCacheName();
130             this.stopCache(name);
131         }
132         cacheFactory.stop(ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase());
133     }
134 
135     private void start() {
136         // Monitor and start flush policies for every configured pageCache
137         for (ContentCachingConfiguration cfg : contentCaching.values()) {
138             final String name = cfg.getCacheName();
139             this.startCache(name);
140         }
141     }
142 }