View Javadoc
1   /**
2    * This file Copyright (c) 2015-2016 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.resourceloader.file;
35  
36  import info.magnolia.dirwatch.WatcherCallback;
37  import info.magnolia.resourceloader.Resource;
38  import info.magnolia.resourceloader.ResourceStub;
39  import info.magnolia.resourceloader.util.PredicatedResourceVisitor;
40  import info.magnolia.resourceloader.util.ResourceTreeWalker;
41  
42  import java.nio.file.Files;
43  import java.nio.file.Path;
44  
45  import com.google.common.base.Function;
46  import com.google.common.base.Predicate;
47  
48  /**
49   * A {@link WatcherCallback} which dispatches file system modification events via {@link Function a callback}.
50   */
51  class FileWatcherCallback implements WatcherCallback {
52      private final FileSystemResourceOrigin origin;
53      private final Predicate<Path> watchedPathFilter;
54      private final Function<Resource, Void> modificationCallback;
55  
56      FileWatcherCallback(FileSystemResourceOrigin origin, Predicate<Path> watchedPathFilter, Function<Resource, Void> modificationCallback) {
57          this.origin = origin;
58          this.watchedPathFilter = watchedPathFilter;
59          this.modificationCallback = modificationCallback;
60      }
61  
62      /**
63       * Contents of the added folders are recursively handled in the same fashion. This is done due to the fact
64       * that only a single event is generated by the file system when a file/folder is added.
65       *
66       * @see <a href="https://jira.magnolia-cms.com/browse/MAGNOLIA-6278">MAGNOLIA-6278</a>
67       */
68      @Override
69      public void added(Path path) {
70          dispatchResourceChange(path, true);
71      }
72  
73      /**
74       * Resource modifications are handled non-recursively since:
75       * - the case of a mere file is essentially non-recursive
76       * - most of the folder modification events are generated in addition to content structure modification (file/sub-directory removed/added).
77       * and => do not need additional care (even can be ignored)
78       */
79      @Override
80      public void modified(Path path) {
81          dispatchResourceChange(path, false);
82      }
83  
84      /**
85       * To be consistent with the logic of {@link #added(Path)} - contents of the removed folders have to be handled recursively as well.
86       * The bad thing is that by the time the folder deletion event is processed - its contents become non-resolvable and => cannot be easily traversed.
87       * TODO - recursive traversal strategy has to be revised (probably subscribers should do that on their own).
88       */
89      @Override
90      public void removed(Path path) {
91          dispatchResourceChange(path, false);
92      }
93  
94      /**
95       * Communicate file system resource changes via {@link #modificationCallback callback}.
96       *
97       * @param handleDirectoryContents indicates whether directory contents should be communicated (recursively) as well
98       */
99      protected void dispatchResourceChange(Path changedResourcePath, boolean handleDirectoryContents) {
100         if (!watchedPathFilter.apply(changedResourcePath)) {
101             return;
102         }
103 
104         final FileSystemResource resource;
105 
106         if (Files.exists(changedResourcePath)) {
107             resource = origin.newResource(changedResourcePath);
108         } else {
109             // Resource has been deleted - notify the subscribers via a stub
110             modificationCallback.apply(ResourceStub.withPath(changedResourcePath.toString()));
111             return;
112         }
113 
114         if (Files.isRegularFile(changedResourcePath)) {
115             modificationCallback.apply(resource);
116         } else if (handleDirectoryContents && Files.isDirectory(changedResourcePath)) {
117             // Use the same path filter for resources as for the file system paths
118             final Predicate<Resource> watchedResourceFilter = new Predicate<Resource>() {
119                 @Override
120                 public boolean apply(Resource res) {
121                     return watchedPathFilter.apply(((FileSystemResource) res).getRealPath());
122                 }
123             };
124 
125             new ResourceTreeWalker(PredicatedResourceVisitor.with(watchedResourceFilter, modificationCallback)).traverse(resource);
126         }
127     }
128 }