Tug’s Blog

Redis, NoSQL and more…

Groovy 101: Extracting XML From Your Database

| Comments

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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:

1
2
3
4
5
6
7
8
9
10
11
12
<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.

Groovy 101: Importing XML in Your Database

| Comments

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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..&lt; 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..&gt; 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 item element.Using the 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)

Oracle Web Services: Sharing Session Between Client Calls

| Comments

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 Wervices 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

1
2
3
4
5
6
...
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)

1
2
3
4
...
 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.

1
2
3
4
5
6
7
8
9
10
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:

1
2
3
4
5
6
7
8
9
10
11
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

| Comments

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.

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 10_g_ 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:

1
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.

Grails 0.4 Released…

| Comments

The Grails developer team is pleased to announce the release 0.4 of Grails. The release can be obtained from the downloads page.

Notable improvements include:

  • ORM enhancements with support for more relationship types, easy transactions and criteria building, constraints to SQL schema mappings, and upgrade to Hibernate 3.2
  • All-new non-invasive plugin system for writing reusable plugins for Grails applications
  • Greater Spring integration thanks to a new syntax for scripting Spring (http://grails.org/Spring+Bean+Builder) and an upgrade to Spring 2.0
  • Easier unit testing of controllers and custom taglibs
  • Validation improvements, including support for inherited constraints and simplified size constraint handling
  • Automatic encoding of unsafe HTML characters and URL parameters in all scaffolding and taglibs
  • Fixes to support Grails on more containers such as GlassFish and over 200 issues and bugs resolved in JIRA
  • Grails now ships with Groovy 1.0!

Oracle JDeveloper (10.1.3.2) / WebCenter Available for Download

| Comments

Oracle JDeveloper (10.1.3.2.0) extends the SOA development features from the previous release by introducing the Oracle WebCenter extension. Oracle WebCenter Suite combines the standards-based, declarative development of JavaServer Faces (JSF), the flexibility and power of portals, and a set of integrated Web 2.0 services to boost end-user productivity. Oracle WebCenter Suite provides the tools and services to embed portlets, content, customizable components, Web 2.0 content, collaboration and communication services directly into your JavaServer Faces (JSF) application. These WebCenter features are in addition to the large set of new features that were introduced in Oracle JDeveloper 10.1.3.0 and 10.1.3.1.0. For more information on WebCenter, visit the Oracle WebCenter Suite page on OTN.

  • Build Portlets
    • Build JSR 168 portlets
    • Build Oracle PDK-Java portlets
    • Expose JavaServer Faces (JSF) applications as portlets
    • Preconfigured OC4J within Oracle JDeveloper
  • Consume Portlets
    • Consume JSR 168 portlets through WSRP
    • Consume other WSRP portlets
    • Consume Oracle PDK-Java portlets
    • Inter-component communication
  • Business User Web 2.0 Enterprise Mashup Tools
    • Rich Text / Blog Portlet
    • WebClipping Portlet
    • OmniPortlet
  • Runtime Customization
    • showDetailFrame
    • panelCustomizable
  • Integrate Content using JCR 1.0
    • Access content using data controls
    • Access to many content repositories
  • WebCenter Services
    • Secure Enterprise Search (SES)
    • Oracle Communication and Mobility Server (OCMS)
    • Oracle Content Database
    • Discussions
    • Wiki
  • Declarative Security
    • ADF Security Wizard
    • Authorization Editor
  • Lifecycle Tool
    • Embedded Lifecycle Tool
    • Command Line Lifecycle Tool
    • ANT Tasks

Some links:

Apple Released Beta Version of Dashcode

| Comments

Dashcode is a new application for developing Dashboard widgets coming in Mac OS X 10.5 (Leopard). This tools is already available as developer preview on Tiger.

I have installed it and it is really great, easy to use, helping you to create good looking widgets. As usual, like all the Apple development tools it is really intuitive. More than a long blog entry on this I am just pointing you to the DashCode page on the Apple site:

Using Oracle Data Integrator on Mac OS X

| Comments

Oracle just released on OTN a new product “Oracle Data Integrator” (ODI), I wanted to quickly take a look to the product, so I have downloaded it and installed it on my Mac. This product is a 100% Java based solution that you can quickly installed on mac following these steps:

1- Download and unzip ODI from OTN download page (bottom).

2- Open a terminal Window and go to the folder where you have unzipped ODI, you should have the following content:

1
2
3
4
5
6
- external
- index.htm
- oracledi
- oracledilwd
- oracledimn
- setup

Open the index.html and select the Getting Started Guide, this will help you to learn more about ODI using a comprehensive scenario.

3- Setup the environment variables:

1
2
3
export ODI_JAVA_HOME=/Library/Java/Home/   (need to be Java 5)

export ODI_HOME=<path to ODI installation folder>/odi/oracled

4- Go to the $ODI_HOME/bin

1
cd $ODI_HOME/bin

5- Start the HSQL databases that contain the sample application and data:

1
./startdemo.sh &

This command starts 3 different instances: repo (metadata repository), src (source db), trg (target db) that are used in the Getting Started guide. To stop the DB run the script ./stopdemo.sh.

6- You can now start the designer too using the command:

1
./designer.sh &

Select the Getting Started project and when you are in the designer switch to the Mac OS X look and feel ;-), using the Menu “Look And Feel > Standard > Mac OS X”.

These are the first steps to start with Oracle Data Integrator, you can now follow the Getting Started Guide, to learm more about the product, and since your environment is set you can run any of the command documented in this guide.