001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.transaction;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.spring.aop.Skip;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.ReflectionUtil;
023    import com.liferay.portal.spring.aop.ServiceBeanAopCacheManager;
024    import com.liferay.portal.spring.aop.ServiceBeanAopCacheManagerUtil;
025    import com.liferay.portal.util.PropsUtil;
026    import com.liferay.portal.util.PropsValues;
027    
028    import java.lang.annotation.Annotation;
029    import java.lang.reflect.Field;
030    
031    import java.util.HashMap;
032    import java.util.concurrent.ConcurrentHashMap;
033    
034    import org.aopalliance.intercept.MethodInvocation;
035    
036    /**
037     * @author Miguel Pastor
038     */
039    public class TransactionsUtil {
040    
041            public static void disableTransactions() {
042                    if (_log.isDebugEnabled()) {
043                            _log.debug("Disable transactions");
044                    }
045    
046                    PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED = false;
047    
048                    try {
049                            Field field = ReflectionUtil.getDeclaredField(
050                                    ServiceBeanAopCacheManager.class, "_annotations");
051    
052                            field.set(
053                                    null,
054                                    new HashMap<MethodInvocation, Annotation[]>() {
055    
056                                            @Override
057                                            public Annotation[] get(Object key) {
058                                                    return _annotations;
059                                            }
060    
061                                            private Annotation[] _annotations = new Annotation[] {
062                                                    new Skip() {
063    
064                                                            @Override
065                                                            public Class<? extends Annotation>
066                                                                    annotationType() {
067    
068                                                                    return Skip.class;
069                                                            }
070    
071                                                    }
072                                            };
073    
074                                    });
075                    }
076                    catch (Exception e) {
077                            throw new RuntimeException(
078                                    "Unexpected error disabling transactions", e);
079                    }
080            }
081    
082            public static void enableTransactions() {
083                    if (_log.isDebugEnabled()) {
084                            _log.debug("Enable transactions");
085                    }
086    
087                    PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED = GetterUtil.getBoolean(
088                            PropsUtil.get(PropsKeys.SPRING_HIBERNATE_SESSION_DELEGATED));
089    
090                    try {
091                            Field field = ReflectionUtil.getDeclaredField(
092                                    ServiceBeanAopCacheManager.class, "_annotations");
093    
094                            field.set(
095                                    null, new ConcurrentHashMap<MethodInvocation, Annotation[]>());
096    
097                            ServiceBeanAopCacheManagerUtil.reset();
098                    }
099                    catch (Exception e) {
100                            throw new RuntimeException(
101                                    "Unexpected error disabling transactions", e);
102                    }
103            }
104    
105            private static final Log _log = LogFactoryUtil.getLog(
106                    TransactionsUtil.class);
107    
108    }