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.concurrent;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ClassUtil;
020    
021    /**
022     * @author Michael C. Han
023     */
024    public abstract class ThrowableAwareRunnable implements Runnable {
025    
026            public Throwable getThrowable() {
027                    return _throwable;
028            }
029    
030            public boolean hasException() {
031                    return (_throwable != null);
032            }
033    
034            @Override
035            public void run() {
036                    long start = System.currentTimeMillis();
037    
038                    try {
039                            if (_log.isInfoEnabled()) {
040                                    _log.info(
041                                            "Processing runnable " + ClassUtil.getClassName(this));
042                            }
043    
044                            doRun();
045                    }
046                    catch (Exception e) {
047                            if (_log.isDebugEnabled()) {
048                                    _log.debug("Unable to process runnable", e);
049                            }
050    
051                            _throwable = e;
052                    }
053                    finally {
054                            if (_log.isInfoEnabled()) {
055                                    _log.info(
056                                            "Completed processing runnable " +
057                                                    ClassUtil.getClassName(this) + " in " +
058                                                            (System.currentTimeMillis() - start) + "ms");
059                            }
060                    }
061            }
062    
063            protected abstract void doRun() throws Exception;
064    
065            private static final Log _log = LogFactoryUtil.getLog(
066                    ThrowableAwareRunnable.class);
067    
068            private Throwable _throwable;
069    
070    }