001/* 002 * Copyright (C) Christian Schulte, 2005-206 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions 007 * are met: 008 * 009 * o Redistributions of source code must retain the above copyright 010 * notice, this list of conditions and the following disclaimer. 011 * 012 * o Redistributions in binary form must reproduce the above copyright 013 * notice, this list of conditions and the following disclaimer in 014 * the documentation and/or other materials provided with the 015 * distribution. 016 * 017 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 018 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 019 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 020 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, 021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 027 * 028 * $JOMC: ValidateClassesTask.java 4613 2012-09-22 10:07:08Z schulte $ 029 * 030 */ 031package org.jomc.ant; 032 033import java.io.File; 034import java.io.IOException; 035import java.util.logging.Level; 036import javax.xml.bind.JAXBContext; 037import javax.xml.bind.JAXBException; 038import javax.xml.bind.util.JAXBSource; 039import javax.xml.transform.Source; 040import org.apache.tools.ant.BuildException; 041import org.jomc.model.Implementation; 042import org.jomc.model.Module; 043import org.jomc.model.Specification; 044import org.jomc.modlet.Model; 045import org.jomc.modlet.ModelContext; 046import org.jomc.modlet.ModelException; 047import org.jomc.modlet.ModelValidationReport; 048import org.jomc.modlet.ObjectFactory; 049import org.jomc.tools.ClassFileProcessor; 050 051/** 052 * Task for validating class file model objects. 053 * 054 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 055 * @version $JOMC: ValidateClassesTask.java 4613 2012-09-22 10:07:08Z schulte $ 056 */ 057public final class ValidateClassesTask extends ClassFileProcessorTask 058{ 059 060 /** The directory holding the class files to validate model objects of. */ 061 private File classesDirectory; 062 063 /** Creates a new {@code ValidateClassesTask} instance. */ 064 public ValidateClassesTask() 065 { 066 super(); 067 } 068 069 /** 070 * Gets the directory holding the class files to validate model objects of. 071 * 072 * @return The directory holding the class files to validate model objects of or {@code null}. 073 * 074 * @see #setClassesDirectory(java.io.File) 075 */ 076 public File getClassesDirectory() 077 { 078 return this.classesDirectory; 079 } 080 081 /** 082 * Sets the directory holding the class files to validate model objects of. 083 * 084 * @param value The new directory holding the class files to validate model objects of or {@code null}. 085 * 086 * @see #getClassesDirectory() 087 */ 088 public void setClassesDirectory( final File value ) 089 { 090 this.classesDirectory = value; 091 } 092 093 /** {@inheritDoc} */ 094 @Override 095 public void preExecuteTask() throws BuildException 096 { 097 super.preExecuteTask(); 098 099 this.assertNotNull( "classesDirectory", this.getClassesDirectory() ); 100 } 101 102 /** 103 * Validates class file model objects. 104 * 105 * @throws BuildException if validating class file model objects fails. 106 */ 107 @Override 108 public void processClassFiles() throws BuildException 109 { 110 ProjectClassLoader classLoader = null; 111 boolean suppressExceptionOnClose = true; 112 113 try 114 { 115 this.log( Messages.getMessage( "validatingModelObjects", this.getModel() ) ); 116 117 classLoader = this.newProjectClassLoader(); 118 final ModelContext context = this.newModelContext( classLoader ); 119 final ClassFileProcessor tool = this.newClassFileProcessor(); 120 final JAXBContext jaxbContext = context.createContext( this.getModel() ); 121 final Model model = this.getModel( context ); 122 final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) ); 123 ModelValidationReport validationReport = context.validateModel( this.getModel(), source ); 124 125 this.logValidationReport( context, validationReport ); 126 tool.setModel( model ); 127 128 if ( validationReport.isModelValid() ) 129 { 130 final Specification s = this.getSpecification( model ); 131 final Implementation i = this.getImplementation( model ); 132 final Module m = this.getModule( model ); 133 134 if ( s != null ) 135 { 136 validationReport = tool.validateModelObjects( s, context, this.getClassesDirectory() ); 137 138 if ( validationReport != null ) 139 { 140 this.logValidationReport( context, validationReport ); 141 142 if ( !validationReport.isModelValid() ) 143 { 144 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) ); 145 } 146 } 147 } 148 149 if ( i != null ) 150 { 151 validationReport = tool.validateModelObjects( i, context, this.getClassesDirectory() ); 152 153 if ( validationReport != null ) 154 { 155 this.logValidationReport( context, validationReport ); 156 157 if ( !validationReport.isModelValid() ) 158 { 159 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) ); 160 } 161 } 162 } 163 164 if ( m != null ) 165 { 166 validationReport = tool.validateModelObjects( m, context, this.getClassesDirectory() ); 167 168 if ( validationReport != null ) 169 { 170 this.logValidationReport( context, validationReport ); 171 172 if ( !validationReport.isModelValid() ) 173 { 174 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) ); 175 } 176 } 177 } 178 179 if ( this.isModulesProcessingRequested() ) 180 { 181 validationReport = tool.validateModelObjects( context, this.getClassesDirectory() ); 182 183 if ( validationReport != null ) 184 { 185 this.logValidationReport( context, validationReport ); 186 187 if ( !validationReport.isModelValid() ) 188 { 189 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) ); 190 } 191 } 192 } 193 194 suppressExceptionOnClose = false; 195 } 196 else 197 { 198 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) ); 199 } 200 } 201 catch ( final IOException e ) 202 { 203 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() ); 204 } 205 catch ( final JAXBException e ) 206 { 207 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() ); 208 } 209 catch ( final ModelException e ) 210 { 211 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() ); 212 } 213 finally 214 { 215 try 216 { 217 if ( classLoader != null ) 218 { 219 classLoader.close(); 220 } 221 } 222 catch ( final IOException e ) 223 { 224 if ( suppressExceptionOnClose ) 225 { 226 this.logMessage( Level.SEVERE, Messages.getMessage( e ), e ); 227 } 228 else 229 { 230 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() ); 231 } 232 } 233 } 234 } 235 236 /** {@inheritDoc} */ 237 @Override 238 public ValidateClassesTask clone() 239 { 240 final ValidateClassesTask clone = (ValidateClassesTask) super.clone(); 241 clone.classesDirectory = 242 this.classesDirectory != null ? new File( this.classesDirectory.getAbsolutePath() ) : null; 243 244 return clone; 245 } 246 247}