Skip to content

Commit

Permalink
Change the way the transaction interceptor is applied, so it is appli…
Browse files Browse the repository at this point in the history
…ed directly to the methods if no class level annotation is present
  • Loading branch information
stuartwdouglas committed Nov 1, 2010
1 parent 836c1d8 commit 6899644
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
*/
package org.jboss.seam.persistence.transaction;

import java.util.HashSet;
import java.util.Set;

import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.util.AnnotationLiteral;

import org.jboss.seam.persistence.util.EjbApi;
import org.jboss.weld.extensions.reflection.annotated.AnnotatedTypeBuilder;
Expand All @@ -54,63 +57,78 @@ public class TransactionExtension implements Extension

private static final Logger log = LoggerFactory.getLogger(TransactionExtension.class);

private final Set<Throwable> exceptions = new HashSet<Throwable>();

/**
* Looks for @Transaction or @TransactionAttribute annotations and if they
* are found adds the transaction intercepter binding
*
*/
public <X> void processAnnotatedType(@Observes ProcessAnnotatedType<X> event)
{
boolean addInterceptor = false;
AnnotatedTypeBuilder<X> builder = null;
AnnotatedType<X> type = event.getAnnotatedType();
boolean addedToClass = false;
if (type.isAnnotationPresent(Transactional.class))
{

addInterceptor = true;
builder = new AnnotatedTypeBuilder<X>().readFromType(type);
builder.addToClass(TransactionInterceptorBindingLiteral.INSTANCE);
addedToClass = true;
}
else if (type.isAnnotationPresent(EjbApi.TRANSACTION_ATTRIBUTE) && !EjbApi.isEjb(event.getAnnotatedType()))
{
checkTransactionAttributeIsValue(type, type);
addInterceptor = true;
builder = new AnnotatedTypeBuilder<X>().readFromType(type);
builder.addToClass(TransactionInterceptorBindingLiteral.INSTANCE);
addedToClass = true;
}

for (AnnotatedMethod<? super X> m : type.getMethods())
if (!addedToClass)
{
if (m.isAnnotationPresent(Transactional.class))
for (AnnotatedMethod<? super X> m : type.getMethods())
{
addInterceptor = true;
}
else if (m.isAnnotationPresent(EjbApi.TRANSACTION_ATTRIBUTE) && !EjbApi.isEjb(event.getAnnotatedType()))
{
checkTransactionAttributeIsValue(type, m);
addInterceptor = true;
if (m.isAnnotationPresent(Transactional.class))
{
if (builder == null)
{
builder = new AnnotatedTypeBuilder<X>().readFromType(type);
}
builder.addToMethod(m, TransactionInterceptorBindingLiteral.INSTANCE);
}
else if (m.isAnnotationPresent(EjbApi.TRANSACTION_ATTRIBUTE) && !EjbApi.isEjb(event.getAnnotatedType()))
{
checkTransactionAttributeIsValue(type, m);
if (builder == null)
{
builder = new AnnotatedTypeBuilder<X>().readFromType(type);
}
builder.addToMethod(m, TransactionInterceptorBindingLiteral.INSTANCE);
}
}
}
if (addInterceptor)
if (builder != null)
{
event.setAnnotatedType(addInterceptorBinding(type));
event.setAnnotatedType(builder.create());
}
}

public <X> AnnotatedType addInterceptorBinding(AnnotatedType<X> type)
private void afterBeanDiscover(@Observes AfterBeanDiscovery event)
{
AnnotatedTypeBuilder<X> builder = new AnnotatedTypeBuilder<X>().readFromType(type);
builder.addToClass(new AnnotationLiteral<TransactionalInterceptorBinding>()
for (Throwable throwable : exceptions)
{
});
return builder.create();
event.addDefinitionError(throwable);
}
}

private void checkTransactionAttributeIsValue(AnnotatedType type, Annotated element)
{
Object attribute = element.getAnnotation(EjbApi.TRANSACTION_ATTRIBUTE);
if (attribute == EjbApi.REQUIRES_NEW)
{
throw new RuntimeException("TransactionAttributeType.REQUIRED_NEW is not supported on Managed Beans that are not EJB's. Annotation was found on type " + type);
exceptions.add(new RuntimeException("TransactionAttributeType.REQUIRED_NEW is not supported on Managed Beans that are not EJB's. Annotation was found on type " + type));
}
if (attribute == EjbApi.NOT_SUPPORTED)
{
throw new RuntimeException("TransactionAttributeType.NOT_SUPPORTED is not supported on Managed Beans that are not EJB's. Annotation was found on type " + type);
exceptions.add(new RuntimeException("TransactionAttributeType.NOT_SUPPORTED is not supported on Managed Beans that are not EJB's. Annotation was found on type " + type));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.transaction;

import javax.enterprise.util.AnnotationLiteral;

class TransactionInterceptorBindingLiteral extends AnnotationLiteral<TransactionalInterceptorBinding>
{
static TransactionInterceptorBindingLiteral INSTANCE = new TransactionInterceptorBindingLiteral();
}

0 comments on commit 6899644

Please sign in to comment.