View Javadoc

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 java.util.HashMap;
19  
20  import org.apache.commons.betwixt.BindingConfiguration;
21  import org.apache.commons.betwixt.expression.Context;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /***  
26    * Extends <code>Context</code> to provide read specific functionality. 
27    *
28    * @author Robert Burrell Donkin
29    * @since 0.5
30    */
31  public class ReadContext extends Context {
32  
33      /*** Beans indexed by ID strings */
34      private HashMap beansById = new HashMap();
35      /*** Classloader to be used to load beans during reading */
36      private ClassLoader classLoader;
37      /*** The read specific configuration */
38      private ReadConfiguration readConfiguration;
39      
40      /*** 
41        * Constructs a <code>ReadContext</code> with the same settings 
42        * as an existing <code>Context</code>.
43        * @param context not null
44        * @param readConfiguration not null
45        */
46      public ReadContext( Context context, ReadConfiguration readConfiguration ) {
47          super( context );
48          this.readConfiguration = readConfiguration;
49      }
50      
51      /***
52        * Constructs a <code>ReadContext</code> with standard log.
53        * @param bindingConfiguration the dynamic configuration, not null
54        * @param readConfiguration the extra read configuration not null
55        */
56      public ReadContext( 
57                      BindingConfiguration bindingConfiguration, 
58                      ReadConfiguration readConfiguration ) {
59          this( 
60                  LogFactory.getLog( ReadContext.class ), 
61                  bindingConfiguration,  
62                  readConfiguration);
63      }
64      
65      /*** 
66        * Base constructor
67        * @param log log to this Log
68        * @param bindingConfiguration the dynamic configuration, not null
69        * @param readConfiguration the extra read configuration not null
70        */
71      public ReadContext(
72                          Log log, 
73                          BindingConfiguration bindingConfiguration, 
74                          ReadConfiguration readConfiguration  ) {
75          super( null, log , bindingConfiguration );
76          this.readConfiguration = readConfiguration;
77      }
78      
79      /*** 
80        * Constructs a <code>ReadContext</code> 
81        * with the same settings as an existing <code>Context</code>.
82        * @param readContext not null
83        */
84      public ReadContext( ReadContext readContext ) {
85          super( readContext );
86          beansById = readContext.beansById;
87          classLoader = readContext.classLoader;
88          readConfiguration = readContext.readConfiguration;
89      }
90      
91      /***
92       * Puts a bean into storage indexed by an (xml) ID.
93       *
94       * @param id the ID string of the xml element associated with the bean
95       * @param bean the Object to store, not null
96       */
97      public void putBean( String id, Object bean ) {
98          beansById.put( id, bean );
99      }
100     
101     /***
102      * Gets a bean from storage by an (xml) ID.
103      *
104      * @param id the ID string of the xml element associated with the bean
105      * @return the Object that the ID references, otherwise null
106      */
107     public Object getBean( String id ) {
108         return beansById.get( id );
109     }
110     
111     /*** 
112      * Clears the beans indexed by id.
113      */
114     public void clearBeans() {
115         beansById.clear();
116     }
117     
118     /***
119       * Gets the classloader to be used.
120       * @return the classloader that should be used to load all classes, possibly null
121       */
122     public ClassLoader getClassLoader() {
123         return classLoader;
124     }
125     
126     /***
127       * Sets the classloader to be used.
128       * @param classLoader the ClassLoader to be used, possibly null
129       */
130     public void setClassLoader( ClassLoader classLoader ) {
131         this.classLoader = classLoader;
132     }
133     
134     /*** 
135       * Gets the <code>BeanCreationChange</code> to be used to create beans 
136       * when an element is mapped.
137       * @return the BeanCreationChain not null
138       */
139     public BeanCreationChain getBeanCreationChain() {
140         return readConfiguration.getBeanCreationChain();
141     }
142 }