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