001: /*
002: * Copyright 2006 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.exolab.castor.builder.conflictresolution;
017:
018: import java.util.HashMap;
019: import java.util.Map;
020: import java.util.StringTokenizer;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: /**
026: * Registry for {@link ClassNameCRStrategy} implementations obtained from the
027: * Castor builder properties file.
028: *
029: * @author <a href="mailto:werner DOT guttmann @gmx DOT net">Werner Guttmann</a>
030: * @version $Revision: 5951 $ $Date: 2006-04-08 08:58:10 -0600 (Sat, 08 Apr 2006) $
031: * @since 1.1
032: */
033: public final class ClassNameCRStrategyRegistry {
034: //--------------------------------------------------------------------------
035:
036: /** The <a href="http://jakarta.apache.org/commons/logging/">Jakarta
037: * Commons Logging</a> instance used for all logging. */
038: private static final Log LOG = LogFactory
039: .getLog(ClassNameCRStrategyRegistry.class);
040:
041: /**
042: * Association between name of {@link ClassNameCRStrategy} implementation
043: * and {@link ClassNameCRStrategy} instance.
044: */
045: private Map _strategies = new HashMap();
046:
047: //--------------------------------------------------------------------------
048:
049: /**
050: * Construct an instance of {@link ClassNameCRStrategyRegistry} that loads
051: * the {@link ClassNameCRStrategy} implementations specified in the given
052: * BuilderConfiguration.
053: *
054: * @param enlistedNameConflictStrategies The BuilderConfiguration.
055: */
056: public ClassNameCRStrategyRegistry(
057: final String enlistedNameConflictStrategies) {
058: StringTokenizer tokenizer = new StringTokenizer(
059: enlistedNameConflictStrategies, ", ");
060: ClassLoader loader = ClassNameCRStrategyRegistry.class
061: .getClassLoader();
062: while (tokenizer.hasMoreTokens()) {
063: String classname = tokenizer.nextToken();
064: try {
065: Class cls = loader.loadClass(classname);
066: Object obj = cls.newInstance();
067: ClassNameCRStrategy strategy = (ClassNameCRStrategy) obj;
068: _strategies.put(strategy.getName(), strategy);
069: } catch (Exception except) {
070: LOG
071: .error("The ClassNameConflictResolutionStrategy "
072: + classname
073: + " "
074: + "specified in the Castor builder properties file could not "
075: + "be instantiated.");
076: }
077: }
078: }
079:
080: //--------------------------------------------------------------------------
081:
082: /**
083: * Returns the names of all the configured {@link ClassNameCRStrategy}
084: * implementations. A {@link ClassNameCRStrategy} instance can be obtained
085: * by the {@link #getClassNameConflictResolutionStrategy} method.
086: *
087: * @return Names of {@link ClassNameCRStrategy} implementations
088: */
089: public String[] getClassNameConflictResolutionStrategyNames() {
090: String[] names = new String[_strategies.size()];
091: return (String[]) _strategies.keySet().toArray(names);
092: }
093:
094: /**
095: * Returns a {@link ClassNameCRStrategy} with the specified name. Returns
096: * null if the named strategy is not supported.
097: *
098: * @param name The name of the ClassNameConflictResolutionStrategy.
099: * @return The TransactionManagerFactory or null if none exists.
100: */
101: public ClassNameCRStrategy getClassNameConflictResolutionStrategy(
102: final String name) {
103: Object factory = _strategies.get(name);
104: if (factory == null) {
105: String msg = "The ClassNameConflictResolutionStrategy '"
106: + name
107: + "' "
108: + "does not exist in the Castor builder properties file "
109: + "and is therefore not supported.";
110: LOG.error(msg);
111: throw new IllegalArgumentException(msg);
112: }
113: return (ClassNameCRStrategy) factory;
114: }
115:
116: }
|