You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2003/04/06 13:41:56 UTC

cvs commit: avalon-phoenix/src/java/org/apache/avalon/phoenix/components/validator DefaultConfigurationValidator.java NoopConfigurationValidator.java

donaldp     2003/04/06 04:41:56

  Modified:    src/conf emx4j-kernel.xml kernel.xml
  Added:       src/java/org/apache/avalon/phoenix/components/validator
                        DefaultConfigurationValidator.java
                        NoopConfigurationValidator.java
  Removed:     src/java/org/apache/avalon/phoenix/components/configuration/validator
                        DefaultConfigurationValidator.java
                        NoopConfigurationValidator.java
  Log:
  Migrate validator to top level component.
  
  Revision  Changes    Path
  1.4       +6 -6      avalon-phoenix/src/conf/emx4j-kernel.xml
  
  Index: emx4j-kernel.xml
  ===================================================================
  RCS file: /home/cvs/avalon-phoenix/src/conf/emx4j-kernel.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- emx4j-kernel.xml	2 Oct 2002 06:51:04 -0000	1.3
  +++ emx4j-kernel.xml	6 Apr 2003 11:41:55 -0000	1.4
  @@ -26,11 +26,11 @@
               See http://mx4j.sourceforge.net/ -
   
                 org.apache.avalon.phoenix.components.manager.MX4JSystemManager
  -              
  +
               There is an impl of system manager that does nothing and it is....
  -            
  +
                 org.apache.avalon.phoenix.components.manager.NoopSystemManager
  -            
  +
               Alternatively, you could comment the whole SystemManager section out.
           -->
   
  @@ -103,15 +103,15 @@
               -->
   
           <component role="org.apache.avalon.phoenix.interfaces.ConfigurationValidator"
  -            class="org.apache.avalon.phoenix.components.configuration.validator.NoopConfigurationValidator"
  +            class="org.apache.avalon.phoenix.components.validator.NoopConfigurationValidator"
               logger="validator"/>
   
   <!--
           <component role="org.apache.avalon.phoenix.interfaces.ConfigurationValidator"
  -            class="org.apache.avalon.phoenix.components.configuration.validator.DelegatingConfigurationValidator"
  +            class="org.apache.avalon.phoenix.components.validator.DelegatingConfigurationValidator"
               logger="validator">
               <delegate schema-type="relax-ng"
  -                class="org.apache.avalon.phoenix.components.configuration.validator.JarvConfigurationValidator">
  +                class="org.apache.avalon.phoenix.components.validator.JarvConfigurationValidator">
                   <schema-language>http://relaxng.org/ns/structure/1.0</schema-language>
               </delegate>
           </component>
  
  
  
  1.34      +1 -1      avalon-phoenix/src/conf/kernel.xml
  
  Index: kernel.xml
  ===================================================================
  RCS file: /home/cvs/avalon-phoenix/src/conf/kernel.xml,v
  retrieving revision 1.33
  retrieving revision 1.34
  diff -u -r1.33 -r1.34
  --- kernel.xml	6 Apr 2003 11:23:21 -0000	1.33
  +++ kernel.xml	6 Apr 2003 11:41:55 -0000	1.34
  @@ -118,7 +118,7 @@
               -->
   
           <component role="org.apache.avalon.phoenix.interfaces.ConfigurationValidator"
  -            class="org.apache.avalon.phoenix.components.configuration.validator.DefaultConfigurationValidator"
  +            class="org.apache.avalon.phoenix.components.validator.DefaultConfigurationValidator"
               logger="validator">
               <schema-type name="relax-ng" uri="http://relaxng.org/ns/structure/1.0"/>
           </component>
  
  
  
  1.1                  avalon-phoenix/src/java/org/apache/avalon/phoenix/components/validator/DefaultConfigurationValidator.java
  
  Index: DefaultConfigurationValidator.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.apache.avalon.phoenix.components.validator;
  
  import org.apache.avalon.phoenix.interfaces.ConfigurationValidator;
  import org.apache.avalon.phoenix.containerkit.profile.ComponentProfile;
  import org.apache.avalon.phoenix.framework.info.SchemaDescriptor;
  import org.apache.avalon.phoenix.tools.configuration.ConfigurationBuilder;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.ConfigurationUtil;
  import org.apache.avalon.framework.configuration.DefaultConfiguration;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.xml.sax.InputSource;
  import org.realityforge.configkit.ConfigValidator;
  import org.realityforge.configkit.ConfigValidatorFactory;
  import org.realityforge.configkit.ValidationResult;
  import org.w3c.dom.Element;
  import java.util.Map;
  import java.util.HashMap;
  import java.io.InputStream;
  
  /**
   * This component validates the components configuration using
   * the ConfigKit toolkit. For backwards compatability it also
   * allows users to specify a map between short types and formal
   * schema uris. The previous implementation of ConfigurationValidator
   * allowed arbitary strings designate a uri and by default was configured
   * with "relax-ng" mapped to "http://relaxng.org/ns/structure/1.0". However
   * this made components that had schemas dependent on kernel configuration.
   * To make components configuration independent components should not specify
   * the full uri for schema language but backwards compatability can be
   * supported via configuration such as;
   *
   * <pre>
   *  &lt;schema-type name="relax-ng" uri="http://relaxng.org/ns/structure/1.0"/&gt;
   * </pre>
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @author <a href="mailto:proyal at apache.org">Peter Royal</a>
   * @version $Revision: 1.1 $ $Date: 2003/04/06 11:41:55 $
   * @phoenix.component
   */
  public class DefaultConfigurationValidator
      extends AbstractLogEnabled
      implements ConfigurationValidator, Configurable
  {
      /**
       * A map between short-names and long URIs.
       */
      private final Map m_uriMap = new HashMap();
  
      /**
       * Setup mapping from short-types to long types.
       *
       * @param configuration the configuration
       * @throws ConfigurationException if configuration is invalid
       */
      public void configure( Configuration configuration )
          throws ConfigurationException
      {
          final Configuration[] children = configuration.getChildren();
          for( int i = 0; i < children.length; i++ )
          {
              final Configuration child = children[ i ];
              final String key = child.getAttribute( "name" );
              final String uri = child.getAttribute( "uri" );
              m_uriMap.put( key, uri );
          }
      }
  
      public boolean isValid( final ComponentProfile component,
                              final ClassLoader classLoader )
          throws ConfigurationException
      {
          final SchemaDescriptor schema = component.getInfo().getConfigurationSchema();
          if( null == schema )
          {
              return true;
          }
  
          if( getLogger().isDebugEnabled() )
          {
              final String message =
                  "Validating component " + component.getMetaData().getName() +
                  " of type " + component.getInfo().getDescriptor().getImplementationKey() +
                  " with schema " + schema.getLocation() + " of type " + schema.getType();
              getLogger().debug( message );
          }
  
          //Get the uri of configuration schema type
          final String type = getType( schema );
          if( !type.equals( schema.getType() ) )
          {
              final String message =
                  "Schema type specified as " + schema.getType() +
                  " was translated to URI " + type + " for component named " +
                  component.getMetaData().getName() + " of type " +
                  component.getInfo().getDescriptor().getImplementationKey() +
                  ". It is recomended that the components Info specify the " +
                  "URI rather than the type for compatability reasons.";
              System.err.println( message );
              getLogger().warn( message );
          }
  
          //Get the InputSource for schema
          final InputSource inputSource =
              getSchemaInputSource( component, classLoader );
  
          //Actually perform the validation
          try
          {
              final ConfigValidator validator = ConfigValidatorFactory.create( type, inputSource );
              final Configuration configuration = component.getMetaData().getConfiguration();
              final DefaultConfiguration newConfiguration = new DefaultConfiguration( "root", configuration.getLocation() );
              newConfiguration.addAll( configuration );
              final Element element = ConfigurationUtil.toElement( newConfiguration );
              final ValidationResult result = validator.validate( element );
              ConfigurationBuilder.processValidationResults( result, getLogger() );
              return true;
          }
          catch( Exception e )
          {
              getLogger().warn( "Failed validation due to: " + e.getMessage(), e );
              return false;
          }
      }
  
      /**
       * Get the uri of schema type.
       * It will attempt to use the type specified by the
       * SchemaDescriptor unless the configuration explicitly
       * maps that type to another URI.
       *
       * @param schema the SchemaDescriptor
       * @return the URI of schema type
       */
      private String getType( final SchemaDescriptor schema )
      {
          final String type = schema.getType();
          final String uri = (String)m_uriMap.get( type );
          if( null != uri )
          {
              return uri;
          }
          else
          {
              return type;
          }
      }
  
      /**
       * Get the input source for schema specified by ComponentInfo object.
       *
       * @param component the component profile
       * @param classLoader the classloader to load schema from
       * @return the InputSource for schema
       * @throws ConfigurationException if unable to locate schema
       */
      private InputSource getSchemaInputSource( final ComponentProfile component, final ClassLoader classLoader ) throws ConfigurationException
      {
          final SchemaDescriptor schema = component.getInfo().getConfigurationSchema();
          final String resource = calcSchemaResource( component );
          final InputStream inputStream = classLoader.getResourceAsStream( resource );
          if( null == inputStream )
          {
              final String message = "Unable to find Schema for component " +
                  component.getMetaData().getName() + " of type " +
                  component.getInfo().getDescriptor().getImplementationKey() +
                  " at location " + resource;
              throw new ConfigurationException( message );
          }
  
          final InputSource inputSource = new InputSource( inputStream );
          inputSource.setSystemId( schema.getLocation() );
          return inputSource;
      }
  
      /**
       * Determine the absolute name of the resource that contains schema.
       * If the location starts with a '/' then the location is absolute
       * otherwise the location is relative to the components class.
       *
       * @param component the component profile
       * @return the absolute name of schema resource
       */
      private String calcSchemaResource( final ComponentProfile component )
      {
          final SchemaDescriptor schema = component.getInfo().getConfigurationSchema();
          final String location = schema.getLocation();
          if( location.startsWith( "/" ) )
          {
              return location;
          }
          else
          {
              final String classname =
                  component.getInfo().getDescriptor().getImplementationKey();
              String resource = classname;
              final int index = classname.lastIndexOf( '.' );
              resource = classname;
              if( -1 != index )
              {
                  resource = classname.substring( 0, index + 1 );
              }
              resource = resource.replace( '.', '/' );
              resource += location;
              return resource;
          }
      }
  }
  
  
  
  1.1                  avalon-phoenix/src/java/org/apache/avalon/phoenix/components/validator/NoopConfigurationValidator.java
  
  Index: NoopConfigurationValidator.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1997-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Avalon", "Phoenix" and "Apache Software Foundation"
      must  not be  used to  endorse or  promote products derived  from this
      software without prior written permission. For written permission, please
      contact apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation. For more  information on the
   Apache Software Foundation, please see <http://www.apache.org/>.
  
  */
  
  package org.apache.avalon.phoenix.components.validator;
  
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.phoenix.containerkit.profile.ComponentProfile;
  import org.apache.avalon.phoenix.interfaces.ConfigurationValidator;
  
  /**
   * A ConfigurationValidator that always says everything is okay
   *
   * @author <a href="mailto:proyal@apache.org">Peter Royal</a>
   */
  public class NoopConfigurationValidator
      implements ConfigurationValidator
  {
      public boolean isValid( ComponentProfile component, ClassLoader classLoader )
          throws ConfigurationException
      {
          return true;
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org