Tuesday, February 27, 2007

Spring and Oracle: I21 blog entry about Oracle contribution to Spring

Rod Johnson in the Interface21 team blog entry named "Oracle Contributing Oracle Application Server Integration Code to Spring Framework" described the contribution of Oracle to Spring framework.

This contribution allows Spring applications to leverage the OracleAS transaction manager and its features. In addition to this contribution and some previous investments of Oracle in Spring, see the Oracle and Spring page of OTN; Oracle continues to invest more and more in Spring, as contributor or as user since, as stated by Rod, Spring has an important roles in Oracle Fusion Middleware, and its upcoming Services Component Architecture offering.

Monday, February 26, 2007

OC4J tip: changing the server information

When you are running OC4J in stand alone mode you are using the HTTP server that is bundle with it. This HTTP server returns by default for the HTTP information the following information: Server: Oracle Containers for J2EE If you want to change that you just need to set the http.server.header property. For example, java -Dhttp.server.header="My blog on Oracle" -jar oc4j.jar will now look like:
HTTP/1.1 200 OK
Date: Mon, 26 Feb 2007 21:52:53 GMT
Server: My blog on Oracle
Last-Modified: Mon, 09 Oct 2006 19:17:10 GMT
Accept-Ranges: bytes
Content-Length: 19882
Connection: close
Content-Type: text/html

Thanks to James Kirsh for this very useful tip...

Thursday, February 22, 2007

The first International Grails eXchange 2007

Packed with presentations on Grails, Groovy, Ajax & Web 2.0 and JavaEE and the core technologies that support the Grails technology, the first Grails eXchange conference (London from May 30th to June 1st) will be the place to be for any member of the Groovy/Grails community... You can already register on the conference web site: http://www.grails-exchange.com

Come to see speakers including Grails project lead Graeme Rocher, Groovy project lead Guillaume LaForge, creator of Spring Rod Johnson, Dojo's Alex Russel and Dylan Schieman as well as speakers from technology-leading organisations such as Google, Interface 21, JBoss, Oracle & Sun Microsystems

Tuesday, February 20, 2007

Groovy 101: Extracting XML from your database

In the previous entry I showed how you can easily take an XML feed and insert the content in the database. Let's do the opposite now, meaning taking the data out of your database as XML. In this post I am using the Sql Dataset again but to create an XML document, using the Groovy MarkupBuilder.

import groovy.sql.Sql;
import groovy.xml.MarkupBuilder;

def sql = Sql.newInstance("jdbc:oracle:thin:@//tgrall-linux:1521/XE",
                                      "HR", "HR", "oracle.jdbc.driver.OracleDriver")
def set = sql.dataSet("EMPLOYEES");

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)

xml.employees() {
  set.each { emp ->
    employee(first: emp.first_name , last: emp.last_name) {
      email( emp.email )
    }

  }
}

println writer.toString();
As you can see, I use the builder to create XML Elements and attributes employee(first: emp.first_name , last: emp.last_name), I do reference the current record of the dataset (emp), and all this in very simple and concise code. This will give a result like:
<employees>
<employee first="'Steven'" last="'King'">
  <email>SKING</email>
</employee>
<employee first="'Neena'" last="'Kochhar'">
  <email>NKOCHHAR</email>
</employee>
<employee first="'Lex'" last="'De">
  <email>LDEHAAN</email>
</employee>
...
</employees>

So once again quite simple.

Sunday, February 18, 2007

Groovy 101: Importing XML in your database

A friend of mine was looking for an easy way to import some XML content in his database. You have many ways to do it. But the easiest for a Java/Groovy developer is to use Groovy, and I have create this small example for him.

Groovy provides really simple solution to parse XML and manipulate your database. The following sample reads an RSS new feed and import the title and link in a table named NEWS that contains two columns TITLE and LINK.


import groovy.sql.Sql;

def rssFeed = "http://www.javablogs.com/ViewDaysBlogs.action?view=rss";
def xmlFeed      = new XmlParser().parse(rssFeed);

def sql = Sql.newInstance("jdbc:oracle:thin:@//tgrall-linux:1521/XE",
                         "GROOVY",
                         "GROOVY",
                         "oracle.jdbc.driver.OracleDriver")
def set = sql.dataSet("NEWS");

(0..< xmlFeed.channel.item.size()).each {
   def item = xmlFeed.channel.item.get(it);
   def title = item.title.value[0];
   def link = item.link.value[0];
   println("Importing $title ...");
   set.add(TITLE: title, LINK: link);
}

First I create a Groovy SQL object and a DataSet to manipulate my data. sql.dataSet("NEWS"). Do not forget, if like me you are using Oracle database, to add the Oracle JDBC driver to your classpath ;-)

Then I create a loop on each items of the RSS feed I am using: (0..> xmlFeed.channel.item.size()).each {...}. As you see, Groovy XML help me to parse, and navigate the XML document.

Like any Groovy iterator you have access to an implicit object available in the loop "it", so I can get the item using the Groovy XML : xmlFeed.channel.item.get(it)

Then you can get the different values you want of the element.Usingthe dataset.add method, you can insert them in the table.This is done using the value pairs notation column:value, this looks like: set.add(TITLE: title, LINK: link)

Thursday, February 15, 2007

Oracle Web Services: Sharing Session between client calls

OracleAS Web Services Runtime provides a support for stateful Web Services that is based on HTTP /Servlet session. Some people will probably say that Web Services should not be stateful, or at least not based on the protocol... However, today most of Web Services are using HTTP, and in some specific cases it is very useful to be able to have a state.

