001    /**
002     * Copyright (c) 2000-2012 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.pacl;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.PropsUtil;
021    import com.liferay.portal.kernel.util.SetUtil;
022    import com.liferay.portal.kernel.util.SortedProperties;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.security.pacl.checker.Checker;
026    
027    import java.security.Permission;
028    
029    import java.util.HashMap;
030    import java.util.Map;
031    import java.util.Properties;
032    import java.util.Set;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public abstract class BasePACLPolicy implements PACLPolicy {
038    
039            public BasePACLPolicy(
040                    String servletContextName, ClassLoader classLoader,
041                    Properties properties) {
042    
043                    _servletContextName = servletContextName;
044                    _classLoader = classLoader;
045                    _properties = properties;
046    
047                    try {
048                            initCheckers();
049                    }
050                    catch (Exception e) {
051                            _log.error(e, e);
052                    }
053            }
054    
055            public ClassLoader getClassLoader() {
056                    return _classLoader;
057            }
058    
059            public Properties getProperties() {
060                    return _properties;
061            }
062    
063            public String getProperty(String key) {
064                    return _properties.getProperty(key);
065            }
066    
067            public String[] getPropertyArray(String key) {
068                    return StringUtil.split(getProperty(key));
069            }
070    
071            public boolean getPropertyBoolean(String key) {
072                    return GetterUtil.getBoolean(getProperty(key));
073            }
074    
075            public Set<String> getPropertySet(String key) {
076                    return SetUtil.fromArray(getPropertyArray(key));
077            }
078    
079            public String getServletContextName() {
080                    return _servletContextName;
081            }
082    
083            public boolean isCheckablePermission(Permission permission) {
084                    Class<?> clazz = permission.getClass();
085    
086                    return _checkers.containsKey(clazz.getName());
087            }
088    
089            @Override
090            public String toString() {
091                    StringBundler sb = new StringBundler(7);
092    
093                    sb.append("{active=");
094                    sb.append(isActive());
095                    sb.append(", hashCode=");
096                    sb.append(hashCode());
097                    sb.append(", servletContextName=");
098                    sb.append(_servletContextName);
099                    sb.append("}");
100    
101                    return sb.toString();
102            }
103    
104            protected Checker getChecker(Class<? extends Permission> clazz) {
105                    return _checkers.get(clazz.getName());
106            }
107    
108            protected Checker initChecker(Checker checker) {
109                    checker.setPACLPolicy(this);
110    
111                    checker.afterPropertiesSet();
112    
113                    return checker;
114            }
115    
116            protected void initCheckers() throws Exception {
117                    Class<?> clazz = getClass();
118    
119                    ClassLoader classLoader = clazz.getClassLoader();
120    
121                    Properties portalProperties = PropsUtil.getProperties(
122                            "portal.security.manager.pacl.policy.checker", false);
123    
124                    portalProperties = new SortedProperties(portalProperties);
125    
126                    if (_log.isInfoEnabled()) {
127                            _log.info(
128                                    "Registering " + portalProperties.size() +
129                                            " PACL policy checkers");
130                    }
131    
132                    for (Map.Entry<Object, Object> entry : portalProperties.entrySet()) {
133                            String key = (String)entry.getKey();
134    
135                            int x = key.indexOf("[");
136                            int y = key.indexOf("]");
137    
138                            String permissionClassName = key.substring(x + 1, y);
139    
140                            String checkerClassName = (String)entry.getValue();
141    
142                            Class<?> checkerClass = classLoader.loadClass(checkerClassName);
143    
144                            Checker checker = (Checker)checkerClass.newInstance();
145    
146                            initChecker(checker);
147    
148                            if (_log.isInfoEnabled()) {
149                                    _log.info(
150                                            "Registering permission " + permissionClassName +
151                                                    " with PACL policy " + checkerClassName);
152                            }
153    
154                            _checkers.put(permissionClassName, checker);
155                    }
156            }
157    
158            private static Log _log = LogFactoryUtil.getLog(BasePACLPolicy.class);
159    
160            private Map<String, Checker> _checkers = new HashMap<String, Checker>();
161    
162            private ClassLoader _classLoader;
163            private Properties _properties;
164            private String _servletContextName;
165    
166    }