Skip to content

Commit

Permalink
Add managed hibernate sessions
Browse files Browse the repository at this point in the history
Add the ability to use seam managed hibernate sessions, and the
corresponding jetty tests. This also innvolved some minor refactoring
of the test setup.
  • Loading branch information
stuartwdouglas committed Oct 27, 2010
1 parent 9c79bdf commit 46afd86
Show file tree
Hide file tree
Showing 39 changed files with 1,265 additions and 249 deletions.
Expand Up @@ -12,6 +12,7 @@
import org.hibernate.Session;
import org.hibernate.TransientObjectException;
import org.hibernate.proxy.HibernateProxy;
import org.jboss.weld.extensions.core.Veto;
import org.jboss.weld.extensions.reflection.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -25,6 +26,7 @@
* @author Stuart Douglas
*
*/
@Veto
public class HibernatePersistenceProvider extends DefaultPersistenceProvider
{

Expand Down
Expand Up @@ -35,7 +35,7 @@
import org.slf4j.LoggerFactory;

/**
* Proxy handler for the entity manager proxy that allows the use of EL in
* Proxy handler for a {@link EntityManager} proxy that allows the use of EL in
* queries.
*
* @author Stuart Douglas
Expand Down
@@ -0,0 +1,34 @@
/*
* 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.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.seam.persistence.hibernate;

import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.ProcessAnnotatedType;

interface HibernateExtension
{
public abstract <T> void processAnnotatedType(ProcessAnnotatedType<T> event, BeanManager manager);

public abstract void afterBeanDiscovery(AfterBeanDiscovery event);

}

This file was deleted.

Expand Up @@ -49,9 +49,9 @@
* @author Stuart Douglas
*
*/
public class HibernateManagedPersistenceContextBeanLifecycle implements ContextualLifecycle<Session>
public class HibernateManagedSessionBeanLifecycle implements ContextualLifecycle<Session>
{
private static final Logger log = LoggerFactory.getLogger(HibernateManagedPersistenceContextBeanLifecycle.class);
private static final Logger log = LoggerFactory.getLogger(HibernateManagedSessionBeanLifecycle.class);

private final Class<?> proxyClass;

Expand All @@ -67,7 +67,7 @@ public class HibernateManagedPersistenceContextBeanLifecycle implements Contextu

private SessionFactory sessionFactory;

public HibernateManagedPersistenceContextBeanLifecycle(Set<Annotation> qualifiers, ClassLoader loader, BeanManager manager)
public HibernateManagedSessionBeanLifecycle(Set<Annotation> qualifiers, ClassLoader loader, BeanManager manager)
{
this.manager = manager;
Set<Class<?>> additionalinterfaces = persistenceProvider.getAdditionalSessionInterfaces();
Expand Down Expand Up @@ -108,7 +108,7 @@ public Session create(Bean<Session> bean, CreationalContext<Session> arg0)
SessionFactory sf = getSessionFactory();
Session session = sf.openSession();
session = (Session) persistenceProvider.proxyDelegate(session);
HibernateManagedPersistenceContextProxyHandler handler = new HibernateManagedPersistenceContextProxyHandler(session, manager, bean.getQualifiers(), getPersistenceContexts(), persistenceProvider);
HibernateManagedSessionProxyHandler handler = new HibernateManagedSessionProxyHandler(session, manager, bean.getQualifiers(), getPersistenceContexts(), persistenceProvider);
Session proxy = (Session) proxyConstructor.newInstance(handler);
((ManagedPersistenceContext) proxy).changeFlushMode(getPersistenceContexts().getFlushMode());
manager.fireEvent(new SeamManagedHibernateSessionCreated(proxy), qualifiers);
Expand Down
@@ -0,0 +1,83 @@
/*
* 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.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.seam.persistence.hibernate;

import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;

import org.jboss.weld.extensions.reflection.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The portable extension for Seam Managed Hibernate Sessions. If hibernate is
* found on the classpath then the real work is done by
* {@link HibernateManagedSessionExtensionImpl}
*
* @author Stuart Douglas
*
*/
public class HibernateManagedSessionExtension implements Extension
{
private static final Logger log = LoggerFactory.getLogger(HibernateManagedSessionExtension.class);

private final boolean enabled;

private HibernateExtension delegate;

public HibernateManagedSessionExtension()
{
boolean en = true;
try
{
// ensure hibernate is on the CP
Reflections.classForName("org.hibernate.Session", getClass().getClassLoader());
delegate = new HibernateManagedSessionExtensionImpl();
}
catch (ClassNotFoundException e)
{
log.debug("Hibernate not found on the classpath, Managed Hibernate Sessions are disabled");
en = false;
}
enabled = en;

}

public <T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> event, BeanManager manager)
{
if (enabled)
{
delegate.processAnnotatedType(event, manager);
}
}

public void afterBeanDiscovery(@Observes AfterBeanDiscovery event)
{
if (enabled)
{
delegate.afterBeanDiscovery(event);
}
}
}

0 comments on commit 46afd86

Please sign in to comment.