1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.betwixt.io.read;
17
18 import org.apache.commons.betwixt.ElementDescriptor;
19 import org.apache.commons.logging.Log;
20
21 /***
22 * Group of factory methods for <code>ChainedBeanCreator</code>'s.
23 * The standard implementations used by Betwixt are present here.
24 *
25 * @author Robert Burrell Donkin
26 * @since 0.5
27 */
28 public class ChainedBeanCreatorFactory {
29
30 /*** Singleton instance for creating derived beans */
31 private static final ChainedBeanCreator derivedBeanCreator
32 = new ChainedBeanCreator() {
33 public Object create(
34 ElementMapping elementMapping,
35 ReadContext context,
36 BeanCreationChain chain) {
37
38 String className
39 = elementMapping
40 .getAttributes().getValue( context.getClassNameAttribute() );
41 if ( className != null ) {
42 try {
43
44 ClassLoader classLoader = context.getClassLoader();
45 if ( classLoader == null ) {
46 context.getLog().warn(
47 "Could not create derived instance: read context classloader not set." );
48 }
49 Class clazz = classLoader.loadClass( className );
50 return clazz.newInstance();
51
52 } catch (Exception e) {
53
54 context.getLog().warn( "Could not create instance of type: " + className );
55 context.getLog().debug( "Create new instance failed: ", e );
56 return null;
57 }
58
59 } else {
60
61 return chain.create( elementMapping, context );
62 }
63 }
64 };
65
66 /***
67 * Creates a <code>ChainedBeanCreator</code> that constructs derived beans.
68 * These have their classname set by an xml attribute.
69 * @return <code>ChainedBeanCreator</code> that implements Derived beans logic, not null
70 */
71 public static final ChainedBeanCreator createDerivedBeanCreator() {
72 return derivedBeanCreator;
73 }
74
75 /*** Singleton instance that creates beans based on type */
76 private static final ChainedBeanCreator elementTypeBeanCreator
77 = new ChainedBeanCreator() {
78 public Object create(
79 ElementMapping element,
80 ReadContext context,
81 BeanCreationChain chain) {
82
83 Log log = context.getLog();
84 Class theClass = null;
85
86 ElementDescriptor descriptor = element.getDescriptor();
87 if ( descriptor != null ) {
88
89 theClass = descriptor.getImplementationClass();
90 }
91
92 if ( theClass == null ) {
93
94 theClass = element.getType();
95 }
96
97 if ( log.isTraceEnabled() ) {
98 log.trace(
99 "Creating instance of class " + theClass.getName()
100 + " for element " + element.getName());
101 }
102
103 try {
104
105 return theClass.newInstance();
106
107 } catch (Exception e) {
108
109 context.getLog().warn(
110 "Could not create instance of type: " + theClass.getName() );
111 context.getLog().debug( "Create new instance failed: ", e );
112 return null;
113 }
114 }
115 };
116
117 /***
118 * Creates a <code>ChainedBeanCreator</code> that constructs beans based on element type.
119 * @return <code>ChainedBeanCreator</code> that implements load by type beans logic, not null
120 */
121 public static final ChainedBeanCreator createElementTypeBeanCreator() {
122 return elementTypeBeanCreator;
123 }
124
125 /*** Singleton instance that creates beans based on IDREF */
126 private static final ChainedBeanCreator idRefBeanCreator
127 = new ChainedBeanCreator() {
128 public Object create(
129 ElementMapping elementMapping,
130 ReadContext context,
131 BeanCreationChain chain) {
132 if ( context.getMapIDs() ) {
133 String idref = elementMapping.getAttributes().getValue( "idref" );
134 if ( idref != null ) {
135
136
137
138
139 context.getLog().trace( "Found IDREF" );
140 Object bean = context.getBean( idref );
141 if ( bean != null ) {
142 if ( context.getLog().isTraceEnabled() ) {
143 context.getLog().trace( "Matched bean " + bean );
144 }
145 return bean;
146 }
147 context.getLog().trace( "No match found" );
148 }
149 }
150 return chain.create( elementMapping, context );
151 }
152 };
153
154 /***
155 * Creates a <code>ChainedBeanCreator</code> that finds existing beans based on their IDREF.
156 * @return <code>ChainedBeanCreator</code> that implements IDREF beans logic, not null
157 */
158 public static final ChainedBeanCreator createIDREFBeanCreator() {
159 return idRefBeanCreator;
160 }
161 }