All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.premiumminds.persistence.annotations.EnsureTransaction Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2014 Premium Minds.
 *
 * This file is part of pm-persistence-utils.
 *
 * pm-persistence-utils 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 3 of the License, or (at your option) any
 * later version.
 *
 * pm-persistence-utils 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 pm-persistence-utils. If not, see .
 */
package com.premiumminds.persistence.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import com.google.inject.Provider;
import com.premiumminds.persistence.TransactionWrapper;

/**
 * Method or class annotation and its interceptor.
 * Wraps a methods or class methods into a transaction
 */
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE})
public @interface EnsureTransaction {

	public static class Interceptor implements MethodInterceptor {
		
		@Inject Provider emProvider;
		
		public Object invoke(final MethodInvocation invocation) throws Throwable {
			return new TransactionWrapper(emProvider.get()) {

				@Override
				public Object runTransaction() throws Throwable {
					return invocation.proceed();
				}
			}.execute();
		}
	}

}