001: /*
002: * Copyright 2005 Ralf Joachim, Werner Guttmann
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.castor.mapping;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Collections;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.StringTokenizer;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.castor.util.ConfigKeys;
028: import org.castor.util.Configuration;
029: import org.exolab.castor.mapping.MappingException;
030: import org.exolab.castor.mapping.MappingLoader;
031:
032: /**
033: * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttmann</a>
034: * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
035: * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
036: */
037: public final class MappingLoaderRegistry {
038:
039: /** The <a href="http://jakarta.apache.org/commons/logging/">Jakarta Commons
040: * Logging </a> instance used for all logging. */
041: private static final Log LOG = LogFactory
042: .getLog(MappingLoaderRegistry.class);
043:
044: /** The cached mapping loader factories. */
045: private final List _mappingLoaderFactories = new ArrayList();
046:
047: /** Already loaded mapping loaders. */
048: private final List _mappingLoaders = new ArrayList();
049:
050: /**
051: * Creates an instance of this registry, loading the mapping loader
052: * factories from the castor.properties file.
053: * @param config Configuration.
054: */
055: public MappingLoaderRegistry(final Configuration config) {
056: String prop = config.getProperty(
057: ConfigKeys.MAPPING_LOADER_FACTORIES, "");
058: StringTokenizer tokenizer = new StringTokenizer(prop, ", ");
059: while (tokenizer.hasMoreTokens()) {
060: String classname = tokenizer.nextToken();
061: try {
062: ClassLoader loader = getClass().getClassLoader();
063: Class cls = loader.loadClass(classname);
064: // Class[] types = new Class[] {ClassLoader.class};
065: Object obj = cls.getConstructor((Class[]) null)
066: .newInstance((Object[]) null);
067: _mappingLoaderFactories.add(obj);
068: } catch (Exception ex) {
069: LOG.error(
070: "Problem instantiating mapping loader factory implementation: "
071: + classname, ex);
072: }
073: }
074: }
075:
076: /**
077: * Deletes all 'cached' mapping loader factories.
078: */
079: public void clear() {
080: Iterator iter = _mappingLoaders.iterator();
081: while (iter.hasNext()) {
082: ((MappingLoader) iter.next()).clear();
083: }
084: }
085:
086: //--------------------------------------------------------------------------
087:
088: /**
089: * Returns a mapping loader for the suitable source and binding type. The engine's
090: * specific mapping loader is used to create binding specific descriptors.
091: * The mapping loader is cached in memory and returned in subsequent method calls.
092: *
093: * @param sourceType The type of the mapping source.
094: * @param bindingType The binding type to load from mapping.
095: * @return A mapping loader
096: * @throws MappingException A mapping error occured preventing
097: * descriptors from being generated from the loaded mapping
098: */
099: public MappingLoader getMappingLoader(final String sourceType,
100: final BindingType bindingType) throws MappingException {
101: Iterator iter = _mappingLoaderFactories.iterator();
102: while (iter.hasNext()) {
103: MappingLoaderFactory loaderFactory = (MappingLoaderFactory) iter
104: .next();
105: if (loaderFactory.getSourceType().equals(sourceType)
106: && (loaderFactory.getBindingType() == bindingType)) {
107: MappingLoader mappingLoader = loaderFactory
108: .getMappingLoader();
109: _mappingLoaders.add(mappingLoader);
110: return mappingLoader;
111: }
112: }
113:
114: String msg = "No mapping loader/factory for: " + "SourceType="
115: + sourceType + " / BindingType=" + bindingType;
116: LOG.error(msg);
117: throw new MappingException(msg);
118: }
119:
120: /**
121: * Returns a list of 'cached' mapping loader factories.
122: * @return a list of 'cached' mapping loader factories.
123: */
124: public Collection getMappingLoaderFactories() {
125: return Collections
126: .unmodifiableCollection(_mappingLoaderFactories);
127: }
128:
129: //--------------------------------------------------------------------------
130: }
|