View Javadoc

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