Overwriting ExtResourceBundle
You could use ExtResourceBundle as is, creating the table from the scripts and setting the
System properties. In a couple of cases I found it actually helpful to overwrite ExtResourceBundle
The principle stayed the same, but the use of it was greatly extended
General considerations
If you want to overwrite ExtResourceBundle you need to re implement the Constructor and re implement
the getExtBundle(..) methods returning the result of getBundleImpl(..) :
public class MyResourceBundle extends ExtResourceBundle {
public MyResourceBundle(String baseName, Locale locale) {
super(baseName,locale);
}
public static ResourcBundle getExtBundle(String baseName) {
return getBundleImpl(new MyResourceBundle(baseName,Locale.getDefault()));
}
public static ResourcBundle getExtBundle(String baseName) {
return getBundleImpl(new MyResourceBundle(baseName,Locale.getDefault()));
}
}
Now you can overwrite the methods which you need to overwrite.
Using another connection
To use a different source from the properties to get your connection from you can overwrite the
protected void makeConnection() throws SQLException and setting the member variable m_con
to a valid connection.
Example one:
Say we have a controller which is a singleton that manages the database connections within an application
protected void makeConnection() throws SQLException {
m_con = Controller.getInstance().getConnection();
}
Example two:
You could use it to get information from an application server managing your database connections.
protected void makeConnection() throws SQLException {
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myJDBC");
m_con = ds.getConnection();
}
Note: The connection is never closed directly by ExtResourceBundle
|
|
Using another table
It might be true that there actually is a table which contains the necessary information for localizing your
application or you want to use another table for collecting your resources. The query which is actually send to
the database is retrieved from the method public String getSQL() overwriting this will thus make it
possible to use a completly different table.
Note:ExtResourceBundle expect the SQL to return just two columns, the first one containing
the key, the second one containing the value
Example:
We assume we have a table called BOILERPLATE with a DOMAIN,KEY and TEXT column and just one column for LOCALE
public String getSQL() {
StringBuffer sb=new StringBuffer();
sb.append("SELECT KEY,TEXT FROM BOILERPLATE WHERE DOMAIN=");
sb.append("'").append(m_domain).append("'");
sb.append(" AND LOCALE='").append(m_locale.toString()).append("'");
return sb;
}
|
|
Differentiate bundles on subdomains
For one project I had to make a difference in creating a great number of keys just slightly different,
to represent labels with slightly different values for screens or divide the domains in subdomains so the
relation to the keys still can be determined. To make this work I have overwritten protected ResourceBundle getParent()
Example:
The name of the domains consists of a subdomain and domain seperated by a dot. If the values are different from one subdomain,
but with the same key, the value is defined in the subdomain, other values are retrieved from the main domain.
protected ResourceBundle getParent() {
int idx = getBaseName().indexOf('.');
if (idx != -1) {
return getExtBundle(getBaseName().substring(idx + 1), getLocale());
} else {
ResourceBundle parent = null;
if (!getLocale().getLanguage().equals("")) {
if (!getLocale().getCountry().equals("")) {
Locale pLoc = new Locale(getLocale().getLanguage(), "");
parent = getExtBundle(getBaseName(), pLoc);
} else {
parent = getExtBundle(getBaseName(), new Locale("", ""));
}
}
return parent;
}
}
|
|