View Javadoc
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  
28  /** To be overridden */
29  public class DefaultJcrListener implements EventListener {
30  	private final static Log log = LogFactory.getLog(DefaultJcrListener.class);
31  	private Session session;
32  	private String path = "/";
33  	private Boolean deep = true;
34  
35  	public void start() {
36  		try {
37  			addEventListener(session().getWorkspace().getObservationManager());
38  			if (log.isDebugEnabled())
39  				log.debug("Registered JCR event listener on " + path);
40  		} catch (Exception e) {
41  			throw new ArgeoJcrException("Cannot register event listener", e);
42  		}
43  	}
44  
45  	public void stop() {
46  		try {
47  			session().getWorkspace().getObservationManager()
48  					.removeEventListener(this);
49  			if (log.isDebugEnabled())
50  				log.debug("Unregistered JCR event listener on " + path);
51  		} catch (Exception e) {
52  			throw new ArgeoJcrException("Cannot unregister event listener", e);
53  		}
54  	}
55  
56  	/** Default is listen to all events */
57  	protected Integer getEvents() {
58  		return Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED
59  				| Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED;
60  	}
61  
62  	/** To be overidden */
63  	public void onEvent(EventIterator events) {
64  		while (events.hasNext()) {
65  			Event event = events.nextEvent();
66  			log.debug(event);
67  		}
68  	}
69  
70  	/** To be overidden */
71  	protected void addEventListener(ObservationManager observationManager)
72  			throws RepositoryException {
73  		observationManager.addEventListener(this, getEvents(), path, deep,
74  				null, null, false);
75  	}
76  
77  	private Session session() {
78  		return session;
79  	}
80  
81  	public void setPath(String path) {
82  		this.path = path;
83  	}
84  
85  	public void setDeep(Boolean deep) {
86  		this.deep = deep;
87  	}
88  
89  	public void setSession(Session session) {
90  		this.session = session;
91  	}
92  
93  }