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.xfire.soap.SoapConstants;
11 import org.codehaus.xfire.type.Type;
12 import org.codehaus.xfire.type.TypeMapping;
13 import org.codehaus.xfire.type.basic.BooleanType;
14 import org.codehaus.xfire.type.basic.DoubleType;
15 import org.codehaus.xfire.type.basic.FloatType;
16 import org.codehaus.xfire.type.basic.IntType;
17 import org.codehaus.xfire.type.basic.LongType;
18
19 /***
20 * Extends and configures the TypeMappingRegistry.
21 *
22 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
23 * @since Oct 31, 2004
24 */
25 public class TypeMappingRegistry
26 extends org.codehaus.xfire.type.DefaultTypeMappingRegistry
27 implements LogEnabled, Configurable
28 {
29 private Logger logger;
30
31 public void configure(PlexusConfiguration config)
32 throws PlexusConfigurationException
33 {
34 PlexusConfiguration tmConfig[] = config.getChildren("typeMapping");
35
36 for ( int i = 0; i < tmConfig.length; i++ )
37 {
38 configureTypeMapping( tmConfig[i] );
39 }
40 }
41
42 private void configureTypeMapping(PlexusConfiguration configuration)
43 throws PlexusConfigurationException
44 {
45 TypeMapping tm = createTypeMapping(false);
46
47 register( configuration.getAttribute( "namespace" ), tm );
48
49 if ( Boolean.valueOf( configuration.getAttribute("default", "false") ).booleanValue() )
50 registerDefault( tm );
51
52 PlexusConfiguration[] types = configuration.getChildren( "type" );
53
54
55
56 tm.register(boolean.class, new QName(SoapConstants.XSD,"boolean"), new BooleanType());
57 tm.register(int.class, new QName(SoapConstants.XSD,"int"), new IntType());
58 tm.register(double.class, new QName(SoapConstants.XSD,"double"), new DoubleType());
59 tm.register(float.class, new QName(SoapConstants.XSD,"float"), new FloatType());
60 tm.register(long.class, new QName(SoapConstants.XSD,"long"), new LongType());
61
62 for ( int i = 0; i < types.length; i++ )
63 {
64 configureType( types[i], tm );
65 }
66 }
67
68 private void configureType( PlexusConfiguration configuration, TypeMapping tm )
69 throws PlexusConfigurationException
70 {
71 try
72 {
73 String ns = configuration.getAttribute("namespace");
74 String name = configuration.getAttribute("name");
75 QName qname = new QName(ns, name);
76
77 Class clazz = loadClass( configuration.getAttribute("class") );
78 Class typeClass = loadClass( configuration.getAttribute("type") );
79
80 Type type = (Type) typeClass.newInstance();
81
82 tm.register(clazz, qname, type);
83
84 logger.debug( "Registered " + typeClass.getName() +
85 " for " + qname + " with class " + clazz.getName() );
86 }
87 catch (Exception e)
88 {
89 if ( e instanceof PlexusConfigurationException )
90 throw (PlexusConfigurationException) e;
91
92 throw new PlexusConfigurationException( "Could not configure type.", e );
93 }
94 }
95
96 public void enableLogging(Logger logger)
97 {
98 this.logger = logger;
99 }
100
101 protected Class loadClass( String className )
102 throws ClassNotFoundException
103 {
104 try
105 {
106 return getClass().getClassLoader().loadClass(className);
107 }
108 catch (ClassNotFoundException cnfe)
109 {
110 try
111 {
112 return Class.forName(className);
113 }
114 catch (ClassNotFoundException cnf2)
115 {
116 return Thread.currentThread().getContextClassLoader().loadClass(className);
117 }
118 }
119 }
120 }