001    /**
002     * Copyright (c) 2000-present 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.module.framework.service.IdentifiableOSGiServiceInvokerUtil;
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                    this(null);
037            }
038    
039            public AsyncProcessCallable(MethodInvocation methodInvocation) {
040                    _methodInvocation = methodInvocation;
041            }
042    
043            @Override
044            public Serializable call() {
045                    try {
046                            if (_methodInvocation != null) {
047                                    _methodInvocation.proceed();
048                            }
049                            else {
050                                    AsyncInvokeThreadLocal.setEnabled(true);
051    
052                                    try {
053                                            _methodHandler.invoke(null);
054                                    }
055                                    finally {
056                                            AsyncInvokeThreadLocal.setEnabled(false);
057                                    }
058                            }
059                    }
060                    catch (Throwable t) {
061                            throw new RuntimeException(t);
062                    }
063    
064                    return null;
065            }
066    
067            @Override
068            public void readExternal(ObjectInput objectInput)
069                    throws ClassNotFoundException, IOException {
070    
071                    _methodHandler = (MethodHandler)objectInput.readObject();
072            }
073    
074            @Override
075            public void writeExternal(ObjectOutput objectOutput) throws IOException {
076                    MethodHandler methodHandler = _methodHandler;
077    
078                    if (methodHandler == null) {
079                            methodHandler =
080                                    IdentifiableOSGiServiceInvokerUtil.createMethodHandler(
081                                            _methodInvocation.getThis(), _methodInvocation.getMethod(),
082                                            _methodInvocation.getArguments());
083                    }
084    
085                    objectOutput.writeObject(methodHandler);
086            }
087    
088            private MethodHandler _methodHandler;
089            private final MethodInvocation _methodInvocation;
090    
091    }