Wednesday Jul 29, 2009

Java Basics - good to know - nice to use

Hi ,

After quite some time I'm back , busy with my automation project and one parser development.

Recently I am exploring some java stuff which was ignore knowingly or do not care to understand funda behind it as long as work is getting done. So i thought of sharing some know items one more time to give them special attention.

Duplicate in HashMap : If new object hash code is same as existing member hashcode then if either new object == to existing object or new objects equals() return true then existing object will be updated. So overwite hashcode and equals() if you want to prevent duplicate based on class variable.

Ex .

Class Employee

{ int empNo; String name; int age; }

You want collection to be store non duplicate based on empNo.

Then add following two overriding version of hashCode() and equals() public int hashCode()

{ return empNo; }

public boolean equals(Employee e)

{ return (this.empNo == e.empNo)?true:false; }

Now when you will add to hashmap object of Employee , it will add new pair if and only if empNo will be different otherwise it will update the existing one

Float.isNaN - Returns true if this Float value is a Not-a-Number (NaN), false otherwise.

ex.

float f = (float) Math.sqrt(-10);

boolean b1 = Float.isNaN(f);

System.out.println(b1); // result is : true

Transient - It flags a field as something that should not be considered part of an object's persistent state. i.e. in simple terms it will not get written to persistence store (whatever it may be) if this keyword is associated with variable Reading and Writing shared variable by


Thread : Each thread has a working memory, in which it may keep copies of the values of variables from the main memory that is shared between all threads.

To access a shared variable, a thread usually first obtains a lock and flushes its working memory. This guarantees that shared values will thereafter be loaded from the shared main memory to the threads working memory. When a thread unlocks a lock it guarantees the values it holds in its working memory will be written back to the main memory.

Monitor : Not class monitor , its object monitor , Each object got monitor. Thread calling synchronized method or synchronized block of this object gets monitor(if available otherwise wait wait). if thread get monitor then others has to wait till finish to execute the same.

Reentrant Monitor : The Java runtime system allows a thread to re-acquire a monitor that it already holds because Java monitors are reentrant. Reentrant monitors are important because they eliminate the possibility of a single thread deadlocking itself on a monitor that it already holds.

notifyAll() : After effects , If multiple threads are waiting for a monitor, the Java runtime system chooses one of the waiting threads to run, making no commitments or guarantees about which thread will be chosen.

Lost a monitor blame wait() : When the thread enters the wait method, the monitor is released atomically, and when the thread exits the wait method, the monitor is acquired again

Lock a Class , what ? : Java virtual machine loads a class file, it creates an instance of class java.lang.Class. When you lock a class, you are actually locking that class's Class object. But why ? To enter in synchronized static method you need class lock.

Wait() ,notify(),notifyAll() : from where you will invoke , anywhere , no no , only from synchronized method/statement. You need to lock to execute this methods.

serialVersionUID : you better write if your object getting serialized. If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during de-serialization

Error Class : lost frontier , how long it will survive . An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch . A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur. ex. IOError , LinkageError , VirtualMachineError etc etc. Quite a few never knew they have good big family :-).

throw ? , what you can throw ? Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement

catch ? What you can catch ? Similarly, only this class or one of its subclasses can be the argument type in a catch clause.

RunTimeException : luxury for not required to catch , should not be thrown in first place , go and fix root cause . A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught

comment / views / enlightment of concepts are welcome. 

Learning never ends and re-learning also never ends. :-)

Enjoy your time. 



Wednesday Feb 11, 2009

Web Space Server - Localization

We are back after hectic one month or so ... It was one of the interesting and challenging period ... So much of excitement so much to do less time .. pressure ... etc etc ... But but , Finally its out .. On Time ... Leaving behind happy faces :-).

Yeah , I'm talking about  Sun Glassfish Web Space Server 10.0 release (derived from Liferay 5.2) , first release from Sun  after one of the unique development relationship between Sun and Liferay .

 To get more details .. Refer this blog on Portal Post .

Download link.

Let move to topic of blog i.e Localization (L10n).

FAQ 1.0 : How can I get localized version Web Space Server ?

