View Javadoc
1   /*
2    * Copyright 2005 Ralf Joachim, Werner Guttmann
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.castor.mapping;
15  
16  import java.util.ArrayList;
17  import java.util.Collection;
18  import java.util.Collections;
19  import java.util.List;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.castor.core.CoreProperties;
24  import org.castor.core.util.AbstractProperties;
25  import org.exolab.castor.mapping.MappingException;
26  import org.exolab.castor.mapping.MappingLoader;
27  
28  /**
29   * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttmann</a>
30   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
31   * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
32   */
33  public final class MappingLoaderRegistry {
34  
35    /**
36     * The <a href="http://jakarta.apache.org/commons/logging/">Jakarta Commons Logging </a> instance
37     * used for all logging.
38     */
39    private static final Log LOG = LogFactory.getLog(MappingLoaderRegistry.class);
40  
41    /** The cached mapping loader factories. */
42    private final List<MappingLoaderFactory> _mappingLoaderFactories =
43        new ArrayList<MappingLoaderFactory>();
44  
45    /** Already loaded mapping loaders. */
46    private final List<MappingLoader> _mappingLoaders = new ArrayList<MappingLoader>();
47  
48    /**
49     * Creates an instance of this registry, loading the mapping loader factories from the
50     * castor.properties file.
51     * 
52     * @param properties Properties.
53     */
54    public MappingLoaderRegistry(final AbstractProperties properties) {
55      Object[] objects = properties.getObjectArray(CoreProperties.MAPPING_LOADER_FACTORIES,
56          getClass().getClassLoader());
57      for (Object mappingLoaderFactory : objects) {
58        _mappingLoaderFactories.add((MappingLoaderFactory) mappingLoaderFactory);
59      }
60    }
61  
62    /**
63     * Deletes all 'cached' mapping loader factories.
64     */
65    public void clear() {
66      for (MappingLoader mappingLoader : _mappingLoaders) {
67        mappingLoader.clear();
68      }
69    }
70  
71    /**
72     * Returns a mapping loader for the suitable source and binding type. The engine's specific
73     * mapping loader is used to create binding specific descriptors. The mapping loader is cached in
74     * memory and returned in subsequent method calls.
75     *
76     * @param sourceType The type of the mapping source.
77     * @param bindingType The binding type to load from mapping.
78     * @return A mapping loader
79     * @throws MappingException A mapping error occurred preventing descriptors from being generated
80     *         from the loaded mapping
81     */
82    public MappingLoader getMappingLoader(final String sourceType, final BindingType bindingType)
83        throws MappingException {
84      for (MappingLoaderFactory mappingLoaderFactory : _mappingLoaderFactories) {
85        if (mappingLoaderFactory.getSourceType().equals(sourceType)
86            && (mappingLoaderFactory.getBindingType() == bindingType)) {
87          MappingLoader mappingLoader = mappingLoaderFactory.getMappingLoader();
88          _mappingLoaders.add(mappingLoader);
89          return mappingLoader;
90        }
91      }
92  
93      String msg = "No mapping loader/factory for: " + "SourceType=" + sourceType + " / BindingType="
94          + bindingType;
95      LOG.error(msg);
96      throw new MappingException(msg);
97    }
98  
99    /**
100    * Returns a list of 'cached' mapping loader factories.
101    * 
102    * @return a list of 'cached' mapping loader factories.
103    */
104   public Collection<MappingLoaderFactory> getMappingLoaderFactories() {
105     return Collections.unmodifiableCollection(_mappingLoaderFactories);
106   }
107 
108 }