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   * Singleton class <code>ComparatorManager</code> maps Comparator names to
29   * configured Comparator implementation classes.
30   */
31  public class ComparatorManager {
32      /**
33       * The sole instance of the receiver.
34       */
35      static private ComparatorManager fieldInstance;
36  
37      /**
38       * Constructor for ComparatorManager.
39       */
40      private ComparatorManager() {
41          super();
42      }
43  
44      /**
45       * Returns the sole instance of the receiver, lazily initialised if
46       * required.
47       * 
48       * @return ComparatorManager
49       */
50      public static synchronized ComparatorManager getInstance() {
51          ComparatorManager current = null;
52          if (null == (current = getInstanceBasic())) {
53              updateInstance();
54              return getInstance();
55          }
56          return current;
57      }
58  
59      /**
60       * Returns the sole instance of the receiver.
61       * 
62       * @return ComparatorManager
63       */
64      private static ComparatorManager getInstanceBasic() {
65          return fieldInstance;
66      }
67  
68      /**
69       * Computes a new instance of the receiver.
70       * 
71       * @return ComparatorManager
72       */
73      protected static ComparatorManager computeInstance() {
74          return new ComparatorManager();
75      }
76  
77      /**
78       * Sets the sole instance.
79       * 
80       * @param instance
81       *                The current instance to set
82       */
83      protected static void setInstance(ComparatorManager instance) {
84          fieldInstance = instance;
85      }
86  
87      /**
88       * Resets the sole instance.
89       */
90      public static void resetInstance() {
91          setInstance(null);
92      }
93  
94      /**
95       * Updates the sole instance.
96       */
97      protected static void updateInstance() {
98          setInstance(computeInstance());
99      }
100 
101     /**
102      * <p>
103      * Method lookup answers the class to which a Comparator name is mapped.
104      * </p>
105      * 
106      * @param name -
107      *                The name of the Comparator
108      * @return Class - The class of the Comparator
109      * @throws LookupException
110      */
111     public Class lookup(String name) throws LookupException {
112         Class comparatorClass = null;
113         try {
114             comparatorClass = getClass().getClassLoader().loadClass(
115                     getClassName(name));
116         } catch (ClassNotFoundException e) {
117             throw new LookupException("Comparator named '" + name
118                     + "' not found.");
119         }
120         if (!Comparator.class.isAssignableFrom(comparatorClass))
121             throw new LookupException("Class " + comparatorClass.getName()
122                     + " must implement " + Comparator.class.getName());
123         return comparatorClass;
124     }
125 
126     /**
127      * <p>
128      * Method newInstance answers an instance of the class to which a Comparator
129      * name is mapped.
130      * </p>
131      * 
132      * @param name -
133      *                The name of the Comparator
134      * @return Class - The class of the Comparator
135      * @throws LookupException
136      */
137     public Comparator newInstance(String name) throws LookupException {
138         try {
139             return (Comparator) lookup(name).newInstance();
140         } catch (InstantiationException e) {
141             throw new LookupException(e.getMessage());
142         } catch (IllegalAccessException e) {
143             throw new LookupException(e.getMessage());
144         }
145     }
146 
147     /**
148      * <p>
149      * Method getClassName answers the name of the class to which a Comparator
150      * name is mapped.
151      * </p>
152      * 
153      * @param name -
154      *                The name of the Comparator
155      * @return String - The name of the class
156      * @throws LookupException
157      */
158     protected String getClassName(String name) throws LookupException {
159         String className;
160         try {
161             className = (String) getClassNameMap().get(name.toLowerCase());
162         } catch (SieveConfigurationException e) {
163             throw new LookupException(
164                     "Lookup failed due to a Configuration Exception: "
165                             + e.getMessage());
166         }
167         if (null == className)
168             throw new LookupException("Command named '" + name
169                     + "' not mapped.");
170         return className;
171     }
172 
173     /**
174      * Method getClassNameMap answers a Map of Comparator names and their class
175      * names.
176      * 
177      * @return Map
178      * @throws SieveConfigurationException
179      */
180     protected Map getClassNameMap() throws SieveConfigurationException {
181         return ConfigurationManager.getInstance().getComparatorMap();
182     }
183 
184 }