KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > io > read > ChainedBeanCreatorFactory


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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 JavaDoc create(
34                                 ElementMapping elementMapping,
35                                 ReadContext context,
36                                 BeanCreationChain chain) {
37                                 
38                 String JavaDoc className
39                     = elementMapping
40                         .getAttributes().getValue( context.getClassNameAttribute() );
41                 if ( className != null ) {
42                     try {
43                         // load the class we should instantiate
44
ClassLoader JavaDoc 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 JavaDoc clazz = classLoader.loadClass( className );
50                         return clazz.newInstance();
51                                         
52                     } catch (Exception JavaDoc e) {
53                         // it would be nice to have a pluggable strategy for exception management
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                     // pass responsibility down the chain
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 JavaDoc create(
79                                 ElementMapping element,
80                                 ReadContext context,
81                                 BeanCreationChain chain) {
82                 
83                 Log log = context.getLog();
84                 Class JavaDoc theClass = null;
85                 
86                 ElementDescriptor descriptor = element.getDescriptor();
87                 if ( descriptor != null ) {
88                     // created based on implementation class
89
theClass = descriptor.getImplementationClass();
90                 }
91                 
92                 if ( theClass == null ) {
93                     // create based on type
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 JavaDoc e) {
108                     // it would be nice to have a pluggable strategy for exception management
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 JavaDoc create(
129                                 ElementMapping elementMapping,
130                                 ReadContext context,
131                                 BeanCreationChain chain) {
132                 if ( context.getMapIDs() ) {
133                     String JavaDoc idref = elementMapping.getAttributes().getValue( "idref" );
134                     if ( idref != null ) {
135                         // XXX need to check up about ordering
136
// XXX this is a very simple system that assumes that
137
// XXX id occurs before idrefs
138
// XXX would need some thought about how to implement a fuller system
139
context.getLog().trace( "Found IDREF" );
140                         Object JavaDoc 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 }
162
Popular Tags