View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  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,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.jsieve;
21  
22  import java.util.Map;
23  
24  import org.apache.jsieve.comparators.Comparator;
25  import org.apache.jsieve.exception.LookupException;
26  
27  /**
28   * Maps Comparator names to configured Comparator implementation classes.
29   */
30  public class ComparatorManagerImpl implements ComparatorManager {
31  
32      private final Map classNameMap;
33  
34      /**
35       * Constructor for ComparatorManager.
36       */
37      public ComparatorManagerImpl(final Map classNameMap) {
38          super();
39          this.classNameMap = classNameMap;
40      }
41  
42      /**
43       * <p>
44       * Method lookup answers the class to which a Comparator name is mapped.
45       * </p>
46       * 
47       * @param name -
48       *            The name of the Comparator
49       * @return Class - The class of the Comparator
50       * @throws LookupException
51       */
52      public Class lookup(String name) throws LookupException {
53          Class comparatorClass = null;
54          try {
55              comparatorClass = getClass().getClassLoader().loadClass(
56                      getClassName(name));
57          } catch (ClassNotFoundException e) {
58              throw new LookupException("Comparator named '" + name
59                      + "' not found.");
60          }
61          if (!Comparator.class.isAssignableFrom(comparatorClass))
62              throw new LookupException("Class " + comparatorClass.getName()
63                      + " must implement " + Comparator.class.getName());
64          return comparatorClass;
65      }
66  
67      /**
68       * <p>
69       * Method newInstance answers an instance of the class to which a Comparator
70       * name is mapped.
71       * </p>
72       * 
73       * @param name -
74       *            The name of the Comparator
75       * @return Class - The class of the Comparator
76       * @throws LookupException
77       */
78      public Comparator getComparator(String name) throws LookupException {
79          try {
80              return (Comparator) lookup(name).newInstance();
81          } catch (InstantiationException e) {
82              throw new LookupException(e.getMessage());
83          } catch (IllegalAccessException e) {
84              throw new LookupException(e.getMessage());
85          }
86      }
87  
88      /**
89       * <p>
90       * Method getClassName answers the name of the class to which a Comparator
91       * name is mapped.
92       * </p>
93       * 
94       * @param name -
95       *            The name of the Comparator
96       * @return String - The name of the class
97       * @throws LookupException
98       */
99      private String getClassName(String name) throws LookupException {
100         String className = (String) classNameMap.get(name.toLowerCase());
101         if (null == className)
102             throw new LookupException("Command named '" + name
103                     + "' not mapped.");
104         return className;
105     }
106 }