To simplify customer experience , there is not extra steps to get localized version. It has been bundled in by default. So when you download install Web Space Server , You by default get localization support.

FAQ 2.0 : In which languages Web Space Server is available ?

Web Space Server is localized out-of-box in 22 languages , out of which Sun provides localization support for Japanese (ja) and Simplified Chinese (zh_CN). Remaining languages are community driven

FAQ 4.0 : Can I add new Language ?

Yes ,Web Space Server is Internationalized. Allow you to add new languages which is not shipped out of box.

Refer Localization section of Admin Guide for exact steps of how to do the same.

FAQ 5.0 : Where I can get more information about Localization ?

Refer Localization section of Admin Guide

FAQ 6.0 :  If you have Ask Me :-) ...

Beginning ... Focus ... Success ... Beginning  ............ (Life goes on )



Monday Dec 22, 2008

How to configure WebSynergy/Liferay on MySQL for UTF-8

Recently I was using WebSynergy with default database (HSQL in memory) and all my localized data were just doing fine. Happily making trip to UI to Database and vice versa. Then when I switch my database to MySQL , everything went haywire , Data turned in to ??? (okie let me be clear , when Data turns to ??? its mostly case of wrong encoding).

It was small challenge for me to get it right on MySQL. Challenge is Challenge. 

I have explored on MySQL side and found that , it very easy to create database which can handle UTF-8 character (by default it comes in ASCII). so create database to handle WebSynergy data with UTF-8 encoding.

CREATE DATABASE mydb DEFAULT CHARACTER SET utf8; 

 :-) .. am I done .. I did a check but situation refuse to change .. there was something else also needs to be done. To call it done.

Database is in UTF-8 but communication between Portal and Database was not in UTF-8.  How to enable that?

I have found a simple straight way.

Add following two properties to "LiferayPool" and "JIRAPool".

useUnicode=true
characterEncoding=utf8 

.. are we done .. Yes , we are done. Enjoy the advantages of MySQL with WebSynergy in language you want to communicate.

"No Problem is small problem until you know the solution" 




Friday Sep 19, 2008

How to do Internationalization(I18n) and Localization(L10n) in Liferay , WebSynergy

Hi .. I'm Back .. was busy exploring more on I18n of Liferay

This blog will describe how to do i18n in development environment of Liferay and WebSynergy(now on will be referred as Portal). Also while adding new Portlets to Portal how to leverage existing Portal UI tag lib and Language utility class for I18n in quicker and easier way.

Resource Bundle

Key Points

  • Portal has only One Resource Bundle for all the components . Refer my previous entry for more details
  • Location in workspace : portal-impl/content/Language.properties
  •  Location in Installation :
          • Unjar portal-impl.jar (it will be lib directory of workspace)
          • Language.properties file will be found in content directory
  • Either you can directly modify this Language.properties OR (most preferred way) to create Language-ext.properties and bundle in same jar. Because if you directly modify Language.properties then you need to manually take care of adding your changes during updated to latest default Language.properties which you will get in install.


Convention to add messages in Resource bundle

  1. This file follows same convention as defined for Liferay's default Language.properties liferay/trunk/portal-impl/content/Language.properties.
  2. Each property should key should be prefix by your portlet name .
  3. File is well-defined in to various section descried below
    1. Portlet Titles : Portlet titles of your portlet. - NEEDS TO VERIFY
      1. Portlet title key should be constructed as javax.portlet.title.PORTLETNAME , where PORTLETNAME is name which you have mentioned in portlet-name tag of your portlet.xml.
        ex. java.portlet.title.FriendsPortlet=Friends Widget
      2. This feature is yet to test (by me) so you can use Portlet specific resource bundle to define title.
    2. Category Titles : Category titles which appears in "Add Application" list
      1. It should be prefix by category.CATEGORYNAME where CATEGORYNAME is name of category.
      2. ex. category.admin=Admin
      3. this category key will be used in liferay-display.xml of portlet which are part of category
        1. ex. <display>
              <category name="category.cms">
                  <category name="category.alfresco">
                      <portlet id="1" />
                  </category>
              </category>
          </display>
    3. Model resources : Model resources should be prefix by model.
      1. ex. model.resource.com.liferay.portal.model.Group=Community
      2. ex. model.resource.com.liferay.portlet.blogs.model.BlogsEntry=Blogs Entry
    4. Action Items : Messages which used as action items. Should be prefix by action.. All words are in uppercase. Word seperator is _
      1. ex. action.ADD_ARTICLE=Add Article
    5. Messages : General messages
      1. Key should be construed from Message. ex Key for message My Friends will be my-friends
      2. Use x for dynamic parameter in messages ex. Key for message This {0} is great will be this-x-is-great
      3. If message is very long (ex. more then 15 words) you can use small key but make sure it is giving information about message
        ex. an-applet-version-of-the-editor-is-also-available=An applet version of the editor is also available. It is a heavier but more user friendly editor that provides colorized text, search and replace, and other functionality. You can choose to use that editor by editing your portlet preferences.
  4. Keep each section of file in ascending order

