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