View Javadoc
1   /*
2    * To change this license header, choose License Headers in Project Properties.
3    * To change this template file, choose Tools | Templates
4    * and open the template in the editor.
5    */
6   
7   package org.miloss.fgsms.services.pcs.impl;
8   
9   import java.util.HashSet;
10  import java.util.concurrent.atomic.AtomicReference;
11  import java.util.regex.Pattern;
12  import org.miloss.fgsms.common.Utility;
13  import org.miloss.fgsms.plugins.federation.FederationInterface;
14  import org.miloss.fgsms.services.interfaces.policyconfiguration.FederationPolicy;
15  import org.miloss.fgsms.services.interfaces.policyconfiguration.FederationPolicyCollection;
16  
17  /**
18   * eventually, all of the validation stuff for the PCS service will end up here
19   * with the goal of reducing the line counts
20   * @author AO
21   */
22  public class ValidationTools {
23  
24      //private static final String ID_PATTERN = "\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*";
25      //private static final Pattern FQCN = Pattern.compile(ID_PATTERN + "(\\." + ID_PATTERN + ")*");
26      // return FQCN.matcher(identifier).matches();
27  
28       // All Java reserved words that must not be used in a valid package name.
29      private static final HashSet reserved;
30  
31      static {
32          reserved = new HashSet();
33          reserved.add("abstract");reserved.add("assert");reserved.add("boolean");
34          reserved.add("break");reserved.add("byte");reserved.add("case");
35          reserved.add("catch");reserved.add("char");reserved.add("class");
36          reserved.add("const");reserved.add("continue");reserved.add("default");
37          reserved.add("do");reserved.add("double");reserved.add("else");
38          reserved.add("enum");reserved.add("extends");reserved.add("false");
39          reserved.add("final");reserved.add("finally");reserved.add("float");
40          reserved.add("for");reserved.add("if");reserved.add("goto");
41          reserved.add("implements");reserved.add("import");reserved.add("instanceof");
42          reserved.add("int");reserved.add("interface");reserved.add("long");
43          reserved.add("native");reserved.add("new");reserved.add("null");
44          reserved.add("package");reserved.add("private");reserved.add("protected");
45          reserved.add("public");reserved.add("return");reserved.add("short");
46          reserved.add("static");reserved.add("strictfp");reserved.add("super");
47          reserved.add("switch");reserved.add("synchronized");reserved.add("this");
48          reserved.add("throw");reserved.add("throws");reserved.add("transient");
49          reserved.add("true");reserved.add("try");reserved.add("void");
50          reserved.add("volatile");reserved.add("while");
51      }
52  
53      /**
54       * Checks if the string that is provided is a valid Java package name (contains only
55       * [a-z,A-Z,_,$], every element is separated by a single '.' , an element can't be one of Java's
56       * reserved words.
57       *
58       * @param name The package name that needs to be validated.
59       * @return <b>true</b> if the package name is valid, <b>false</b> if its not valid.
60       */
61      public static final boolean isValidPackageName(String name) {
62          String[] parts=name.split("\\.",-1);
63          for (String part:parts){
64              System.out.println(part);
65              if (reserved.contains(part)) return false;
66              if (!validPart(part)) return false;
67          }
68          
69          return true;
70      }
71  
72      /**
73       * Checks that a part (a word between dots) is a valid part to be used in a Java package name.
74       * @param part The part between dots (e.g. *PART*.*PART*.*PART*.*PART*).
75       * @return <b>true</b> if the part is valid, <b>false</b> if its not valid.
76       */
77      private static boolean validPart(String part){
78          if (part==null || part.length()<1){
79              // Package part is null or empty !
80              return false;
81          }
82          if (Character.isJavaIdentifierStart(part.charAt(0))){
83              for (int i = 0; i < part.length(); i++){
84                  char c = part.charAt(i);
85                  if (!Character.isJavaIdentifierPart(c)){
86                      // Package part contains invalid JavaIdentifier !
87                      return false;
88                  }
89              }
90          }else{
91              // Package part does not begin with a valid JavaIdentifier !
92              return false;
93          }
94  
95          return true;
96      }
97      
98  
99      public static void validateFederationPolicies(FederationPolicyCollection federationPolicyCollection) {
100         if (federationPolicyCollection == null) {
101             return;
102         }
103         if (federationPolicyCollection.getFederationPolicy().isEmpty()) {
104             return;
105         }
106         for (FederationPolicy pol : federationPolicyCollection.getFederationPolicy()) {
107             if (Utility.stringIsNullOrEmpty(pol.getImplementingClassName())) {
108                 throw new IllegalArgumentException("federation class name is null or empty");
109             }
110             AtomicReference<String> outMessage = new AtomicReference<String>("");
111             try {
112                 FederationInterface plugin = (FederationInterface) Class.forName(pol.getImplementingClassName()).newInstance();
113                 if (!plugin.ValidateConfiguration(pol, outMessage)) {
114                     throw new IllegalArgumentException("federation policy parameters are invalid, msg: " + outMessage.get());
115                 }
116             } catch (Throwable ex) {
117                 PCS4jBean.log.error("Unable to load class " + pol.getImplementingClassName(), ex);
118                 throw new IllegalArgumentException("federation policy class is invalid: " + pol.getImplementingClassName() + ": " + ex.getMessage());
119             }
120         }
121     }
122     
123     
124 }