donaldp 2002/10/30 21:51:39
Added: metaclass/src/java/org/apache/excalibur/metaclass/runtime
MetaClassIO.java MetaClassIntrospector.java
Log:
Add into CVS to keep a record of their existance
Revision Changes Path
1.1
jakarta-avalon-excalibur/metaclass/src/java/org/apache/excalibur/metaclass/runtime/MetaClassIO.java
Index: MetaClassIO.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.excalibur.metaclass.runtime;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Properties;
import org.apache.excalibur.metaclass.model.Attribute;
import org.apache.excalibur.metaclass.model.FieldDescriptor;
import org.apache.excalibur.metaclass.model.MetaClass;
import org.apache.excalibur.metaclass.model.MethodDescriptor;
import org.apache.excalibur.metaclass.model.ParameterDescriptor;
/**
* This is a utility class that writes out a MetaClass object
* to a stream using binary format outlined in documentation.
*
* @author <a href="mailto:peter at apache.org">Peter Donald</a>
* @version $Revision: 1.1 $ $Date: 2002/10/31 05:51:39 $
*/
public class MetaClassIO
{
/**
* The current version of MetaClass object.
*/
public static final int VERSION = 100;
public MetaClass deserialize( final InputStream input )
throws IOException
{
final DataInputStream data = new DataInputStream( input );
final int version = data.readInt();
if( VERSION != version )
{
final String message =
"Version mismatch." +
" Expected: " + VERSION +
" Actual: " + version;
throw new IOException( message );
}
final String classname = data.readUTF();
final int classModifiers = data.readInt();
final Attribute[] classAttributes = readAttributes( data );
final int fieldCount = data.readInt();
final ArrayList fieldSet = new ArrayList();
for( int i = 0; i < fieldCount; i++ )
{
final String name = data.readUTF();
final String type = data.readUTF();
final int modifiers = data.readInt();
final Attribute[] attributes = readAttributes( data );
final FieldDescriptor field =
new FieldDescriptor( name, type, modifiers, attributes );
fieldSet.add( field );
}
final int methodCount = data.readInt();
final ArrayList methodSet = new ArrayList();
for( int i = 0; i < methodCount; i++ )
{
final String name = data.readUTF();
final String type = data.readUTF();
final ParameterDescriptor[] parameters = readParameters( data );
final int modifiers = data.readInt();
final Attribute[] attributes = readAttributes( data );
final MethodDescriptor method =
new MethodDescriptor( name, type,
modifiers, parameters,
attributes );
methodSet.add( method );
}
final FieldDescriptor[] fields =
(FieldDescriptor[])fieldSet.toArray( new FieldDescriptor[
fieldSet.size() ] );
final MethodDescriptor[] methods =
(MethodDescriptor[])methodSet.toArray( new MethodDescriptor[
methodSet.size() ] );
return new MetaClass( classname, classModifiers,
classAttributes, fields,
methods );
}
public void serialize( final MetaClass info,
final OutputStream output )
throws IOException
{
final DataOutputStream data = new DataOutputStream( output );
try
{
data.writeInt( VERSION );
data.writeUTF( info.getClassname() );
data.writeInt( info.getModifiers() );
writeAttributes( data, info.getAttributes() );
final FieldDescriptor[] fields = info.getFields();
data.writeInt( fields.length );
for( int i = 0; i < fields.length; i++ )
{
final FieldDescriptor field = fields[ i ];
data.writeUTF( field.getName() );
data.writeUTF( field.getType() );
data.writeInt( field.getModifiers() );
writeAttributes( data, field.getAttributes() );
}
final MethodDescriptor[] methods = info.getMethods();
data.writeInt( methods.length );
for( int i = 0; i < methods.length; i++ )
{
final MethodDescriptor method = methods[ i ];
data.writeUTF( method.getName() );
data.writeUTF( method.getReturnType() );
writeParameters( data, method.getParameters() );
data.writeInt( method.getModifiers() );
writeAttributes( data, method.getAttributes() );
}
}
finally
{
data.flush();
}
}
/**
* Read in a set of method parameters.
*
* @param data the input
* @return the method parameters
* @throws IOException if unable to read attributes
*/
private ParameterDescriptor[] readParameters( final DataInputStream data )
throws IOException
{
final ArrayList parameters = new ArrayList();
final int count = data.readInt();
for( int i = 0; i < count; i++ )
{
final String name = data.readUTF();
final String type = data.readUTF();
final ParameterDescriptor parameter =
new ParameterDescriptor( name, type );
parameters.add( parameter );
}
return (ParameterDescriptor[])parameters.toArray( new ParameterDescriptor[
parameters.size() ] );
}
private void writeParameters( final DataOutputStream data,
final ParameterDescriptor[] parameters )
throws IOException
{
data.writeInt( parameters.length );
for( int i = 0; i < parameters.length; i++ )
{
final ParameterDescriptor parameter = parameters[ i ];
data.writeUTF( parameter.getName() );
data.writeUTF( parameter.getType() );
}
}
/**
* Read in a set of attributes.
*
* @param data the input
* @return the attributes
* @throws IOException if unable to read attributes
*/
private Attribute[] readAttributes( final DataInputStream data )
throws IOException
{
final int count = data.readInt();
final ArrayList attributeSet = new ArrayList();
for( int i = 0; i < count; i++ )
{
final String name = data.readUTF();
final Properties properties = readAttributeParameters( data );
final Attribute attribute = new Attribute( name, properties );
attributeSet.add( attribute );
}
return (Attribute[])attributeSet.toArray( new Attribute[ attributeSet.size()
] );
}
/**
* Read in a set of attribute parameters.
*
* @param data the input
* @return the parameters
* @throws IOException if unable to read parameters
*/
private Properties readAttributeParameters( final DataInputStream data )
throws IOException
{
final Properties parameters = new Properties();
final int count = data.readInt();
for( int i = 0; i < count; i++ )
{
final String name = data.readUTF();
final String value = data.readUTF();
parameters.setProperty( name, value );
}
return parameters;
}
/**
* Write out the specified attributes.
*
* @param data the output
* @param attributes the attributes
* @throws IOException if unable to write attributes
*/
private void writeAttributes( final DataOutputStream data,
final Attribute[] attributes )
throws IOException
{
data.writeInt( attributes.length );
for( int i = 0; i < attributes.length; i++ )
{
final Attribute attribute = attributes[ i ];
data.writeUTF( attribute.getName() );
writeAttributeParameters( data, attribute );
}
}
/**
* Write out the parameters of an attribute.
*
* @param data the output
* @param attribute the attribute
* @throws IOException if unable to write parameters
*/
private void writeAttributeParameters( final DataOutputStream data,
final Attribute attribute )
throws IOException
{
final String[] names = attribute.getParameterNames();
data.writeInt( names.length );
for( int i = 0; i < names.length; i++ )
{
final String name = names[ i ];
final String value = attribute.getParameter( name );
data.writeUTF( name );
data.writeUTF( value );
}
}
}
1.1
jakarta-avalon-excalibur/metaclass/src/java/org/apache/excalibur/metaclass/runtime/MetaClassIntrospector.java
Index: MetaClassIntrospector.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.excalibur.metaclass.runtime;
import java.io.InputStream;
import java.util.WeakHashMap;
import org.apache.excalibur.metaclass.model.MetaClass;
/**
* This class is responsible for loading and
* caching the {@link MetaClass} objects for coresponding java classes.
*
* @author <a href="mailto:peter at apache.org">Peter Donald</a>
* @version $Revision: 1.1 $ $Date: 2002/10/31 05:51:39 $
*/
public final class MetaClassIntrospector
{
/**
* Class used to read the MetaData.
*/
private static final MetaClassIO c_metaClassIO = new MetaClassIO();
/**
* The cache in which info objects are stored.
*/
private static final WeakHashMap c_cache = new WeakHashMap();
/**
* Private constructor so that class can not be instantiated.
*/
private MetaClassIntrospector()
{
}
/**
* Return a {@link MetaClass} for specified class.
*
* @param clazz the class to {@link MetaClass} for
* @return the newly created {@link MetaClass}
* @throws Exception if unable to create {@link MetaClass}
*/
public static MetaClass getClassInfo( final Class clazz )
throws Exception
{
MetaClass info = (MetaClass)c_cache.get( clazz );
if( null != info )
{
return info;
}
else
{
info = createClassInfo( clazz.getName(),
clazz.getClassLoader() );
c_cache.put( clazz, info );
return info;
}
}
/**
* Create a {@link MetaClass} for specified class.
*
* @param classname the classname of the class to {@link MetaClass} for
* @param classLoader the classLoader to load {@link MetaClass} from
* @return the newly created {@link MetaClass}
* @throws Exception if unable to create {@link MetaClass}
*/
private static MetaClass createClassInfo( final String classname,
final ClassLoader classLoader )
throws Exception
{
final String info = deriveResourceName( classname, ".mad" );
final InputStream inputStream = classLoader.getResourceAsStream( info );
if( null == inputStream )
{
final String message =
"Unabel to locate metadata for " + classname;
throw new Exception( message );
}
return c_metaClassIO.deserialize( inputStream );
}
/**
* Derive the resourcename for specified class using specified postfix.
*
* @param classname the name of class
* @param postfix the postfix to add to end of resource
* @return the name of resource
*/
private static String deriveResourceName( final String classname,
final String postfix )
{
return classname.replace( '.', '/' ) + postfix;
}
}
--
To unsubscribe, e-mail: <mailto:avalon-cvs-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@;jakarta.apache.org>