001    /**
002     * Copyright (c) 2000-2013 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.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(methodInvocation);
045    
046                    if (bufferedIncrement == _nullBufferedIncrement) {
047                            return null;
048                    }
049    
050                    Object[] arguments = methodInvocation.getArguments();
051    
052                    Object value = arguments[arguments.length - 1];
053    
054                    CacheKeyGenerator cacheKeyGenerator =
055                            CacheKeyGeneratorUtil.getCacheKeyGenerator(
056                                    BufferedIncrementAdvice.class.getName());
057    
058                    cacheKeyGenerator.append(methodInvocation.toString());
059    
060                    for (int i = 0; i < arguments.length - 1; i++) {
061                            cacheKeyGenerator.append(StringUtil.toHexString(arguments[i]));
062                    }
063    
064                    Serializable batchKey = cacheKeyGenerator.finish();
065    
066                    Increment<?> increment = IncrementFactory.createIncrement(
067                            bufferedIncrement.incrementClass(), value);
068    
069                    BufferedIncreasableEntry bufferedIncreasableEntry =
070                            new BufferedIncreasableEntry(
071                                    nextMethodInterceptor, methodInvocation, batchKey, increment);
072    
073                    if (_batchablePipe.put(bufferedIncreasableEntry)) {
074                            if (bufferedIncrement.parallel()) {
075                                    MessageBusUtil.sendMessage(
076                                            DestinationNames.BUFFERED_INCREMENT_PARALLEL,
077                                            _batchablePipe);
078                            }
079                            else {
080                                    MessageBusUtil.sendMessage(
081                                            DestinationNames.BUFFERED_INCREMENT_SERIAL, _batchablePipe);
082                            }
083                    }
084    
085                    return nullResult;
086            }
087    
088            @Override
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    }