Wednesday, February 25, 2009

Maven practice

- Unzip the maven installable and show the settings.xml. Set the repository folder to the repo in the maven training area. Also uncomment the proxy and change settings.

- Then run the create archetype command.
mvn archetype:create -DartifactId=test -DgroupId=sample.oracle.toplink

- Display the folder that is generated. Run the "mvn install" command to display how build, packaging and installing is done.

- Copy the resulting pom to SampleApp folder and make changes to artifactId, packaging(to pom), add modules like this:
<modules>
<module>Model</module>
<module>ViewController</module>
</modules>

- Copy pom to Model and ViewController and do similar things. Apply the parent tag these pom files:
<parent>
<groupId>sample.oracle.toplink</groupId>
<artifactId>TestApp</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
Change the artifact id, name, packaging and remove the dependencies for now.
- In pom.xml of Model & ViewController put in the build tag with the proper source directory because the project is a jdev project.
<build>
<sourceDirectory>src</sourceDirectory>
</build>
- Now compile with the command "mvn compile" and you should get compile errors. Firstly, because the wrong jdk is being used. Correct it by putting the following in both pom files:
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
- Now, to the pom.xml of Model & ViewController add resource folder and inclusion details in the build tag to the pom file which may look like this:
<resources>
<resource>
<directory>src</directory>
<includes>
<include>**/*.gif</include>
<include>**/*.jpg</include>
<include>**/*.jpeg</include>
<include>**/*.png</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*-apf.xml</include>
<include>**/*.ejx</include>
<include>**/*.xcfg</include>
<include>**/*.cpx</include>
<include>**/*.dcx</include>
<include>**/*.wsdl</include>
<include>**/*.ini</include>
<include>**/*.tld</include>
<include>**/*.tag</include>
<include>**/*.jpx</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>

- Now to install dependent jars of oracle into the local repo.
<use utility>
- Add dependencies to the pom file.
- Do the same to the web project. But it requires different treatment. Firstly, add the Model project as a dependency to this project. That is add a dependency like this:
<dependency>
<groupId>sample.oracle.toplink</groupId>
<artifactId>Model</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Then add the following plugin so that the packaging that takes place is of war type:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webappDirectory>public_html</webappDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
- Now to create an ear file one must create a new folder called ear in the parent folder that contains the Model and ViewController project. This will be like a new maven project but only dealing with creation of the ear file. Copy the pom file created in the first stages to this folder and do the following changes:
- Add the parent tag like before. Make changes to the artifactId, name and packaging(it must be ear now).
- Add the war project as a dependency. It should look like this:
<dependencies>
<dependency>
<groupId>sample.oracle.toplink</groupId>
<artifactId>UI</artifactId>
<version>10.1.3.3</version>
<type>war</type>
</dependency>
<dependency>
<groupId>sample.oracle.toplink</groupId>
<artifactId>Model</artifactId>
<version>10.1.3.3</version>
<type>ejb</type>
</dependency>
</dependencies>
- Add a build tag like this to be able to build an ear file:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<displayName>TestApp</displayName>
<description>Test Application</description>
<modules>
<ejbModule>
<groupId>sample.oracle.toplink</groupId>
<artifactId>Model</artifactId>
</ejbModule>
<webModule>
<groupId>sample.oracle.toplink</groupId>
<artifactId>UI</artifactId>
<contextRoot>/test</contextRoot>
</webModule>
</modules>
<earSourceExcludes>*.jar</earSourceExcludes>
</configuration>
</plugin>
</plugins>
</build>
- This should create an ear file. But when you try to deploy the ear file in the oc4j server, you will start getting errors about a few classes not found. These were the jars that got left out during installation of jars in the local repo because the xml file didn't have mappings for a few libraries. I still haven't been able to find the file that contains all mappings for libraries. These jars you will have to manually install and put in pom as dependencies. For now, the libs that are'nt available are open source ones. These are the easiest to use. You just define them as dependency, they will be automatically installed in the local repo.
- Make the folders src/main/resources/META-INF and put the orion-application.xml file into it, which you get from building an ear file from the jdeveloper. (It is always advisable to make a war deployment profile in jdeveloper, before going ahead with this exercise. Also, try deploying it on a standalone OC4J instance and see it running.) You will also need to add this to the pom.xml so that this file is dropped in the right place before packaging(note the resources tag only).
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>../${project.artifactId}-${project.version}</targetPath>
</resource>
</resources>
<plugins>

Tuesday, February 17, 2009

Developing an ADF/Faces-Toplink application

In this post I'd like to address a few common issues that developers face while creating an ADF/Faces and Toplink application. For such an application, usually this is the flow of steps:

  1. This is assuming the usage of JDeveloper for development.
  2. Create an application with two projects. One for model(which will contain toplink objects) and one for the UserInterface.
  3. First, in the model project, start by creating "Java objects from tables". Make the toplink POJOs Serializable. Create the named toplink queries(find, update and insert) methods. Create the Facade Session bean to facade the named queries. Then create a DataControl out of the Session Bean. Mind you, this Datacontrol is good as long as things work by dragging and dropping into the JSF pages.
  4. Now, create the JSF pages in the UI project. The best way to do this is by going to the Overview window of the faces-config.xml file. Here you drop the jsf pages and navigations. Then you double-click on the jsf pages and create the actual jsf jsp pages.
  5. Sometimes when you'd like to call the facade session bean methods on your own then you have the job of looking it up. But a few pointers first, that I have noticed: Always make the facade session bean implement the Remote interface(implementing only Local is logical but doesn't work :( ), following is how the SessionBean looks like:

  6. @Stateless(name="SessionEJB1")
    public class SessionEJB1Bean implements SessionEJB1, SessionEJB1Local {


    and the following is the code to lookup the session bean in JDev 10.1.3.2/3:

    InitialContext ctx=new InitialContext();
    Object o = ctx.lookup("SessionEJB1");
    SessionEJB1 ejb = (SessionEJB1)PortableRemoteObject.narrow(o, SessionEJB1.class);
    return ejb;


  7. One big challenge in such applications is the creation of new objects or in other words new rows in a table, how would you handle the new ID to be assigned. If you are using Oracle database(I do not experience with other databases) the best way to handle assignment of new IDs is to use native sequencing. In the top-level of the toplink mapping set the sequencing to use "Native sequencing". Obviously, you will need to have sequences created in the database for the tables in which new rows are going to be created. In the toplink mapping window go to the details of a table and set the sequencing information by using the sequence name you created for that table in the database. That's it! You will not have to set the ID parameter in the new java object before persisting it. The method to use is the persistEntity(Object) method in the facade session bean. It returns the persisted object which contains the newly assigned ID. Cool!

Monday, February 16, 2009

More JSF

Inverting the bean value requires the ! (or not) operator:
<h:inputtext rendered="#{!bean.hide}">

You can concatenate plain strings and value binding expressions, simply by placing them next to each other. Consider, for example,
<h:outputtext value="#{messages.greeting}, #{user.name}!">

Here is a typical use of a method binding expression.
<h:commandbutton action="#{user.checkPassword}">


The h:dataTable tag iterates over data to create an HTML table. Here's how you use it:
<h:datatable value="#{items}" var="item"><h:datatable value="#{items}" var="item">
The value attribute represents the data over which h:dataTable iterates; that data must be one of the following:
  • an array

  • an instance of java.util.List

  • an instance of java.sql.ResultSet

  • an instance of javax.servlet.jsp.jstl.sql.Result

  • an instance of javax.faces.model.DataModel

Java Server Faces - obtaining value binding in java

FacesContext context = FacesContext.getCurrentInstance();

ValueBinding binding = context.getApplication().createValueBinding("#{user.name}");

String name = (String) binding.getValue(context);