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