001    /**
002     * Copyright (c) 2000-2010 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.scripting;
016    
017    import com.liferay.portal.util.PropsValues;
018    
019    import java.util.Arrays;
020    import java.util.HashSet;
021    import java.util.Set;
022    import java.util.regex.Matcher;
023    import java.util.regex.Pattern;
024    
025    /**
026     * @author Alberto Montero
027     * @author Brian Wing Shun Chan
028     */
029    public class ClassVisibilityChecker {
030    
031            public static final String ALL_CLASSES = "all_classes";
032    
033            public ClassVisibilityChecker(Set<String> allowedClasses) {
034                    if ((allowedClasses != null) && allowedClasses.contains(ALL_CLASSES)) {
035                            _allowAll = true;
036                    }
037    
038                    if (_forbiddenClasses.contains(ALL_CLASSES)) {
039                            _denyAll = true;
040                    }
041    
042                    if (!_allowAll && !_denyAll) {
043                            _allowedPatterns = new HashSet<Pattern>();
044    
045                            for (String allowedClass : allowedClasses) {
046                                    Pattern allowedPattern = Pattern.compile(allowedClass);
047    
048                                    _allowedPatterns.add(allowedPattern);
049                            }
050                    }
051            }
052    
053            public boolean isVisible(String className) {
054                    if (_denyAll || _forbiddenClasses.contains(className)) {
055                            return false;
056                    }
057    
058                    if (_allowAll) {
059                            return true;
060                    }
061    
062                    for (Pattern allowedPattern: _allowedPatterns) {
063                            Matcher matcher = allowedPattern.matcher(className);
064    
065                            if (matcher.find()) {
066                                    return true;
067                            }
068                    }
069    
070                    return false;
071            }
072    
073            private static Set<String> _forbiddenClasses = new HashSet<String>(
074                    Arrays.asList(PropsValues.SCRIPTING_FORBIDDEN_CLASSES));
075    
076            private boolean _allowAll;
077            private Set<Pattern> _allowedPatterns;
078            private boolean _denyAll;
079    
080    }