Hi Everyone,
Below is an email I sent to Larry long ago (pre Apache Knox) about the i18n and logging framework in the code. I thought I has put something more thorough together previously but I can find it. This information needs to be migrated to the Knox wiki.
Kevin.

-------- Original Message --------
Subject:        Internationalization
Date:   Thu, 03 Jan 2013 15:16:29 -0500
From:   Kevin Minder <[email protected]>
To:     Larry McCay <[email protected]>



Larry,
I've been meaning to mention this but I keep forgetting. I have a model for i18n for both resources and message logging that I really like and I want to make sure you understand how it works and can start using it.

Basically take a look at

ambari-gateway/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayMessages.java
ambari-gateway/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayResources.java

and then at the usages of some of these methods in say
ambari-gateway/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServer.java

As private statics dynamic proxies are created for the interfaces.
private static GatewayResources res = ResourcesFactory.get( GatewayResources.class ); private static GatewayMessages log = MessagesFactory.get( GatewayMessages.class );

Then you can call the methods anywhere in the class
  log.failedToParseCommandLine( e );
in this case
@Message( level = MessageLevel.FATAL, text = "Failed to parse command line: {0}" ) void failedToParseCommandLine( @StackTrace( level = MessageLevel.DEBUG ) ParseException e ); means log a message at level fatal and create the message from the text and the toString of the param. If the logger level is debug or finer the stack trace is also logged.

Resources are similar.
        System.out.println( res.gatewayVersionMessage(
            buildProperties.getProperty( "build.version", "unknown" ),
            buildProperties.getProperty( "build.hash", "unknown" ) ) );
in this case
  @Resource( text="Apache Hadoop Gateway {0} ({1})" )
  String gatewayVersionMessage( String version, String hash );
will merge the version and hash information into the text and return it.

I haven't done it yet but I'll eventually write and APT <http://docs.oracle.com/javase/1.5.0/docs/guide/apt/index.html> plugin to extract the text in the annotation out into resource bundles for translation. *Note: My preference is to have one resource and message class for each module but sometimes this doesn't make sense.*

If you are interested the code can be found at
ambari-gateway/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/**

*Note: The performance key here is to avoid as much as possible any string concatenation. Pass all the parts to the message method and use the format text to do the concatenation.* *Under the covers it uses the **java.text.MessageFormat class so that is the syntax required.*

Kevin.


Reply via email to