1 package org.apache.torque.engine.database.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Hashtable;
20 import java.util.List;
21
22 import org.apache.torque.engine.EngineException;
23
24 /***
25 * A name generation factory.
26 *
27 * @author <a href="mailto:dlr@finemaltcoding.com>Daniel Rall</a>
28 * @version $Id: NameFactory.java 239626 2005-08-24 12:19:51Z henning $
29 */
30 public class NameFactory
31 {
32 /***
33 * The fully qualified class name of the Java name generator.
34 */
35 public static final String JAVA_GENERATOR =
36 JavaNameGenerator.class.getName();
37
38 /***
39 * The fully qualified class name of the constraint name generator.
40 */
41 public static final String CONSTRAINT_GENERATOR =
42 ConstraintNameGenerator.class.getName();
43
44 /***
45 * The single instance of this class.
46 */
47 private static NameFactory instance = new NameFactory();
48
49 /***
50 * The cache of <code>NameGenerator</code> algorithms in use for
51 * name generation, keyed by fully qualified class name.
52 */
53 private Hashtable algorithms;
54
55 /***
56 * Creates a new instance with storage for algorithm implementations.
57 */
58 protected NameFactory()
59 {
60 algorithms = new Hashtable(5);
61 }
62
63 /***
64 * Factory method which retrieves an instance of the named generator.
65 *
66 * @param name The fully qualified class name of the name
67 * generation algorithm to retrieve.
68 * @return A name generator
69 */
70 protected NameGenerator getAlgorithm(String name)
71 throws EngineException
72 {
73 synchronized (algorithms)
74 {
75 NameGenerator algorithm = (NameGenerator) algorithms.get(name);
76 if (algorithm == null)
77 {
78 try
79 {
80 algorithm =
81 (NameGenerator) Class.forName(name).newInstance();
82 }
83 catch (InstantiationException e)
84 {
85 throw new EngineException("Unable to instantiate class "
86 + name + ": Make sure it's in your run-time classpath", e);
87 }
88 catch (Exception e)
89 {
90 throw new EngineException(e);
91 }
92 algorithms.put(name, algorithm);
93 }
94 return algorithm;
95 }
96 }
97
98 /***
99 * Given a list of <code>String</code> objects, implements an
100 * algorithm which produces a name.
101 *
102 * @param algorithmName The fully qualified class name of the
103 * {@link org.apache.torque.engine.database.model.NameGenerator}
104 * implementation to use to generate names.
105 * @param inputs Inputs used to generate a name.
106 * @return The generated name.
107 * @throws EngineException an exception
108 */
109 public static String generateName(String algorithmName, List inputs)
110 throws EngineException
111 {
112 NameGenerator algorithm = instance.getAlgorithm(algorithmName);
113 return algorithm.generateName(inputs);
114 }
115 }