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