View Javadoc
1   /*
2    * Copyright 2005-2008 Werner Guttmann
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.exolab.castor.builder.printing;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.exolab.castor.builder.BuilderConfiguration;
24  
25  /**
26   * Registry for {@link JClassPrinterFactory} implementations obtained from the
27   * Castor XML code generator property file and used by the XML code generator during
28   * its operation.
29   * 
30   * @author <a href="mailto:Werner.Guttmann@morganstanley.com">Werner Guttmann</a>
31   * @version $Revision: 7134 $ $Date: 2006-04-08 08:58:10 -0600 (Sat, 08 Apr 2006) $
32   * @since 1.2.1
33   */
34  public class JClassPrinterFactoryRegistry {
35  
36      /** The <a href="http://jakarta.apache.org/commons/logging/">Jakarta
37       *  Commons Logging</a> instance used for all logging. */
38      private final Log _log = LogFactory.getLog(JClassPrinterFactoryRegistry.class);
39  
40      /** Association between name of implementation and JClassPrinterFactory instance. */
41      private Map<String, JClassPrinterFactory> _factories = new HashMap<String, JClassPrinterFactory>();
42  
43      /**
44       * Construct an instance of JClassPrinterFactoryRegistry that loads the
45       * {@link JClassPrinterFactory} implementations specified in the given
46       * Configuration.
47       * 
48       * @param config The LocalConfiguration.
49       */
50      public JClassPrinterFactoryRegistry(final BuilderConfiguration config) {
51          String jClassPrinterFactories = config.getJClassPrinterFactories();
52          String[] factoryClassNames = jClassPrinterFactories.split(",");
53          
54          for (int i = 0; i < factoryClassNames.length; i++) {
55              JClassPrinterFactory factory;
56              try {
57                  factory = (JClassPrinterFactory) 
58                      Class.forName(factoryClassNames[i]).newInstance();
59              } catch (InstantiationException e) {
60                  throw new IllegalStateException("Property entry '" 
61                          + factoryClassNames[i] + "' does"
62                          + " not represent a valid class name.");
63              } catch (IllegalAccessException e) {
64                  throw new IllegalStateException("Property entry '" 
65                          + factoryClassNames[i] + "' does" 
66                          + " not represent a valid class name.");
67              } catch (ClassNotFoundException e) {
68                  throw new IllegalStateException("Property entry '" 
69                          + factoryClassNames[i] + "' does" 
70                          + " not represent a valid class name.");
71              }
72              _factories.put(factory.getName(), factory);
73          }
74      }
75  
76      /**
77       * Returns the names of all the configured {@link JClassPrinterFactory}
78       * implementations. A {@link JClassPrinterFactory} instance can be obtained
79       * by the {@link #getJClassPrinterFactory} method.
80       *
81       * @return Names of {@link JClassPrinterFactory} implementations
82       */
83      public String[] getJClassPrinterFactoryNames() {
84          String[] names = new String[_factories.size()];
85          return _factories.keySet().toArray(names);
86      }
87  
88      /**
89       * Returns a {@link JClassPrinterFactory} with the specified name. Returns
90       * null if the named factory is not supported.
91       *
92       * @param name The name of the JClassPrinterFactory.
93       * @return The {@link JClassPrinterFactory} or null if none exists.
94       */
95      public JClassPrinterFactory getJClassPrinterFactory(final String name) {
96          Object factory = _factories.get(name);
97          if (factory == null) {
98              String msg = "The JClassPrinterFactory '" + name + "' "
99                         + "does not exist in the Castor XML code generator properties file "
100                        + "and is therefore not supported.";
101             _log.error(msg);
102             throw new IllegalArgumentException(msg);
103         }
104         
105         if (_log.isDebugEnabled()) {
106             _log.debug("Returning JClassPrinterFactory with name " + name);
107         }
108         return (JClassPrinterFactory) factory;
109     }
110 
111 }