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.backgroundtask;
016    
017    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskThreadLocalManager;
018    import com.liferay.portal.kernel.cluster.ClusterInvokeThreadLocal;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.GroupThreadLocal;
021    import com.liferay.portal.kernel.util.LocaleThreadLocal;
022    import com.liferay.portal.kernel.util.MapUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.security.auth.CompanyThreadLocal;
026    import com.liferay.portal.security.auth.PrincipalThreadLocal;
027    import com.liferay.portal.security.permission.PermissionChecker;
028    import com.liferay.portal.security.permission.PermissionCheckerFactory;
029    import com.liferay.portal.security.permission.PermissionThreadLocal;
030    import com.liferay.portal.service.UserLocalServiceUtil;
031    
032    import java.io.Serializable;
033    
034    import java.util.HashMap;
035    import java.util.Locale;
036    import java.util.Map;
037    
038    /**
039     * @author Michael C. Han
040     */
041    public class BackgroundTaskThreadLocalManagerImpl
042            implements BackgroundTaskThreadLocalManager {
043    
044            @Override
045            public void deserializeThreadLocals(
046                    Map<String, Serializable> taskContextMap) {
047    
048                    Map<String, Serializable> threadLocalValues =
049                            (Map<String, Serializable>)taskContextMap.get(
050                                    KEY_THREAD_LOCAL_VALUES);
051    
052                    setThreadLocalValues(threadLocalValues);
053            }
054    
055            @Override
056            public Map<String, Serializable> getThreadLocalValues() {
057                    Map<String, Serializable> threadLocalValues = new HashMap<>();
058    
059                    threadLocalValues.put("companyId", CompanyThreadLocal.getCompanyId());
060                    threadLocalValues.put(
061                            "clusterInvoke", ClusterInvokeThreadLocal.isEnabled());
062                    threadLocalValues.put(
063                            "defaultLocale", LocaleThreadLocal.getDefaultLocale());
064                    threadLocalValues.put("groupId", GroupThreadLocal.getGroupId());
065                    threadLocalValues.put("principalName", PrincipalThreadLocal.getName());
066                    threadLocalValues.put(
067                            "siteDefaultLocale", LocaleThreadLocal.getSiteDefaultLocale());
068                    threadLocalValues.put(
069                            "themeDisplayLocale", LocaleThreadLocal.getThemeDisplayLocale());
070    
071                    return threadLocalValues;
072            }
073    
074            @Override
075            public void serializeThreadLocals(
076                    Map<String, Serializable> taskContextMap) {
077    
078                    HashMap<String, Serializable> taskContextThreadLocalValues =
079                            (HashMap<String, Serializable>)taskContextMap.get(
080                                    KEY_THREAD_LOCAL_VALUES);
081    
082                    if (taskContextThreadLocalValues == null) {
083                            taskContextThreadLocalValues = new HashMap<>();
084    
085                            taskContextMap.put(
086                                    KEY_THREAD_LOCAL_VALUES, taskContextThreadLocalValues);
087                    }
088    
089                    Map<String, Serializable> currentThreadLocalValues =
090                            getThreadLocalValues();
091    
092                    taskContextThreadLocalValues.putAll(currentThreadLocalValues);
093            }
094    
095            public void setPermissionCheckerFactory(
096                    PermissionCheckerFactory permissionCheckerFactory) {
097    
098                    _permissionCheckerFactory = permissionCheckerFactory;
099            }
100    
101            @Override
102            public void setThreadLocalValues(
103                    Map<String, Serializable> threadLocalValues) {
104    
105                    if (MapUtil.isEmpty(threadLocalValues)) {
106                            return;
107                    }
108    
109                    long companyId = GetterUtil.getLong(threadLocalValues.get("companyId"));
110    
111                    if (companyId > 0) {
112                            CompanyThreadLocal.setCompanyId(companyId);
113                    }
114    
115                    Boolean clusterInvoke = (Boolean)threadLocalValues.get("clusterInvoke");
116    
117                    if (clusterInvoke != null) {
118                            ClusterInvokeThreadLocal.setEnabled(clusterInvoke);
119                    }
120    
121                    Locale defaultLocale = (Locale)threadLocalValues.get("defaultLocale");
122    
123                    if (defaultLocale != null) {
124                            LocaleThreadLocal.setDefaultLocale(defaultLocale);
125                    }
126    
127                    long groupId = GetterUtil.getLong(threadLocalValues.get("groupId"));
128    
129                    if (groupId > 0) {
130                            GroupThreadLocal.setGroupId(groupId);
131                    }
132    
133                    String principalName = GetterUtil.getString(
134                            threadLocalValues.get("principalName"));
135    
136                    if (Validator.isNotNull(principalName)) {
137                            PrincipalThreadLocal.setName(principalName);
138                    }
139    
140                    if (Validator.isNotNull(principalName)) {
141                            try {
142                                    User user = UserLocalServiceUtil.fetchUser(
143                                            PrincipalThreadLocal.getUserId());
144    
145                                    PermissionChecker permissionChecker =
146                                            _permissionCheckerFactory.create(user);
147    
148                                    PermissionThreadLocal.setPermissionChecker(permissionChecker);
149                            }
150                            catch (Exception e) {
151                                    throw new RuntimeException(e);
152                            }
153                    }
154    
155                    Locale siteDefaultLocale = (Locale)threadLocalValues.get(
156                            "siteDefaultLocale");
157    
158                    if (siteDefaultLocale != null) {
159                            LocaleThreadLocal.setSiteDefaultLocale(siteDefaultLocale);
160                    }
161    
162                    Locale themeDisplayLocale = (Locale)threadLocalValues.get(
163                            "themeDisplayLocale");
164    
165                    if (themeDisplayLocale != null) {
166                            LocaleThreadLocal.setThemeDisplayLocale(themeDisplayLocale);
167                    }
168            }
169    
170            protected static final String KEY_THREAD_LOCAL_VALUES = "threadLocalValues";
171    
172            private PermissionCheckerFactory _permissionCheckerFactory;
173    
174    }