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