KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > CoreMessageResources


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.core;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 import org.apache.struts.util.MessageResourcesFactory;
30 import org.apache.struts.util.PropertyMessageResources;
31
32 /**
33  * Extension of the standard struts {@link org.apache.struts.util.PropertyMessageResources}
34  * that is capable of loading resources from SSL-Explorer plugins.
35  *
36  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
37  * @see com.sslexplorer.core.CoreMessageResourcesFactory
38  */

39 public class CoreMessageResources extends PropertyMessageResources {
40     
41     // Private instance variables
42

43     private ClassLoader JavaDoc classLoader;
44
45     /**
46      * Constructor
47      *
48      * @param factory factory
49      * @param config config
50      * @param classLoader class loader
51      */

52     public CoreMessageResources(MessageResourcesFactory factory, String JavaDoc config, ClassLoader JavaDoc classLoader) {
53         super(factory, config);
54         this.classLoader = classLoader;
55     }
56
57     /* (non-Javadoc)
58      * @see org.apache.struts.util.PropertyMessageResources#loadLocale(java.lang.String)
59      */

60     protected synchronized void loadLocale(String JavaDoc localeKey) {
61
62         if (log.isTraceEnabled()) {
63             log.trace("loadLocale(" + localeKey + ")");
64         }
65         
66         // Have we already attempted to load messages for this locale?
67
if (locales.get(localeKey) != null) {
68             return;
69         }
70         
71         locales.put(localeKey, localeKey);
72
73         // Set up to load the property resource for this locale key, if we can
74
String JavaDoc name = config.replace('.', '/');
75         if (localeKey.length() > 0) {
76             name += "_" + localeKey;
77         }
78         
79         name += ".properties";
80         InputStream JavaDoc is = null;
81         Properties JavaDoc props = new Properties JavaDoc();
82
83         // Load the specified property resource
84
if (log.isTraceEnabled()) {
85             log.trace(" Loading resource '" + name + "'");
86         }
87                 
88         is = classLoader.getResourceAsStream(name);
89         if (is != null) {
90             try {
91                 props.load(is);
92                 
93             } catch (IOException JavaDoc e) {
94                 log.error("loadLocale()", e);
95             } finally {
96                 try {
97                     is.close();
98                 } catch (IOException JavaDoc e) {
99                     log.error("loadLocale()", e);
100                 }
101             }
102         }
103         else {
104             if (log.isDebugEnabled())
105                 log.debug("Failed to locate message resources " + name + ".");
106         }
107         
108         if (log.isTraceEnabled()) {
109             log.trace(" Loading resource completed");
110         }
111
112         // Copy the corresponding values into our cache
113
if (props.size() < 1) {
114             return;
115         }
116         
117         synchronized (messages) {
118             Iterator JavaDoc names = props.keySet().iterator();
119             while (names.hasNext()) {
120                 String JavaDoc key = (String JavaDoc) names.next();
121                 if (log.isTraceEnabled()) {
122                     log.trace(" Saving message key '" + messageKey(localeKey, key));
123                 }
124                 messages.put(messageKey(localeKey, key), props.getProperty(key));
125             }
126         }
127
128     }
129
130
131     /**
132      * Get an iterator of all message keys
133      *
134      * @return message keys iterator
135      */

136     public Iterator JavaDoc keys() {
137         return messages.keySet().iterator();
138     }
139
140     /**
141      * Remove a message from the resources
142      *
143      * @param key key of message
144      */

145     public void removeKey(String JavaDoc key) {
146         messages.remove(key);
147     }
148
149     /**
150      * Get a map of all locales
151      *
152      * @return map of locales
153      */

154     public HashMap JavaDoc getLocales() {
155         return locales;
156     }
157     
158     /**
159      * Set the value of a message given its locale and key.
160      *
161      * @param locale locale
162      * @param key key
163      * @param value value
164      */

165     public void setMessage(Locale JavaDoc locale, String JavaDoc key, String JavaDoc value) {
166         setMessage(localeKey(locale), key, value);
167     }
168     
169     /**
170      * Set the value of a message given its locale key and message key.
171      *
172      * @param localeKey locale key
173      * @param key key
174      * @param value value
175      */

176     public void setMessage(String JavaDoc localeKey, String JavaDoc key, String JavaDoc value) {
177         messages.put(messageKey(localeKey == null ? "" : localeKey, key), value);
178     }
179
180 }
181
Popular Tags