1 29 38 39 package org.planetamessenger.mos.engine; 40 41 import org.planetamessenger.security.*; 42 import org.planetamessenger.io.*; 43 import org.planetamessenger.db.*; 44 45 import java.sql.*; 46 import java.sql.Connection ; 47 import java.sql.PreparedStatement ; 48 import java.sql.ResultSet ; 49 import java.util.jar.*; 50 import java.util.*; 51 import java.io.*; 52 53 54 public class JLanguageManager { 55 56 public static final String DEFAULT_LANGUAGE = "english_us"; 57 public static final String DEFAULT_LOCALE = "en"; 58 private final String LANGUAGE_ATTRIBUTE = "Language"; 59 private final String OWNER_ATTRIBUTE = "Owner"; 60 private final String LICENSE_ATTRIBUTE = "License"; 61 private final String HOMEPAGE_ATTRIBUTE = "HomePage"; 62 private final String DESCRIPTION_ATTRIBUTE = "Description"; 63 private final String VERSION_ATTRIBUTE = "Version"; 64 65 private JJarClassLoader languageClassLoader = null; 66 private JDatabase database = null; 67 private String strLanguageId; 68 69 70 73 public JLanguageManager() { 74 75 languageClassLoader = new JJarClassLoader(); 76 database = JSharedObjects.getDatabase(); 77 78 try { 79 loadDefaultLanguage(); 80 } catch( Exception e ) { 81 try { 82 loadLanguage( DEFAULT_LANGUAGE ); 83 } catch( Exception ex ) { 84 System.err.println( "JLanguageManager.JLanguageManager() - " + ex ); 85 } 86 } 87 } 88 89 93 public void destroy() { 94 95 try { 96 languageClassLoader.close(); 97 } catch( java.util.jar.JarException e ) { 98 System.err.println( "JLanguageManager.destroy() - " + e ); 99 } 100 } 101 102 106 public void loadDefaultLanguage() throws Exception { 107 108 String strLanguageId; 109 int nProfileId = JSharedObjects.getProfileManager().getDefaultProfile(); 110 ResultSet resultSet = null; 111 Connection conn = null; 112 PreparedStatement st = null; 113 114 115 try{ 116 conn = JSharedObjects.getDatabase().getConnection(); 117 st = conn.prepareStatement( "SELECT language_id FROM profile WHERE profile_id = ?" ); 118 st.setLong( 1, nProfileId ); 119 120 resultSet = st.executeQuery(); 121 122 if( resultSet == null ) 123 throw new Exception ( "JLanguageManager.loadDefaultLanguage() - Database error" ); 124 125 resultSet.next(); 126 strLanguageId = ( resultSet.getString( "language_id" ) == null ? "" : resultSet.getString( "language_id" ) ); 127 resultSet.close(); 128 loadLanguage( strLanguageId ); 129 } catch( java.sql.SQLException e ) { 130 throw new Exception ( "JLanguageManager.loadDefaultLanguage() - SQLException " + e ); 131 } catch( Exception e ) { 132 throw new Exception ( "JLanguageManager.loadDefaultLanguage() - Exception " + e ); 133 } finally { 134 resultSet.close(); 135 st.close(); 136 } 137 } 138 139 143 public void loadLanguage( String strLanguageId ) throws Exception { 144 145 try { 146 languageClassLoader.open( JSharedObjects.getConfiguration().getLanguagesPath() + strLanguageId + ".jar" ); 147 this.strLanguageId = strLanguageId; 148 JSharedObjects.getMainWindow().windowRefresh( org.planetamessenger.mos.forms.JMOSWindow.LANGUAGE_REFRESH ); 149 } catch( JarException e ) { 150 System.err.println( "JLanguageManager.loadLanguage() - " + e ); 151 strLanguageId = ""; 152 throw new Exception ( "JLanguageManager.loadLanguage() - Language not found" ); 153 } 154 } 155 156 159 public String getCurrentLanguage() { 160 161 return strLanguageId; 162 } 163 164 169 public ArrayList<JLanguageField> getLanguageList() { 170 171 JarFile langJarFile; 172 JarEntry jarEntry; 173 JLanguageField item = null; 174 JFilenameFilter filter = new JFilenameFilter(); 175 File langDir = new File( JSharedObjects.getConfiguration().getLanguagesPath() ); 176 ArrayList<JLanguageField> langVector = new ArrayList<JLanguageField>(); 177 String [] strDirList; 178 179 180 filter.add( "jar" ); 181 filter.add( "zip" ); 182 183 strDirList = langDir.list( filter ); 184 185 for( int nCount = 0; nCount < strDirList.length; nCount++ ) { 187 Manifest manifest; 188 String strLanguageName = strDirList[nCount].substring( 0, strDirList[nCount].indexOf( '.' ) ); 189 190 try { 191 langJarFile = new JarFile( JSharedObjects.getConfiguration().getLanguagesPath() + strDirList[nCount] ); 192 manifest = langJarFile.getManifest(); 193 item = new JLanguageField(); 194 195 item.setLanguageId( strLanguageName ); 196 item.setLanguage( manifest.getMainAttributes().getValue( LANGUAGE_ATTRIBUTE ) ); 197 item.setOwner( manifest.getMainAttributes().getValue( OWNER_ATTRIBUTE ) ); 198 item.setLicense( manifest.getMainAttributes().getValue( LICENSE_ATTRIBUTE ) ); 199 item.setHomePage( manifest.getMainAttributes().getValue( HOMEPAGE_ATTRIBUTE ) ); 200 item.setDescription( manifest.getMainAttributes().getValue( DESCRIPTION_ATTRIBUTE ) ); 201 item.setVersion( manifest.getMainAttributes().getValue( VERSION_ATTRIBUTE ) ); 202 203 langVector.add( item ); 204 langJarFile.close(); 205 langJarFile = null; 206 } catch( java.io.IOException e ) { 207 System.err.println( "JLanguageManager.getLanguagesList() - " + e ); 208 continue; 209 } 210 } 211 212 langDir = null; 213 filter = null; 214 215 return langVector; 216 } 217 218 223 public JLanguageField getLanguage( String strLanguageId, ArrayList<JLanguageField> vLanguageList ) { 224 225 JLanguageField item = null; 226 227 for( int nCount = 0; nCount <= vLanguageList.size(); nCount++ ) { 228 item = vLanguageList.get( nCount ); 229 230 if( item.getLanguageId().compareTo( strLanguageId ) == 0 ) 231 return item; 232 } 233 234 return null; 235 } 236 237 241 public String getString( String strKeyName ) throws Exception { 242 243 ResourceBundle resourceBundle = ResourceBundle.getBundle( strLanguageId, Locale.getDefault(), languageClassLoader ); 244 245 if( resourceBundle == null ) 246 throw new Exception ( "JLanguageManager.getString() - Language not loaded" ); 247 else { 248 String strValue = resourceBundle.getString( strKeyName ); 249 250 return ( strValue == null ? "RESOURCE ID " + strKeyName + " NOT FOUND" : strValue ); 251 } 252 } 253 254 260 public String getStringEx( String strKeyName ) { 261 262 try { 263 return getString( strKeyName ); 264 } catch( Exception e ) { 265 System.err.println( "JLanguageManager.getStringEx() - " + e ); 266 return strKeyName; 267 } 268 } 269 270 275 public boolean updateProfile( long nProfileId, String strLanguageId ) { 276 277 Connection conn = null; 278 PreparedStatement st = null; 279 280 281 if( nProfileId == -1 ) 282 return false; 283 284 try { 285 conn = JSharedObjects.getDatabase().getConnection(); 286 st = conn.prepareStatement( "UPDATE profile SET language_id=? WHERE profile_id=?" ); 287 st.setString( 1, strLanguageId ); 288 st.setLong( 2, nProfileId ); 289 290 if( st.executeUpdate() <= 0 ) 291 return false; 292 293 } catch( SQLException e ) { 294 System.err.println( "JLanguageManager.updateProfile() - " + e ); 295 return false; 296 } finally { 297 if( st != null ) 298 try { 299 st.close(); 300 } catch( SQLException e ) { 301 System.err.println( "JLanguageManager.updateProfile() - " + e ); 302 } 303 } 304 305 return true; 306 } 307 } 308 309 | Popular Tags |