KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > validator > DefaultConfigurationValidator


1 /*
2  * Copyright (C) The Loom Group. All rights reserved.
3  *
4  * This software is published under the terms of the Loom
5  * Software License version 1.1, a copy of which has been included
6  * with this distribution in the LICENSE.txt file.
7  */

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 JavaDoc;
24
25 /**
26  * This component validates the components configuration using the ConfigKit
27  * toolkit.
28  *
29  * @author Peter Donald
30  * @author Peter Royal
31  * @version $Revision: 1.4 $ $Date: 2004/07/02 23:56:13 $
32  * @dna.component
33  */

34 public class DefaultConfigurationValidator
35     extends AbstractLogEnabled
36     implements ConfigurationValidator
37 {
38     /**
39      * Check to see if configuration is valid for specified component.
40      *
41      * @param component the ComponentProfile
42      * @param classLoader the ClassLoader (to load schema from if necessary)
43      * @return true if configuration is valid
44      * @throws ConfigurationException if expected schema is missing
45      */

46     public boolean isValid( final ComponentProfile component,
47                             final ClassLoader JavaDoc 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 JavaDoc classname =
58             component.getInfo().getType().getName();
59         if( getLogger().isDebugEnabled() )
60         {
61             final String JavaDoc 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         //Get the uri of configuration schema type
74
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 JavaDoc 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 JavaDoc 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 JavaDoc e )
110         {
111             final String JavaDoc 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