1 8 package org.codehaus.loom.components.validator; 9 10 import org.codehaus.loom.components.util.ConfigUtil; 11 import org.codehaus.loom.components.util.ConfigurationBuilder; 12 import org.codehaus.loom.components.util.info.SchemaDescriptor; 13 import org.codehaus.loom.components.util.profile.ComponentProfile; 14 import org.codehaus.loom.interfaces.ConfigurationValidator; 15 import org.codehaus.spice.configkit.ComponentConfigUtil; 16 import org.codehaus.spice.configkit.ConfigValidator; 17 import org.codehaus.spice.configkit.ValidationResult; 18 import org.codehaus.dna.AbstractLogEnabled; 19 import org.codehaus.dna.Configuration; 20 import org.codehaus.dna.ConfigurationException; 21 import org.codehaus.dna.impl.ConfigurationUtil; 22 import org.codehaus.dna.impl.DefaultConfiguration; 23 import org.w3c.dom.Element ; 24 25 34 public class DefaultConfigurationValidator 35 extends AbstractLogEnabled 36 implements ConfigurationValidator 37 { 38 46 public boolean isValid( final ComponentProfile component, 47 final ClassLoader classLoader ) 48 throws ConfigurationException 49 { 50 final SchemaDescriptor schema = component.getInfo() 51 .getConfigurationSchema(); 52 if( null == schema ) 53 { 54 return true; 55 } 56 57 final String classname = 58 component.getInfo().getType().getName(); 59 if( getLogger().isDebugEnabled() ) 60 { 61 final String message = 62 "Validating component " + 63 component.getTemplate().getName() + 64 " of type " + 65 classname + 66 " with schema " + 67 schema.getLocation() + 68 " of type " + 69 schema.getType(); 70 getLogger().debug( message ); 71 } 72 73 try 75 { 76 final ConfigValidator validator = ComponentConfigUtil 77 .getComponentConfigValidator( classname, 78 classLoader, 79 schema.getLocation(), 80 schema.getType() ); 81 if( null == validator ) 82 { 83 final String message = 84 "Missing schema for component " + 85 component.getTemplate().getName() + 86 " of type " + 87 classname + 88 " with schema " + 89 schema.getLocation() + 90 " of type " + 91 schema.getType(); 92 getLogger().warn( message ); 93 return false; 94 } 95 final Configuration configuration = component.getTemplate() 96 .getConfiguration(); 97 final DefaultConfiguration newConfiguration = 98 new DefaultConfiguration( "root", 99 configuration.getPath(), 100 configuration.getLocation() ); 101 ConfigUtil.copy( newConfiguration, configuration ); 102 final Element element = ConfigurationUtil.toElement( 103 newConfiguration ); 104 final ValidationResult result = validator.validate( element ); 105 ConfigurationBuilder.processValidationResults( result, 106 getLogger() ); 107 return true; 108 } 109 catch( Exception e ) 110 { 111 final String msg = component.getTemplate().getName() + 112 " failed validation due to: " 113 + e.getMessage(); 114 getLogger().warn( msg, e ); 115 return false; 116 } 117 } 118 } 119 | Popular Tags |