001: package de.webman.config;
002:
003: import de.webman.util.registry.Manager;
004: import de.webman.util.registry.RegistryException;
005: import de.webman.util.log4j.WebmanCategory;
006: import org.apache.log4j.Category;
007: import com.teamkonzept.webman.db.TKWebmanDBManager;
008: import com.teamkonzept.lib.TKException;
009: import java.sql.SQLException;
010: import com.teamkonzept.db.TKPrepQuery;
011: import java.util.Locale;
012: import com.teamkonzept.lib.PropertyManager;
013: import com.teamkonzept.lib.ConfigurationListener;
014: import com.teamkonzept.lib.ConfigurationManager;
015:
016: /**
017: * A manager which initializes and setups the configuration and database
018: * connection. It should be registered and loaded recently early, at least
019: * before any database access.<p>
020: *
021: * @author <a href="mailto:gregor@webman.de">Gregor Klinke</a>
022: * @version $Revision: 1.3 $
023: **/
024: public class ConfigMgr implements Manager, ConfigurationListener {
025: /* $Id $ */
026:
027: /**
028: * logging facility
029: **/
030: private static Category cat = Category.getInstance(ConfigMgr.class);
031:
032: /**
033: * the property group, the locale is configured in
034: **/
035: public static final String LOCALE_PROPERTY_GROUP = "WEBMAN";
036:
037: /**
038: * the constructor, only to be used by the related factory
039: * @param basedir directory from where to read the configuration
040: * @throws RegistryException if anything fails
041: **/
042: ConfigMgr(String basedir) throws RegistryException {
043: cat.info("setup database connection");
044:
045: try {
046: TKWebmanDBManager.initConnection("/webmandb.ini", true);
047:
048: // call this once, otherwise prepared queries seems not to run
049: // (found in WebManThread)
050: TKPrepQuery.enableCleanup();
051: } catch (TKException e) {
052: cat.error("Exception during pre init : ", e);
053: throw new RegistryException(e);
054: }
055:
056: setDefaultLocale();
057:
058: /* deregister the current thread! This is not done automatically
059: by the webman backend code ..., really, we do need a better
060: solution herefore! */
061: try {
062: TKWebmanDBManager.deregister(true);
063: } catch (SQLException e) {
064: throw new RegistryException(e);
065: }
066:
067: ConfigurationManager.getInstance()
068: .registerConfigurationListener(this ,
069: LOCALE_PROPERTY_GROUP);
070:
071: cat.info("database connection successful");
072: }
073:
074: /**
075: * looks up an object from the manager.
076: *
077: * @param key the key under which the object is stored in the manager
078: * @return the found object or <code>null</code> if not found
079: * @throws RegistryException if anything fails
080: **/
081: public Object get(String key) throws RegistryException {
082: /* to wrap this and use the property manager, we need a scope, something like LEGACY */
083: // PropertyManager.getPropertyManager(scope).getValue(key);
084: return null;
085: }
086:
087: /**
088: * looks up an object from the manager. If no such object found return a default value
089: *
090: * @param key the key under which the object is stored in the manager
091: * @param def the default object
092: * @return the found object or <code>null</code> if not found
093: * @throws RegistryException if anything fails
094: **/
095: public Object get(String key, Object def) throws RegistryException {
096: Object o = get(key);
097: return o != null ? o : def;
098: }
099:
100: /**
101: * sets a object in the manager.
102: *
103: * @param key the key under which the object is stored in the manager,
104: * must not be <code>null</code>
105: * @param obj the object to store
106: * @throws RegistryException if anything fails
107: **/
108: public void put(String key, Object obj) throws RegistryException {
109: // TODO
110: }
111:
112: /**
113: * removes any object stored under a given key from the registry. If
114: * no object was stored under such a key nothing happens.
115: *
116: * @param key the key for which the object should be removed.
117: * @throws RegistryException if anything fails
118: **/
119: public void remove(String key) throws RegistryException {
120: // TODO
121: }
122:
123: /* --------------------------------------------------------------------
124: functions to set the default local
125: -------------------------------------------------------------------- */
126:
127: /**
128: * if the WEBMAN property group changes, this event is fired
129: * @throws TKException if anything fails
130: **/
131: public void configurationChanged() throws TKException {
132: setDefaultLocale();
133: }
134:
135: /**
136: * set the default locale according to the WEBMAN.LOCALE property
137: * setting
138: **/
139: private void setDefaultLocale() {
140: boolean lclset = false;
141:
142: try {
143: PropertyManager man = PropertyManager
144: .getPropertyManager(LOCALE_PROPERTY_GROUP);
145: String lnm = man.getValue("LOCALE", "de_DE");
146:
147: if (lnm != null) {
148: Locale defl = new Locale(getLocaleLanguagePart(lnm),
149: getLocaleCountryPart(lnm));
150:
151: Locale[] all = Locale.getAvailableLocales();
152: for (int i = 0; i < all.length; i++) {
153: if (all[i].equals(defl)) {
154: Locale.setDefault(defl);
155:
156: cat.info("Requested default locale: '" + lnm
157: + "', use: '" + defl + "'");
158:
159: lclset = true;
160: }
161: }
162:
163: if (!lclset) {
164: cat.error("Locale setting '" + defl
165: + "' not known on system");
166: }
167: }
168: } catch (Exception e) {
169: ;
170: }
171:
172: if (!lclset) {
173: cat.warn("No locale setting, use system default: '"
174: + Locale.getDefault() + "'");
175: }
176: }
177:
178: /**
179: * extracts the iso639 language part of a locale name
180: * @param localeName the locale name
181: * @return the language part or "" if none found
182: **/
183: private String getLocaleLanguagePart(String localeName) {
184: if (localeName != null) {
185: int idx = localeName.indexOf("_");
186:
187: if (idx > 0)
188: return localeName.substring(0, idx);
189: return localeName;
190: }
191: return "";
192: }
193:
194: /**
195: * extracts the iso 3166 2 country part of a locale name
196: * @param localeName the locale name
197: * @return the country part or "" if none found
198: **/
199: private String getLocaleCountryPart(String localeName) {
200: if (localeName != null) {
201: int idx = localeName.indexOf("_");
202:
203: if (idx > 0)
204: return localeName.substring(idx + 1);
205: return localeName;
206: }
207: return "";
208: }
209: }
|