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