How to use liferay ui taglib to fetch messages in JSP/Java Script

There are various ways to fetch message from resource bundle.

NOTE : All the following approach will consider Language.properties as Resource bundle and will fetch messages form there based on locale. However following methods support -ext mechanism i.e It will first look in Language-ext.properties file.

  1. liferay-ui:message tag
  2. LanguageUtil class
  3. UnicodeLanguageUtil class
  4. Other liferay ui tags - I18n'ed

liferay-ui:message tag

  • For simple messages which does not have dynamic parameter use this tag
    ex. < liferay-ui:message key="my-friends"/ >
  • This tag internally calls LangaugeUtil class

LangaugeUtil class

LanguageUtil class has various formatter methods use them based on situation.

  • For messages which has dynamic parameter use following method. Make last argument as "true" if you want your parameter to be translated as well
    ex. <%= LanguageUtil.format(pageContext, "upload-a-gif-or-jpeg-that-is-x-pixels-tall-and-x-pixels-wide", new Object {"120", "100"}, false) %>
  • For messages which has html code along with parameter then use LangaugeWrapper as argument instead of Object
    ex.<%= LanguageUtil.format(pageContext, "your-account-with-login-x-is-not-active", new LanguageWrapper {new LanguageWrapper("", user.getFullName(), ""), new LanguageWrapper("< b >< i >", userLogin, "< /b >")}, false) %>

Other liferay ui tags and LanguageUtil class methods- I18n'ed

  • liferay-ui:icon-help - This tag is i18n'ed. You can use key value in message attribute of this tag
    • < liferay-ui:icon-help message="allow-dictionary-words-help"/ >
  • LanguageUtil.getTimeDescription - to get localized time descrption
    • <%= LanguageUtil.getTimeDescription(pageContext, _DURATIONSi? \* 1000) %>

UnicodeLanguageUtil Class

  • Use this class in Java Script in JSP pages
  • UnicodeLanguageUtil has similar method singature as found in LanguageUtil class.
  • ex. function copyFromLive() {

if (confirm('<%= UnicodeLanguageUtil.get(pageContext, "are-you-sure-you-want-to-copy-from-live-and-overwrite-the-existing-staging-configuration") %>')) { document. fm. <%= Constants.CMD %>.value = "copy_from_live"; submitForm(document. fm); } }

  • Also use in alert within jsp
  • ex2.
    " method="post" name=" fm" onSubmit="alert('<%= UnicodeLanguageUtil.get(pageContext, "please-be-patient") %>'); submitForm(this); return false;">

Import and define tag lib in your jsp or included jsp

  • <%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
  • Import following classes
    • <%@ page import="com.liferay.portal.kernel.language.LanguageUtil" %>
    • <%@ page import="com.liferay.portal.kernel.language.LanguageWrapper" %>
    • <%@ page import="com.liferay.portal.kernel.language.UnicodeLanguageUtil" %>

How to use liferay Language Utill classes to fetch messages in JAVA programs

