1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.scripting;
16  
17  import com.liferay.portal.util.PropsValues;
18  
19  import java.util.Arrays;
20  import java.util.HashSet;
21  import java.util.Set;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  /**
26   * <a href="ClassVisibilityChecker.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Alberto Montero
29   * @author Brian Wing Shun Chan
30   */
31  public class ClassVisibilityChecker {
32  
33      public static final String ALL_CLASSES = "all_classes";
34  
35      public ClassVisibilityChecker(Set<String> allowedClasses) {
36          if ((allowedClasses != null) && allowedClasses.contains(ALL_CLASSES)) {
37              _allowAll = true;
38          }
39  
40          if (_forbiddenClasses.contains(ALL_CLASSES)) {
41              _denyAll = true;
42          }
43  
44          if (!_allowAll && !_denyAll) {
45              _allowedPatterns = new HashSet<Pattern>();
46  
47              for (String allowedClass : allowedClasses) {
48                  Pattern allowedPattern = Pattern.compile(allowedClass);
49  
50                  _allowedPatterns.add(allowedPattern);
51              }
52          }
53      }
54  
55      public boolean isVisible(String className) {
56          if (_denyAll || _forbiddenClasses.contains(className)) {
57              return false;
58          }
59  
60          if (_allowAll) {
61              return true;
62          }
63  
64          for (Pattern allowedPattern: _allowedPatterns) {
65              Matcher matcher = allowedPattern.matcher(className);
66  
67              if (matcher.find()) {
68                  return true;
69              }
70          }
71  
72          return false;
73      }
74  
75      private static Set<String> _forbiddenClasses = new HashSet<String>(
76          Arrays.asList(PropsValues.SCRIPTING_FORBIDDEN_CLASSES));
77  
78      private boolean _allowAll;
79      private Set<Pattern> _allowedPatterns;
80      private boolean _denyAll;
81  
82  }