KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > engine > database > model > NameFactory


1 package org.apache.torque.engine.database.model;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.Hashtable JavaDoc;
20 import java.util.List JavaDoc;
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,v 1.6 2004/02/22 06:27:19 jmcnally Exp $
29  */

30 public class NameFactory
31 {
32     /**
33      * The fully qualified class name of the Java name generator.
34      */

35     public static final String JavaDoc 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 JavaDoc 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 JavaDoc algorithms;
54
55     /**
56      * Creates a new instance with storage for algorithm implementations.
57      */

58     protected NameFactory()
59     {
60         algorithms = new Hashtable JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc generateName(String JavaDoc algorithmName, List JavaDoc inputs)
110         throws EngineException
111     {
112         NameGenerator algorithm = instance.getAlgorithm(algorithmName);
113         return algorithm.generateName(inputs);
114     }
115 }
116
Popular Tags