In order to fetch localized messages in java program we will be using same Util classes which has been used in JSP/Java Script

  • LanguageUtil class
  • If your message does not have any dynamic parameter then use any of LanguageUtil.get(...,key) method. this method will fetch message assicated with key. There are various overloaded version of LanguageUtil.get are available , use desired get method based on situation.  Some of commonely used version of get method.
    • LanguageUtil.get(pageContext,key)
    • LanguageUtil.get(companyId,key) etc.
    • there are other overloaded version of get method is available. You need to use them based on your requirement.
    • Refer class "portal/trunk/portal-kernel/src/com/liferay/portal/kernel/language/LanguageUtil.java for details.
  • If your message has  dynamic parameter then use any of LanguageUtil.format(...,key,Object) method.  this method will fetch message assicated with key. There are various overloaded version of LanguageUtil.form are available , use desired format method based on situation.  Some of commonely used version of form method.
    • LanguageUtil.get(pageContext,key, object)
    • LanguageUtil.get(pageContext,key, new Object[])
    • there are other overloaded version of format method is available. You need to use them based on your requirement.
    • Refer class "portal/trunk/portal-kernel/src/com/liferay/portal/kernel/language/LanguageUtil.java for details.

How to use liferay Language Utill classes to fetch messages in Velocity templates

Portal uses velocity engine to create templates i.e. .vm files which are used to define layout and in themes.

  • Set Language Id : #set ($language_id = $user.getLanguageId()) in your init template.
  • You can use following syntex to fetch messages from Language.properties file
    • #language ("KEY-NAME")
  • Some places I have also seen usage of language util as follow.
    • $languageUtil.get($company_id, $locale, "add-application"))

There is also certain changes required in portlet.xml <resource-bundle> tag to allow container to fetch portlet title from common resource bundle. Watch out for this space , I will update it shortly. 

 Never shy away from re using stuff which already exists ... It gives more time to innovate...

Wednesday Aug 13, 2008

One resource bundle per One Application - I18n and L10n will be easy

<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("UA-2367267-3"); pageTracker._trackPageview(); </script>

First what is with this some what mystry title.

In simple words , I would like to highlight advantage of keeping only one resource bundle( aka. one property file which contains all the localizable messages) per One Application.

Source of insipiration was Liferay.

Let me give you more idea of how it will look like.

1) Make sure all your translatable strings are put on one property file  ex. Language.properties

This will help to your customer who might want to add new langauge or want to add new strings to existing language or change strings to existing langauge. Easy to find where to do localization.

2) Organized it well . Define a rule to write key name.

ex. first_name=First Name

     account_number=Account Number

This will help to easily find of what this key represents in localized bundle.

3) Keep it in Alphabetical order

This will ensure that duplicate properties does not exists in your bundle.

4) Now you can keep common utility class which will load the bundle based on Locale user wanted

This will help all your other class as they do not need to bother about loading bundle. They can focus on solving business logic (that's where money comes from)

Last but not the least ... Innovation is mother of all invension ... so innovate whether it is necessasity or not :-) ...


Thursday Aug 07, 2008

Hibernate - POJO and HBM file creation best practice

