Skip to Content.
Sympa Menu

notify-dpml - svn commit: r1345 - in development/main/metro/composition/impl/src/main/net/dpml/composition: model/impl util

notify-dpml AT lists.ibiblio.org

Subject: DPML Notify

List archive

Chronological Thread  
  • From: mcconnell AT netcompartner.com
  • To: notify-dpml AT lists.ibiblio.org
  • Subject: svn commit: r1345 - in development/main/metro/composition/impl/src/main/net/dpml/composition: model/impl util
  • Date: Mon, 03 Jan 2005 01:16:15 +0100

Author: mcconnell
Date: Mon Jan 3 01:16:15 2005
New Revision: 1345

Modified:

development/main/metro/composition/impl/src/main/net/dpml/composition/model/impl/DefaultComponentModel.java

development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationValidator.java

development/main/metro/composition/impl/src/main/net/dpml/composition/util/Resource.java

development/main/metro/composition/impl/src/main/net/dpml/composition/util/XercesSchemaValidator.java
Log:
Format updates and improvement to temp file handling and deletion on error.

Modified:
development/main/metro/composition/impl/src/main/net/dpml/composition/model/impl/DefaultComponentModel.java
==============================================================================
---
development/main/metro/composition/impl/src/main/net/dpml/composition/model/impl/DefaultComponentModel.java
(original)
+++
development/main/metro/composition/impl/src/main/net/dpml/composition/model/impl/DefaultComponentModel.java
Mon Jan 3 01:16:15 2005
@@ -1,5 +1,5 @@
/*
- * Copyright 2004 Stephen J. McConnell.
+ * Copyright 2004-2005 Stephen J. McConnell.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -316,8 +316,10 @@
StageModel[] stages = getStageModels();
for( int i=0; i<stages.length; i++ )
{
- if( null == stages[i].getProvider() )
+ if( null == stages[i].getProvider() )
+ {
return false;
+ }
}
return true;
}
@@ -721,7 +723,8 @@
* @param config the supplied configuration
* @exception IllegalStateException if the component type backing the
* model does not implement the configurable interface
- * @exception ModelException if the supplied configuration is null or not
successfully validated (if requested)
+ * @exception ModelException if the supplied configuration is null or
+ * not successfully validated (if requested)
*/
public void setConfiguration( Configuration config )
throws IllegalStateException, ModelException
@@ -759,6 +762,7 @@
{
throw new NullPointerException( "config" );
}
+
Configuration t_config = config;
if( policy )
{
@@ -766,22 +770,25 @@
}
String schema = this.getType().getInfo().getConfigurationSchema();
Boolean shouldWeValidate = Boolean.valueOf(schema);
- if ( shouldWeValidate.booleanValue() )
+ if( shouldWeValidate.booleanValue() )
{
//TODO: this is the only validator we have, this should be some
factory later on
try
{
- XercesSchemaValidator validator = new XercesSchemaValidator();
- //TODO: we have just one policy to locate schemas yet :
Colocation to the component class
- String classname = this.getType().getInfo().getClassname();
- String iSchemaLocation = classname.replace( '.', '/' )
- + ".xschema";
- InputStream schemaStream =
this.getClass().getClassLoader().getResourceAsStream( iSchemaLocation );
- validator.validate(t_config, schemaStream );
+ XercesSchemaValidator validator = new
XercesSchemaValidator();
+ //TODO: we have just one policy to locate schemas yet :
Colocation to the component class
+ String classname = this.getType().getInfo().getClassname();
+ String iSchemaLocation =
+ classname.replace( '.', '/' ) + ".xschema";
+ InputStream schemaStream =
+ this.getClass().getClassLoader().getResourceAsStream(
iSchemaLocation );
+ validator.validate( t_config, schemaStream );
}
- catch (Exception e)
+ catch( Exception e )
{
- throw new ModelException ( "could not set configuration", e
);
+ final String error =
+ "Configuration validation error.";
+ throw new ModelException( error, e );
}
}
m_config = t_config;

Modified:
development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationValidator.java
==============================================================================
---
development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationValidator.java
(original)
+++
development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationValidator.java
Mon Jan 3 01:16:15 2005
@@ -1,5 +1,6 @@
/*
* Copyright 2004 Peter Neubauer.
+ * Copyright 2005 Stephen McConnell.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,16 +23,17 @@

import net.dpml.composition.model.ModelException;
import net.dpml.configuration.Configuration;
+
/**
* interface for plugging in of different validation strategies
*/
public interface ConfigurationValidator
{
/**
- * @param iConfig the comfiguration to validate
- * @param iSchema the schema to validate against
+ * @param config the comfiguration to validate
+ * @param schema the schema to validate against
* @throws ModelException the validation failed
*/
- void validate( Configuration iConfig, InputStream iSchema )
+ void validate( Configuration config, InputStream schema )
throws ModelException;
}

