KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > config > ConfigMgr


1 package de.webman.config;
2
3 import de.webman.util.registry.Manager;
4 import de.webman.util.registry.RegistryException;
5 import de.webman.util.log4j.WebmanCategory;
6 import org.apache.log4j.Category;
7 import com.teamkonzept.webman.db.TKWebmanDBManager;
8 import com.teamkonzept.lib.TKException;
9 import java.sql.SQLException JavaDoc;
10 import com.teamkonzept.db.TKPrepQuery;
11 import java.util.Locale JavaDoc;
12 import com.teamkonzept.lib.PropertyManager;
13 import com.teamkonzept.lib.ConfigurationListener;
14 import com.teamkonzept.lib.ConfigurationManager;
15
16 /**
17  * A manager which initializes and setups the configuration and database
18  * connection. It should be registered and loaded recently early, at least
19  * before any database access.<p>
20  *
21  * @author <a HREF="mailto:gregor@webman.de">Gregor Klinke</a>
22  * @version $Revision: 1.3 $
23  **/

24 public class ConfigMgr
25     implements Manager, ConfigurationListener
26 {
27     /* $Id $ */
28
29     /**
30      * logging facility
31      **/

32     private static Category cat = Category.getInstance(ConfigMgr.class);
33
34     /**
35      * the property group, the locale is configured in
36      **/

37     public static final String JavaDoc LOCALE_PROPERTY_GROUP = "WEBMAN";
38     
39     /**
40      * the constructor, only to be used by the related factory
41      * @param basedir directory from where to read the configuration
42      * @throws RegistryException if anything fails
43      **/

44     ConfigMgr(String JavaDoc basedir)
45         throws RegistryException
46     {
47         cat.info("setup database connection");
48         
49         try {
50             TKWebmanDBManager.initConnection("/webmandb.ini", true);
51
52             // call this once, otherwise prepared queries seems not to run
53
// (found in WebManThread)
54
TKPrepQuery.enableCleanup();
55         }
56         catch (TKException e) {
57             cat.error("Exception during pre init : " , e);
58             throw new RegistryException(e);
59         }
60
61         setDefaultLocale();
62         
63         /* deregister the current thread! This is not done automatically
64            by the webman backend code ..., really, we do need a better
65            solution herefore! */

66         try {
67             TKWebmanDBManager.deregister(true);
68         }
69         catch (SQLException JavaDoc e) {
70             throw new RegistryException(e);
71         }
72
73         ConfigurationManager
74             .getInstance().registerConfigurationListener(this,
75                                                          LOCALE_PROPERTY_GROUP);
76
77         cat.info("database connection successful");
78     }
79     
80     /**
81      * looks up an object from the manager.
82      *
83      * @param key the key under which the object is stored in the manager
84      * @return the found object or <code>null</code> if not found
85      * @throws RegistryException if anything fails
86      **/

87     public Object JavaDoc get(String JavaDoc key)
88         throws RegistryException
89     {
90         /* to wrap this and use the property manager, we need a scope, something like LEGACY */
91         // PropertyManager.getPropertyManager(scope).getValue(key);
92
return null;
93     }
94
95     /**
96      * looks up an object from the manager. If no such object found return a default value
97      *
98      * @param key the key under which the object is stored in the manager
99      * @param def the default object
100      * @return the found object or <code>null</code> if not found
101      * @throws RegistryException if anything fails
102      **/

103     public Object JavaDoc get(String JavaDoc key, Object JavaDoc def)
104         throws RegistryException
105     {
106         Object JavaDoc o = get(key);
107         return o != null ? o : def;
108     }
109     
110     /**
111      * sets a object in the manager.
112      *
113      * @param key the key under which the object is stored in the manager,
114      * must not be <code>null</code>
115      * @param obj the object to store
116      * @throws RegistryException if anything fails
117      **/

118     public void put(String JavaDoc key, Object JavaDoc obj)
119         throws RegistryException
120     {
121         // TODO
122
}
123     
124     
125     /**
126      * removes any object stored under a given key from the registry. If
127      * no object was stored under such a key nothing happens.
128      *
129      * @param key the key for which the object should be removed.
130      * @throws RegistryException if anything fails
131      **/

132     public void remove(String JavaDoc key)
133         throws RegistryException
134     {
135         // TODO
136
}
137
138
139     /* --------------------------------------------------------------------
140        functions to set the default local
141        -------------------------------------------------------------------- */

142
143     /**
144      * if the WEBMAN property group changes, this event is fired
145      * @throws TKException if anything fails
146      **/

147     public void configurationChanged ()
148         throws TKException
149     {
150         setDefaultLocale();
151     }
152
153     /**
154      * set the default locale according to the WEBMAN.LOCALE property
155      * setting
156      **/

157     private void setDefaultLocale() {
158         boolean lclset = false;
159         
160         try {
161             PropertyManager man = PropertyManager
162                 .getPropertyManager(LOCALE_PROPERTY_GROUP);
163             String JavaDoc lnm = man.getValue("LOCALE", "de_DE");
164             
165             if (lnm != null) {
166                 Locale JavaDoc defl = new Locale JavaDoc(getLocaleLanguagePart(lnm),
167                                          getLocaleCountryPart(lnm));
168                 
169                 Locale JavaDoc[] all = Locale.getAvailableLocales();
170                 for (int i = 0; i < all.length; i++) {
171                     if (all[i].equals(defl)) {
172                         Locale.setDefault(defl);
173                         
174                         cat.info("Requested default locale: '" + lnm + "', use: '" +
175                                  defl + "'");
176                         
177                         lclset = true;
178                     }
179                 }
180                 
181                 if (!lclset) {
182                     cat.error("Locale setting '" + defl + "' not known on system");
183                 }
184             }
185         }
186         catch (Exception JavaDoc e) {
187             ;
188         }
189         
190         if (!lclset) {
191             cat.warn("No locale setting, use system default: '" +
192                      Locale.getDefault() + "'");
193         }
194     }
195     
196     /**
197      * extracts the iso639 language part of a locale name
198      * @param localeName the locale name
199      * @return the language part or "" if none found
200      **/

201     private String JavaDoc getLocaleLanguagePart(String JavaDoc localeName) {
202         if (localeName != null) {
203             int idx = localeName.indexOf("_");
204             
205             if (idx > 0)
206                 return localeName.substring(0, idx);
207             return localeName;
208         }
209         return "";
210     }
211     
212     /**
213      * extracts the iso 3166 2 country part of a locale name
214      * @param localeName the locale name
215      * @return the country part or "" if none found
216      **/

217     private String JavaDoc getLocaleCountryPart(String JavaDoc localeName) {
218         if (localeName != null) {
219             int idx = localeName.indexOf("_");
220             
221             if (idx > 0)
222                 return localeName.substring(idx + 1);
223             return localeName;
224         }
225         return "";
226     }
227 }
228
229
Popular Tags