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.bean.IdentifiableBeanInvokerUtil;
018    import com.liferay.portal.kernel.util.MethodHandler;
019    
020    import java.io.Externalizable;
021    import java.io.IOException;
022    import java.io.ObjectInput;
023    import java.io.ObjectOutput;
024    
025    import org.aopalliance.intercept.MethodInvocation;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class AsyncRunnable implements Externalizable, Runnable {
031    
032            public AsyncRunnable() {
033            }
034    
035            public AsyncRunnable(MethodInvocation methodInvocation) {
036                    _methodInvocation = methodInvocation;
037            }
038    
039            @Override
040            public void readExternal(ObjectInput objectInput)
041                    throws ClassNotFoundException, IOException {
042    
043                    _methodHandler = (MethodHandler)objectInput.readObject();
044            }
045    
046            @Override
047            public void run() {
048                    try {
049                            if (_methodInvocation != null) {
050                                    _methodInvocation.proceed();
051                            }
052                            else {
053                                    AsyncInvokeThreadLocal.setEnabled(true);
054    
055                                    try {
056                                            _methodHandler.invoke(null);
057                                    }
058                                    finally {
059                                            AsyncInvokeThreadLocal.setEnabled(false);
060                                    }
061                            }
062                    }
063                    catch (Throwable t) {
064                            throw new RuntimeException(t);
065                    }
066            }
067    
068            @Override
069            public void writeExternal(ObjectOutput objectOutput) throws IOException {
070                    MethodHandler methodHandler = _methodHandler;
071    
072                    if (methodHandler == null) {
073                            methodHandler = IdentifiableBeanInvokerUtil.createMethodHandler(
074                                    _methodInvocation);
075                    }
076    
077                    objectOutput.writeObject(methodHandler);
078            }
079    
080            private MethodHandler _methodHandler;
081            private MethodInvocation _methodInvocation;
082    
083    }