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.service;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.security.auth.CompanyThreadLocal;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.security.auth.PrincipalThreadLocal;
026    import com.liferay.portal.security.permission.PermissionChecker;
027    import com.liferay.portal.security.permission.PermissionThreadLocal;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public abstract class BaseServiceImpl implements BaseService {
033    
034            public static final String[] ANONYMOUS_NAMES = {
035                    BaseServiceImpl.JRUN_ANONYMOUS, BaseServiceImpl.ORACLE_ANONYMOUS,
036                    BaseServiceImpl.SUN_ANONYMOUS, BaseServiceImpl.WEBLOGIC_ANONYMOUS
037            };
038    
039            public static final String JRUN_ANONYMOUS = "anonymous-guest";
040    
041            public static final String ORACLE_ANONYMOUS = "guest";
042    
043            public static final String SUN_ANONYMOUS = "ANONYMOUS";
044    
045            public static final String WEBLOGIC_ANONYMOUS = "<anonymous>";
046    
047            public User getGuestOrUser() throws PortalException, SystemException {
048                    try {
049                            return getUser();
050                    }
051                    catch (PrincipalException pe) {
052                            try {
053                                    return UserLocalServiceUtil.getDefaultUser(
054                                            CompanyThreadLocal.getCompanyId());
055                            }
056                            catch (Exception e) {
057                                    throw pe;
058                            }
059                    }
060            }
061    
062            public long getGuestOrUserId() throws PrincipalException {
063                    try {
064                            return getUserId();
065                    }
066                    catch (PrincipalException pe) {
067                            try {
068                                    return UserLocalServiceUtil.getDefaultUserId(
069                                            CompanyThreadLocal.getCompanyId());
070                            }
071                            catch (Exception e) {
072                                    throw pe;
073                            }
074                    }
075            }
076    
077            public PermissionChecker getPermissionChecker() throws PrincipalException {
078                    PermissionChecker permissionChecker =
079                            PermissionThreadLocal.getPermissionChecker();
080    
081                    if (permissionChecker == null) {
082                            throw new PrincipalException("PermissionChecker not initialized");
083                    }
084    
085                    return permissionChecker;
086            }
087    
088            public User getUser() throws PortalException, SystemException {
089                    return UserLocalServiceUtil.getUserById(getUserId());
090            }
091    
092            public long getUserId() throws PrincipalException {
093                    String name = PrincipalThreadLocal.getName();
094    
095                    if (name == null) {
096                            throw new PrincipalException();
097                    }
098    
099                    if (Validator.isNull(name)) {
100                            throw new PrincipalException("Principal is null");
101                    }
102                    else {
103                            for (int i = 0; i < ANONYMOUS_NAMES.length; i++) {
104                                    if (StringUtil.equalsIgnoreCase(name, ANONYMOUS_NAMES[i])) {
105                                            throw new PrincipalException(
106                                                    "Principal cannot be " + ANONYMOUS_NAMES[i]);
107                                    }
108                            }
109                    }
110    
111                    return GetterUtil.getLong(name);
112            }
113    
114            protected ClassLoader getClassLoader() {
115                    Class<?> clazz = getClass();
116    
117                    return clazz.getClassLoader();
118            }
119    
120    }