Helping ordinary people create extraordinary websites!

A New Strategy of Language Pack Management for Wireless Apps

By Huang Chang & Tong Chun Jie
2005-04-18

LPProxy Bundle
The LPProxy bundle acts as a proxy for delegating all LP bundles-related requests. Like the LP bundle, it also registers to the Service Management Framework run time at startup. Upon receiving the getString(String key) from other bundles, the LPProxy bundle first checks whether the desired LP bundle has already existed in the Service Management Framework run time. It downloads the LP bundle from the Service Management Framework bundle server if the bundle is not available. After that, the LPProxy bundle starts the desired bundle and finds the service reference of the LPService, which provides the getString(String key) method for accessing the resource bundle, by comparing the bundle name. Finally, the LPProxy bundle creates the instance of LPService and calls its getString(String key) method for the multilingual data.

LPProxy provides the following methods:

void setBaseName(String baseName): Sets the base name of the LP bundle. If a bundle is named as LP_en_US, for example, the base name should be LP.
String getBaseName(): Gets the base name of the LP bundle.
void setLocale(Locale locale): Sets the locale of the LP bundle.
Locale getLocale(): Gets the locale of the LP bundle.
String getString(String key): Gets a string for the given key from the LP bundle.

The getString(String key) method is the key function, as shown in Listing 2.

Listing 2. The getString(String key) method and its related methods

public String getString(String key) {

init();
return lpSV.getString(key);
}

//initialize and install the required bundle.
private void init()
{
//Reinstall LocaleData bundle if the baseName or the locale changes.
if(reloadBundle)
{
if(this.lpBundle!=null){
//Need to uninstall current Language Pack bundle
ctx.ungetService(lpSR);
try {
lpBundle.uninstall();
} catch (BundleException e) {
e.printStackTrace();
}
}

//Install LocaleData bundle from local runtime or remote SMF Server
if(baseName == null )
{
System.out.println("You must specify the bundle name
and the resource base name");
}else{
try{
//Install LocaleData bundle then start it.
String bName="smfbd:/" + baseName+"_" +
this.locale.getLanguage() + "_"
+this.locale.getCountry();
lpBundle = ctx.installBundle(bName);
lpBundle.start();
//Get the ServiceReference instance
lpSR=locateResourceBundle();
//Get the LPService instance
lpSV = (LPService)ctx.getService(lpSR);
reloadBundle=false;
}catch(BundleException e)
{
e.printStackTrace();
}
}
}else
{
System.out.println("Bundle has already installed");
}
}

//Create ServiceReference instance for LocaleData Bundle.
private ServiceReference locateResourceBundle()
{
try {
//Get all available ServiceReference instances from run time.
ServiceReference sr[]=ctx.getServiceReferences(svName,null);
for(int i=0;i<sr.length;i++){
//Get the target bundle comparing the bundle name.
String key=(String)sr[i].getProperty("Bundle-Name");
if(key.equals(this.baseName+"_"+this.locale.getLanguage()
+"_"+this.locale.getCountry()))
return sr[i];
}
} catch (InvalidSyntaxException e) {
e.printStackTrace();
}
return null;
}




Tutorial pages:

First published by IBM DeveloperWorks


 2 Votes

You might also want to check these out:


Leave a Comment on "A New Strategy of Language Pack Management for Wireless Apps"
You must be logged in to post a comment.

Link to This Tutorial Page!


GET OUR NEWSLETTERS