Recently I was working on Hibernate and created a number of POJO and their corresponding hbm file. I am  using Netbeans (who has support for Hibernate plugin , real helpful , http://netbeans.org )

Certain best practice I would like to make work more interesting.

  • Keep table name and POJO name same.

ex. Create new Java Class  i.e Person , Use hibernate plugin to create .hbm file , Give same name as Class file , Select Person as class name on next screen.

  This will create .hbm file with name as Person , no need to provide table attribute. Hibernate will create table with same name.

This aovides unnecessary complexities in distinguice table name and class name

  • Keep POJO variable name same as Field name

      Do no mention Column attribute while declaring <Property> in .hbm file. Hibernate will create column with same name.

  • Try to keep ID as primaray key for all the Tables where possible. Less to remember and more time to figure out solution to problem on hand. 
  • How can I forgot my Dear I18n. There is nothing much as long as you make sure that created Database has UTF-8 encoding.
  • One more point , Certain tables are used to keep default list. If the list compoment is localizable make sure you take care of fetching localized values from proper resource bundle before publishing to User Interface.

Resources :

More will follow as I learn more ... enjoy your life ... n keep doing innovation ... only way to survive ...


Friday Jun 13, 2008

Netbeans 6.1 Multilingual released , Portal Pack ML will follow soon

Checkout ML release for Netbeans 6.1  It is localized in three languages Japanese (Ja) , Simplified Chinese (zh_CN) and Portuguese (pt_BR). I have download and installed , it looks great. And major difference is perfromance from last release , it quite fast. have fun with Netbeans ..

We are working on ML release of  Portal Pack 2.0(http://portalpack.netbeans.org) It will be released soon. Watch this space for more details on Portal Pack 2.0 ML. 

Wednesday Jun 11, 2008

Localization in Portal Server 7.2 (recently released)

Recently Portal Server 7.2 is released. Checkout more details about new features and download at this blog.

In this release we have localized in seven languages as done in earlier releases.

German (de) , Spanish (es) , French (fr) , Japanese (ja) , Korean (ko) , Simplified Chinese (zh_CN) , Tranditional Chinese (zh_TW).

I will talk some key changes we have done in localization.

1) There is NO l10n packages. then where is l10n files? , L10n files are packaged in base packages. We call it "Unified Packaging" 

What is advantage of this?

- Customer does not need to perform any special step to install  and configure localized version of portal. It will give seamless experience to l10n users

- Upgrade and migration would be much easier

2) In open source workspace , location of l10n files has been changed from what it was in close source workspace. Now each l10n file is placed along with corresponding english. It will also community/developer to easily find localized files in workspace.

Visit  localization wiki page to get more details on localization activities in portal server. It is also provides various useful "How to" to help l10n users.

Tuesday Jun 10, 2008

I18n of Junit Testcases

I always believe that Maximum I18n testing should be done before build release to QA. To do that I keep finding out various way to do more and more i18n testing during development.

In that I stuck with this idea of creating Junit testcase for I18n testing. Then I relize it would be good to make Junit testcase itself  I18n. So that input to Junit testcase can be non-english data. It will check the flow of non-english data in application. I have done that for one of module for my product. i.e Portal Server.

Objective

I18n'ed JUnit test case input which will help in doing I18n testing in JUnit.

How to do it?

I18n input of Junit Test case

  • What I mean here by I18n'ed test case is to remove hard coded input(which are localizable) from Testcase and load them using ResourceBundle. So to test those testcases with non english value would be easy (creating a new resource bundle with non-english values).
  • so from developer point of view , instead of hard coding input to test case in Java program they have to read from resource bundle. Then g11n(or developer) team can create resource bundle for other language and integrate in same framework. It will then run the same testcases with non English values. this way we can reuse junit testcases to perform i18n unit testing.

Example

ex. Let me give you example of one of CMS junit testcase
Testcase to create category.
source : CategoryTest.java
Before :

testCategoryCreation{

String categoryName="TestCategoryCreation";

...(code to test creation) ...

}

In this testcase category name "TestCategory" is hardcoded in java program. I have removed this hardcoding and load this value from Resource Bundle.

Now : CategoryTest.java

One new class variable needs to be added.

ResourceBundle rb;

Two new function will be added:


Locale selectLocale(){
return new Locale("en");
}
void loadResourceBundle(){
rb = ResourceBundle.getBundle("CustomResource",selectLocale()) ;
}
CategoryTest(){
loadResourceBundle();
...
}
testCategoryCreation{
String categoryName=rb.getString("TestCategoryCreation");
...(code to test creation)
... }

As shown above, resource bundle will be load once for Test program and it will be used by all the testcases within it. so now when developer run this testcases in english environment and it will work as it was previously.

Now How to leverage this testcase for I18n testing.
Create one interface: I18nTestFramework - which will have one method
Locale selectLocale();

Base Testcase Program has to implement this Interface.


i.e public CategoryTest extends TestCase implements I18nTestFramework

How to provide Localized value for Testcase

Then we need to create New Test program for language in which you want to do I18n testing which will have only one method selectLocale(). ex. "ja".

CategoryJaTest extends CategoryTest {
Locale selectLocale(){
return new Locale("ja");
}
}

Now on whenever Junit framework will be executed. It will execute this test program also and since locale set to "ja" . Resource bundle for japanese will be loaded. which will be used to test all the tests of CategoryTest since it is extended by CategoryI18nTest.

