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.dao.shard.ShardUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.ClassUtil;
022    
023    import java.util.Arrays;
024    import java.util.List;
025    
026    /**
027     * @author Michael C. Han
028     */
029    public abstract class ThrowableAwareRunnable implements Runnable {
030    
031            public ThrowableAwareRunnable(String shardName) {
032                    _shardName = shardName;
033            }
034    
035            public Throwable getThrowable() {
036                    return _throwable;
037            }
038    
039            public boolean hasException() {
040                    return (_throwable != null);
041            }
042    
043            @Override
044            public void run() {
045                    long start = System.currentTimeMillis();
046    
047                    String currentShardName = ShardUtil.getCurrentShardName();
048    
049                    List<String> shardNames = Arrays.asList(
050                            ShardUtil.getAvailableShardNames());
051    
052                    try {
053                            if (!shardNames.isEmpty()) {
054                                    if (shardNames.contains(_shardName)) {
055                                            ShardUtil.pushCompanyService(_shardName);
056    
057                                            ShardUtil.setTargetSource(_shardName);
058                                    }
059                                    else {
060                                            throw new PortalException("Invalid shard name");
061                                    }
062                            }
063    
064                            if (_log.isInfoEnabled()) {
065                                    _log.info(
066                                            "Processing runnable " + ClassUtil.getClassName(this));
067                            }
068    
069                            doRun();
070                    }
071                    catch (Exception e) {
072                            _log.error("Unable to process runnable: " + e.getMessage());
073    
074                            _throwable = e;
075                    }
076                    finally {
077                            if (!shardNames.isEmpty() && shardNames.contains(_shardName)) {
078                                    ShardUtil.setTargetSource(currentShardName);
079    
080                                    ShardUtil.popCompanyService();
081                            }
082    
083                            if (_log.isInfoEnabled()) {
084                                    _log.info(
085                                            "Completed processing runnable " +
086                                                    ClassUtil.getClassName(this) + " in " +
087                                                            (System.currentTimeMillis() - start) + "ms");
088                            }
089                    }
090            }
091    
092            protected abstract void doRun() throws Exception;
093    
094            private static Log _log = LogFactoryUtil.getLog(
095                    ThrowableAwareRunnable.class);
096    
097            private String _shardName;
098            private Throwable _throwable;
099    
100    }