001    /**
002     * Copyright (c) 2000-2011 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.StringUtil;
026    import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
027    
028    import java.io.Serializable;
029    
030    import java.lang.annotation.Annotation;
031    
032    import org.aopalliance.intercept.MethodInvocation;
033    
034    /**
035     * @author Zsolt Berentey
036     * @author Shuyang Zhou
037     */
038    public class BufferedIncrementAdvice
039            extends AnnotationChainableMethodAdvice<BufferedIncrement> {
040    
041            @Override
042            @SuppressWarnings("rawtypes")
043            public Object before(MethodInvocation methodInvocation) throws Throwable {
044                    BufferedIncrement bufferedIncrement = findAnnotation(
045                            methodInvocation);
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(methodInvocation.toString());
060    
061                    for (int i = 0; i < arguments.length - 1; i++) {
062                            cacheKeyGenerator.append(StringUtil.toHexString(arguments[i]));
063                    }
064    
065                    Serializable 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            @Override
090            public BufferedIncrement getNullAnnotation() {
091                    return _nullBufferedIncrement;
092            }
093    
094            @SuppressWarnings("rawtypes")
095            private static BatchablePipe<String, BufferedIncreasableEntry>
096                    _batchablePipe = new BatchablePipe<String, BufferedIncreasableEntry>();
097    
098            private static BufferedIncrement _nullBufferedIncrement =
099                    new BufferedIncrement() {
100    
101                            public Class<? extends Annotation> annotationType() {
102                                    return BufferedIncrement.class;
103                            }
104    
105                            public Class<? extends Increment<?>> incrementClass() {
106                                    return null;
107                            }
108    
109                            public boolean parallel() {
110                                    return true;
111                            }
112    
113                    };
114    
115    }