View Javadoc
1   /*
2    * Copyright 2005 Ralf Joachim, Werner Guttmann
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.castor.mapping;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.castor.core.CoreProperties;
26  import org.castor.core.util.AbstractProperties;
27  import org.exolab.castor.mapping.MappingException;
28  import org.exolab.castor.mapping.MappingLoader;
29  
30  /**
31   * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttmann</a>
32   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
33   * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
34   */
35  public final class MappingLoaderRegistry {
36  
37      /** The <a href="http://jakarta.apache.org/commons/logging/">Jakarta Commons
38       *  Logging </a> instance used for all logging. */
39      private static final Log LOG = LogFactory.getLog(MappingLoaderRegistry.class);
40      
41      /** The cached mapping loader factories. */
42      private final List<MappingLoaderFactory>  _mappingLoaderFactories = new ArrayList<MappingLoaderFactory>();
43  
44      /** Already loaded mapping loaders. */
45      private final List<MappingLoader> _mappingLoaders = new ArrayList<MappingLoader>();
46      
47      /**
48       * Creates an instance of this registry, loading the mapping loader
49       * factories from the castor.properties file. 
50       * @param properties Properties.
51       */
52      public MappingLoaderRegistry(final AbstractProperties properties) {
53          Object[] objects = properties.getObjectArray(CoreProperties.MAPPING_LOADER_FACTORIES, getClass().getClassLoader());
54          for (Object mappingLoaderFactory : objects) {
55              _mappingLoaderFactories.add((MappingLoaderFactory) mappingLoaderFactory);
56          }
57      }
58  
59      /**
60       * Deletes all 'cached' mapping loader factories.
61       */
62      public void clear() {
63          for (MappingLoader mappingLoader : _mappingLoaders) {
64              mappingLoader.clear();
65          }
66      }
67  
68      /**
69       * Returns a mapping loader for the suitable source and binding type. The engine's
70       * specific mapping loader is used to create binding specific descriptors.
71       * The mapping loader is cached in memory and returned in subsequent method calls.
72       *
73       * @param sourceType The type of the mapping source.
74       * @param bindingType The binding type to load from mapping.
75       * @return A mapping loader
76       * @throws MappingException A mapping error occurred preventing
77       *         descriptors from being generated from the loaded mapping
78       */
79      public MappingLoader getMappingLoader(
80              final String sourceType,
81              final BindingType bindingType) throws MappingException {
82          for (MappingLoaderFactory mappingLoaderFactory : _mappingLoaderFactories) {
83              if (mappingLoaderFactory.getSourceType().equals(sourceType)
84                      && (mappingLoaderFactory.getBindingType() == bindingType)) {
85                  MappingLoader mappingLoader = mappingLoaderFactory.getMappingLoader();
86                  _mappingLoaders.add(mappingLoader);
87                  return mappingLoader;
88              }
89          }
90          
91          String msg = "No mapping loader/factory for: " + "SourceType=" + sourceType
92                     + " / BindingType=" + bindingType;
93          LOG.error(msg);
94          throw new MappingException(msg);
95      }
96      
97      /**
98       * Returns a list of 'cached' mapping loader factories.
99       * @return a list of 'cached' mapping loader factories.
100      */
101     public Collection<MappingLoaderFactory> getMappingLoaderFactories() {
102         return Collections.unmodifiableCollection(_mappingLoaderFactories);
103     }
104 
105 }