Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
NameFactory |
|
| 2.6666666666666665;2.667 |
1 | package org.apache.torque.engine.database.model; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE | |
5 | * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the | |
7 | * License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | |
12 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | |
13 | * specific language governing permissions and limitations under the License. | |
14 | */ | |
15 | ||
16 | import java.util.Hashtable; | |
17 | import java.util.List; | |
18 | ||
19 | import org.apache.torque.engine.EngineException; | |
20 | ||
21 | /** | |
22 | * A name generation factory. | |
23 | * | |
24 | * @author <a href="mailto:dlr@finemaltcoding.com>Daniel Rall</a> | |
25 | * @version $Id: NameFactory.java,v 1.1 2007-10-21 07:57:27 abyrne Exp $ | |
26 | */ | |
27 | public class NameFactory { | |
28 | /** | |
29 | * The fully qualified class name of the Java name generator. | |
30 | */ | |
31 | 0 | public static final String JAVA_GENERATOR = JavaNameGenerator.class.getName(); |
32 | ||
33 | /** | |
34 | * The fully qualified class name of the constraint name generator. | |
35 | */ | |
36 | 0 | public static final String CONSTRAINT_GENERATOR = ConstraintNameGenerator.class.getName(); |
37 | ||
38 | /** | |
39 | * The single instance of this class. | |
40 | */ | |
41 | 0 | private static NameFactory instance = new NameFactory(); |
42 | ||
43 | /** | |
44 | * The cache of <code>NameGenerator</code> algorithms in use for name generation, keyed by fully qualified class | |
45 | * name. | |
46 | */ | |
47 | private Hashtable algorithms; | |
48 | ||
49 | /** | |
50 | * Creates a new instance with storage for algorithm implementations. | |
51 | */ | |
52 | 0 | protected NameFactory() { |
53 | 0 | algorithms = new Hashtable(5); |
54 | 0 | } |
55 | ||
56 | /** | |
57 | * Factory method which retrieves an instance of the named generator. | |
58 | * | |
59 | * @param name | |
60 | * The fully qualified class name of the name generation algorithm to retrieve. | |
61 | * @return A name generator | |
62 | */ | |
63 | protected NameGenerator getAlgorithm(String name) throws EngineException { | |
64 | 0 | synchronized (algorithms) { |
65 | 0 | NameGenerator algorithm = (NameGenerator) algorithms.get(name); |
66 | 0 | if (algorithm == null) { |
67 | try { | |
68 | 0 | algorithm = (NameGenerator) Class.forName(name).newInstance(); |
69 | 0 | } catch (InstantiationException e) { |
70 | 0 | throw new EngineException("Unable to instantiate class " + name |
71 | + ": Make sure it's in your run-time classpath", e); | |
72 | 0 | } catch (Exception e) { |
73 | 0 | throw new EngineException(e); |
74 | 0 | } |
75 | 0 | algorithms.put(name, algorithm); |
76 | } | |
77 | 0 | return algorithm; |
78 | 0 | } |
79 | } | |
80 | ||
81 | /** | |
82 | * Given a list of <code>String</code> objects, implements an algorithm which produces a name. | |
83 | * | |
84 | * @param algorithmName | |
85 | * The fully qualified class name of the {@link org.apache.torque.engine.database.model.NameGenerator} | |
86 | * implementation to use to generate names. | |
87 | * @param inputs | |
88 | * Inputs used to generate a name. | |
89 | * @return The generated name. | |
90 | * @throws EngineException | |
91 | * an exception | |
92 | */ | |
93 | public static String generateName(String algorithmName, List inputs) throws EngineException { | |
94 | 0 | NameGenerator algorithm = instance.getAlgorithm(algorithmName); |
95 | 0 | return algorithm.generateName(inputs); |
96 | } | |
97 | } |