001: /*
002: * Created on Feb 9, 2004
003: *
004: * To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package org.vfny.geoserver.global;
008:
009: import org.geotools.validation.FeatureValidation;
010: import org.geotools.validation.IntegrityValidation;
011: import org.geotools.validation.PlugIn;
012: import org.geotools.validation.Validation;
013: import org.geotools.validation.ValidationProcessor;
014: import org.geotools.validation.dto.ArgumentDTO;
015: import org.geotools.validation.dto.PlugInDTO;
016: import org.geotools.validation.dto.TestDTO;
017: import org.geotools.validation.dto.TestSuiteDTO;
018: import org.geotools.validation.xml.ValidationException;
019: import org.geotools.validation.xml.XMLReader;
020: import java.io.File;
021: import java.util.HashMap;
022: import java.util.HashSet;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.Set;
026: import java.util.logging.Level;
027: import java.util.logging.Logger;
028:
029: /**
030: * GeoValidator purpose.
031: * <p>
032: * Description of GeoValidator ...
033: * </p>
034: *
035: * @author dzwiers, Refractions Research, Inc.
036: * @author $Author: jive $ (last modification)
037: * @version $Id: GeoValidator.java 7769 2007-11-15 15:11:39Z aaime $
038: */
039: public class GeoValidator extends ValidationProcessor {
040: public static final String WEB_CONTAINER_KEY = "GeoValidator";
041:
042: /**
043: * GeoValidator constructor.
044: * <p>
045: * super();
046: * </p>
047: *
048: */
049: public GeoValidator() {
050: super ();
051: }
052:
053: /**
054: * Creates a new geo validator.
055: *
056: * @param config The configuration module.
057: */
058: public GeoValidator(Config config) {
059: loadPlugins(config.dataDirectory());
060: }
061:
062: /**
063: * Loads validations plugins.
064: *
065: * @param dataDir The data directory.
066: */
067: protected void loadPlugins(File dataDir) {
068: Map plugIns = null;
069: Map testSuites = null;
070:
071: try {
072: File plugInDir = GeoserverDataDirectory.findConfigDir(
073: dataDir, "plugIns");
074: File validationDir = GeoserverDataDirectory.findConfigDir(
075: dataDir, "validation");
076:
077: if (plugInDir != null && plugInDir.exists()) {
078: plugIns = XMLReader.loadPlugIns(plugInDir);
079:
080: if (validationDir != null && validationDir.exists()) {
081: testSuites = XMLReader.loadValidations(
082: validationDir, plugIns);
083: }
084: }
085: } catch (Exception e) {
086: org.geotools.util.logging.Logging.getLogger(
087: "org.vfny.geoserver.global").log(Level.WARNING,
088: "loading plugins", e);
089: }
090:
091: if (testSuites == null)
092: testSuites = new HashMap();
093: if (plugIns == null)
094: plugIns = new HashMap();
095:
096: load(testSuites, plugIns);
097: }
098:
099: /**
100: * ValidationProcessor constructor.
101: *
102: * <p>
103: * Builds a ValidationProcessor with the DTO provided.
104: * </p>
105: *
106: * @see load(Map,Map)
107: * @param testSuites Map a map of names -> TestSuiteDTO objects
108: * @param plugIns Map a map of names -> PlugInDTO objects
109: */
110: public GeoValidator(Map testSuites, Map plugIns) {
111: super ();
112: load(testSuites, plugIns);
113: }
114:
115: private Map testSuites;
116: private Map plugIns;
117: private Map errors;
118:
119: /**
120: * Map of errors encountered during loading process
121: * <p>
122: * Map of true (loaded), false (never used), or exception (error) keyed
123: * by PlugIn and Test DataTransferObjects.
124: * </p>
125: * @return Map of status by PlugInDTO and TestDTO
126: */
127: public Map getErrors() {
128: return errors;
129: }
130:
131: /**
132: * load purpose.
133: * <p>
134: * loads this instance data into this instance.
135: * </p>
136: * @param testSuites
137: * @param plugIns
138: */
139: public void load(Map testSuites, Map plugIns) {
140: this .plugIns = plugIns;
141: this .testSuites = testSuites;
142: errors = new HashMap();
143:
144: // step 1 make a list required plug-ins
145: Set plugInNames = new HashSet();
146: Iterator i = testSuites.keySet().iterator();
147:
148: while (i.hasNext()) {
149: TestSuiteDTO dto = (TestSuiteDTO) testSuites.get(i.next());
150: Iterator j = dto.getTests().keySet().iterator();
151:
152: while (j.hasNext()) {
153: TestDTO tdto = (TestDTO) dto.getTests().get(j.next());
154: plugInNames.add(tdto.getPlugIn().getName());
155: }
156: }
157:
158: // Mark all plug-ins as not loaded
159: //
160: i = plugIns.values().iterator();
161:
162: while (i.hasNext()) {
163: PlugInDTO dto = (PlugInDTO) i.next();
164: errors.put(dto, Boolean.FALSE);
165: }
166:
167: // step 2 configure plug-ins with defaults
168: Map defaultPlugIns = new HashMap(plugInNames.size());
169: i = plugInNames.iterator();
170:
171: while (i.hasNext()) {
172: String plugInName = (String) i.next();
173: PlugInDTO dto = (PlugInDTO) plugIns.get(plugInName);
174: Class plugInClass = null;
175:
176: try {
177: plugInClass = Class.forName(dto.getClassName());
178: } catch (ClassNotFoundException e) {
179: //Error, using default.
180: errors.put(dto, e);
181: e.printStackTrace();
182: }
183:
184: if (plugInClass == null) {
185: plugInClass = Validation.class;
186: }
187:
188: Map plugInArgs = dto.getArgs();
189:
190: if (plugInArgs == null) {
191: plugInArgs = new HashMap();
192: }
193:
194: try {
195: PlugIn plugIn = new org.geotools.validation.PlugIn(
196: plugInName, plugInClass, dto.getDescription(),
197: plugInArgs);
198: defaultPlugIns.put(plugInName, plugIn);
199: } catch (ValidationException e) {
200: e.printStackTrace();
201: // Update dto entry w/ an error?
202: errors.put(dto, e);
203:
204: continue;
205: }
206:
207: // mark dto entry as a success
208: errors.put(dto, Boolean.TRUE);
209: }
210:
211: // step 3 configure plug-ins with tests + add to processor
212: i = testSuites.keySet().iterator();
213:
214: while (i.hasNext()) {
215: TestSuiteDTO tdto = (TestSuiteDTO) testSuites.get(i.next());
216: Iterator j = tdto.getTests().keySet().iterator();
217:
218: while (j.hasNext()) {
219: TestDTO dto = (TestDTO) tdto.getTests().get(j.next());
220:
221: // deal with test
222: Map testArgs = dto.getArgs();
223:
224: if (testArgs == null) {
225: testArgs = new HashMap();
226: } else {
227: Map m = new HashMap();
228: Iterator k = testArgs.keySet().iterator();
229:
230: while (k.hasNext()) {
231: ArgumentDTO adto = (ArgumentDTO) testArgs.get(k
232: .next());
233: m.put(adto.getName(), adto.getValue());
234: }
235:
236: testArgs = m;
237: }
238:
239: try {
240: PlugIn plugIn = (org.geotools.validation.PlugIn) defaultPlugIns
241: .get(dto.getPlugIn().getName());
242: Validation validation = plugIn.createValidation(dto
243: .getName(), dto.getDescription(), testArgs);
244:
245: if (validation instanceof FeatureValidation) {
246: addValidation((FeatureValidation) validation);
247: }
248:
249: if (validation instanceof IntegrityValidation) {
250: addValidation((IntegrityValidation) validation);
251: }
252: } catch (ValidationException e) {
253: e.printStackTrace();
254: // place test error under the plugIn DTO that spawned it
255: errors.put(dto, e);
256:
257: //error should log here
258: continue;
259: }
260:
261: errors.put(dto, Boolean.TRUE);
262: }
263:
264: errors.put(tdto, Boolean.TRUE);
265: }
266: }
267:
268: public Object toPlugInDTO() {
269: return plugIns;
270: }
271:
272: public Object toTestSuiteDTO() {
273: return testSuites;
274: }
275:
276: public Map getPlugIns() {
277: return plugIns;
278: }
279:
280: public Map getTestSuites() {
281: return testSuites;
282: }
283: }
|