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  import info.magnolia.objectfactory.Components;
43  
44  import java.util.Collections;
45  import java.util.HashMap;
46  import java.util.HashSet;
47  import java.util.Map;
48  import java.util.Set;
49  
50  import javax.inject.Inject;
51  import javax.inject.Named;
52  
53  /**
54   * The CacheModule holds several named ContentCachingConfiguration instances and a CacheFactory.
55   *
56   * @see ContentCachingConfiguration
57   * @see CacheFactory
58   */
59  public class CacheModule extends AbstractCacheModule {
60  
61      private final Set<CacheModuleLifecycleListener> listeners = Collections.synchronizedSet(new HashSet<CacheModuleLifecycleListener>());
62      private CacheFactory cacheFactory = new DelegatingCacheFactory();
63      private Map<String, ContentCachingConfiguration> contentCaching = new HashMap<String, ContentCachingConfiguration>();
64      private ContentCompression compression;
65  
66      @Inject
67      public CacheModule(CacheMonitor cacheMonitor, @Named(SystemEventBus.NAME) EventBus systemEventBus) {
68          super(cacheMonitor);
69          systemEventBus.addHandler(ModulesStartedEvent.class, new ModulesStartedEvent.Handler() {
70              @Override
71              public void onModuleStartupCompleted(ModulesStartedEvent event) {
72                  start();
73              }
74          });
75      }
76  
77      // only needed as a workaround for MGNLCACHE-223 until MAGNOLIA-7618
78      public CacheModule() {
79          super(Components.getComponent(CacheMonitor.class));
80      }
81  
82      /**
83       * Inject {@link info.magnolia.module.cache.inject.CacheFactoryProvider}} instead.
84       */
85      @Override
86      public CacheFactory getCacheFactory() {
87          return cacheFactory;
88      }
89  
90      public void setCacheFactory(CacheFactory cacheFactory) {
91          this.cacheFactory = cacheFactory;
92      }
93  
94      public Map<String, ContentCachingConfiguration> getContentCaching() {
95          return contentCaching;
96      }
97  
98      public void setContentCaching(Map<String, ContentCachingConfiguration> contentCaching) {
99          this.contentCaching = contentCaching;
100     }
101 
102     public void setCompression(ContentCompression compression) {
103         this.compression = compression;
104     }
105 
106     public ContentCompression getCompression() {
107         return compression;
108     }
109 
110     public void register(CacheModuleLifecycleListener listener) {
111         listeners.add(listener);
112     }
113 
114     @Override
115     public ContentCachingConfiguration getContentCaching(String name) {
116         return this.getContentCaching().get(name) != null ? this.getContentCaching().get(name) : this.getContentCaching().get(DEFAULT_CACHE_CONFIG);
117     }
118 
119     @Override
120     public void start(ModuleLifecycleContext moduleLifecycleContext) {
121         cacheFactory.start(ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase());
122         this.getCacheMonitor().countStart();
123         if (ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase()) {
124             start();
125             for (CacheModuleLifecycleListener listener : listeners) {
126                 listener.onCacheModuleStart();
127             }
128         }
129     }
130 
131     @Override
132     public void stop(ModuleLifecycleContext moduleLifecycleContext) {
133         this.getCacheMonitor().stop();
134         for (ContentCachingConfiguration cfg : contentCaching.values()) {
135             final String name = cfg.getCacheName();
136             this.stopCache(name);
137         }
138         cacheFactory.stop(ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase());
139     }
140 
141     private void start() {
142         // Monitor and start flush policies for every configured pageCache
143         for (ContentCachingConfiguration cfg : contentCaching.values()) {
144             final String name = cfg.getCacheName();
145             this.startCache(name);
146         }
147     }
148 }