]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/DefaultJcrListener.java
Synchronize login
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr / src / main / java / org / argeo / jcr / DefaultJcrListener.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.jcr;
18
19 import javax.jcr.RepositoryException;
20 import javax.jcr.Session;
21 import javax.jcr.observation.Event;
22 import javax.jcr.observation.EventIterator;
23 import javax.jcr.observation.EventListener;
24 import javax.jcr.observation.ObservationManager;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.argeo.ArgeoException;
29
30 /** To be overridden */
31 public class DefaultJcrListener implements EventListener {
32 private final static Log log = LogFactory.getLog(DefaultJcrListener.class);
33 private Session session;
34 private String path = "/";
35 private Boolean deep = true;
36
37 public void start() {
38 try {
39 addEventListener(session().getWorkspace().getObservationManager());
40 if (log.isDebugEnabled())
41 log.debug("Registered JCR event listener on " + path);
42 } catch (Exception e) {
43 throw new ArgeoException("Cannot register event listener", e);
44 }
45 }
46
47 public void stop() {
48 try {
49 session().getWorkspace().getObservationManager()
50 .removeEventListener(this);
51 if (log.isDebugEnabled())
52 log.debug("Unregistered JCR event listener on " + path);
53 } catch (Exception e) {
54 throw new ArgeoException("Cannot unregister event listener", e);
55 }
56 }
57
58 /** Default is listen to all events */
59 protected Integer getEvents() {
60 return Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED
61 | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED;
62 }
63
64 /** To be overidden */
65 public void onEvent(EventIterator events) {
66 while (events.hasNext()) {
67 Event event = events.nextEvent();
68 log.debug(event);
69 }
70 }
71
72 /** To be overidden */
73 protected void addEventListener(ObservationManager observationManager)
74 throws RepositoryException {
75 observationManager.addEventListener(this, getEvents(), path, deep,
76 null, null, false);
77 }
78
79 private Session session() {
80 return session;
81 }
82
83 public void setPath(String path) {
84 this.path = path;
85 }
86
87 public void setDeep(Boolean deep) {
88 this.deep = deep;
89 }
90
91 public void setSession(Session session) {
92 this.session = session;
93 }
94
95 }