View Javadoc

1   package org.codehaus.xfire.plexus.java;
2   
3   import javax.xml.namespace.QName;
4   
5   import org.codehaus.plexus.configuration.PlexusConfiguration;
6   import org.codehaus.plexus.configuration.PlexusConfigurationException;
7   import org.codehaus.plexus.logging.LogEnabled;
8   import org.codehaus.plexus.logging.Logger;
9   import org.codehaus.plexus.personality.plexus.lifecycle.phase.Configurable;
10  import org.codehaus.xfire.SOAPConstants;
11  import org.codehaus.xfire.java.mapping.TypeMapping;
12  import org.codehaus.xfire.java.type.BooleanType;
13  import org.codehaus.xfire.java.type.DoubleType;
14  import org.codehaus.xfire.java.type.FloatType;
15  import org.codehaus.xfire.java.type.IntType;
16  import org.codehaus.xfire.java.type.LongType;
17  
18  /***
19   * Extends and configures the TypeMappingRegistry.
20   * 
21   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
22   * @since Oct 31, 2004
23   */
24  public class TypeMappingRegistry
25      extends org.codehaus.xfire.java.mapping.DefaultTypeMappingRegistry
26      implements LogEnabled, Configurable
27  {
28      private Logger logger;
29  
30      public void configure(PlexusConfiguration config) 
31          throws PlexusConfigurationException
32      {
33          PlexusConfiguration tmConfig[] = config.getChildren("typeMapping");
34          
35          for ( int i = 0; i < tmConfig.length; i++ )
36          {
37              configureTypeMapping( tmConfig[i] );
38          }
39      }
40  
41      private void configureTypeMapping(PlexusConfiguration configuration)
42          throws PlexusConfigurationException
43      {
44          TypeMapping tm = createTypeMapping(false);
45          
46          register( configuration.getAttribute( "namespace" ), tm );
47          
48          if ( Boolean.valueOf( configuration.getAttribute("default", "false") ).booleanValue() )
49              registerDefault( tm );
50          
51          PlexusConfiguration[] types = configuration.getChildren( "type" );
52          
53          // register primitive types manually since there is no way
54          // to do Class.forName("boolean") et al.
55          tm.register(boolean.class, new QName(SOAPConstants.XSD,"boolean"), BooleanType.class);
56          tm.register(int.class, new QName(SOAPConstants.XSD,"int"), IntType.class);
57          tm.register(double.class, new QName(SOAPConstants.XSD,"double"), DoubleType.class);
58          tm.register(float.class, new QName(SOAPConstants.XSD,"float"), FloatType.class);
59          tm.register(long.class, new QName(SOAPConstants.XSD,"long"), LongType.class);
60          
61          for ( int i = 0; i < types.length; i++ )
62          {
63              configureType( types[i], tm );
64          }
65      }
66      
67      private void configureType( PlexusConfiguration configuration, TypeMapping tm )
68          throws PlexusConfigurationException
69      {
70          try
71          {
72              String ns = configuration.getAttribute("namespace");
73              String name = configuration.getAttribute("name");
74              QName qname = new QName(ns, name);
75              
76              Class clazz = loadClass( configuration.getAttribute("class") );
77              Class typeClass = loadClass( configuration.getAttribute("type") );
78  
79              tm.register( clazz,
80                           qname,
81                           typeClass );
82              
83              logger.debug( "Registered " + typeClass.getName() + 
84                                " for " + qname + " with class " + clazz.getName() );
85          }
86          catch (Exception e)
87          {
88              if ( e instanceof PlexusConfigurationException )
89                  throw (PlexusConfigurationException) e;
90              
91              throw new PlexusConfigurationException( "Could not configure type.", e );
92          }                     
93      }
94  
95      public void enableLogging(Logger logger)
96      {
97          this.logger = logger;
98      }
99  }