Modified:
development/main/metro/composition/impl/src/main/net/dpml/composition/util/Resource.java
==============================================================================
---
development/main/metro/composition/impl/src/main/net/dpml/composition/util/Resource.java
(original)
+++
development/main/metro/composition/impl/src/main/net/dpml/composition/util/Resource.java
Mon Jan 3 01:16:15 2005
@@ -1,5 +1,6 @@
/*
- * Copyright 2000-2004 The Apache Software Foundation
+ * Copyright 2000-2004 The Apache Software Foundation
+ * Copyright 2005 Stephen McConnell.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +18,6 @@

package net.dpml.composition.util;

-
/**
* describes a File or a ZipEntry
*
@@ -29,7 +29,8 @@
* @author <a href="mailto:dev-dpml AT lists.ibiblio.org";>The Digital Product
Meta Library</a>
* @version $Id$
*/
-public class Resource {
+public class Resource
+{
private String name = null;
private boolean exists = true;
private long lastmodified = 0;
@@ -38,7 +39,8 @@
/**
* default constructor
*/
- public Resource() {
+ public Resource()
+ {
}

/**
@@ -49,7 +51,8 @@
* @param name relative path of the resource. Expects
* &quot;/&quot; to be used as the directory separator.
*/
- public Resource(String name) {
+ public Resource( String name )
+ {
this(name, false, 0, false);
}

@@ -59,16 +62,18 @@
* @param name relative path of the resource. Expects
* &quot;/&quot; to be used as the directory separator.
*/
- public Resource(String name, boolean exists, long lastmodified) {
- this(name, exists, lastmodified, false);
+ public Resource( String name, boolean exists, long lastmodified )
+ {
+ this( name, exists, lastmodified, false );
}

/**
* @param name relative path of the resource. Expects
* &quot;/&quot; to be used as the directory separator.
*/
- public Resource(String name, boolean exists, long lastmodified,
- boolean directory) {
+ public Resource(
+ String name, boolean exists, long lastmodified, boolean directory )
+ {
this.name = name;
this.exists = exists;
this.lastmodified = lastmodified;
@@ -86,7 +91,8 @@
*
* <p>&quot;/&quot; will be used as the directory separator.</p>
*/
- public String getName() {
+ public String getName()
+ {
return name;
}

@@ -94,17 +100,21 @@
* @param name relative path of the resource. Expects
* &quot;/&quot; to be used as the directory separator.
*/
- public void setName(String name) {
+ public void setName(String name)
+ {
this.name = name;
}
+
/**
* the exists attribute tells whether a file exists
*/
- public boolean isExists() {
+ public boolean isExists()
+ {
return exists;
}

- public void setExists(boolean exists) {
+ public void setExists( boolean exists )
+ {
this.exists = exists;
}

@@ -114,32 +124,41 @@
* @return 0 if the resource does not exist to mirror the behavior
* of {@link java.io.File File}.
*/
- public long getLastModified() {
+ public long getLastModified()
+ {
return !exists || lastmodified < 0 ? 0 : lastmodified;
}

- public void setLastModified(long lastmodified) {
+ public void setLastModified(long lastmodified)
+ {
this.lastmodified = lastmodified;
}
+
/**
* tells if the resource is a directory
* @return boolean flag indicating if the resource is a directory
*/
- public boolean isDirectory() {
+ public boolean isDirectory()
+ {
return directory;
}

- public void setDirectory(boolean directory) {
+ public void setDirectory( boolean directory )
+ {
this.directory = directory;
}

/**
* @return copy of this
*/
- public Object clone() {
- try {
+ public Object clone()
+ {
+ try
+ {
return super.clone();
- } catch (CloneNotSupportedException e) {
+ }
+ catch (CloneNotSupportedException e)
+ {
throw new Error("CloneNotSupportedException for a "
+ "Clonable Resource caught?");
}
@@ -148,13 +167,14 @@
/**
* delegates to a comparison of names.
*/
- public int compareTo(Object other) {
- if (!(other instanceof Resource)) {
- throw new IllegalArgumentException("Can only be compared with "
+ public int compareTo(Object other)
+ {
+ if( !( other instanceof Resource ) )
+ {
+ throw new IllegalArgumentException( "Can only be compared with "
+ "Resources");
}
Resource r = (Resource) other;
return getName().compareTo(r.getName());
}
-
}

Modified:
development/main/metro/composition/impl/src/main/net/dpml/composition/util/XercesSchemaValidator.java
==============================================================================
---
development/main/metro/composition/impl/src/main/net/dpml/composition/util/XercesSchemaValidator.java
(original)
+++
development/main/metro/composition/impl/src/main/net/dpml/composition/util/XercesSchemaValidator.java
Mon Jan 3 01:16:15 2005
@@ -1,5 +1,6 @@
/*
- * Copyright 2004 Peter Neubauer.
+ * Copyright 2004-2005 Peter Neubauer.
+ * Copyright 2005 Stephen McConnell.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
not
* use this file except in compliance with the License. You may obtain a
copy of
@@ -32,6 +33,7 @@
import net.dpml.configuration.ConfigurationException;

import org.apache.xerces.parsers.SAXParser;
+
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
@@ -44,92 +46,112 @@
*/
public class XercesSchemaValidator implements ConfigurationValidator
{
+ private static final String NO_NAMESPACE_SCHEMA_LOCATION =
+
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";;
+
+ public static final String JAXP_SCHEMA_LANGUAGE =
+ "http://java.sun.com/xml/jaxp/properties/schemaLanguage";;
+
+ public static final String W3C_XML_SCHEMA =
+ "http://www.w3.org/2001/XMLSchema";;
+
+ private static final String VALIDATION_FEATURE_URI =
+ "http://xml.org/sax/features/validation";;
+
+ private static final String SCHEMA_FEATURE_URI =
+ "http://apache.org/xml/features/validation/schema";;
+
+ private static final String CHECKING_FEATURE_URI =
+ "http://apache.org/xml/features/validation/schema-full-checking";;
+
private ConfigurationSerializer m_serializer;

private SAXParser m_parser;

private XMLReader m_xmlReader;

- public static final String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";;
-
- public static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";;
-
- public XercesSchemaValidator() throws ParserConfigurationException,
- SAXException
- {
- m_serializer = new ConfigurationSerializer();
- m_parser = setupParser();
- }
+ public XercesSchemaValidator()
+ throws ParserConfigurationException, SAXException
+ {
+ m_serializer = new ConfigurationSerializer();
+ m_parser = setupParser();
+ }

- private void validateConfiguration( String iSchemaPath,
- Configuration iConfig ) throws ModelException
+ private void validateConfiguration( String path,
+ Configuration config ) throws ModelException
{

Validator handler = new Validator();
try
{
- String xml = m_serializer.serialize( iConfig );
+ String xml = m_serializer.serialize( config );
m_parser.setErrorHandler( handler );
- m_parser
- .setProperty(
-
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";,
- iSchemaPath );
- m_parser.parse( new InputSource( new ByteArrayInputStream( xml
- .getBytes() ) ) );
+ m_parser.setProperty(
+ NO_NAMESPACE_SCHEMA_LOCATION, path );
+ m_parser.parse(
+ new InputSource(
+ new ByteArrayInputStream( xml.getBytes() ) ) );
}
- catch ( SAXException e )
+ catch( SAXException e )
{
- throw new ModelException( "Could not parse:"
- + handler.getSaxParseException().getMessage(), handler
- .getSaxParseException() );
+ final String error =
+ "Could not parse the configuration due to: "
+ + handler.getSaxParseException().getMessage();
+ final Throwable cause = handler.getSaxParseException();
+ throw new ModelException( error, cause );
}
- catch ( IOException e )
+ catch( IOException e )
{
- throw new ModelException( "IO Error:"
- + handler.getSaxParseException().getMessage(), handler
- .getSaxParseException() );
+ final String error =
+ "IO error prevented sucessful configuration parsing.";
+ final Throwable cause = handler.getSaxParseException();
+ throw new ModelException( error, cause );
}
- catch ( ConfigurationException e )
+ catch( ConfigurationException e )
{
- throw new ModelException( "could not serialize Configuration", e
);
+ final String error =
+ "Configuration serialization error.";
+ throw new ModelException( error, e );
}
- if ( handler.hasValidationError() )
+
+ if( handler.hasValidationError() )
{
- throw new ModelException( "Configuration has Errors:"
- + handler.getSaxParseException().getMessage(), handler
- .getSaxParseException() );
+ final String error =
+ "Configuration validation failure due to: "
+ + handler.getSaxParseException().getMessage();
+ final Throwable cause = handler.getSaxParseException();
+ throw new ModelException( error, cause );
}
}

private SAXParser setupParser() throws ParserConfigurationException,
SAXException
{
-
SAXParser saxParser = new SAXParser();
try
{
saxParser.setProperty( JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA );
- saxParser.setFeature( "http://xml.org/sax/features/validation";,
- true );
- saxParser.setFeature(
- "http://apache.org/xml/features/validation/schema";, true
);
- saxParser
- .setFeature(
-
"http://apache.org/xml/features/validation/schema-full-checking";,
- true );
- }
- catch ( SAXNotRecognizedException x )
- {
- throw new ParserConfigurationException( "the parser " + saxParser
- + " does not support the property " +
JAXP_SCHEMA_LANGUAGE
- + ": " + x );
+ saxParser.setFeature( VALIDATION_FEATURE_URI, true );
+ saxParser.setFeature( SCHEMA_FEATURE_URI, true );
+ saxParser.setFeature( CHECKING_FEATURE_URI, true );
+ }
+ catch ( SAXNotRecognizedException e )
+ {
+ final String error =
+ "The parser ["
+ + saxParser
+ + "] does not support the property ["
+ + JAXP_SCHEMA_LANGUAGE
+ + ":"
+ + e;
+ throw new ParserConfigurationException( error );
}
return saxParser;
}

private class Validator extends DefaultHandler
{
- private boolean m_validationError = false;
+ private boolean m_validationError = false;

private SAXParseException m_saxParseException = null;

@@ -162,7 +184,7 @@
}

/**
- * Validates a CConfiguration by
+ * Validates a Configuration by
* 1. serializing the configuration object into a XML configuration
* 2. try to locate the .xschema file, in the same manner as .xinfo
files (same location as Component)
* 3. write the contents of that resource into a temp File
@@ -172,46 +194,53 @@
* @see
net.dpml.composition.util.ConfigurationValidator#validate(net.dpml.configuration.Configuration,
* net.dpml.meta.info.Type)
*/
- public void validate( Configuration iConfig, InputStream is )
+ public void validate( Configuration config, InputStream input )
throws ModelException
{
- if ( iConfig == null )
+ if( config == null )
{
throw new ModelException( "null configuration!" );
}

-
//did we get something?
- if (null == is)
+ if (null == input)
{
- throw new ModelException( "Schema input stream was null" );
+ throw new ModelException( "Null schema input stream." );
}
- BufferedReader in = new BufferedReader( new InputStreamReader( is )
);
- String inputLine;
- File tempFile;
- StringBuffer buffer = new StringBuffer();
+
+ final File tempFile = createTempFile();
+ InputStreamReader reader = new InputStreamReader( input );
+ BufferedReader in = new BufferedReader( reader );
+ final StringBuffer buffer = new StringBuffer();
try
{
- while ( ( inputLine = in.readLine() ) != null )
+ String inputLine;
+ while( ( inputLine = in.readLine() ) != null )
{
buffer.append( inputLine );
}
in.close();
- tempFile = new File( "temp.xshcema" );
- FileOutputStream os = new FileOutputStream( tempFile );
+ final FileOutputStream os = new FileOutputStream( tempFile );
os.write( buffer.toString().getBytes() );
os.flush();
os.close();
}
- catch ( Exception e )
+ catch ( Throwable e )
{
- throw new ModelException( "got an exception", e );
+ if( null != tempFile )
+ {
+ tempFile.delete();
+ }
+ final String error =
+ "Unexpected error while attempting to read configuration.";
+ throw new ModelException( error, e );
}
+
try
{
- validateConfiguration( tempFile.getAbsolutePath(), iConfig );
+ validateConfiguration( tempFile.getAbsolutePath(), config );
}
- catch (ModelException e)
+ catch( ModelException e )
{
throw e;
}
@@ -220,4 +249,23 @@
tempFile.delete();
}
}
+
+ /**
+ * Return a temporary file used during schema validation.
+ * @return the temporary file
+ * @exception if an error occurs while attempting create the temporary
file
+ */
+ private File createTempFile() throws ModelException
+ {
+ try
+ {
+ return File.createTempFile( "METRO_SCHEMA_VALIDATION", "xschema"
);
+ }
+ catch( Throwable e )
+ {
+ final String error =
+ "Unexpected error while attempt to create a temporary schema
validation file.";
+ throw new ModelException( error, e );
+ }
+ }
}
\ No newline at end of file



  • svn commit: r1345 - in development/main/metro/composition/impl/src/main/net/dpml/composition: model/impl util, mcconnell, 01/02/2005

Archive powered by MHonArc 2.6.24.

Top of Page