KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > visitor > PropertiesVisitorImpl


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22
23 package org.jboss.test.visitor;
24
25 import java.io.InputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.lang.reflect.Field JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import org.jboss.logging.Logger;
34
35 /**
36  * A TypeVisitor which builds a map of type, method and field properties
37  *
38  * @author Scott.Stark@jboss.org
39  * @version $Revision: 56504 $
40  */

41 public class PropertiesVisitorImpl implements TypeVisitor
42 {
43    private static Logger log = Logger.getLogger(PropertiesVisitorImpl.class);
44    /** HashMap<Class, Properties> */
45    private HashMap JavaDoc<Class JavaDoc, Properties JavaDoc> typeProperties = new HashMap JavaDoc<Class JavaDoc, Properties JavaDoc>();
46    /** HashMap<String, Properties> */
47    private HashMap JavaDoc<String JavaDoc, Properties JavaDoc> methodProperties = new HashMap JavaDoc<String JavaDoc, Properties JavaDoc>();
48    /** HashMap<String, Properties> */
49    private HashMap JavaDoc<String JavaDoc, Properties JavaDoc> fieldProperties = new HashMap JavaDoc<String JavaDoc, Properties JavaDoc>();
50
51    /**
52     * @return HashMap<Class, Properties>
53     */

54    public HashMap JavaDoc<Class JavaDoc, Properties JavaDoc> getTypeProperties()
55    {
56       return typeProperties;
57    }
58
59    /**
60     * @return HashMap<String, Properties>
61     */

62    public HashMap JavaDoc<String JavaDoc, Properties JavaDoc> getMethodProperties()
63    {
64       return methodProperties;
65    }
66    
67    /**
68     * @return HashMap<String, Properties>
69     */

70    public HashMap JavaDoc<String JavaDoc, Properties JavaDoc> getFieldProperties()
71    {
72       return fieldProperties;
73    }
74
75    // Begin TypeVisitor interface
76

77    /**
78     * Look for a properties file for the type using
79     * @param type
80     */

81    public void visitClass(Class JavaDoc type)
82    {
83       String JavaDoc className = type.getName();
84       int dot = className.lastIndexOf('.');
85       if( dot >= 0 )
86          className = className.substring(dot+1);
87       String JavaDoc name = className + ".properties";
88       loadProperties(type, type, name, typeProperties);
89    }
90
91    /**
92     * Calls visitClass(i) for each ifaces[n]
93     * @param ifaces
94     */

95    public void visitInterfaces(Class JavaDoc[] ifaces)
96    {
97       for(int n = 0; n < ifaces.length; n ++)
98       {
99          Class JavaDoc i = ifaces[n];
100          visitClass(i);
101       }
102    }
103
104    public void visitMethods(Method JavaDoc[] methods)
105    {
106       for(int n = 0; n < methods.length; n ++)
107       {
108          Method JavaDoc m = methods[n];
109          String JavaDoc name = m.getName();
110          Class JavaDoc type = m.getDeclaringClass();
111          loadProperties(type, name, name, methodProperties);
112       }
113    }
114
115    public void visitFields(Field JavaDoc[] fields)
116    {
117       for(int n = 0; n < fields.length; n ++)
118       {
119          Field JavaDoc f = fields[n];
120          String JavaDoc name = f.getName();
121          Class JavaDoc type = f.getDeclaringClass();
122          loadProperties(type, name, name, fieldProperties);
123       }
124    }
125    // End TypeVisitor interface
126

127    /**
128     *
129     * @param <T> the key type
130     * @param type - the class used to load the name resource via
131     * Class.getResource(name)
132     * @param key - the key into map
133     * @param name - the resource name to search for
134     * @param map - Map<key, Properties> to populate
135     */

136    protected <T> void loadProperties(Class JavaDoc type, T key, String JavaDoc name, HashMap JavaDoc<T, Properties JavaDoc> map)
137    {
138       URL JavaDoc res = type.getResource(name);
139       if( res != null )
140       {
141          Properties JavaDoc props = map.get(key);
142          if( props == null )
143          {
144             props = new Properties JavaDoc();
145             map.put(key, props);
146          }
147          try
148          {
149             InputStream JavaDoc is = res.openStream();
150             props.load(is);
151             is.close();
152          }
153          catch (IOException JavaDoc e)
154          {
155             log.warn("Failed to load properties for name: "+name, e);
156          }
157          // FIXME - this is to ignore problems when it tries to load directories as property files?
158
catch (NullPointerException JavaDoc e)
159          {
160             log.warn("Failed to load properties for name: "+name, e);
161          }
162       }
163    }
164 }
165
Popular Tags