]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.server.jcr/src/org/argeo/jcr/DefaultJcrListener.java
Remove DocBook from defaults CNDs
[lgpl/argeo-commons.git] / org.argeo.server.jcr / src / org / argeo / jcr / DefaultJcrListener.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.jcr;
17
18 import javax.jcr.RepositoryException;
19 import javax.jcr.Session;
20 import javax.jcr.observation.Event;
21 import javax.jcr.observation.EventIterator;
22 import javax.jcr.observation.EventListener;
23 import javax.jcr.observation.ObservationManager;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.ArgeoException;
28
29 /** To be overridden */
30 public class DefaultJcrListener implements EventListener {
31 private final static Log log = LogFactory.getLog(DefaultJcrListener.class);
32 private Session session;
33 private String path = "/";
34 private Boolean deep = true;
35
36 public void start() {
37 try {
38 addEventListener(session().getWorkspace().getObservationManager());
39 if (log.isDebugEnabled())
40 log.debug("Registered JCR event listener on " + path);
41 } catch (Exception e) {
42 throw new ArgeoException("Cannot register event listener", e);
43 }
44 }
45
46 public void stop() {
47 try {
48 session().getWorkspace().getObservationManager()
49 .removeEventListener(this);
50 if (log.isDebugEnabled())
51 log.debug("Unregistered JCR event listener on " + path);
52 } catch (Exception e) {
53 throw new ArgeoException("Cannot unregister event listener", e);
54 }
55 }
56
57 /** Default is listen to all events */
58 protected Integer getEvents() {
59 return Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED
60 | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED;
61 }
62
63 /** To be overidden */
64 public void onEvent(EventIterator events) {
65 while (events.hasNext()) {
66 Event event = events.nextEvent();
67 log.debug(event);
68 }
69 }
70
71 /** To be overidden */
72 protected void addEventListener(ObservationManager observationManager)
73 throws RepositoryException {
74 observationManager.addEventListener(this, getEvents(), path, deep,
75 null, null, false);
76 }
77
78 private Session session() {
79 return session;
80 }
81
82 public void setPath(String path) {
83 this.path = path;
84 }
85
86 public void setDeep(Boolean deep) {
87 this.deep = deep;
88 }
89
90 public void setSession(Session session) {
91 this.session = session;
92 }
93
94 }