View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 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.beans.config;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.DefaultContent;
38  import info.magnolia.cms.util.ObservationUtil;
39  import info.magnolia.cms.util.SystemContentWrapper;
40  import info.magnolia.context.MgnlContext;
41  import info.magnolia.repository.RepositoryConstants;
42  
43  import java.util.ArrayList;
44  import java.util.HashSet;
45  import java.util.Iterator;
46  import java.util.List;
47  import java.util.Set;
48  
49  import javax.jcr.Node;
50  import javax.jcr.RepositoryException;
51  import javax.jcr.Session;
52  import javax.jcr.observation.EventIterator;
53  import javax.jcr.observation.EventListener;
54  
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  
59  /**
60   * A lot of "manager" objects are observed. Will mean that they reload the registered content after the content was changed. To
61   * centralize this code we use this abstract manager. A subclass will implement onRegister and onClear.
62   * @author philipp
63   */
64  public abstract class ObservedManager {
65      protected final Logger log = LoggerFactory.getLogger(getClass());
66  
67      /**
68       * UUIDs of the registered main nodes. They will get registered again after a change.
69       */
70      protected Set<String> registeredUUIDs = new HashSet<String>();
71  
72      /**
73       * Register a node. The uuid is cached and then onRegister() called.
74       * @param node the node to register
75       */
76      public synchronized void register(Content node) {
77          if (node == null) {
78              log.warn("Tried to register a non-existing node!");
79              return;
80          }
81  
82          ObservationUtil.registerDeferredChangeListener(RepositoryConstants.CONFIG, node.getHandle(), new EventListener() {
83  
84              @Override
85              public void onEvent(EventIterator events) {
86                  reload();
87              }
88          }, 1000, 5000);
89  
90          try {
91              registeredUUIDs.add(node.getUUID());
92              onRegister(new SystemContentWrapper(node));
93          }
94          catch (Exception e) {
95              log.warn("Was not able to register [" + node.getHandle() + "]", e);
96          }
97      }
98  
99      /**
100      * Calls onClear and reregister the nodes by calling onRegister.
101      */
102     public synchronized void reload() {
103         // Call onClear and reregister the nodes by calling onRegister
104         onClear();
105 
106         Session session = null;
107         try {
108             session = MgnlContext.getSystemContext().getJCRSession(RepositoryConstants.CONFIG);
109         } catch (RepositoryException e) {
110             throw new RuntimeException(e);
111         }
112 
113         // copy to avoid ConcurrentModificationException since the list get changed during iteration
114         List<String> uuids = new ArrayList<String>(registeredUUIDs);
115 
116         for (Iterator<String> iter = uuids.iterator(); iter.hasNext();) {
117             String uuid = iter.next();
118             try {
119                 Node node = session.getNodeByIdentifier(uuid);
120                 reload(new DefaultContent(node));
121             }
122             catch (Exception e) {
123                 registeredUUIDs.remove(uuid);
124                 log.warn("Can't reload the the node [" + uuid + "]");
125             }
126         }
127         return;
128     }
129 
130     /**
131      * Reload a specific node.
132      * @param node
133      */
134     protected void reload(Content node) {
135         onRegister(node);
136     }
137 
138     /**
139      * Clears the registered UUIDs and calls onClear().
140      */
141     public void clear() {
142         this.registeredUUIDs.clear();
143         onClear();
144     }
145 
146     /**
147      * Registers a node.
148      * @param node
149      */
150     protected abstract void onRegister(Content node);
151 
152     /**
153      * The implementor should clear everthing. If needed the nodes will get registered.
154      */
155     protected abstract void onClear();
156 
157 }