001
014
015 package com.liferay.portal.security.lang;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.ServiceLoader;
020 import com.liferay.portal.util.PropsValues;
021
022 import java.util.List;
023
024
029 public class SecurityManagerUtil {
030
031 public static final boolean ENABLED = (System.getSecurityManager() != null);
032
033 public static void destroy() {
034 if (_portalSecurityManager == null) {
035 return;
036 }
037
038 _portalSecurityManager.destroy();
039
040 _portalSecurityManager = null;
041 }
042
043 public static PortalSecurityManager getPortalSecurityManager() {
044 return _portalSecurityManager;
045 }
046
047 public static void init() {
048 if (_portalSecurityManagerStrategy != null) {
049 return;
050 }
051
052 _portalSecurityManagerStrategy = PortalSecurityManagerStrategy.parse(
053 PropsValues.PORTAL_SECURITY_MANAGER_STRATEGY);
054
055 if (_portalSecurityManagerStrategy ==
056 PortalSecurityManagerStrategy.LIFERAY) {
057
058 if (!ENABLED) {
059 _log.error(
060 "Plugin security management is not enabled. Enable a " +
061 "security manager, then restart.");
062
063 _portalSecurityManagerStrategy =
064 PortalSecurityManagerStrategy.DEFAULT;
065
066 return;
067 }
068
069 loadPortalSecurityManager();
070
071 if (_portalSecurityManager == null) {
072 _portalSecurityManagerStrategy =
073 PortalSecurityManagerStrategy.DEFAULT;
074
075 if (_log.isInfoEnabled()) {
076 _log.info(
077 "No portal security manager implementation was " +
078 "located. Continuing with the default security " +
079 "strategy.");
080 }
081
082 return;
083 }
084 }
085
086 if (_portalSecurityManagerStrategy ==
087 PortalSecurityManagerStrategy.LIFERAY) {
088
089 System.setSecurityManager((SecurityManager)_portalSecurityManager);
090 }
091 }
092
093 public static boolean isDefault() {
094 init();
095
096 if (_portalSecurityManagerStrategy ==
097 PortalSecurityManagerStrategy.DEFAULT) {
098
099 return true;
100 }
101
102 return false;
103 }
104
105 public static boolean isLiferay() {
106 init();
107
108 if (_portalSecurityManagerStrategy ==
109 PortalSecurityManagerStrategy.LIFERAY) {
110
111 return true;
112 }
113
114 return false;
115 }
116
117 private static void loadPortalSecurityManager() {
118 try {
119 List<PortalSecurityManager> portalSecurityManagers =
120 ServiceLoader.load(PortalSecurityManager.class);
121
122 if (portalSecurityManagers.isEmpty()) {
123 return;
124 }
125
126 _portalSecurityManager = portalSecurityManagers.get(0);
127 }
128 catch (Exception e) {
129 _log.error(e, e);
130 }
131 }
132
133 private static Log _log = LogFactoryUtil.getLog(SecurityManagerUtil.class);
134
135 private static PortalSecurityManager _portalSecurityManager;
136 private static PortalSecurityManagerStrategy _portalSecurityManagerStrategy;
137
138 private enum PortalSecurityManagerStrategy {
139
140 DEFAULT, LIFERAY;
141
142 public static PortalSecurityManagerStrategy parse(String value) {
143 if (PropsValues.TCK_URL) {
144 return DEFAULT;
145 }
146 else if (value.equals("liferay")) {
147 return LIFERAY;
148 }
149
150 return DEFAULT;
151 }
152
153 }
154
155 }