View Javadoc

1   /*
2    * $Id: ModuleConfigFactory.java 421119 2006-07-12 04:49:11Z wsmoak $
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  package org.apache.struts.config;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.struts.util.RequestUtils;
23  
24  /***
25   * A factory interface for creating {@link ModuleConfig}s.
26   *
27   * @version $Rev: 421119 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
28   *          $
29   * @see ModuleConfig
30   */
31  public abstract class ModuleConfigFactory {
32      /***
33       * The Java class to be used for <code>ModuleConfigFactory</code>
34       * instances.
35       */
36      protected static Class clazz = null;
37  
38      /***
39       * Commons Logging instance.
40       */
41      private static final Log LOG = LogFactory.getLog(ModuleConfigFactory.class);
42  
43      /***
44       * The fully qualified class name to be used for <code>ModuleConfigFactory</code>
45       * instances.
46       */
47      protected static String factoryClass =
48          "org.apache.struts.config.impl.DefaultModuleConfigFactory";
49  
50      /***
51       * Create and return a newly instansiated {@link ModuleConfig}. This
52       * method must be implemented by concrete subclasses.
53       *
54       * @param prefix Module prefix for Configuration
55       */
56      public abstract ModuleConfig createModuleConfig(String prefix);
57  
58      // ------------------------------------------------------ Static Properties
59  
60      /***
61       * The fully qualified class name that is used for <code>ModuleConfigFactory</code>
62       * instances.
63       *
64       * @return class name that is used for <code>ModuleConfigFactory</code>
65       *         instances
66       */
67      public static String getFactoryClass() {
68          return (ModuleConfigFactory.factoryClass);
69      }
70  
71      /***
72       * Set the fully qualified class name that is used for
73       * <code>ModuleConfigFactory</code> instances.
74       *
75       * @param factoryClass name that is used for <code>ModuleConfigFactory</code>
76       *                     instances
77       */
78      public static void setFactoryClass(String factoryClass) {
79          ModuleConfigFactory.factoryClass = factoryClass;
80          ModuleConfigFactory.clazz = null;
81      }
82  
83      // --------------------------------------------------------- Static Methods
84  
85      /***
86       * Create and return a <code>ModuleConfigFactory</code> instance of the
87       * appropriate class, which can be used to create customized
88       * <code>ModuleConfig</code> instances.  If no such factory can be
89       * created, return <code>null</code> instead.
90       */
91      public static ModuleConfigFactory createFactory() {
92          ModuleConfigFactory factory = null;
93  
94          try {
95              if (clazz == null) {
96                  clazz = RequestUtils.applicationClass(factoryClass);
97              }
98  
99              factory = (ModuleConfigFactory) clazz.newInstance();
100         } catch (ClassNotFoundException e) {
101             LOG.error("ModuleConfigFactory.createFactory()", e);
102         } catch (InstantiationException e) {
103             LOG.error("ModuleConfigFactory.createFactory()", e);
104         } catch (IllegalAccessException e) {
105             LOG.error("ModuleConfigFactory.createFactory()", e);
106         }
107 
108         return factory;
109     }
110 }