View Javadoc

1   /* Copyright (c) 2008 Sascha Kohlmann
2    *
3    * This program is free software: you can redistribute it and/or modify
4    * it under the terms of the GNU Affero General Public License as published by
5    * the Free Software Foundation, either version 3 of the License, or
6    * (at your option) any later version.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Affero General Public License for more details.
12   *
13   * You should have received a copy of the GNU Affero General Public License
14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15   */
16  package net.sf.eos.entity;
17  
18  import static net.sf.eos.config.ConfigurationKey.Type.CLASSNAME;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  import java.util.Locale;
24  
25  import net.sf.eos.EosException;
26  import net.sf.eos.config.Configuration;
27  import net.sf.eos.config.ConfigurationKey;
28  import net.sf.eos.config.Configured;
29  import net.sf.eos.config.FactoryMethod;
30  
31  /**
32   * The common name resolver maps an ID of an entity to the common name of
33   * the entity.
34   * @author Sascha Kohlmann
35   */
36  public abstract class CommonNameResolver extends Configured {
37  
38      /** For logging. */
39      private static final Log LOG =
40          LogFactory.getLog(CommonNameResolver.class.getName());
41  
42      /** The configuration key name for the classname of the factory.
43       * @see #newInstance(Configuration) */
44      @SuppressWarnings("nls")
45      @ConfigurationKey(type=CLASSNAME,
46                              description="Resolver remaps an entity ID to a "
47                                          + "common name (human readable).")
48      public final static String COMMON_NAME_RESOLVER_IMPL_CONFIG_NAME =
49              "net.sf.eos.entity.CommonNameResolver.impl";
50  
51      /**
52       * Creates a new instance of a of the recognizer. If the
53       * {@code Configuration} contains a key
54       * {@link #COMMON_NAME_RESOLVER_IMPL_CONFIG_NAME}
55       * a new instance of the classname in the value will instantiate.
56       * @param config the configuration
57       * @return a new instance
58       * @throws EosException if it is not possible to instantiate an instance
59       */
60      @FactoryMethod(key=COMMON_NAME_RESOLVER_IMPL_CONFIG_NAME)
61      public final static CommonNameResolver newInstance(final Configuration config)
62                  throws EosException {
63  
64          final Thread t = Thread.currentThread();
65          ClassLoader classLoader = t.getContextClassLoader();
66          if (classLoader == null) {
67              classLoader =
68                  AbstractDictionaryBasedEntityRecognizer.class.getClassLoader();
69          }
70  
71          final String clazzName = config.get(COMMON_NAME_RESOLVER_IMPL_CONFIG_NAME);
72  
73          try {
74              final Class<? extends CommonNameResolver> clazz = 
75                  (Class<? extends CommonNameResolver>) 
76                      Class.forName(clazzName, true, classLoader);
77              try {
78  
79                  final CommonNameResolver instance = clazz.newInstance();
80                  if (LOG.isDebugEnabled()) {
81                      LOG.debug("CommonNameResolver instance: "
82                                + instance.getClass().getName());
83                  }
84                  instance.configure(config);
85                  return instance;
86  
87              } catch (final InstantiationException e) {
88                  throw new EosException(e);
89              } catch (final IllegalAccessException e) {
90                  throw new EosException(e);
91              }
92          } catch (final ClassNotFoundException e) {
93              throw new EosException(e);
94          }
95      }
96  
97      /**
98       * Resolves the common entity name for the default locale of the platform.
99       * @param id the ID of the entity
100      * @return a common name or {@code null} if the implementation is
101      *         unable to resolve a common name.
102      * @throws EosException if an error occurs
103      */
104     public String resolveCommonName(final String id) throws EosException {
105         final Locale locale = Locale.getDefault();
106         return resolveCommonName(id, locale);
107     }
108 
109     /**
110      * Resolves the common entity name for the given locale if available.
111      * @param id the ID of the entity
112      * @param locale to get the right language of the common name.
113      * @return a common name or {@code null} if the implementation is
114      *         unable to resolve a common name.
115      * @throws EosException if an error occurs
116      */
117     public abstract String resolveCommonName(final String id,
118                                              final Locale locale) throws EosException;
119 }