1
2
3
4
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
19
20
21
22 public class ValidationTools {
23
24
25
26
27
28
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
55
56
57
58
59
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
74
75
76
77 private static boolean validPart(String part){
78 if (part==null || part.length()<1){
79
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
87 return false;
88 }
89 }
90 }else{
91
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 }