Skip to content

Instantly share code, notes, and snippets.

@gastaldi
Created March 9, 2011 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gastaldi/861586 to your computer and use it in GitHub Desktop.
Save gastaldi/861586 to your computer and use it in GitHub Desktop.
Decorating JCR Repository
/*
* JBoss, Home of Professional Open Source
* Copyright [2010], Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.seam.jcr;
import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.jcr.Credentials;
import javax.jcr.LoginException;
import javax.jcr.NoSuchWorkspaceException;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
import org.jboss.seam.jcr.annotations.JcrEventListener;
import org.jboss.seam.jcr.annotations.JcrRepository;
import org.jboss.seam.jcr.events.JcrCDIEventListener;
/**
* Decorator class for JCR 2.0 {@link Repository} implementations
*
* @author george
*
*/
@Decorator
public abstract class SeamJcrRepository implements Repository
{
@Inject
@JcrRepository
@Delegate
private Repository repository;
@Inject
private BeanManager beanManager;
@Override
public Session login() throws LoginException, RepositoryException
{
Session session = repository.login();
registerListener(session);
return session;
}
@Override
public Session login(Credentials credentials) throws LoginException, RepositoryException
{
Session session = repository.login(credentials);
registerListener(session);
return session;
}
@Override
public Session login(Credentials credentials, String workspaceName) throws LoginException, NoSuchWorkspaceException, RepositoryException
{
Session session = repository.login(credentials, workspaceName);
registerListener(session);
return session;
}
@Override
public Session login(String workspaceName) throws LoginException, NoSuchWorkspaceException, RepositoryException
{
Session session = repository.login(workspaceName);
registerListener(session);
return session;
}
/**
* Registers this {@link EventListener} into an {@link ObservationManager}
* using the supplied config
*
* @param ann
* @param obsManager
* @throws RepositoryException
*/
private void registerListener(Session session) throws RepositoryException
{
session.getWorkspace().getObservationManager().addEventListener(new JcrCDIEventListener(beanManager), JcrEventListener.ALL_EVENTS, "/", true, null, null, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment