View Javadoc
1   /**
2    * This file Copyright (c) 2017-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.flushpolicy;
35  
36  import info.magnolia.dirwatch.DirectoryWatcherService;
37  import info.magnolia.dirwatch.WatcherCallback;
38  import info.magnolia.init.MagnoliaConfigurationProperties;
39  import info.magnolia.module.cache.Cache;
40  import info.magnolia.module.cache.CacheFactory;
41  import info.magnolia.module.cache.CacheModule;
42  import info.magnolia.module.cache.FlushPolicy;
43  import info.magnolia.resourceloader.file.FileSystemResourceOrigin;
44  
45  import java.io.IOException;
46  import java.nio.file.Path;
47  import java.nio.file.Paths;
48  import java.util.ArrayList;
49  import java.util.List;
50  import java.util.regex.Pattern;
51  
52  import javax.inject.Inject;
53  import javax.inject.Provider;
54  
55  /**
56   * Simple flush policy that will empty whole cache in case of file system origin changes.
57   */
58  public class LightModuleChangesFlushPolicy implements FlushPolicy {
59  
60      private List<PathToCacheMapping> pathToCacheMappings = new ArrayList<>();
61  
62      private final Provider<CacheFactory> cacheFactoryProvider;
63      private final DirectoryWatcherService directoryWatcherService;
64      private final Path lightModulePath;
65  
66      @Inject
67      public LightModuleChangesFlushPolicy(Provider<CacheFactory> cacheFactoryProvider, DirectoryWatcherService directoryWatcherService, MagnoliaConfigurationProperties properties) {
68          this.cacheFactoryProvider = cacheFactoryProvider;
69          this.directoryWatcherService = directoryWatcherService;
70          this.lightModulePath = Paths.get(properties.getProperty(FileSystemResourceOrigin.RESOURCES_DIR_PROPERTY));
71      }
72  
73      @Override
74      public void start(Cache cache) {
75          try {
76              directoryWatcherService.register(lightModulePath, path -> true, new WatcherCallback() {
77                  @Override
78                  public void added(Path path) {
79                      modified(path);
80                  }
81  
82                  @Override
83                  public void removed(Path path) {
84                      modified(path);
85                  }
86  
87                  @Override
88                  public void modified(Path path) {
89                      cacheFactoryProvider.get().getCacheNames().forEach((String cacheName) -> {
90                          if (pathToCacheMappings.stream().anyMatch(pathToCacheMapping -> cacheName.equals(pathToCacheMapping.getCacheName()) && pathToCacheMapping.compiledPattern.matcher(lightModulePath.relativize(path).toString()).matches())) {
91                              cacheFactoryProvider.get().getCache(cacheName).clear();
92                          }
93                      });
94                  }
95              });
96          } catch (IOException e) {
97              throw new RuntimeException(e);
98          }
99      }
100 
101     public List<PathToCacheMapping> getPathToCacheMappings() {
102         return pathToCacheMappings;
103     }
104 
105     public void setPathToCacheMappings(List<PathToCacheMapping> pathToCacheMappings) {
106         this.pathToCacheMappings = pathToCacheMappings;
107     }
108 
109     @Override
110     public void stop(Cache cache) {
111     }
112 
113     /**
114      * Configuration class for assigning light module's directories to trigger cache flushing.
115      */
116     public static class PathToCacheMapping {
117 
118         private String cacheName = CacheModule.DEFAULT_CACHE_CONFIG;
119         private String pattern = ".*";
120 
121         private Pattern compiledPattern;
122 
123         public void init() {
124             compiledPattern = Pattern.compile(pattern);
125         }
126 
127         public String getPattern() {
128             return pattern;
129         }
130 
131         public void setPattern(String pattern) {
132             this.pattern = pattern;
133         }
134 
135         public String getCacheName() {
136             return cacheName;
137         }
138 
139         public void setCacheName(String cacheName) {
140             this.cacheName = cacheName;
141         }
142     }
143 }