View Javadoc

1   package org.apache.torque.engine.database.model;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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,v 1.1 2007-10-21 07:57:27 abyrne Exp $
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 }