View Javadoc

1   /**
2    * This file Copyright (c) 2012 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.module.observation;
35  
36  import info.magnolia.module.ModuleLifecycle;
37  import info.magnolia.module.ModuleLifecycleContext;
38  import info.magnolia.module.observation.util.ObservationUtil;
39  
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  import javax.jcr.observation.EventListener;
44  
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  /**
49   * This is the configuration bean of the observation module. It has to be
50   * registered in the module descriptor file under
51   * src/main/resources/META-INF/magnolia/observation.xml.
52   *
53   * The bean properties used in this class will be initialized by Content2Bean
54   * which means that properties of in the node
55   * config:/modules/observation/config/* are populated to this bean when the
56   * module is initialized.
57   */
58  public class ObservationModule implements ModuleLifecycle {
59      private static final Logger logger = LoggerFactory.getLogger(ObservationModule.class);
60  
61      // list of eventlisteners
62      private List listenerConfigurations = new ArrayList();
63  
64      public ObservationModule() {
65      }
66  
67      public List getListenerConfigurations() {
68          return listenerConfigurations;
69      }
70  
71      public void setListenerConfigurations(List listenerConfigurations) {
72          this.listenerConfigurations = listenerConfigurations;
73      }
74  
75      /**
76       * Adds a new listener, activates the listener if it meets the conditions.
77       */
78      public void addListenerConfiguration(ObservationConfiguration obs) {
79  
80          // sets configuration so everything can be accessible in the listener
81          if (obs.getListener() != null
82                  && obs.getListener() instanceof BaseConfiguration) {
83              ((BaseConfiguration) obs.getListener()).setConfiguration(obs);
84          }
85          if (obs.isActive() && obs.isPopulated()) {
86              EventListener tmpListener = obs.getListener();
87              // check if is a delayed listener
88              if (obs.getDelay() > 0 && obs.getMaxDelay() > 0
89                      && !obs.isSynchronous()) {
90                  obs.deferredListener = ObservationUtil
91                          .instanciateDeferredEventListener(obs.getListener(),
92                                  obs.getDelay(), obs.getMaxDelay());
93                  tmpListener = obs.deferredListener;
94  
95              }
96              if (obs.isSynchronous()) {
97                  obs.synchListener = ObservationUtil
98                          .instanciateSynchEventListener(obs.getListener());
99                  tmpListener = obs.synchListener;
100 
101             }
102             // register the listener
103             ObservationUtil.registerChangeListener(obs.getRepository(), obs
104                     .getPath(), obs.getIncludeSubNodes(), obs.getNodeType(),
105                     obs.getConvertedEventTypes(), tmpListener);
106             // assuming that we can register as many as we want for the same
107             // path
108             listenerConfigurations.add(obs);
109         }
110     }
111 
112     public void start(ModuleLifecycleContext moduleLifecycleContext) {
113     }
114 
115     /**
116      * Will unregister all the registered eventlisteners before any change is performed, we wont lose instances this way.
117      */
118     public void stop(ModuleLifecycleContext moduleLifecycleContext) {
119         // Unregister all events
120         java.util.Iterator i = this.listenerConfigurations.iterator();
121         ObservationConfiguration obs;
122         EventListener tmpListener;
123         while (i.hasNext()) {
124             obs = (ObservationConfiguration) i.next();
125             // we might not have any data yet
126             tmpListener = obs.getListener();
127             if (tmpListener != null) {
128                 if (obs.deferredListener != null) {
129                     tmpListener = obs.deferredListener;
130                 } else if (obs.synchListener != null) {
131                     tmpListener = obs.synchListener;
132                 }
133                 ObservationUtil.unregisterChangeListener(obs.getRepository(),
134                         tmpListener);
135             }
136         }// end while
137 
138     }
139 }