You can create Category<locale>Test java program , ResourceBundle(properties) file for all the language in which I18n testing needs to be done.

(phir milenge ... (cu later)) 

Monday Jun 09, 2008

I18n Best practices for web application- Part 1

I have created clear and concise best practices list for making your application I18n'ed.

General

  • No hardcoded strings
  • Data format I18n
  • Number format I18n
  • Usage of  Apostrophe in Message Format class. 
  • Currency I18n
  • Sort I18n
  • Calendar I18n
  • Read/write file in UTF-8
    • Use methods like :public OutputStreamWriter(OutputStream out, String charsetName); public InputStreamReader(InputStream in, String charsetName)
  • Locale lookup should be correct - Find out what lookup mechanism needs to be used.
  • Send/Receive Locale when communicating with other compoments
  • If any property in property file does not need to be locaized put a comment on top property file.
  • Make sure property file does not have duplicate properties
  • When you use Message format to parse string make sure "'"(aphostrophy) in string is escaped.
  • File system name should be allowed to i18n

JSP Prgoram

  • Encoding of file should be "UTF-8"
    • <%@ page contentType="mimeType ;charset=characterSet ?" | "text/html; charset=UTF-8" %>
  • Use JSTL taglib to include the messages in the JSP file.
  • Request and Response should be in "UTF-8"

Resource Bundle

  • Should not use duplicate key properties file
  • Try to create less number of Property files
  • Try to use same message which already found in other components or even products if it make sense.
  • Do not change layout of message once translated
  • Property file for UI message and Log message should be SEPARATE
  • Message should not be slang or culturally sensitive
  • Always create _en.properties file along with base property file

GUI

  • Each button/textfield should have enough space to accommodate localized sentences
  • Layout and Text rendering
  • Input validation should consider multibyte data

Logging

  • If you are writing a WEB UI, the UI should appear in the client locale, not the server locale. Logging should be done in the server locale to avoid log messages with mixed data from different locales. Use UTF-8 as encoding for the web UI, because UTF-8 supports all languages.

Database

  • Reading and writing data to/from database should be in UTF-8

JUnit Testcase

  • Make sure all your Junit Testcase is I18n'ed. This will help in great way to integrate i18n testing in Unit testing.

AJAX

Refer AJAX I18n

References

Wednesday Jan 30, 2008

I18n 360' testing approach at Step-IN 2008 Conference

Recently I had been to one of the most beautiful hotel aka. palace in Bangalore.  No marks for guessing its Leela Palace.
I had been there before but this time reason was not for pubbing (athena)  as it used to be.I was there to give talk at Step-in 2008 Conference (ASIA's biggest testing conference - as organizers claims).

My talk was on Internationalization(I18n) 360' testing approach , the concept which close to my heart and job. Its fresh , unique and quick at results. Implemented in Portal Server 7.2 aka.OpenPortal development.

My topic was selected for Pre-conference tutorial , A long 210 minutes( 3 hours and 30 minutes). It was indeed long time to stand , deliver and interact but my passion toward the subject was a big help. Curiosity of delegates in terms of their questions regarding I18n , their day to day use cases , their business case has never made me felt that it was long. In fact I had to rush a bit in last stage of Presentation.

 

Let move to what I shared there.

It was started with Introduction of I18n world (I18n , L10n , G11n) which also covered all the basic terminologies like Character , Charset , Unicode , UTF-x , Glyph , Font , Locale. My audience was enjoying every bit of information as most of them were aware about I18n but this details made them realize what the hack it is. This session was resulted in lots of questions and interesting discussion. They allowed to move ahead with the promise that I will spend quality time after talk  to share more of my understanding about I18n world.

Next session was regarding I18n 360' testing approach , which in simple teams means involving I18n in every stage of product development , starting as early as product planning. How I18n play role in Requirement phase , Design phase , Implementation phase and most importantly QA phase. Even in documentation also I18n has role to play. Usage of real time example and case study has made this session most entertain one. If it got you curious enough here is the Presentation for you

After that I had shown them live demo of how to do i18n testing in stand alone application and web application. This followed by demo of  I18n testing automation using Open Source automation tool Canoo. At last as a common practice Question and Answers.

 It was a wonderful and memorable experience for me. It was first time I had been to such reputed conference to represent Sun. I had been to Universities for talk earlier but this was unique and challenging as audience was mix of managers , consultants  and testing engineers.

Tuesday Sep 18, 2007

AJAX Internationalization (I18n) in Portal Server

In latest release of Portal Server AJAX capability has been introduced in the form of AJAX containers.

First ,  What is  AJAX containers in Portal Server?

The AJAXTableContainerProvider integrates Asynchronous JavaScript and XML (AJAX) capabilities at the portal framework level. The container provides asynchronous loading of individual channels and portlets. Therefore, a slow channel or portlet will not affect the loading time of the other channels and portlets on a page, improving overall performance. The AJAXTableContainerProvider includes AJAX based interaction for all container controls and features which provides for a much richer and faster user experience.

Let move to core item of this blog,  AJAX I18n.

Some clerification before we procedd.
- Sources(js , jsp , java) are referred from OpenPortal repository.
Location of ajaxcontainers in portal source code:
.../openportal/portal/samples/ajaxcontainers

Thare are two containers provided in Portal Server
1) AJAXEditContainer
2) AJAXTableCotainerProvider