In this post, I am not explaining how to enable stateful services and clients, since it is documented in the Java Classes and Stateful Web Services chapter of the developer guide. Here I am show you how you can using client side programming share the same state (session) between different web services calls (even different services running in the same server side application).

The tip used here is about the association of cookies to the client instance (JAX-WS Stub or Call object). Here the code you have to write to do that using DII, will be very similar when using static Stub

1. Enable the state management

...
Service service = sf.createService(qName);
QName port = new QName("CartService");
Call call = service.createCall(port);
call.setProperty(Stub.SESSION_MAINTAIN_PROPERTY, Boolean.valueOf(true));  // this is necessary to be able to manipulate cookie
...

2. Create a Map that contains the Cookies and assign it to the call (or Stub)

... Map cookieMap = new HashMap();
call.setProperty(ClientConstants.COOKIE_MAP, cookieMap);
...

This specific step associates a map that will contains all the cookie with the call/stub instance. You will be able then to manipulate the Map to get or set the cookies.

3. How to get the JSESSION cookie

private Cookie getJSessionCookie(Call call) {
   Map cookies = (Map)call.getProperty(ClientConstants.COOKIE_MAP);

   if (cookies != null && !cookies.isEmpty()) {
       Iterator it =  cookies.values().iterator();
       while (it.hasNext()) {
           Cookie cookie = (Cookie)it.next();
           if (cookie.getName().equals("JSESSIONID")) {
               return cookie;
           }
       }
   }

   return null;
}

Note that the Cookie object is an instance of Oracle HTTPClient.Cookie.

4. Utilizing the Cookie

So now you have all the information to be able to get the Session information when the stateful conversation has started;

In this example each time the call.invoke() is done, a counter is incremented on the server.

Call call = service.createCall(port);
call.setProperty(Stub.SESSION_MAINTAIN_PROPERTY, Boolean.valueOf(true));  // this is necessary to be able to manipulate cookie
 Map cookieMap = new HashMap();
call.setProperty(ClientConstants.COOKIE_MAP, cookieMap);
... // The session will only be created after the first invoke
 call.invoke(...); // counter = 1 call.invoke(...); // counter = 2 since on the same session

... // the session is now created so you can get the cookie
 Cookie mySession = getJSessionCookie(call)
...

You can now use the cookie in another call using the following code:

mySession ..  // was extracted from the call #1
...// now I am creating a new call instance (myNewCall) that could be in another class
Call myNewCall = service.createCall(port);
myNewCall.setProperty(Stub.SESSION_MAINTAIN_PROPERTY, Boolean.valueOf(true));  // this is necessary to be able to manipulate cookie
Map cookieMap = new HashMap();
// add the cookie to the map this will add the cookie to the HTTP request so it will be associated to the same session (/state)
cookieMap.put(mySession,mySession);// associate the cookie Map to the call
myNewCall.setProperty(ClientConstants.COOKIE_MAP, cookieMap);
...
myNewCall.invoke;  // counter = 3 since we share the same session
...
Using this sample you have 2 instances of a client calling a service and reusing the same session -state-. You can also use the same approach to have 2 different clients talking to different services and share the same session. To do that you will have on the server side to use the HTTP Session directly to store your data between calls, and share it between services.

Oracle Application Server 10gR3: iHat

When preparing a complex topology, where you have multiple HTTP servers ,talking with many OC4J instances, it is sometimes hard to understand what is going on. Oracle Application Server Control provides the complete view of your topologies in different pages. If you want to have a quick overview of your topology, you may want a more graphical view of your Oracle Application Server instance.

One of the tool that I use a lot to present OracleAS and its components is OracleAS Hi-Av Tool 10g (10.1.3) also known as iHat. This utility uses Oracle Process & Notification Manager (OPMN) to gather information of all the components used in your topology, representing it in a nice graphical viewer. In addition to use iHat to show the different components, I do also use that to validate my configuration.


iHat Grid View

In this case I am showing a specific instance, that contains 3 OC4Js instance, with 2 of them that are in the same group. When using iHat you will notice that you can, in addition to have some monitoring information start, stop, restart the different components directly from the view. How do you install and start iHat?

1- Download iHat from Utilities for Oracle Application Server 10g OTN page

2- Unzip it, and this becomes the $IHAT_HOME

3- You have an ORACLE_HOME that is pointing to one of the OracleAS instance, so you can start iHat using the follling command:
java -cp $ORACLE_HOME/opmn/lib/optic.jar:$IHAT_HOME/ihat.jar oracle.ias.opmn.ihat.WebServer 8181 $ORACLE_HOME

Using this command, iHat is starting an HTTP server on port 8181, and use the OPMN configuration of the $ORACLE_HOME that I you have entered as parameter. iHat provided other parameters such as the host-name and OPMN port if you want to connect remotely without dependency on the $ORACLE_HOME. All this, is documented in the readme file located in the iHat folder.

Wednesday, February 7, 2007

New Groovy Plugin for Oracle JDeveloper 10g

You can find a first release of the Groovy extension for JDeveloper on the Groovy site:
 http://groovy.codehaus.org/Oracle+JDeveloper+Plugin

The plugin creates a new JDeveloper library for Groovy 1.0, allows you to create and run scripts. I hope to be able to provide more feature such as syntax coloring, structure recognition, ... lot of ideas here...