Sunday, June 24, 2007

Maven 101

Project Object Model (POM) is the fundamental unit of work in Maven. It is an xml file that contains information about the project and configuration details used by Maven to build the project.
build directory -> target
source directory -> src/main/java
test source directory -> /src/main/test
The goals or plugins are now configured in the pom.xml. When executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal.
Configuration that can be specified in the POM are:

  • project dependencies

  • plugins

  • goals

  • build profiles

  • project version

  • description

  • developers

  • mailing lists


The minimum requirement for a POM are the following:

  • project root

  • modelVersion

  • groupId

  • artifactId

  • version


Project: the top level element in all Maven pom.xml files
modelVersion This element indicates what version of the object model this POM is using.
groupId: This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of aproject and is typically based on the fully qualified domain name of your organization.
artifactId: this element indicates the unique base name of the primary artifact being generated by this project. The primary artifact for a project is typically a JAR file.
Introduction to the Standard Directory Layout
Having a common directory layout would allow for users familiar with one Maven project to immediately feel at home in another Maven project. The advantages are analogous to adopting a site-wide look and feel.
src/main/java: Application/Library sources
src/main/resources: Application Library resources
src/main/filters: Resource filter files
src/main/assembly: Assembly descriptors
src/main/config: Configuration files
src/main/webapp: Web application sources
src/test/java: Test sources
src/test/resources: Test resources
src/test/filters: Test resource filter files
src/site: Site

Introduction to Repositories
A repository in Maven is used to hold build artifacts and dependencies of varying types.
There are strictly only two types of repositories: local and remote.
The local repository refers to a copy on your own installation that is a chache of the remote downloads, and also contains the temporary build artifacts that you have not yet released.
The remote repository refer to any other type of repository, accessed by a variety of protocols such as file://and http://. These repositories might be truely remote repository set up by a third party to provide their artifacts for downloading. Other "remote" repositories may be internal repositories set up on a file or HTTP server within your company, used to share private artifacts between development teams and for releases.
$mvn archetype:create -DgroupId=mavenbook -DartifactId=my-app
archetype:create is called a Maven "goal". The plugin is the prefix "archetype" and the the goal is to "create". -Dname=value pairs are arguments.
A phase is a step in what Maven calls the "build lifecycle".

ref

Maven war plugin
The default resource directory for all maven2 projects is src/main/resources which will end up in target/classes and in WEB-INF/classes in the war.

src/main/resources -> target/classes
src/main/resources -> WEB-INF/classes (war file)

ref

Maven2 step by step

Friday, June 8, 2007

Domain Model

Implementing a Domain Model

Business concerns should be separated from the crosscutting concerns (such as transactions, persistence and authorization) - leakage of concerns.

Domain model implementation is such an important piece of code that it shouldn't depend on other Java APIs. For example, code in the domain model shouldn't perform JNDI lookups or call the database via the JDBC API.This alows you to reuse the domain model implementation virtualy anywhere. Most importantly, it makes it easy to unit test the domain model outside of any application server or other managed environment.

Transparent persistence means a complete separation of concerns between the persistent classes of the domain model and the persistence logic itself, where the persistent classes are unware of - and have no dependency to - the persistence mechanism.

Hibernate compares the objects by value - not by object identify - to determine if the property's persistent state needs to be updated. For example, the following getter method won't result in unnecessay SQL UPDATEs:

public String getFirstname() {
return new String (firstname);
}

However, there is one very important exception. Collections are compared by identify!
The following code should be avoided:

public void setNames(List namesList) {
names = (String[]) namesList.toArray();
}

public List getNames() {
return Arrays.asList(names);
}

It is recommended practice to use one mapping file per persistent class. The convention is to give the file the same name as the mapped class, appending an hbm suffix: for example, Category.hbm.xml.

Property and class mappings

Hibernate uses reflection to determine the Java type of the property and therefore the type name can be omitted.

hbm2ddl tool can be used to generate database schema.
not-null attribute should be used to report illegal null property values without going to the database.



Detection of illegal null values is mainly useful for providing sensible exceptions
at development time. It isn’t intended for true data validation, which is outside
the scope of Hibernate.

Derived properties

The value of a derived property is calculated at runtime by evaluation of an expression. You define the expression using the formula attribute. For example

formula="TOTAL + TAX_RATE * TOTAL"
type="big_decimal"/>

The given SQL formula is evaluated every time the entity is retrieved from the database.

Identify versus equality
Object identity, ==, is a notion defined by the Java virtual machine. Two object references are identical if they point to te same memory location.

On the other hand, object equality is a notion defined by classes that implement the equals() method, sometims also referred to as equivalencee. Equivalence means that two different (non-identical) objects have the same value.

Database identify - Objects stored in a relational database are identical if they represent the same row, or equivalently, share the same table and primary key value.

Wednesday, June 6, 2007

Shell Scripting

At login time

Your login shel consults /etc/profile which is owned by root,
your home ~/.bash_profile which is owned by yourself,
the /etc/bashrc which is owned by root and your home ~/.bashrc which owned by yourself.

Each time a new shell is started

It executes the /etc/bashrc and ~/.bashrc.

Notice that starting a new shell without logging out and in again (a child process) means that the shell has no need to run the profile files again.

Useful command combination

Count number of files in subdirectories
find . -type -f | wc -l

Monday, June 4, 2007

Java Major and Minor Versions

Java compiler
major version: 47
classes compiled by jdk 1.3

major version: 48
classes compiled by jdk 1.4

major version: 49
classes compiled by jdk 1.5

Compiled by Ant javac

major version: 47
target= 1.3

major version: 48
target = 1.4

major version: 49
target = 1.5

classes compiled by a newer version of jdk can not be run by an older version of jvm without specifically specifying the targeted source version.

Friday, June 1, 2007

Web Application Deployment Descriptor

All servlet containers that are compliant with the 2.3 servlet specification are required to support the following types of deployment information:

Initialization parameters
Session configuration
Servlet declarations
Servlet mappings
Application lifecycle listener classes
Filter definitions and mappings
Welcome file list
Error pages

Optional:

Tag library mappings
JNDI References

When a web application is installed in a container, the container is responsible for assigning a ServletContext to it. There is a single instance of a ServletContext object for each web application depolyed in a container.

The ServletContext provides an external view of the web container environment for a servlet. A servlet can use the ServletContext object to gain access to external resources, log events, and store attributes and objects that other servlet instances in the same context can access. It's essentially an application-scope shared resource.

Because a servlet is associated with a specific web application, all requests that begin with a specific request path (known as the context path) are routed to the web application associated with that servlet. Servlets associated with the default application have an empty string ("") as the context path.




Application events provide notifications of a change in state of the servlet context (Each web application uses its own servlet context) or of an HTTP session object.

You write event listener classes that respond to these changes in state, and you configure and deploy them in a Web applicatioin. The servlet container generates events that cause the event listener classes to do something. In other words, the servlet container calls the methods on a user's event listener class.