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: ValidateClasspathTask.java 4174 2012-01-15 09:30:01Z schulte2005 $ 029 * 030 */ 031 package org.jomc.ant; 032 033 import java.io.IOException; 034 import java.util.logging.Level; 035 import javax.xml.bind.JAXBContext; 036 import javax.xml.bind.JAXBException; 037 import javax.xml.bind.util.JAXBSource; 038 import javax.xml.transform.Source; 039 import org.apache.tools.ant.BuildException; 040 import org.jomc.model.Implementation; 041 import org.jomc.model.Module; 042 import org.jomc.model.Specification; 043 import org.jomc.modlet.Model; 044 import org.jomc.modlet.ModelContext; 045 import org.jomc.modlet.ModelException; 046 import org.jomc.modlet.ModelValidationReport; 047 import org.jomc.modlet.ObjectFactory; 048 import org.jomc.tools.ClassFileProcessor; 049 050 /** 051 * Task for validating class path model objects. 052 * 053 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 054 * @version $JOMC: ValidateClasspathTask.java 4174 2012-01-15 09:30:01Z schulte2005 $ 055 */ 056 public final class ValidateClasspathTask extends ClassFileProcessorTask 057 { 058 059 /** Creates a new {@code ValidateClasspathTask} instance. */ 060 public ValidateClasspathTask() 061 { 062 super(); 063 } 064 065 /** 066 * Validates class file model objects. 067 * 068 * @throws BuildException if validating class file model objects fails. 069 */ 070 @Override 071 public void processClassFiles() throws BuildException 072 { 073 ProjectClassLoader classLoader = null; 074 boolean suppressExceptionOnClose = true; 075 076 try 077 { 078 this.log( Messages.getMessage( "validatingClasspath", this.getModel() ) ); 079 080 classLoader = this.newProjectClassLoader(); 081 final ModelContext context = this.newModelContext( classLoader ); 082 final ClassFileProcessor tool = this.newClassFileProcessor(); 083 final JAXBContext jaxbContext = context.createContext( this.getModel() ); 084 final Model model = this.getModel( context ); 085 final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) ); 086 ModelValidationReport validationReport = context.validateModel( this.getModel(), source ); 087 088 this.logValidationReport( context, validationReport ); 089 tool.setModel( model ); 090 091 if ( validationReport.isModelValid() ) 092 { 093 final Specification s = this.getSpecification( model ); 094 final Implementation i = this.getImplementation( model ); 095 final Module m = this.getModule( model ); 096 097 if ( s != null ) 098 { 099 validationReport = tool.validateModelObjects( s, context ); 100 this.logValidationReport( context, validationReport ); 101 102 if ( !validationReport.isModelValid() ) 103 { 104 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) ); 105 } 106 } 107 108 if ( i != null ) 109 { 110 validationReport = tool.validateModelObjects( i, context ); 111 this.logValidationReport( context, validationReport ); 112 113 if ( !validationReport.isModelValid() ) 114 { 115 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) ); 116 } 117 } 118 119 if ( m != null ) 120 { 121 validationReport = tool.validateModelObjects( m, context ); 122 this.logValidationReport( context, validationReport ); 123 124 if ( !validationReport.isModelValid() ) 125 { 126 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) ); 127 } 128 } 129 130 if ( this.isModulesProcessingRequested() ) 131 { 132 validationReport = tool.validateModelObjects( context ); 133 this.logValidationReport( context, validationReport ); 134 135 if ( !validationReport.isModelValid() ) 136 { 137 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) ); 138 } 139 } 140 141 suppressExceptionOnClose = false; 142 } 143 else 144 { 145 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) ); 146 } 147 } 148 catch ( final IOException e ) 149 { 150 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() ); 151 } 152 catch ( final JAXBException e ) 153 { 154 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() ); 155 } 156 catch ( final ModelException e ) 157 { 158 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() ); 159 } 160 finally 161 { 162 try 163 { 164 if ( classLoader != null ) 165 { 166 classLoader.close(); 167 } 168 } 169 catch ( final IOException e ) 170 { 171 if ( suppressExceptionOnClose ) 172 { 173 this.logMessage( Level.SEVERE, Messages.getMessage( e ), e ); 174 } 175 else 176 { 177 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() ); 178 } 179 } 180 } 181 } 182 183 /** {@inheritDoc} */ 184 @Override 185 public ValidateClasspathTask clone() 186 { 187 return (ValidateClasspathTask) super.clone(); 188 } 189 190 }