Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## XLanguageSystem.java
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- public class XLanguageSystem {
- private String defaultLanguage = "en";
- private String tablePrefix = "lang_";
- private int cacheSize = 2;
- private Connection connection = null;
- private Statement statement = null;
- private ResultSet result = null;
- private HashMap<String, HashMap<String, String>> cache = new HashMap<String, HashMap<String, String>>();
- public XLanguageSystem() {
- System.out.println("Starting...");
- // --- database specific variables - begin ---
- String host = "localhost";
- String username = "xcache";
- String password = "C3sbMsKp5zFXKRyy";
- String database = "xcache";
- // --- database specific variables - end ---
- // --- connect to database - start ---
- try {
- System.out.print("Connection to database... ");
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- this.connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database, username, password);
- System.out.println("DONE");
- System.out.print("Creating Statement... ");
- this.statement = connection.createStatement();
- System.out.println("DONE\n");
- }
- catch(Exception e) {
- this.failure(e);
- }
- // --- connect to database - end ---
- // --- getting languages - begin ---
- this.selectQuery("SHOW TABLES LIKE '" + this.tablePrefix + "%'");
- try {
- while(this.result.next())
- this.createCache(this.result.getString(1).replace(this.tablePrefix, ""));
- }
- catch(Exception e) {
- this.failure(e);
- }
- this.freeResultSet();
- // --- getting languages - end ---
- }
- public void close() {
- System.out.println("\nShutting down...");
- this.freeResultSet();
- try {
- if(this.statement != null) {
- System.out.print("Closing Statement... ");
- this.statement.close();
- System.out.println("DONE");
- }
- if(this.connection != null) {
- System.out.print("Closing Connection... ");
- this.connection.close();
- System.out.println("DONE");
- }
- System.exit(0);
- }
- catch(Exception e) {
- this.failure(e);
- }
- }
- public String getEntry(String lang, String trans_id) {
- if(this.languageExists(lang)) { // language exists...
- if(this.cacheElementExists(lang, trans_id)) // element exists in language-cache...
- return this.getCacheElement(lang, trans_id); // return element from the language-cache
- else { // element does not exist in language-cache...
- String trans = this.selectTranslationFromDB(lang, trans_id); // select trans_id value from lang-table
- if(trans == trans_id) // trans-id value was not found in lang-table...
- trans = this.getDefaultTranslation(trans_id); // get default-translation for trans_id
- return trans;
- }
- }
- else // language does not exist...
- return this.getDefaultTranslation(trans_id);
- }
- public void addLanguage(String lang) {
- if(!this.languageExists(lang)) { // if language to add does not exists allready...
- System.out.print("Adding language '" + lang + "'... ");
- this.createCache(lang);
- String sqlQuery = "CREATE TABLE " + this.tablePrefix + lang + " ("
- + "id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,"
- + "trans_id VARCHAR(200) NOT NULL,"
- + "trans LONGTEXT NOT NULL,"
- + "userid INT(11) NOT NULL,"
- + "active TINYINT(1) DEFAULT '0',"
- + "KEY trans_id (trans_id),"
- + "KEY userid (userid)"
- + ") ENGINE=MyISAM;";
- this.updateQuery(sqlQuery);
- System.out.println("DONE");
- }
- else
- System.out.println("Language '" + lang + "' does allready exist.");
- }
- public LinkedList<String> getAvailableLanguages() {
- return new LinkedList<String>(this.cache.keySet());
- }
- public void removeCacheElement(String lang, String trans_id) {
- // if langueage exists and key exists in this language-cache... remove it...
- if(this.languageExists(lang) && this.cacheElementExists(lang, trans_id))
- this.cache.get(lang).remove(trans_id);
- }
- private void failure(Exception e) {
- System.out.println("FAILED\n");
- e.printStackTrace();
- System.exit(-1);
- }
- private void updateQuery(String sqlQuery) {
- try {
- this.statement.executeUpdate(sqlQuery);
- }
- catch(Exception e) {
- this.failure(e);
- }
- }
- private void selectQuery(String sqlQuery) {
- try {
- this.result = this.statement.executeQuery(sqlQuery);
- }
- catch(Exception e) {
- this.failure(e);
- }
- }
- private void freeResultSet() {
- try {
- if(this.result != null)
- this.result.close();
- }
- catch(Exception e) {
- this.failure(e);
- }
- }
- private void createCache(String lang) {
- this.cache.put(lang, new HashMap<String, String>());
- }
- private String getCacheElement(String lang, String trans_id) {
- System.out.println("Fetching '" + trans_id + "' for language '" + lang + "' from cache...");
- // get element and remove it temporarly from the cache...
- String trans = this.cache.get(lang).remove(trans_id);
- this.cache.get(lang).put(trans_id, trans); // add element back to the cache {element needs to be removed and readded to implement LFU}
- return trans;
- }
- private String selectTranslationFromDB(String lang, String trans_id) {
- System.out.println("Fetching '" + trans_id + "' for language '" + lang + "' from database...");
- this.selectQuery("SELECT trans FROM " + this.tablePrefix + lang + " WHERE trans_id = '" + trans_id + "' AND active = '1'");
- String trans = "";
- try {
- if(this.result.next()) { // found entry in lang-table...
- trans = this.result.getString(1); // get entry
- if(this.cache.get(lang).size() == this.cacheSize) { // cache is full...
- // remove first key entry {is the oldes in terms of LFU-policy}...
- for(String key : this.cache.get(lang).keySet()) {
- this.cache.get(lang).remove(key);
- break;
- }
- }
- this.cache.get(lang).put(trans_id, trans); // add found trans to cache...
- }
- else // entry not found in database...
- trans = trans_id; // set trans_id to be returned...
- }
- catch(Exception e) {
- this.failure(e);
- }
- this.freeResultSet();
- return trans;
- }
- private String getDefaultTranslation(String trans_id) {
- if(this.cacheElementExists(this.defaultLanguage, trans_id)) // trans_id exists in lang-cache of default language...
- return this.getCacheElement(this.defaultLanguage, trans_id); // return trans-id value from cache
- else // trans_id does not exist in lang-cache of default language...
- return this.selectTranslationFromDB(this.defaultLanguage, trans_id); // select trans-id value from lang-table...
- }
- private boolean languageExists(String lang) {
- return this.cache.containsKey(lang);
- }
- private boolean cacheElementExists(String lang, String trans_id) {
- return this.cache.get(lang).containsKey(trans_id);
- }
- public void printCache(String lang) {
- System.out.println("\n==========");
- for(String trans_id : this.cache.get(lang).keySet())
- System.out.println(trans_id + " => " + this.cache.get(lang).get(trans_id));
- System.out.println("==========\n");
- }
- }
- ## Xtest.java
- public class Xtest {
- public static void main(String[] args) {
- XLanguageSystem xcache = new XLanguageSystem();
- System.out.println("Available languages:");
- for(String lang : xcache.getAvailableLanguages())
- System.out.println(lang);
- System.out.println();
- xcache.addLanguage("de");
- xcache.addLanguage("fr");
- xcache.printCache("de");
- System.out.println(xcache.getEntry("de", "foo"));
- xcache.printCache("de");
- System.out.println(xcache.getEntry("de", "bar"));
- xcache.printCache("de");
- System.out.println(xcache.getEntry("de", "tar"));
- xcache.printCache("de");
- System.out.println(xcache.getEntry("de", "bar"));
- xcache.printCache("de");
- System.out.println(xcache.getEntry("de", "foo"));
- xcache.printCache("de");
- System.out.println("Removing trans_id 'foo' from cache of language 'de'...");
- xcache.removeCacheElement("de", "foo");
- xcache.printCache("de");
- xcache.close();
- }
- }
- ## Xtest_output
- Starting...
- Connection to database... DONE
- Creating Statement... DONE
- Available languages:
- de
- fr
- en
- Language 'de' does allready exist.
- Language 'fr' does allready exist.
- ==========
- ==========
- Fetching 'foo' for language 'de' from database...
- foo ist foobar
- ==========
- foo => foo ist foobar
- ==========
- Fetching 'bar' for language 'de' from database...
- bar is barfoo
- ==========
- foo => foo ist foobar
- bar => bar is barfoo
- ==========
- Fetching 'tar' for language 'de' from database...
- tar ungleich zip
- ==========
- tar => tar ungleich zip
- bar => bar is barfoo
- ==========
- Fetching 'bar' for language 'de' from cache...
- bar is barfoo
- ==========
- tar => tar ungleich zip
- bar => bar is barfoo
- ==========
- Fetching 'foo' for language 'de' from database...
- foo ist foobar
- ==========
- foo => foo ist foobar
- bar => bar is barfoo
- ==========
- Removing trans_id 'foo' from cache of language 'de'...
- ==========
- bar => bar is barfoo
- ==========
- Shutting down...
- Closing Statement... DONE
- Closing Connection... DONE
Add Comment
Please, Sign In to add comment