For example I will use "AJAXTableCotainerProvider".

AJAXTableCotainerProvider:

In this AJAX container thare two primary source for I18n

1)Java Server Pages(JSP) Internationalization(I18n)
Location in OpenPortal repository:
.../openportal/portal/samples/ajaxcontainers/src/channels/AJAXTableContainerProvider

How these JSPs are I18n'ed?
JSP I18n is quite straight forward , lets understand it by example of one of the jsp from source i.e. table.jsp

Steps for JSP i18n: 

1.1) Defining I18n tag library:
<%@ taglib uri="/tld/i18n.tld" prefix="i18n"%>
This tag libarary contains various tags required to perform I18n functions.

1.2) Loading the resource bundle:
<i18n:setBundle baseName="ajaxcontainers" var="ajaxcontainersRB"/>
This tag will load resource bundle "ajaxcontainers"  based on user locale.
Have a look at base resource bundle ajaxcontainers.properties

1.3) Fetching value from resource bundle
<i18n:message key="loading.page" bundle="ajaxcontainersRB"/>
"message" tag will fetch value from resource bundles for specified key.

2)Java Scripts(JS) Internationalization(I18n)

You have reached to most interesting part of this blog i.e Java Script I18n.
The fact that java script does not have any defined framework for I18n makes it very interesting and scope of innovation. In this blog I will explain one of the innovative and well proven approach used for I18n of java script in AJAX container.

Location of Java Scripts in Source :
.../openportal/portal/samples/ajaxcontainers/src/web-src/js

Following steps are require for JS I18n.
2.1) Method to fetch all the properties from resource bundle in table.jsp.
public JSONObject getLocalizedStrings(java.util.ResourceBundle rb)
      throws JSONException {
JSONObject localizedStrings = new JSONObject();
      if (rb == null) {
                      localizedStrings.put("x", "y");
      }
      if (rb != null) {
              for (Enumeration e = rb.getKeys() ; e.hasMoreElements()  ;)   {
                      String key = (String)e.nextElement();
                      String val = rb.getString(key);
                      localizedStrings.put(key, val);
              }
      }
      return localizedStrings;
}

2.2)  Creating  "tableContainerProviderModel" JSON <http://www.json.org/> object in table.jsp.
JSONObject tableContainerProviderModel = new JSONObject();
This JSON object will be used to hold localized values in terms of property along with other properties

2.4) Next step would be to add localized value to this JSON object using method to fetch localized value from resource bundle.
tableContainerProviderModel.put("localizedStrings", getLocalizedStrings(ajaxcontainersRB));

2.5) Now we need variable which can be used in Java Script. Creating "containerModel" variable in table.jsp and assigning "tableContainerProviderModle" JSON Object to it.

