View Javadoc
1   /**
2    * This file Copyright (c) 2003-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.cms.util;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.module.ModuleRegistry;
38  import info.magnolia.repository.RepositoryConstants;
39  
40  import java.util.ArrayList;
41  import java.util.Collections;
42  import java.util.List;
43  
44  import javax.jcr.Node;
45  import javax.jcr.RepositoryException;
46  import javax.jcr.Session;
47  import javax.jcr.observation.EventIterator;
48  import javax.jcr.observation.EventListener;
49  
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  
54  /**
55   * Base class for managers that monitor configuration within modules and react when its changed to reload. Configuration
56   * is read from a sub path of each modules configuration node. Subclasses override either reload(List<Node>) or the pair
57   * onClear() and onRegister().
58   */
59  public abstract class ModuleConfigurationObservingManager extends WorkspaceObservingManager {
60  
61      private final Logger log = LoggerFactory.getLogger(getClass());
62  
63      private final String pathWithinModule;
64      private final ModuleRegistry moduleRegistry;
65      private final List<String> observedPaths = new ArrayList<String>();
66  
67      protected ModuleConfigurationObservingManager(String pathWithinModule, ModuleRegistry moduleRegistry) {
68          super(RepositoryConstants.CONFIG, null, true, (String[]) null);
69  
70          this.pathWithinModule = pathWithinModule;
71          this.moduleRegistry = moduleRegistry;
72      }
73  
74      @Override
75      protected void registerChangeListener(final EventListener eventListener) {
76          for (String observedPath : observedPaths) {
77              ObservationUtil.registerChangeListener(getWorkspace(), observedPath, isIncludeSubNodes(), getNodeTypes(), getEventTypesMask(), new EventListener() {
78                  @Override
79                  public void onEvent(EventIterator events) {
80                      eventListener.onEvent(events);
81                  }
82              });
83          }
84      }
85  
86      @Override
87      protected void onStart() {
88          for (String moduleName : moduleRegistry.getModuleNames()) {
89              String path = "/modules/" + moduleName + "/" + pathWithinModule;
90              observedPaths.add(path);
91          }
92      }
93  
94      @Override
95      protected void reload() {
96          try {
97              List<Node> nodes = getObservedNodes();
98              reload(nodes);
99          } catch (RepositoryException e) {
100             log.error("Reload of observed nodes failed", e);
101         }
102     }
103 
104     protected void reload(List<Node> nodes) throws RepositoryException {
105         onClear();
106         for (Node node : nodes) {
107             try {
108                 onRegister(node);
109             } catch (Exception e) {
110                 log.warn("Failed to reload the node [{}]", node.getPath());
111             }
112         }
113     }
114 
115     protected void onClear() throws RepositoryException {
116         // defaults to no action
117     }
118 
119     protected void onRegister(Node node) throws RepositoryException {
120         // defaults to no action
121     }
122 
123     protected List<Node> getObservedNodes() throws RepositoryException {
124 
125         Session session = getSession();
126 
127         List<Node> nodes = new ArrayList<Node>();
128         for (String observedPath : observedPaths) {
129             try {
130                 if (session.nodeExists(observedPath)) {
131                     nodes.add(session.getNode(observedPath));
132                 }
133             } catch (RepositoryException e) {
134                 log.error("Failed to acquire node for observed path [{}]", observedPath, e);
135             }
136         }
137         return nodes;
138     }
139 
140     protected Session getSession() throws RepositoryException {
141         return MgnlContext.getSystemContext().getJCRSession(RepositoryConstants.CONFIG);
142     }
143 
144     protected List<String> getObservedPaths() {
145         return Collections.unmodifiableList(observedPaths);
146     }
147 
148 }