View Javadoc

1   /**
2    * This file Copyright (c) 2008-2014 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.module.InstallContext;
37  import info.magnolia.module.ModuleLifecycle;
38  import info.magnolia.module.ModuleLifecycleContext;
39  import info.magnolia.module.ModuleManager;
40  import info.magnolia.module.ModuleRegistry;
41  import info.magnolia.module.cache.mbean.CacheMonitor;
42  
43  import java.util.HashMap;
44  import java.util.HashSet;
45  import java.util.Map;
46  import java.util.Set;
47  import javax.inject.Inject;
48  
49  /**
50   * The CacheModule holds several named CacheConfiguration instances and a CacheFactory.
51   * @see CacheConfiguration
52   * @see CacheFactory
53   *
54   * @author gjoseph
55   * @version $Revision: $ ($Author: $)
56   */
57  public class CacheModule implements ModuleLifecycle {
58      private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CacheModule.class);
59  
60      private final ModuleManager moduleManager;
61      private final CacheMonitor cacheMonitor;
62  
63      private final Set<CacheModuleLifecycleListener> listeners = new HashSet<CacheModuleLifecycleListener>();
64      private CacheFactory cacheFactory;
65      private Map<String, CacheConfiguration> configurations = new HashMap<String, CacheConfiguration>();
66      private ContentCompression compression;
67  
68      /**
69       * @deprecated TODO since 4.5 - find another, cleaner, mechanism to provide the information this module needs instead of using ModuleManager.
70       */
71      @Inject
72      public CacheModule(ModuleManager moduleManager, CacheMonitor cacheMonitor) {
73          this.moduleManager = moduleManager;
74          this.cacheMonitor = cacheMonitor;
75      }
76  
77      public CacheFactory getCacheFactory() {
78          return cacheFactory;
79      }
80  
81      public void setCacheFactory(CacheFactory cacheFactory) {
82          this.cacheFactory = cacheFactory;
83      }
84  
85      public Map<String, CacheConfiguration> getConfigurations() {
86          return configurations;
87      }
88  
89      public void setConfigurations(Map<String, CacheConfiguration> configurations) {
90          this.configurations = configurations;
91      }
92  
93      public void addConfiguration(String name, CacheConfiguration config) {
94          configurations.put(name, config);
95      }
96  
97      public CacheConfiguration getConfiguration(String name) {
98          return configurations.get(name);
99      }
100 
101     public void setCompression(ContentCompression compression) {
102         this.compression = compression;
103     }
104 
105     public ContentCompression getCompression() {
106         return compression;
107     }
108 
109     public void register(CacheModuleLifecycleListener listener) {
110         listeners.add(listener);
111     }
112 
113     // TODO : i still feel like we should separate module config bean and lifecycle
114     @Override
115     public void start(ModuleLifecycleContext moduleLifecycleContext) {
116         // TODO : this is implementation dependent - some factories might need or want to be notified also on restart..
117         // if (moduleLifecycleContext.getPhase() == ModuleLifecycleContext.PHASE_SYSTEM_STARTUP) {
118         cacheFactory.start();
119         // }
120 
121         cacheMonitor.countStart();
122 
123         for (CacheConfiguration cfg : configurations.values()) {
124             final String name = cfg.getName();
125             final Cache cache = cacheFactory.getCache(name);
126             cacheMonitor.addCache(name);
127             if (cfg.getFlushPolicy() != null) {
128                 cfg.getFlushPolicy().start(cache);
129             } else {
130                 log.warn("Flush Policy is not configured properly for {} cache configuration.", name);
131             }
132         }
133 
134         for (CacheModuleLifecycleListener listener : listeners) {
135             listener.onCacheModuleStart();
136         }
137 
138         // if we're starting up the system, flush caches if we the system has just been installed or updated
139         if (moduleLifecycleContext.getPhase() == ModuleLifecycleContext.PHASE_SYSTEM_STARTUP) {
140             final InstallContext installContext = moduleManager.getInstallContext();
141             // InstallContext.status is set by ModuleManagerImpl.performInstallOrUpdate()
142             if (installContext.getStatus() != null) {
143                 log.info("Flushing all caches ...");
144                 for (CacheConfiguration config : configurations.values()) {
145                     cacheFactory.getCache(config.getName()).clear();
146                     log.info("  flushed cache: {}", config.getName());
147                 }
148             }
149         }
150     }
151 
152     @Override
153     public void stop(ModuleLifecycleContext moduleLifecycleContext) {
154         cacheMonitor.stop();
155         for (CacheConfiguration cfg : configurations.values()) {
156             final String name = cfg.getName();
157             Cache cache = null;
158             try {
159                 cache = cacheFactory.getCache(name);
160             } catch (IllegalStateException e) {
161                 log.warn("Cache {} is not running anymore. Check your configuration and log files to find out if there were any errors forcing cache shutdown.", name);
162             }
163             if (cfg.getFlushPolicy() != null) {
164                 cfg.getFlushPolicy().stop(cache);
165             } else {
166                 log.warn("Flush Policy is not configured properly for {} cache configuration.", name);
167             }
168         }
169 
170         // TODO : this is implementation dependent - some factories might need or want to be notified also on restart..
171         // TODO : there was a reason for this checking  of the phase, but i can't remember ...
172         // if (moduleLifecycleContext.getPhase() == ModuleLifecycleContext.PHASE_SYSTEM_SHUTDOWN) {
173         cacheFactory.stop();
174         // }
175     }
176 
177     /**
178      * @deprecated since 4.5, use IoC/CDI
179      */
180     public static CacheModule getInstance() {
181         return ModuleRegistry.Factory.getInstance().getModuleInstance(CacheModule.class);
182     }
183 }