View Javadoc

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