001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.ant;
019:
020: import java.io.BufferedInputStream;
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.InputStream;
024:
025: import org.apache.catalina.startup.Constants;
026: import org.apache.catalina.startup.DigesterFactory;
027: import org.apache.tomcat.util.digester.Digester;
028: import org.apache.tools.ant.BuildException;
029: import org.xml.sax.InputSource;
030:
031: /**
032: * Task for validating a web application deployment descriptor, using XML
033: * schema validation.
034: *
035: * @author Remy Maucherat
036: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
037: * @since 5.0
038: */
039:
040: public class ValidatorTask extends BaseRedirectorHelperTask {
041:
042: // ----------------------------------------------------- Instance Variables
043:
044: // ------------------------------------------------------------- Properties
045:
046: /**
047: * The path to the webapp directory.
048: */
049: protected String path = null;
050:
051: public String getPath() {
052: return (this .path);
053: }
054:
055: public void setPath(String path) {
056: this .path = path;
057: }
058:
059: // --------------------------------------------------------- Public Methods
060:
061: /**
062: * Execute the specified command. This logic only performs the common
063: * attribute validation required by all subclasses; it does not perform
064: * any functional logic directly.
065: *
066: * @exception BuildException if a validation error occurs
067: */
068: public void execute() throws BuildException {
069:
070: if (path == null) {
071: throw new BuildException("Must specify 'path'");
072: }
073:
074: File file = new File(path, Constants.ApplicationWebXml);
075: if ((!file.exists()) || (!file.canRead())) {
076: throw new BuildException("Cannot find web.xml");
077: }
078:
079: // Commons-logging likes having the context classloader set
080: ClassLoader oldCL = Thread.currentThread()
081: .getContextClassLoader();
082: Thread.currentThread().setContextClassLoader(
083: ValidatorTask.class.getClassLoader());
084:
085: Digester digester = DigesterFactory.newDigester(true, true,
086: null);
087: try {
088: file = file.getCanonicalFile();
089: InputStream stream = new BufferedInputStream(
090: new FileInputStream(file));
091: InputSource is = new InputSource(file.toURL()
092: .toExternalForm());
093: is.setByteStream(stream);
094: digester.parse(is);
095: handleOutput("web.xml validated");
096: } catch (Throwable t) {
097: if (isFailOnError()) {
098: throw new BuildException("Validation failure", t);
099: } else {
100: handleErrorOutput("Validation failure: " + t);
101: }
102: } finally {
103: Thread.currentThread().setContextClassLoader(oldCL);
104: closeRedirector();
105: }
106:
107: }
108:
109: }
|