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.security.permission;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.security.auth.PrincipalThreadLocal;
021    import com.liferay.portal.service.UserLocalServiceUtil;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public abstract class DoAsUserThread extends Thread {
027    
028            public DoAsUserThread(long userId) {
029                    this(userId, 1);
030            }
031    
032            public DoAsUserThread(long userId, int retries) {
033                    _userId = userId;
034    
035                    _retries = retries;
036            }
037    
038            public boolean isSuccess() {
039                    return _success;
040            }
041    
042            @Override
043            public void run() {
044                    for (int i = 0; i < _retries; i++) {
045                            try {
046                                    PrincipalThreadLocal.setName(_userId);
047    
048                                    User user = UserLocalServiceUtil.getUserById(_userId);
049    
050                                    PermissionChecker permissionChecker =
051                                            PermissionCheckerFactoryUtil.create(user);
052    
053                                    PermissionThreadLocal.setPermissionChecker(permissionChecker);
054    
055                                    doRun();
056    
057                                    _success = true;
058    
059                                    return;
060                            }
061                            catch (Exception e) {
062                                    _log.error(e, e);
063                            }
064                            finally {
065                                    PrincipalThreadLocal.setName(null);
066                                    PermissionThreadLocal.setPermissionChecker(null);
067                            }
068                    }
069            }
070    
071            protected abstract void doRun() throws Exception;
072    
073            private static final Log _log = LogFactoryUtil.getLog(DoAsUserThread.class);
074    
075            private final int _retries;
076            private boolean _success;
077            private final long _userId;
078    
079    }