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.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                            _log.error("Unable to process runnable: " + e.getMessage());
048    
049                            _throwable = e;
050                    }
051                    finally {
052                            if (_log.isInfoEnabled()) {
053                                    _log.info(
054                                            "Completed processing runnable " +
055                                                    ClassUtil.getClassName(this) + " in " +
056                                                            (System.currentTimeMillis() - start) + "ms");
057                            }
058                    }
059            }
060    
061            protected abstract void doRun() throws Exception;
062    
063            private static Log _log = LogFactoryUtil.getLog(
064                    ThrowableAwareRunnable.class);
065    
066            private Throwable _throwable;
067    
068    }