1 package org.codehaus.xfire.aegis.mapping;
2
3 import java.util.Collection;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import javax.naming.ConfigurationException;
8
9 import org.codehaus.plexus.configuration.PlexusConfiguration;
10 import org.codehaus.plexus.configuration.PlexusConfigurationException;
11 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Configurable;
12 import org.codehaus.xfire.AbstractXFireComponent;
13 import org.codehaus.xfire.XFireRuntimeException;
14 import org.codehaus.xfire.aegis.type.Type;
15 import org.dom4j.QName;
16
17 /***
18 * DefaultTypeRegistry
19 *
20 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
21 */
22 public class DefaultTypeRegistry
23 extends AbstractXFireComponent
24 implements TypeRegistry, Configurable
25 {
26 private Map types = new HashMap();
27
28 /***
29 * @see org.codehaus.xfire.aegis.mapping.TypeRegistry#register(org.dom4j.QName, java.lang.Class)
30 */
31 public void register(QName typeQ, Class typeClass)
32 {
33 types.put( typeQ, typeClass );
34 }
35
36 /***
37 * @see org.codehaus.xfire.aegis.mapping.TypeRegistry#getTypeClass(org.dom4j.QName)
38 */
39 public Class getTypeClass(QName typeQ)
40 {
41 return (Class) types.get(typeQ);
42 }
43
44 /***
45 * @see org.codehaus.xfire.aegis.mapping.TypeRegistry#createType(org.dom4j.QName)
46 */
47 public Type createType(QName typeQ)
48 {
49 try
50 {
51 Class clazz = getTypeClass(typeQ);
52
53 if ( clazz == null )
54 return null;
55
56 Type type = (Type) clazz.newInstance();
57
58 return type;
59 }
60 catch (InstantiationException e)
61 {
62 throw new XFireRuntimeException("Could not create type.", e);
63 }
64 catch (IllegalAccessException e)
65 {
66 throw new XFireRuntimeException("Could not create type.", e);
67 }
68 }
69
70 /***
71 * @see org.codehaus.xfire.aegis.mapping.TypeRegistry#unregisterType(org.dom4j.QName)
72 */
73 public Class unregisterType(QName typeQ)
74 {
75 return (Class) types.remove( typeQ );
76 }
77
78 /***
79 * @see org.codehaus.xfire.aegis.mapping.TypeRegistry#getAllTypes()
80 */
81 public Collection getAllTypes()
82 {
83 return types.values();
84 }
85
86 /***
87 * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
88 */
89 public void configure(PlexusConfiguration config)
90 throws PlexusConfigurationException
91 {
92 PlexusConfiguration[] types = config.getChildren("types");
93
94 for ( int i = 0; i < types.length; i++ )
95 {
96 String namespace = types[i].getAttribute("namespace");
97
98 PlexusConfiguration[] type = types[i].getChildren("type");
99
100 for ( int j = 0; j < type.length; j++ )
101 {
102 createType( type[j], namespace );
103 }
104 }
105 }
106
107 /***
108 * @param configuration
109 * @param namespace
110 * @throws ConfigurationException
111 */
112 private void createType(PlexusConfiguration configuration, String namespace)
113 throws PlexusConfigurationException
114 {
115 String name = configuration.getAttribute("name");
116 String className = configuration.getAttribute("type");
117
118 ClassLoader cl = Thread.currentThread().getContextClassLoader();
119
120 QName q = QName.get(name, namespace);
121 try
122 {
123 register(q, cl.loadClass(className));
124 }
125 catch (ClassNotFoundException e)
126 {
127 throw new PlexusConfigurationException("Couldn't configure type.", e);
128 }
129 }
130 }