001    /**
002     * Copyright (c) 2000-2011 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.increment;
016    
017    import com.liferay.portal.kernel.cache.key.CacheKeyGenerator;
018    import com.liferay.portal.kernel.cache.key.CacheKeyGeneratorUtil;
019    import com.liferay.portal.kernel.concurrent.BatchablePipe;
020    import com.liferay.portal.kernel.increment.BufferedIncrement;
021    import com.liferay.portal.kernel.increment.Increment;
022    import com.liferay.portal.kernel.increment.IncrementFactory;
023    import com.liferay.portal.kernel.messaging.DestinationNames;
024    import com.liferay.portal.kernel.messaging.MessageBusUtil;
025    import com.liferay.portal.kernel.util.MethodTargetClassKey;
026    import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
027    
028    import java.lang.annotation.Annotation;
029    
030    import org.aopalliance.intercept.MethodInvocation;
031    
032    /**
033     * @author Zsolt Berentey
034     * @author Shuyang Zhou
035     */
036    public class BufferedIncrementAdvice
037            extends AnnotationChainableMethodAdvice<BufferedIncrement> {
038    
039            @SuppressWarnings("rawtypes")
040            public Object before(MethodInvocation methodInvocation) throws Throwable {
041                    MethodTargetClassKey methodTargetClassKey = buildMethodTargetClassKey(
042                            methodInvocation);
043    
044                    BufferedIncrement bufferedIncrement = findAnnotation(
045                            methodTargetClassKey);
046    
047                    if (bufferedIncrement == _nullBufferedIncrement) {
048                            return null;
049                    }
050    
051                    Object[] arguments = methodInvocation.getArguments();
052    
053                    Object value = arguments[arguments.length - 1];
054    
055                    CacheKeyGenerator cacheKeyGenerator =
056                            CacheKeyGeneratorUtil.getCacheKeyGenerator(
057                                    BufferedIncrementAdvice.class.getName());
058    
059                    cacheKeyGenerator.append(methodTargetClassKey.toString());
060    
061                    for (int i = 0; i < arguments.length - 1; i++) {
062                            cacheKeyGenerator.append(String.valueOf(arguments[i]));
063                    }
064    
065                    String batchKey = cacheKeyGenerator.finish();
066    
067                    Increment<?> increment = IncrementFactory.createIncrement(
068                            bufferedIncrement.incrementClass(), value);
069    
070                    BufferedIncreasableEntry bufferedIncreasableEntry =
071                            new BufferedIncreasableEntry(
072                                    nextMethodInterceptor, methodInvocation, batchKey, increment);
073    
074                    _batchablePipe.put(bufferedIncreasableEntry);
075    
076                    MessageBusUtil.sendMessage(
077                            DestinationNames.BUFFERED_INCREMENT, _batchablePipe);
078    
079                    return nullResult;
080            }
081    
082            public BufferedIncrement getNullAnnotation() {
083                    return _nullBufferedIncrement;
084            }
085    
086            @SuppressWarnings("rawtypes")
087            private static BatchablePipe<String, BufferedIncreasableEntry>
088                    _batchablePipe = new BatchablePipe<String, BufferedIncreasableEntry>();
089    
090            private static BufferedIncrement _nullBufferedIncrement =
091                    new BufferedIncrement() {
092    
093                            public Class<? extends Annotation> annotationType() {
094                                    return BufferedIncrement.class;
095                            }
096    
097                            public Class<? extends Increment<?>> incrementClass() {
098                                    return null;
099                            }
100    
101                    };
102    
103    }