View Javadoc

1   /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartWeb Framework please visit
20   *
21   *                        http://smartweb.sourceforge.net
22   */
23  package net.smartlab.web;
24  
25  import java.lang.reflect.Constructor;
26  import java.net.URL;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.Map;
30  import java.util.Properties;
31  import java.util.TreeMap;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  /**
37   * This class allows to use a properties file to define enumeration elements but
38   * needs the definition of two public constructors. Implementors of this class need to
39   * define two constructors: one without parameters which simply calls
40   * <code>super()</code> and an additional one taking two strings and calling
41   * <code>super(String, String)</code>.
42   * 
43   * @author rlogiacco
44   */
45  public abstract class PropertiesEnumeration extends StringEnumeration {
46  
47  	private static final long serialVersionUID = 228095287903246251L;
48  
49  	private static final Class[] TYPES = new Class[] {String.class, String.class};
50  
51  	private static Map enumerations = new HashMap();
52  
53  	/**
54  	 * Provides logging capabilities.
55  	 */
56  	protected final static Log logger = LogFactory.getLog(PropertiesEnumeration.class);
57  
58  
59  	/**
60  	 * The empty public constructor which MUST be exposed by subclasses.
61  	 */
62  	public PropertiesEnumeration() {
63  		super();
64  	}
65  
66  	/**
67  	 * The parametrized public constructor which MUST be exposed by subclasses.
68  	 *
69  	 * @param code the enumeration element code
70  	 * @param display the enumeration element display name
71  	 */
72  	public PropertiesEnumeration(String code, String display) {
73  		super(code, display);
74  	}
75  
76  	/**
77  	 * @see net.smartlab.web.StringEnumeration#decode(java.lang.String)
78  	 */
79  	public StringEnumeration decode(String code) {
80  		if (logger.isDebugEnabled()) {
81  			logger.debug("decode(\"" + code + "\") - start");
82  		}
83  		Map values = (Map)enumerations.get(this.getClass());
84  		if (values == null) {
85  			synchronized (this.getClass()) {
86  				try {
87  					String classname = this.getClass().getName();
88  					URL url = this.getClass().getResource(
89  							classname.substring(classname.lastIndexOf('.') + 1) + ".properties");
90  					Properties properties = new Properties();
91  					properties.load(url.openStream());
92  					Iterator entries = properties.entrySet().iterator();
93  					Constructor instantiator = this.getClass().getConstructor(TYPES);
94  					values = new TreeMap();
95  					while (entries.hasNext()) {
96  						Map.Entry entry = (Map.Entry)entries.next();
97  						if (logger.isTraceEnabled()) {
98  							logger.trace("   adding (" + entry.getKey() + ", " + entry.getValue() + ")");
99  						}
100 						values.put(entry.getKey(), instantiator.newInstance(new Object[] {entry.getKey(),
101 								entry.getValue()}));
102 					}
103 					enumerations.put(this.getClass(), values);
104 				} catch (NoSuchMethodException nsme) {
105 					logger.fatal("missing constructor " + this.getClass().getName() + "(String, String)", nsme);
106 				} catch (Exception e) {
107 					logger.error("decode() - failed", e);
108 				}
109 			}
110 		}
111 		return (StringEnumeration)values.get(code);
112 	}
113 }