001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                    if (_batchablePipe.put(bufferedIncreasableEntry)) {
075                            if (bufferedIncrement.parallel()) {
076                                    MessageBusUtil.sendMessage(
077                                            DestinationNames.BUFFERED_INCREMENT_PARALLEL,
078                                            _batchablePipe);
079                            }
080                            else {
081                                    MessageBusUtil.sendMessage(
082                                            DestinationNames.BUFFERED_INCREMENT_SERIAL, _batchablePipe);
083                            }
084                    }
085    
086                    return nullResult;
087            }
088    
089            public BufferedIncrement getNullAnnotation() {
090                    return _nullBufferedIncrement;
091            }
092    
093            @SuppressWarnings("rawtypes")
094            private static BatchablePipe<String, BufferedIncreasableEntry>
095                    _batchablePipe = new BatchablePipe<String, BufferedIncreasableEntry>();
096    
097            private static BufferedIncrement _nullBufferedIncrement =
098                    new BufferedIncrement() {
099    
100                            public Class<? extends Annotation> annotationType() {
101                                    return BufferedIncrement.class;
102                            }
103    
104                            public Class<? extends Increment<?>> incrementClass() {
105                                    return null;
106                            }
107    
108                            public boolean parallel() {
109                                    return true;
110                            }
111    
112                    };
113    
114    }