View Javadoc
1   /**
2    * This file Copyright (c) 2003-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.cms.util;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.module.ModuleRegistry;
38  import info.magnolia.observation.WorkspaceEventListenerRegistration;
39  import info.magnolia.repository.RepositoryConstants;
40  
41  import java.util.ArrayList;
42  import java.util.LinkedHashMap;
43  import java.util.List;
44  import java.util.Map;
45  
46  import javax.jcr.Node;
47  import javax.jcr.RepositoryException;
48  import javax.jcr.Session;
49  import javax.jcr.observation.EventListener;
50  
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  import com.google.common.collect.Lists;
55  
56  
57  /**
58   * Base class for managers that monitor configuration within modules and react when its changed to reload. Configuration
59   * is read from a sub path of each modules configuration node. Subclasses override either reload(List<Node>) or the pair
60   * onClear() and onRegister().
61   */
62  public abstract class ModuleConfigurationObservingManager extends WorkspaceObservingManager {
63  
64      private final Logger log = LoggerFactory.getLogger(getClass());
65  
66      private final String pathWithinModule;
67      private final ModuleRegistry moduleRegistry;
68      private final Map<String, WorkspaceEventListenerRegistration.Handle> pathToListenerMap = new LinkedHashMap<>();
69  
70      protected ModuleConfigurationObservingManager(String pathWithinModule, ModuleRegistry moduleRegistry) {
71          super(RepositoryConstants.CONFIG, null, true, (String[]) null);
72  
73          this.pathWithinModule = pathWithinModule;
74          this.moduleRegistry = moduleRegistry;
75      }
76  
77      @Override
78      protected void registerChangeListener(final EventListener eventListener) {
79          for (Map.Entry<String, WorkspaceEventListenerRegistration.Handle> entry : pathToListenerMap.entrySet()) {
80              try {
81                  WorkspaceEventListenerRegistration.Handle listenerRegistration = WorkspaceEventListenerRegistration.observe(getWorkspace(), entry.getKey(), instantiateEventListener())
82                          .withSubNodes(isIncludeSubNodes())
83                          .withNodeTypes(getNodeTypes())
84                          .withEventTypesMask(getEventTypesMask())
85                          .withDelay(getDelay(), getMaxDelay())
86                          .register();
87                  entry.setValue(listenerRegistration);
88              } catch (RepositoryException e) {
89                  log.warn("Error occurred during registration of an event listener", e);
90              }
91          }
92      }
93  
94      @Override
95      protected void onStart() {
96          // just in case if #start() is invoked multiple times unregister all listeners
97          for (WorkspaceEventListenerRegistration.Handle listenerRegistrationHandle : pathToListenerMap.values()) {
98              try {
99                  listenerRegistrationHandle.unregister();
100             } catch (RepositoryException e) {
101                 log.error("Unable to remove event listener from workspace {}", getWorkspace(), e);
102             }
103         }
104         pathToListenerMap.clear();
105         for (String moduleName : moduleRegistry.getModuleNames()) {
106             String path = "/modules/" + moduleName + "/" + pathWithinModule;
107             pathToListenerMap.put(path, null);
108         }
109     }
110 
111     @Override
112     protected void reload() {
113         try {
114             List<Node> nodes = getObservedNodes();
115             reload(nodes);
116         } catch (RepositoryException e) {
117             log.error("Reload of observed nodes failed", e);
118         }
119     }
120 
121     protected void reload(List<Node> nodes) throws RepositoryException {
122         onClear();
123         for (Node node : nodes) {
124             try {
125                 onRegister(node);
126             } catch (Exception e) {
127                 log.warn("Failed to reload the node [{}]", node.getPath());
128             }
129         }
130     }
131 
132     protected void onClear() throws RepositoryException {
133         // defaults to no action
134     }
135 
136     protected void onRegister(Node node) throws RepositoryException {
137         // defaults to no action
138     }
139 
140     protected List<Node> getObservedNodes() throws RepositoryException {
141 
142         Session session = getSession();
143 
144         List<Node> nodes = new ArrayList<Node>();
145         for (String observedPath : pathToListenerMap.keySet()) {
146             try {
147                 if (session.nodeExists(observedPath)) {
148                     nodes.add(session.getNode(observedPath));
149                 }
150             } catch (RepositoryException e) {
151                 log.error("Failed to acquire node for observed path [{}]", observedPath, e);
152             }
153         }
154         return nodes;
155     }
156 
157     protected Session getSession() throws RepositoryException {
158         return MgnlContext.getSystemContext().getJCRSession(RepositoryConstants.CONFIG);
159     }
160 
161     protected List<String> getObservedPaths() {
162         return Lists.newArrayList(pathToListenerMap.keySet());
163     }
164 
165 }