Wednesday, December 31, 2008

Locale.getDefault() to get default locale

To get the default Locale of the host JVM invoke Locale.getDefault()

Creating a unique, sorted list of countries in Java

Following code snippets creates a hash of unique, sorted country names with the country name as the key and ISO 639 country code as the value


Locale[] allCountries = Locale.getAvailableLocales();
Map uniqueCountries = new TreeMap();
for (Locale currentCountry : allCountries)
{
if (! "".equals(currentCountry.getISO3Country()))
{
String countryName =
currentCountry.getDisplayCountry(
FacesContext.getCurrentInstance().getViewRoot(
).getLocale());
uniqueCountries.put(
countryName, currentCountry.getISO3Country());
}
}


Then you can use the map to do whatever. Here is an example to generate a list of JSF SelectItems

List countrySelectItems = new LinkedList();

for (String currentCountryCode : uniqueCountries.keySet())
{
countrySelectItems.add(
new SelectItem(
uniqueCountries.get(currentCountryCode),
currentCountryCode
)
);
}

Saturday, November 29, 2008

Maven Basics

This post is based on this serverside article.

The basic concept of Maven is a project. A project can create only one artifact. E.g. a web project can create a war file as an artifact. To work around this restriction of one artifact per project, a project can have sub-projects. Each of the sub-projects can have a artificat by themselves. The project's responsibility now is to take the sub-project's artifacts and make one artifact.

A project is defined as any directory with a project.xml in it. If sub-directories of a project directory have project.xml then they are project directories too.

All project artifacts (artifacts resulting from projects) are stored in repositories. There are remote and local repositories. Local repository is created in ./maven/repository. In windows its "c:/Documents and Settings/. In maven dependencies are expressed between artifacts. Like to create the artificat my_application.war, there is a dependency on version 1.0.2 of commons-logging. This dependency specification is laid out in project.xml. When maven is executed, it reads the project.xml, finds that you depend on commons-logging, picks up the specified version of commons-logging from the remote repository and dump it into the local repository and the version in the local repository is used to build your project's artifact (war in this case).
The structure of repository on a windows box:
//jars/.
c:/Documents and Settings/babu.subburathinam/maven/repository/commons-loggging/jars/commons-logging-1.0.7.jar

Instead of each project having a copy of its dependencies, all libraries are lodged in a repository and all projects share the libraries available in the repository. Each project will inturn publish its artifact on to the repository. This process of a project publishing its artifact is called "install"ing in maven lingo. This process of each project publishing its (snapshot and release) artifacts to a central repository helps in continuous integration. This is how: Daemon processes running in build servers can build each project with its updated dependencies several times a day, deploy and test the built artifacts. Thus, integration issues if any will surface much before the release date of a specific project.

Inputs to maven:
One of the input files to maven is Project Object Model (POM) file. This file describes the project to maven. This file has the following structure:

01
02
03 3
04 Sample-Maven-Project
05 sample-project
06 1.1
07 Sample Maven Project
08
09
10
11
12
13
14
15
16
17


Line 2 - Root element of XML
Line 3 - This tag is unused but needed.
Line 4 - A directory with this name is created in Maven repository to hold the artifacts of projects sharing the group id.
Line 5,6 - The id and version is used to create the artifact name as -.jar
Line 7 - Name of the project

The project Management section has project information such as the organization, its web site, location of SCM (Software configuration management), deployment and issue tracking sites, developer lists, mailing lists, etc...Most of this section is optional. The contents of project.xml can be extended. Most of the content is defined at the enterprise level and each project can override what is appropriate to it.
An example of the project management section:
01
02 Foobar Travels
03 http://www.foobar.com
04 http://www.foobar.com/logo.jpg
05

06
07 2003
08 foobar.blah.*
09 http://www.foobar.com/project-logo.jpg |
10 Project description goes here
11 Short Description
12 http://www.foobar.com
13 http://jira.foobar.com
14 http://staging.foobar.com
15 /etc/staging
16 /etc/builds
17
18
19 cvs:pserver:anon@foobar.com:/foo
20 http://scm.foobar.com
21

22
23
24
25 Dev List
26 subscribe-dev@foobar.com
27 unsubscribe-dev@foobar.com
28

29 ...
30 ...
31

32
33
34
35 Srikanth Shenoy
36 shenoy
37 srikanth@srikanth.org
38

39 ...
40 ...
41


* Lines 01-05 - Organization details
* Line 08 - Top level package for the project
* Line 09 - Project Logo
* Line 12 - Project web site
* Line 14 - The site where the project is hosted
* Line 15 - Physical location of project deployment
* Line 16 - Physical location where the project distributions are available
* Lines 18-21 - SCM to access the project source
* Lines 23-31 - Mailing list for the project
* Lines 33-41 - Developers in the project


General structure of a maven project:

Maven Project Root
- maven.xml // Project definition file
- src // source directory
-- conf // Configuration within source
---xyz.properties // config gile
--java // java source
---com
----access
-----dev
------Hello.java
- test // test directory
-- conf // test configuration
---abc.properties // test config file
--java // java test files
---com
----access
-----dev
------TestHello.java

Project dependency section

In this section the project indicates all the dependecies that it has on artifacts of other projects. An example:

01
02
03 BeanUtils
04 commons-beanutils
05 1.5
06


commons-logging
commons-logging
1.0.3


castor
castor
0.9.4.3



Line 1 - Starts the dependencies
Line 3 - The artifact that this project depends on is at the directory named "BeanUtils" in the repository
Line 4,5 - The artifact name is "commons-beanutils-1.5.jar" (using -.jar)

Project build section

This section indicates the location of source, test and resource files. This is defined at the org. level or main project level for sub-projects to follow. If this section is not specified, no build ever gets done. Once build is over, all unit tests specified in the unit test section are executed. The contents of this section should match the actual layout of the code in the filesystem.

01
02 srikanth@srikanth.org
03 ${basedir}/src/java
04 ${basedir}/test/java
05
06
07 **/*Test.java
08

09

10
11
12
13 ${basedir}/src/conf
14
15 *.properties
16

17

18

19




* Line 02 - Email address to send notification about the build status
* Line 03 - Folder containing the source files for the project. The source can be java, jsp and so on.
* Line 04 - Directory containing the unit test files for the project.
* Lines 05-09 - The test file name pattern to run after the build is completed
* Lines 11-19 - Resources to be copied in case a jar is created.

Project reports sections
Once build is done, reports and documentation about the build are generated.
e.g.


maven-changes-plugin
maven-jdepend-plugin
maven-checkstyle-plugin
maven-pmd-plugin
maven-junit-report-plugin
maven-clover-plugin
maven-changelog-plugin
maven-file-activity-plugin
maven-developer-activity-plugin
maven-file-activity-plugin
maven-license-plugin
maven-linkcheck-plugin
maven-jxr-plugin