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