View Javadoc
1   /*
2    * Copyright 2006 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.exolab.castor.builder.conflictresolution;
15  
16  import java.util.HashMap;
17  import java.util.Map;
18  import java.util.StringTokenizer;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  /**
24   * Registry for {@link ClassNameCRStrategy} implementations obtained from the Castor builder
25   * properties file.
26   *
27   * @author <a href="mailto:werner DOT guttmann @gmx DOT net">Werner Guttmann</a>
28   * @version $Revision: 5951 $ $Date: 2006-04-08 08:58:10 -0600 (Sat, 08 Apr 2006) $
29   * @since 1.1
30   */
31  public final class ClassNameCRStrategyRegistry {
32    // --------------------------------------------------------------------------
33  
34    /**
35     * The <a href="http://jakarta.apache.org/commons/logging/">Jakarta Commons Logging</a> instance
36     * used for all logging.
37     */
38    private static final Log LOG = LogFactory.getLog(ClassNameCRStrategyRegistry.class);
39  
40    /**
41     * Association between name of {@link ClassNameCRStrategy} implementation and
42     * {@link ClassNameCRStrategy} instance.
43     */
44    private Map<String, ClassNameCRStrategy> _strategies = new HashMap<String, ClassNameCRStrategy>();
45  
46    // --------------------------------------------------------------------------
47  
48    /**
49     * Construct an instance of {@link ClassNameCRStrategyRegistry} that loads the
50     * {@link ClassNameCRStrategy} implementations specified in the given BuilderConfiguration.
51     *
52     * @param enlistedNameConflictStrategies The BuilderConfiguration.
53     */
54    public ClassNameCRStrategyRegistry(final String enlistedNameConflictStrategies) {
55      StringTokenizer tokenizer = new StringTokenizer(enlistedNameConflictStrategies, ", ");
56      ClassLoader loader = ClassNameCRStrategyRegistry.class.getClassLoader();
57      while (tokenizer.hasMoreTokens()) {
58        String classname = tokenizer.nextToken();
59        try {
60          Class<?> cls = loader.loadClass(classname);
61          Object obj = cls.newInstance();
62          ClassNameCRStrategy strategy = (ClassNameCRStrategy) obj;
63          _strategies.put(strategy.getName(), strategy);
64        } catch (Exception except) {
65          LOG.error("The ClassNameConflictResolutionStrategy " + classname + " "
66              + "specified in the Castor builder properties file could not " + "be instantiated.");
67        }
68      }
69    }
70  
71    // --------------------------------------------------------------------------
72  
73    /**
74     * Returns the names of all the configured {@link ClassNameCRStrategy} implementations. A
75     * {@link ClassNameCRStrategy} instance can be obtained by the
76     * {@link #getClassNameConflictResolutionStrategy} method.
77     *
78     * @return Names of {@link ClassNameCRStrategy} implementations
79     */
80    public String[] getClassNameConflictResolutionStrategyNames() {
81      String[] names = new String[_strategies.size()];
82      return _strategies.keySet().toArray(names);
83    }
84  
85    /**
86     * Returns a {@link ClassNameCRStrategy} with the specified name. Returns null if the named
87     * strategy is not supported.
88     *
89     * @param name The name of the ClassNameConflictResolutionStrategy.
90     * @return The TransactionManagerFactory or null if none exists.
91     */
92    public ClassNameCRStrategy getClassNameConflictResolutionStrategy(final String name) {
93      Object factory = _strategies.get(name);
94      if (factory == null) {
95        String msg = "The ClassNameConflictResolutionStrategy '" + name + "' "
96            + "does not exist in the Castor builder properties file "
97            + "and is therefore not supported.";
98        LOG.error(msg);
99        throw new IllegalArgumentException(msg);
100     }
101     return (ClassNameCRStrategy) factory;
102   }
103 
104 }