<script type="text/javascript">
      var containerModel = <%=tableContainerProviderModel.toString(4)%>
      var pageStyles = new sunportal.AJAXPageStyles('<dtpc:getStaticContentPath/>');
      pageStyles.init();
</script>

2.6) Loading of java script in table.jsp using <script> tag
<script type="text/javascript" src="<dt:scontent/>/desktop/ajaxcontainers/js/sunportal/AJAXUtils.js"></script>

We done with our intial setup i.e these steps will be performed intially when user makes request to server.  All the further interaction will be through Java Scripts and localized string will be fetched from JSON object.

How lets see?

2.7)  JS Function below used by all JS to fetch localized value : sunportal.getLocalizedString
This  function is defined in AJAXUtils.js as below:

sunportal.getLocalizedString=function(){
var _1=arguments[0];
var _2=containerModel.localizedStrings[_1];
for(var i=1;i<arguments.length;i++){
var j=i-1;
var re=new RegExp("\\\\{"+j+"\\\\}","g");
_2=_2.replace(re,arguments[i]);
}
return _2;
};
As you can see this function takes "key" as argument and fetch loalized value from containerModel.localizedStrings Map which we have poplulated in step 2.2.

2.8) Let see how this functions is used in Java Scripts at Client side.
eg. AJAXChangeLayout.js
td.innerHTML=sunportal.getLocalizedString("full.top.portlets");
This function used by all the Java Scripts to fetch localized values without  accessing resource bundles stored at Server side.

I hope , I am able explain it correctly ... Any questions/comments are welcome ...:-).

Friday Aug 03, 2007

OpenPortal at University Day in Bangalore!!

    I have been involved with Sun's University relations initiative for a few months now. The present project involves evangelizing Sun's technologies in colleges so as to capture the mindshare of students.

    Acharya Institutes is situated on the outskirts of Bangalore and we needed to get there by 8AM, so I had to pull myself out of the bed very early (I rarely see early mornings , it was beautiful :))

    As we reached the institute, all of us were amazed by this huge campus of 100 acres with around 20 buildings under construction and mind blowing architecture (award winning apparently) . Mr Vishwanathan, who is the HoD and other college authorities welcomed us and took us to the auditorium.

    Audience was final year students from various colleges of Bangalore and adding to our surprise, there were International students (from Africa) with basic knowledge of Java. My goal was to get them interested in OpenPortal and Portlets. I started with some visuals of OpenPortal samples and iGoogle to give them an idea of Portal Server. I introduced the concepts of Portlet development and explained why this is interesting since they are eager to figure out why this is important to them. I then presented the main topic of the day “OpenPortal Portlet Container” and “NetBeans Portal Pack plugins” by showing several screenshots and a demo.

     Demo got everyone's absolute attention as they were amazed to see with just few clicks on NetBeans (pre-installed with Portal Pack and configured with OpenPortal Portlet Container), a Portlet got developed, deployed and most importantly running in our OpenPortal Portlet Container. It was pretty cool to show this demo to these students (customers!) I was thrilled to see the curiosity and excitement in their eyes! Kudos to NetBeans Portal Pack and OpenPortal Portlet Container team for such a wonderful gift to industry.

    This demo resulted in healthy Q&A session as students showed a lot of interest and asked great questions about Portlet Development and NetBeans. One of common question worth to mention was “Can they really contribute with limited knowledge they have”, my answer was “Yes” with reasoning, and they can learn a lot from experts in an Open Source project and develop themselves. There is lot of scope for contributions in any Open Source project, not just 'Write Code' as they seem to think.

    As a final remark I have informed them Portlets are here to stay and conquer the web development world so be a part of it … Pointed them to Open Portal, OpenPortal Portlet Repository.

    Next in turn was “Application development using NetBeans” by Amol Desai then much deserved lunch. At the end we have received Memento and “In Office Golf Kit” which was best part according to my team mates when I reached office after long journey in peek hour traffic of Bangalore. An End of Wonderful Experience but Beginning of OpenPortal at University programs in Bangalore.

(from left) Prof . Vishwanathan , Amol , Myself , Srinith , Students @ Acharya

 


 

About

Mahipalsinh Rana

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today