View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  
16  package org.esigate.extension;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.List;
21  import java.util.Properties;
22  
23  import org.esigate.ConfigurationException;
24  import org.esigate.Driver;
25  import org.esigate.util.Parameter;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  /**
30   * Factory for all ESIGate extension classes (authenticator, cookie store).
31   * 
32   * @author Nicolas Richeton
33   * @author Francois-Xavier Bonnet
34   * 
35   */
36  public final class ExtensionFactory {
37      private static final Logger LOG = LoggerFactory.getLogger(ExtensionFactory.class);
38  
39      private ExtensionFactory() {
40  
41      }
42  
43      /**
44       * Get an extension as configured in properties.
45       * 
46       * @param properties
47       *            properties
48       * @param parameter
49       *            the class name parameter
50       * @param d
51       *            driver
52       * 
53       * @param <T>
54       *            class which extends Extension class which extends Extension
55       * @return instance of {@link Extension} or null.
56       */
57      public static <T extends Extension> T getExtension(Properties properties, Parameter<String> parameter, Driver d) {
58          T result;
59          String className = parameter.getValue(properties);
60          if (className == null) {
61              return null;
62          }
63          try {
64              if (LOG.isDebugEnabled()) {
65                  LOG.debug("Creating  extension " + className);
66              }
67              result = (T) Class.forName(className).newInstance();
68              result.init(d, properties);
69          } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
70              throw new ConfigurationException(e);
71          }
72          return result;
73      }
74  
75      /**
76       * Get an extension list, as defined in the properties (comma-separated list).
77       * 
78       * @param properties
79       * @param parameter
80       * @param d
81       * @return the extension list
82       */
83      public static <T extends Extension> List<T> getExtensions(Properties properties,
84              Parameter<Collection<String>> parameter, Driver d) {
85          Collection<String> className = parameter.getValue(properties);
86          List<T> finalResult = new ArrayList<>();
87          for (String cName : className) {
88              try {
89                  T result;
90  
91                  if (LOG.isDebugEnabled()) {
92                      LOG.debug("Creating  extension " + className);
93                  }
94                  result = (T) Class.forName(cName).newInstance();
95                  result.init(d, properties);
96                  finalResult.add(result);
97              } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
98                  throw new ConfigurationException(e);
99              }
100         }
101         return finalResult;
102     }
103 
104 }