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 ; 10 import com.teamkonzept.db.TKPrepQuery; 11 import java.util.Locale ; 12 import com.teamkonzept.lib.PropertyManager; 13 import com.teamkonzept.lib.ConfigurationListener; 14 import com.teamkonzept.lib.ConfigurationManager; 15 16 24 public class ConfigMgr 25 implements Manager, ConfigurationListener 26 { 27 28 29 32 private static Category cat = Category.getInstance(ConfigMgr.class); 33 34 37 public static final String LOCALE_PROPERTY_GROUP = "WEBMAN"; 38 39 44 ConfigMgr(String basedir) 45 throws RegistryException 46 { 47 cat.info("setup database connection"); 48 49 try { 50 TKWebmanDBManager.initConnection("/webmandb.ini", true); 51 52 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 66 try { 67 TKWebmanDBManager.deregister(true); 68 } 69 catch (SQLException 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 87 public Object get(String key) 88 throws RegistryException 89 { 90 91 return null; 93 } 94 95 103 public Object get(String key, Object def) 104 throws RegistryException 105 { 106 Object o = get(key); 107 return o != null ? o : def; 108 } 109 110 118 public void put(String key, Object obj) 119 throws RegistryException 120 { 121 } 123 124 125 132 public void remove(String key) 133 throws RegistryException 134 { 135 } 137 138 139 142 143 147 public void configurationChanged () 148 throws TKException 149 { 150 setDefaultLocale(); 151 } 152 153 157 private void setDefaultLocale() { 158 boolean lclset = false; 159 160 try { 161 PropertyManager man = PropertyManager 162 .getPropertyManager(LOCALE_PROPERTY_GROUP); 163 String lnm = man.getValue("LOCALE", "de_DE"); 164 165 if (lnm != null) { 166 Locale defl = new Locale (getLocaleLanguagePart(lnm), 167 getLocaleCountryPart(lnm)); 168 169 Locale [] 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 e) { 187 ; 188 } 189 190 if (!lclset) { 191 cat.warn("No locale setting, use system default: '" + 192 Locale.getDefault() + "'"); 193 } 194 } 195 196 201 private String getLocaleLanguagePart(String 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 217 private String getLocaleCountryPart(String 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 |