View Javadoc

1   package org.codehaus.xfire.plexus.type;
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.plexus.personality.plexus.lifecycle.phase.Initializable;
11  import org.codehaus.xfire.aegis.type.Type;
12  import org.codehaus.xfire.aegis.type.TypeMapping;
13  import org.codehaus.xfire.util.ClassLoaderUtils;
14  
15  /***
16   * Extends and configures the TypeMappingRegistry.
17   * 
18   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
19   * @since Oct 31, 2004
20   */
21  public class TypeMappingRegistry
22      extends org.codehaus.xfire.aegis.type.DefaultTypeMappingRegistry
23      implements LogEnabled, Configurable, Initializable
24  {
25      private Logger logger;
26  
27      public void configure(PlexusConfiguration config)
28          throws PlexusConfigurationException
29      {
30          PlexusConfiguration tmConfig[] = config.getChildren("typeMapping");
31  
32          for ( int i = 0; i < tmConfig.length; i++ )
33          {
34              configureTypeMapping( tmConfig[i] );
35          }
36      }
37  
38      private void configureTypeMapping(PlexusConfiguration configuration)
39          throws PlexusConfigurationException
40      {
41          TypeMapping tm = createTypeMapping(false);
42  
43          register( configuration.getAttribute( "namespace" ), tm );
44  
45          if ( Boolean.valueOf( configuration.getAttribute("default", "false") ).booleanValue() )
46              registerDefault( tm );
47  
48          PlexusConfiguration[] types = configuration.getChildren( "type" );
49  
50          for ( int i = 0; i < types.length; i++ )
51          {
52              configureType( types[i], tm );
53          }
54      }
55  
56      private void configureType( PlexusConfiguration configuration, TypeMapping tm )
57          throws PlexusConfigurationException
58      {
59          try
60          {
61              String ns = configuration.getAttribute("namespace");
62              String name = configuration.getAttribute("name");
63              QName qname = new QName(ns, name);
64  
65              Class clazz = ClassLoaderUtils.loadClass( configuration.getAttribute("class"), getClass() );
66              Class typeClass = ClassLoaderUtils.loadClass( configuration.getAttribute("type"), getClass() );
67  
68              Type type = (Type) typeClass.newInstance();
69  
70              tm.register(clazz, qname, type);
71  
72              logger.debug( "Registered " + typeClass.getName() +
73                                " for " + qname + " with class " + clazz.getName() );
74          }
75          catch (Exception e)
76          {
77              if ( e instanceof PlexusConfigurationException )
78                  throw (PlexusConfigurationException) e;
79  
80              throw new PlexusConfigurationException( "Could not configure type.", e );
81          }
82      }
83  
84      public void enableLogging(Logger logger)
85      {
86          this.logger = logger;
87      }
88  
89      public void initialize()
90      {
91          createDefaultMappings();
92      }
93  }