Hi Alex,

Thanks a lot for your reply. 

Well, at the begining I configured the listeners with the sessionFactory 
bean in the spring's applicationContext.xml.
Then I installed jackrabbit-jca on jboss, and I don't know how to 
configure spring to obtain the session(hope if you can help me in this it 
will be perfect because I'd like to use spring to look after all that 
stuff although I know it's not the point to talk about here), so I created 
my own class that has getSession() and registerListeners() methods.

I also created ObservationUtil.java class to registerListener() and check 
isRegisteredListener().

And because I don't want to configure all listeners with all their 
properties in one place, I did the following:

1. Created an EventListenerProperties class which is just a POJO with all 
properties needed for the observationManager() method.
2. Created a CustomEventListener interface that extends the EventListener 
interface and has some extra methods, for now the important method to 
mention here is buildEventListenerProperties() method that need to be 
implemented by sub classes so that each CustomEventListener implementation 
will have its own properties embedded.
3. Created MyNodeAddedListener1 and MyNodeAddedListener2 that implements 
the CustomEventListener interface.

Please find here with attached the code each in a separate file.




public class MyNodeAddedListener1 implements CustomEventListener {

    @Autowired
    private transient JcrSessionFactory jcrSessionFactory;
    
    @Override
    public EventListenerProperties buildEventListenerProperties() {
        return new EventListenerProperties.Builder(Event.NODE_ADDED, ABS_PATH).
        nodeTypeName(new String[]{NodeType.NT_FILE}).isDeep(true).build();
    }
    
    @Override
    public void onEvent(EventIterator events) {
        
System.out.println("***********************************************************");
        System.out.println("*** MyNodeAddedListener1's onEvent() has been 
invoked.. ***");
        
System.out.println("***********************************************************");
    }

}
public class ObservationUtil {

    public static boolean isRegisteredListener(final Session session, 
            final Class clazz){
        try {
            EventListenerIterator iterator = 
                
session.getWorkspace().getObservationManager().getRegisteredEventListeners();
            while(iterator.hasNext()){
                EventListener e = iterator.nextEventListener();
                if(clazz.equals(e.getClass())){
                    return true;
                }
            }
        } catch (UnsupportedRepositoryOperationException e) {
            e.printStackTrace();
        } catch (RepositoryException e) {
            e.printStackTrace();
        }
        return false;
    }
    
    public static void registerListener(final Session session, 
            final EventListener eventListener){
        if(eventListener instanceof CustomEventListener){
            EventListenerProperties listenerProperties = 
                
((CustomEventListener)eventListener).buildEventListenerProperties();
            try {
                ObservationManager observationManager =
                    session.getWorkspace().getObservationManager();
                observationManager.addEventListener(eventListener,
                        listenerProperties.getEventTypes(),
                        listenerProperties.getAbsPath(),
                        listenerProperties.isDeep(),
                        listenerProperties.getUuid(),
                        listenerProperties.getNodeTypeName(),
                        listenerProperties.isNoLocal());
            } catch (RepositoryException e) {
                e.printStackTrace();
            }
        }
    }
}
public class JcrSessionFactory {

    public Session getSession() {
        try {
            final InitialContext ctx = new InitialContext();
            final Repository repository = (Repository) 
ctx.lookup("java:jcr/local") ;
            final Credentials credentials = new SimpleCredentials(USER_NAME, 
PASSWORD) ;
            Session session = repository.login(credentials);
            this.registerListeners(session);
            return session;
        } catch (final NamingException e) {
            e.printStackTrace();
        } catch (final LoginException e) {
            e.printStackTrace();
        } catch (final RepositoryException e) {
            e.printStackTrace();
        }
        return null;
    }

    private void registerListeners(final Session session){
        if(!ObservationUtil.isRegisteredListener(session, 
MyNodeAddedListener1.class)){
            ObservationUtil.registerListener(session, new 
MyNodeAddedListener1());   
        } else if(!ObservationUtil.isRegisteredListener(session, 
MyNodeAddedListener2.class)){
            ObservationUtil.registerListener(session, new 
MyNodeAddedListener2()); 
        }
    }
    
}
public class MyNodeAddedListener2 implements CustomEventListener {

    @Autowired
    private transient JcrSessionFactory jcrSessionFactory;
    
    @Override
    public EventListenerProperties buildEventListenerProperties() {
        return new EventListenerProperties.Builder(Event.NODE_ADDED, ABS_PATH).
        nodeTypeName(new String[]{NodeType.NT_FILE}).isDeep(true).build();
    }
    
    @Override
    public void onEvent(EventIterator events) {
        
System.out.println("***********************************************************");
        System.out.println("*** MyNodeAddedListener2's onEvent() has been 
invoked.. ***");
        
System.out.println("***********************************************************");
    }

}

Reply via email to