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.resiliency.service;
016    
017    import com.liferay.portal.kernel.model.User;
018    import com.liferay.portal.kernel.process.ProcessCallable;
019    import com.liferay.portal.kernel.process.ProcessException;
020    import com.liferay.portal.kernel.security.auth.PrincipalThreadLocal;
021    import com.liferay.portal.kernel.security.permission.PermissionChecker;
022    import com.liferay.portal.kernel.security.permission.PermissionCheckerFactoryUtil;
023    import com.liferay.portal.kernel.security.permission.PermissionThreadLocal;
024    import com.liferay.portal.kernel.service.UserLocalServiceUtil;
025    import com.liferay.portal.kernel.util.MethodHandler;
026    
027    import java.io.Externalizable;
028    import java.io.IOException;
029    import java.io.ObjectInput;
030    import java.io.ObjectOutput;
031    import java.io.Serializable;
032    
033    /**
034     * @author Shuyang Zhou
035     */
036    public class ServiceMethodProcessCallable
037            implements Externalizable, ProcessCallable<Serializable> {
038    
039            /**
040             * The empty constructor is required by {@link Externalizable}. Do not use
041             * this for any other purpose.
042             */
043            public ServiceMethodProcessCallable() {
044            }
045    
046            public ServiceMethodProcessCallable(MethodHandler methodHandler) {
047                    _methodHandler = methodHandler;
048    
049                    PermissionChecker permissionChecker =
050                            PermissionThreadLocal.getPermissionChecker();
051    
052                    if (permissionChecker != null) {
053                            _userId = permissionChecker.getUserId();
054                    }
055            }
056    
057            @Override
058            public Serializable call() throws ProcessException {
059                    String oldName = PrincipalThreadLocal.getName();
060                    PermissionChecker oldPermissionChecker =
061                            PermissionThreadLocal.getPermissionChecker();
062    
063                    try {
064                            if (_userId != 0) {
065                                    PrincipalThreadLocal.setName(_userId);
066    
067                                    User user = UserLocalServiceUtil.fetchUser(_userId);
068    
069                                    if (user != null) {
070                                            PermissionChecker permissionChecker =
071                                                    PermissionCheckerFactoryUtil.create(user);
072    
073                                            PermissionThreadLocal.setPermissionChecker(
074                                                    permissionChecker);
075                                    }
076                            }
077    
078                            return (Serializable)_methodHandler.invoke();
079                    }
080                    catch (Exception e) {
081                            throw new ProcessException(e);
082                    }
083                    finally {
084                            PrincipalThreadLocal.setName(oldName);
085                            PermissionThreadLocal.setPermissionChecker(oldPermissionChecker);
086                    }
087            }
088    
089            @Override
090            public void readExternal(ObjectInput objectInput)
091                    throws ClassNotFoundException, IOException {
092    
093                    _methodHandler = (MethodHandler)objectInput.readObject();
094                    _userId = objectInput.readLong();
095            }
096    
097            @Override
098            public void writeExternal(ObjectOutput objectOutput) throws IOException {
099                    objectOutput.writeObject(_methodHandler);
100                    objectOutput.writeLong(_userId);
101            }
102    
103            private MethodHandler _methodHandler;
104            private long _userId;
105    
106    }