001/* 002 * Copyright (C) 2005 Christian Schulte <cs@schulte.it> 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 5179 2016-04-15 02:58:23Z 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 5179 2016-04-15 02:58:23Z schulte $ 056 */ 057public final class ValidateClassesTask extends ClassFileProcessorTask 058{ 059 060 /** 061 * The directory holding the class files to validate model objects of. 062 */ 063 private File classesDirectory; 064 065 /** 066 * Creates a new {@code ValidateClassesTask} instance. 067 */ 068 public ValidateClassesTask() 069 { 070 super(); 071 } 072 073 /** 074 * Gets the directory holding the class files to validate model objects of. 075 * 076 * @return The directory holding the class files to validate model objects of or {@code null}. 077 * 078 * @see #setClassesDirectory(java.io.File) 079 */ 080 public File getClassesDirectory() 081 { 082 return this.classesDirectory; 083 } 084 085 /** 086 * Sets the directory holding the class files to validate model objects of. 087 * 088 * @param value The new directory holding the class files to validate model objects of or {@code null}. 089 * 090 * @see #getClassesDirectory() 091 */ 092 public void setClassesDirectory( final File value ) 093 { 094 this.classesDirectory = value; 095 } 096 097 /** 098 * {@inheritDoc} 099 */ 100 @Override 101 public void preExecuteTask() throws BuildException 102 { 103 super.preExecuteTask(); 104 105 this.assertNotNull( "classesDirectory", this.getClassesDirectory() ); 106 } 107 108 /** 109 * Validates class file model objects. 110 * 111 * @throws BuildException if validating class file model objects fails. 112 */ 113 @Override 114 public void processClassFiles() throws BuildException 115 { 116 ProjectClassLoader classLoader = null; 117 118 try 119 { 120 this.log( Messages.getMessage( "validatingModelObjects", this.getModel() ) ); 121 122 classLoader = this.newProjectClassLoader(); 123 final ModelContext context = this.newModelContext( classLoader ); 124 final ClassFileProcessor tool = this.newClassFileProcessor(); 125 final JAXBContext jaxbContext = context.createContext( this.getModel() ); 126 final Model model = this.getModel( context ); 127 final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) ); 128 ModelValidationReport validationReport = context.validateModel( this.getModel(), source ); 129 130 this.logValidationReport( context, validationReport ); 131 tool.setModel( model ); 132 133 if ( validationReport.isModelValid() ) 134 { 135 final Specification s = this.getSpecification( model ); 136 final Implementation i = this.getImplementation( model ); 137 final Module m = this.getModule( model ); 138 139 if ( s != null ) 140 { 141 validationReport = tool.validateModelObjects( s, context, this.getClassesDirectory() ); 142 143 if ( validationReport != null ) 144 { 145 this.logValidationReport( context, validationReport ); 146 147 if ( !validationReport.isModelValid() ) 148 { 149 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) ); 150 } 151 } 152 } 153 154 if ( i != null ) 155 { 156 validationReport = tool.validateModelObjects( i, context, this.getClassesDirectory() ); 157 158 if ( validationReport != null ) 159 { 160 this.logValidationReport( context, validationReport ); 161 162 if ( !validationReport.isModelValid() ) 163 { 164 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) ); 165 } 166 } 167 } 168 169 if ( m != null ) 170 { 171 validationReport = tool.validateModelObjects( m, context, this.getClassesDirectory() ); 172 173 if ( validationReport != null ) 174 { 175 this.logValidationReport( context, validationReport ); 176 177 if ( !validationReport.isModelValid() ) 178 { 179 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) ); 180 } 181 } 182 } 183 184 if ( this.isModulesProcessingRequested() ) 185 { 186 validationReport = tool.validateModelObjects( context, this.getClassesDirectory() ); 187 188 if ( validationReport != null ) 189 { 190 this.logValidationReport( context, validationReport ); 191 192 if ( !validationReport.isModelValid() ) 193 { 194 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) ); 195 } 196 } 197 } 198 199 classLoader.close(); 200 classLoader = null; 201 } 202 else 203 { 204 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) ); 205 } 206 } 207 catch ( final IOException e ) 208 { 209 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() ); 210 } 211 catch ( final JAXBException e ) 212 { 213 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() ); 214 } 215 catch ( final ModelException e ) 216 { 217 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() ); 218 } 219 finally 220 { 221 try 222 { 223 if ( classLoader != null ) 224 { 225 classLoader.close(); 226 } 227 } 228 catch ( final IOException e ) 229 { 230 this.logMessage( Level.SEVERE, Messages.getMessage( e ), e ); 231 } 232 } 233 } 234 235 /** 236 * {@inheritDoc} 237 */ 238 @Override 239 public ValidateClassesTask clone() 240 { 241 final ValidateClassesTask clone = (ValidateClassesTask) super.clone(); 242 clone.classesDirectory = 243 this.classesDirectory != null ? new File( this.classesDirectory.getAbsolutePath() ) : null; 244 245 return clone; 246 } 247 248}