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.kernel.process.local;
016    
017    import com.liferay.portal.kernel.process.ProcessCallable;
018    import com.liferay.portal.kernel.process.ProcessException;
019    import com.liferay.portal.kernel.process.log.ProcessOutputStream;
020    
021    import java.io.IOException;
022    import java.io.Serializable;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class RequestProcessCallable<T extends Serializable>
028            implements ProcessCallable<T> {
029    
030            public RequestProcessCallable(long id, ProcessCallable<T> processCallable) {
031                    _id = id;
032                    _processCallable = processCallable;
033            }
034    
035            @Override
036            public T call() throws ProcessException {
037                    ProcessOutputStream processOutputStream =
038                            LocalProcessLauncher.ProcessContext.getProcessOutputStream();
039    
040                    T result = null;
041                    Throwable throwable = null;
042    
043                    try {
044                            result = _processCallable.call();
045    
046                            return result;
047                    }
048                    catch (Throwable t) {
049                            throwable = t;
050    
051                            throw t;
052                    }
053                    finally {
054                            try {
055                                    processOutputStream.writeProcessCallable(
056                                            new ResponseProcessCallable<T>(_id, result, throwable));
057                            }
058                            catch (IOException ioe) {
059                                    throw new ProcessException(ioe);
060                            }
061                    }
062            }
063    
064            private static final long serialVersionUID = 1L;
065    
066            private final long _id;
067            private final ProcessCallable<T> _processCallable;
068    
069    }