View Javadoc

1   /**
2    * This file Copyright (c) 2008-2011 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 javax.jcr.observation.Event;
37  import javax.jcr.observation.EventListener;
38  
39  import org.apache.jackrabbit.core.observation.SynchronousEventListener;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  /**
44   * Bean for the observation module, loaded automatically by Content2Bean.
45   * 
46   * @author tmiyar
47   * 
48   */
49  public class ObservationConfiguration {
50      private final static Logger log = LoggerFactory
51              .getLogger(ObservationConfiguration.class);
52  
53      // name with which you will identify your event listener
54      private String name;
55  
56      // text with some description
57      private String description;
58  
59      // allows to activate/deactivate the event listener
60      private boolean active = false;
61  
62      // the types for which the event listener will be triggered, if more than
63      // one separate them by ","
64      // possible values
65      // NODE_ADDED,NODE_REMOVED,PROPERTY_ADDED,PROPERTY_CHANGED,PROPERTY_REMOVED
66      private String eventTypes = "NODE_ADDED,NODE_REMOVED,PROPERTY_ADDED,PROPERTY_CHANGED,PROPERTY_REMOVED";
67  
68      // path where the event listener will be active
69      private String path;
70  
71      // the repository where the event listener will be activated
72      private String repository;
73  
74      // to restrict for which nodes would the event listener be active, if empty,
75      // it will be active for all node types
76      private String nodeType = null;
77  
78      // the event listener will be executed for the subnodes as well
79      private boolean includeSubNodes = false;
80  
81      // time you want to wait to activate an event listener
82      private long delay;
83  
84      // max time to wait before activating the event listener
85      private long maxDelay;
86  
87      // the listener instance of the eventlistener when the delay and maxdelay
88      // are specified,
89      // not for the configuration
90      public EventListener deferredListener;
91  
92      // user listener instance
93      private EventListener listener;
94  
95      public SynchronousEventListener synchListener;
96  
97      private boolean synchronous = false;
98  
99      public boolean isSynchronous() {
100         return synchronous;
101     }
102 
103     public void setSynchronous(boolean synchronous) {
104         this.synchronous = synchronous;
105     }
106 
107     public ObservationConfiguration() {
108     }
109 
110     public ObservationConfiguration(String name, String description,
111             boolean active, EventListener listener, String repository,
112             String path, String eventTypes, boolean includeSubNodes,
113             String nodeType, long delay, long maxDelay) {
114 
115         this.name = name;
116         this.description = description;
117         this.active = active;
118         this.listener = listener;
119         this.repository = repository;
120         this.path = path;
121         this.eventTypes = eventTypes;
122         this.includeSubNodes = includeSubNodes;
123         this.nodeType = nodeType;
124         this.delay = delay;
125         this.maxDelay = maxDelay;
126     }
127 
128     public long getDelay() {
129         return delay;
130     }
131 
132     public void setDelay(long delay) {
133         this.delay = delay;
134     }
135 
136     public long getMaxDelay() {
137         return maxDelay;
138     }
139 
140     public void setMaxDelay(long maxDelay) {
141         this.maxDelay = maxDelay;
142     }
143 
144     public String getNodeType() {
145         return nodeType;
146     }
147 
148     public void setNodeType(String nodeType) {
149         this.nodeType = nodeType;
150     }
151 
152     public boolean getIncludeSubNodes() {
153         return includeSubNodes;
154     }
155 
156     public void setIncludeSubNodes(boolean includeSubNodes) {
157         this.includeSubNodes = includeSubNodes;
158     }
159 
160     public boolean isActive() {
161         return active;
162     }
163 
164     public void setActive(boolean active) {
165         this.active = active;
166     }
167 
168     public String getDescription() {
169         return description;
170     }
171 
172     public void setDescription(String description) {
173         this.description = description;
174     }
175 
176     public EventListener getListener() {
177         return this.listener;
178     }
179 
180     public void setListener(EventListener listener) {
181         this.listener = listener;
182     }
183 
184     /**
185      * Gets custom user name (node name) for the eventlistener configuration.
186      */
187     public String getName() {
188         return name;
189     }
190 
191     /**
192      * Sets custom user name (node name) for the eventlistener configuration.
193      */
194     public void setName(String name) {
195         this.name = name;
196     }
197 
198     public String getEventTypes() {
199         return eventTypes;
200     }
201 
202     public void setEventTypes(String eventTypes) {
203         if (eventTypes != null && eventTypes.length() > 0) {
204             this.eventTypes = eventTypes;
205         }
206         getConvertedEventTypes();
207     }
208 
209     public String getPath() {
210         return path;
211     }
212 
213     public void setPath(String path) {
214         this.path = path;
215     }
216 
217     public String getRepository() {
218         return repository;
219     }
220 
221     public void setRepository(String repository) {
222         this.repository = repository;
223     }
224 
225     /**
226      * Translates the eventTypes String into int value.
227      */
228     public int getConvertedEventTypes() {
229 
230         String[] eTypes = eventTypes.split(",");
231         int eConvertedTypes = 0;
232         String item;
233         for (int i = 0; i < eTypes.length; i++) {
234             item = (eTypes[i]).trim();
235             if (item.equals("NODE_ADDED")) {
236                 eConvertedTypes |= Event.NODE_ADDED;
237             } else if (item.equals("NODE_REMOVED")) {
238                 eConvertedTypes |= Event.NODE_REMOVED;
239             } else if (item.equals("PROPERTY_ADDED")) {
240                 eConvertedTypes |= Event.PROPERTY_ADDED;
241             } else if (item.equals("PROPERTY_CHANGED")) {
242                 eConvertedTypes |= Event.PROPERTY_CHANGED;
243             } else if (item.equals("PROPERTY_REMOVED")) {
244                 eConvertedTypes |= Event.PROPERTY_REMOVED;
245             } else {
246                 log.error("Event Type not recognized " + item);
247             }
248         }
249         return eConvertedTypes;
250     }
251 
252     /**
253      * If all mandatory fields are present and valid we can instantiate the event listener.
254      */
255     public boolean isPopulated() {
256         boolean isPopulated = false;
257         if (this.repository != null && this.repository.length() > 0
258                 && this.path != null && this.path.length() > 0
259                 && this.getConvertedEventTypes() > 0
260                 && this.getListener() != null) {
261             isPopulated = true;
262         }
263         return isPopulated;
264     }
265 
266 }