1 package com.trendmicro.grid.acl.metadata;
2
3 import com.trendmicro.grid.acl.commons.Paths;
4 import net.sf.tinyjee.util.FileUtils;
5
6 import java.io.File;
7 import java.util.Arrays;
8 import java.util.Collection;
9 import java.util.HashSet;
10 import java.util.concurrent.Callable;
11
12
13
14
15
16
17
18 public final class ValidationContext implements ValidatorResolver {
19
20 public static final String DEFAULT_METADATA_PROFILES_DOCUMENT = "default-metadata-profiles.xml";
21 public static final String METADATA_PROFILES_TO_HTML = "metadata-profile-to-html.xsl";
22 public static final String METADATA_PROFILES_DOCUMENT =
23 System.getProperty("gacl.metadata.profiles", "metadata-profiles.xml");
24
25 private static final ValidationContext INSTANCE = new ValidationContext();
26
27 public static ValidationContext getInstance() {
28 return INSTANCE;
29 }
30
31 final ValidatorFactory validatorFactory;
32 final ThreadLocal<ValidationInstructions> references = new ThreadLocal<ValidationInstructions>();
33
34 private ValidationContext() {
35 try {
36 File configDir = Paths.getConfigPath();
37 File profiles = new File(configDir, METADATA_PROFILES_DOCUMENT);
38
39 ProfilesDocument profilesDocument;
40 if (profiles.isFile())
41 profilesDocument = ProfilesDocument.load(profiles);
42 else {
43 profiles = new File(configDir, DEFAULT_METADATA_PROFILES_DOCUMENT);
44 profilesDocument = ProfilesDocument.load(
45 getClass().getResourceAsStream("/" + DEFAULT_METADATA_PROFILES_DOCUMENT));
46
47 if (configDir.isDirectory()) {
48 ProfilesDocument.getXmlSerializer().save(profilesDocument, profiles, true, 4);
49 FileUtils.copy(getClass().getResourceAsStream("/" + METADATA_PROFILES_TO_HTML),
50 new File(configDir, METADATA_PROFILES_TO_HTML), true);
51 }
52 }
53
54 validatorFactory = new ProfilesDocumentValidatorFactory(profilesDocument);
55 } catch (Exception e) {
56 throw new RuntimeException(e);
57 }
58 }
59
60
61
62
63
64
65
66
67
68 public ValidationInstructions getActiveValidationInstructions() {
69 return references.get();
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83 public void enableValidation(String fqProfileName, AccessLevel level, Collection<String> roles) {
84 if (fqProfileName == null)
85 disableValidation();
86 else {
87 final ValidationInstructions currentInstructions = references.get();
88 if (currentInstructions != null) {
89 if (currentInstructions.getActiveProfileName().equals(fqProfileName) &&
90 currentInstructions.getLevel() == level &&
91 currentInstructions.getRoles().equals(roles))
92 return;
93 }
94
95 references.set(new ValidationInstructions(fqProfileName, level, roles));
96 }
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110 public void enableValidation(String fqProfileName, AccessLevel level, String... roles) {
111 enableValidation(fqProfileName, level, new HashSet<String>(Arrays.asList(roles)));
112 }
113
114
115
116
117
118
119
120 public void disableValidation() {
121 references.remove();
122 }
123
124
125
126
127 @Override
128 public Validator getValidator() {
129 final ValidationInstructions ref = references.get();
130 return ref == null ? null : validatorFactory.newValidator(ref);
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 public <V> V doUnvalidated(Callable<V> callable) throws Exception {
162 final ValidationInstructions instructions = references.get();
163 try {
164 references.remove();
165 return callable.call();
166 } finally {
167 references.set(instructions);
168 }
169 }
170 }