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