View Javadoc

1   package org.codehaus.xfire.loom.type;
2   
3   import javax.xml.namespace.QName;
4   
5   import org.apache.avalon.framework.configuration.Configurable;
6   import org.apache.avalon.framework.configuration.Configuration;
7   import org.apache.avalon.framework.configuration.ConfigurationException;
8   import org.apache.avalon.framework.logger.LogEnabled;
9   import org.apache.avalon.framework.logger.Logger;
10  
11  import org.codehaus.xfire.aegis.type.DefaultTypeMappingRegistry;
12  import org.codehaus.xfire.aegis.type.Type;
13  import org.codehaus.xfire.aegis.type.TypeMapping;
14  import org.codehaus.xfire.aegis.type.basic.BooleanType;
15  import org.codehaus.xfire.aegis.type.basic.DoubleType;
16  import org.codehaus.xfire.aegis.type.basic.FloatType;
17  import org.codehaus.xfire.aegis.type.basic.IntType;
18  import org.codehaus.xfire.aegis.type.basic.LongType;
19  import org.codehaus.xfire.soap.SoapConstants;
20  import org.codehaus.xfire.util.ClassLoaderUtils;
21  
22  /***
23   * Extends and configures the TypeMappingRegistry.
24   *
25   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
26   * @since Oct 31, 2004
27   */
28  public class TypeMappingRegistry extends DefaultTypeMappingRegistry implements LogEnabled, Configurable
29  {
30      private Logger logger;
31  
32      public TypeMappingRegistry()
33      {
34          super( true );
35      }
36  
37      public void configure( final Configuration config )
38          throws ConfigurationException
39      {
40          final Configuration[] tmConfig = config.getChildren( "typeMapping" );
41  
42          for( int i = 0; i < tmConfig.length; i++ )
43          {
44              configureTypeMapping( tmConfig[i] );
45          }
46      }
47  
48      private void configureTypeMapping( final Configuration configuration )
49          throws ConfigurationException
50      {
51          final String namespace = configuration.getAttribute( "namespace" );
52          TypeMapping tm = getTypeMapping( namespace );
53  
54          if( null == tm )
55          {
56              final String parentNamespace = configuration.getAttribute( "parentNamespace",
57                                                                         getDefaultTypeMapping().getEncodingStyleURI() );
58  
59              tm = createTypeMapping( parentNamespace, false );
60  
61              register( namespace, tm );
62  
63              if( configuration.getAttributeAsBoolean( "default", false ) )
64              {
65                  registerDefault( tm );
66              }
67  
68              // register primitive types manually since there is no way
69              // to do Class.forName("boolean") et al.
70              tm.register( boolean.class, new QName( SoapConstants.XSD, "boolean" ), new BooleanType() );
71              tm.register( int.class, new QName( SoapConstants.XSD, "int" ), new IntType() );
72              tm.register( double.class, new QName( SoapConstants.XSD, "double" ), new DoubleType() );
73              tm.register( float.class, new QName( SoapConstants.XSD, "float" ), new FloatType() );
74              tm.register( long.class, new QName( SoapConstants.XSD, "long" ), new LongType() );
75          }
76  
77          final Configuration[] types = configuration.getChildren( "type" );
78  
79          for( int i = 0; i < types.length; i++ )
80          {
81              configureType( types[i], tm );
82          }
83      }
84  
85      private void configureType( final Configuration configuration, final TypeMapping tm )
86          throws ConfigurationException
87      {
88          try
89          {
90              final String ns = configuration.getAttribute( "namespace" );
91              final String name = configuration.getAttribute( "name" );
92              final QName qname = new QName( ns, name );
93  
94              final Class clazz = ClassLoaderUtils.loadClass( configuration.getAttribute( "class" ), getClass() );
95              final Class typeClass = ClassLoaderUtils.loadClass( configuration.getAttribute( "type" ), getClass() );
96  
97              tm.register( clazz,
98                           qname,
99                           (Type)typeClass.newInstance() );
100 
101             logger.debug( "Registered " + typeClass.getName() + " for " + qname + " with class " + clazz.getName() );
102         }
103         catch( ConfigurationException e )
104         {
105             throw e;
106         }
107         catch( Exception e )
108         {
109             throw new ConfigurationException( "Could not configure type at " + configuration.getLocation(), e );
110         }
111     }
112 
113     public void enableLogging( final Logger logger )
114     {
115         this.logger = logger;
116     }
117 }