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.messaging.async;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.messaging.MessageBusUtil;
020    import com.liferay.portal.kernel.messaging.async.Async;
021    import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
022    
023    import java.lang.annotation.Annotation;
024    import java.lang.reflect.Method;
025    
026    import java.util.Map;
027    
028    import org.aopalliance.intercept.MethodInvocation;
029    
030    /**
031     * @author Shuyang Zhou
032     * @author Brian Wing Shun Chan
033     */
034    public class AsyncAdvice extends AnnotationChainableMethodAdvice<Async> {
035    
036            @Override
037            public Object before(final MethodInvocation methodInvocation)
038                    throws Throwable {
039    
040                    Async async = findAnnotation(methodInvocation);
041    
042                    if (async == _nullAsync) {
043                            return null;
044                    }
045    
046                    Method method = methodInvocation.getMethod();
047    
048                    if (method.getReturnType() != void.class) {
049                            if (_log.isWarnEnabled()) {
050                                    _log.warn(
051                                            "Async annotation on method " + method.getName() +
052                                                    " does not return void");
053                            }
054    
055                            return null;
056                    }
057    
058                    String destinationName = null;
059    
060                    if ((_destinationNames != null) && !_destinationNames.isEmpty()) {
061                            Object thisObject = methodInvocation.getThis();
062    
063                            destinationName = _destinationNames.get(thisObject.getClass());
064                    }
065    
066                    if (destinationName == null) {
067                            destinationName = _defaultDestinationName;
068                    }
069    
070                    MessageBusUtil.sendMessage(
071                            destinationName,
072                            new Runnable() {
073    
074                                    @Override
075                                    public void run() {
076                                            try {
077                                                    nextMethodInterceptor.invoke(methodInvocation);
078                                            }
079                                            catch (Throwable t) {
080                                                    throw new RuntimeException(t);
081                                            }
082                                    }
083    
084                                    @Override
085                                    public String toString() {
086                                            return methodInvocation.toString();
087                                    }
088    
089                            });
090    
091                    return nullResult;
092            }
093    
094            public String getDefaultDestinationName() {
095                    return _defaultDestinationName;
096            }
097    
098            @Override
099            public Async getNullAnnotation() {
100                    return _nullAsync;
101            }
102    
103            public void setDefaultDestinationName(String defaultDestinationName) {
104                    _defaultDestinationName = defaultDestinationName;
105            }
106    
107            public void setDestinationNames(Map<Class<?>, String> destinationNames) {
108                    _destinationNames = destinationNames;
109            }
110    
111            private static Log _log = LogFactoryUtil.getLog(AsyncAdvice.class);
112    
113            private static Async _nullAsync =
114                    new Async() {
115    
116                            @Override
117                            public Class<? extends Annotation> annotationType() {
118                                    return Async.class;
119                            }
120    
121                    };
122    
123            private String _defaultDestinationName;
124            private Map<Class<?>, String> _destinationNames;
125    
126    }