Archive for the ‘Java’ Category

Setting up Oracle XE to work with IBM WebSphere Portal IWWCM

Published by Rob on May 21st, 2008 - in Java, Oracle, WebSphere Portal

In order to make the Oracle XE database work with IBM WebSphere Portal IWWCM there are a number of steps to perform.

  1. Ensure that the database is accessible using the Oracle SID (see the early post Oracle SID!=Service name)
  2. Next you will need to increase the number of proceses that can be connected to the instance, as there are a large number of connection pools set up by the system, which the database will need to support. The following command worked in my environment:
        ALTER SYSTEM SET PROCESSES=150 SCOPE=SPFILE;
  3. After having performed these base setup commands and options, you can run the IWWCM migration scripts to migrate existing data.
  4. To ensure that the connection pooling works correctly, you will need to grant the following privileges to public:
    1
    grant select on pending_trans$ to public;

    1
    grant select on dba_2pc_pending to public;

    1
    grant select on dba_pending_transactions to public;

  5. Finally, grant the following privilege to each user to which connections are made from the application server.
        grant execute on dbms_system to <user>;

After having performed these steps you will have an Oracle XE database that will work happily with IWWCM.

Memory Settings for Eclipse on OSX

For the regular readers of my blog, you may be wondering why I am stepping over to the dark side, and using Eclipse. Not to fear – it is just for an Eclipse plug-in. Using the right tool for the right job is important to always do.

When recently using the SAP Memory Analyzer tool to look at a large (1GByte) hpprof output file, I was running out of Heap space, so was lookin for where to set the -Xmx and -Xms settings on Eclipes. It was somewhat harder to find than desired, and the internet didn’t tell my as quickly as I wanted, so here’s the answer (while the video at http://www.brooksandrus.com/bog_assets/screencasts/eclipsejvmheap/ was cool, a simple text blog entry would have made me happy).

The eclipse.ini file in Eclipse.app/Contents/MacOS is the file that needs to be edited. (not the one that for me was close in location /Applications/eclipse/configuration/ eclipse.ini was bad, /Applications/eclipse/Eclipse.app/Contents/MacOS/ eclipse.ini was good).

After finding the right eclipse.ini, setting the memory is straightfoward. Simpley update the -Xmx and -Xms settings (see: The Sun Tuning java guide for what these do if you don’t know).

Using BeanShell for Enterprise Development

Published by Rob on May 12th, 2008 - in Development, Java

BeanShell promises lightweight scripting for Java. Support for BeanShell is included with the Apache ant build tool, and it is targetted to be included as a part of the standard edition of Java with JSR-274. I’ve recently included BeanShell in a project, and will share what I have achieved with this, and some of the motivations and considerations when doing this.

My primary reason for adding BeanShell was that we use some heavy enterprise APIs. We are able to mock these out for development, but have experienced consistent issues when working with the APIs, primarily caused by missing or misleading documentation. This means that there is often a need to write code to understand the APIs. To deploy to the application server takes about 3 minutes, and the wrestling required to make the code work is often much more painful that expected. It is pain which motivates the creation of some nice tools, and this pain motivated the creation of a simple tool using BeanShell.

In it’s simplest form, the tool allows the execution of code on the server that has been input into a text area. This is a powerful tool for developers and admins, but is somewhat (ok very) risky from a security standpoint, and needs to be treated as such.  Getting BeanShell up and running is really straightforward.  I've only got a couple of recommendations and comments to make about doing so.

  1. Integrating BeanShell itself is really straightforward. Simply have a servlet (action/jsp/whatever) which reads in a request parameter, uses an instance of the beanshell interpreter class to eval the string. The quickstart guide makes it clear and easy to get started (makes it quick to start :) ).
  2. In order to make the interpreter useful, you will want to have add imports of the important packages for your application (such as to your Data Access Layer), and also the set some variables on the context. Prepend the imports to the scripts before they are eval'ed, and set the variables using the interpreter set(String, Object) method.
  3. In addition to the result of the script, and the application variables that I put on the interpreter, I also add in a PrintStream, making it easy to output debug information.
  4. Finally, it is really useful to keep track of what statements have been executed, and the results of the statements.  Save these to the session, and list these on the page. A simple javascript function makes it possible to replace the contents of the text area with previous scripts, and also the output, and result messages.  This rounds out a simple little tool nicely.1

By doing this, it is possible to explore APIs, and work at a very quick pace. Whenever something behaves unexpectedly on the server, one can quickly explore what has happened, and produce the updates to production code, which are required to make this work. BeanShell is a powerful tool for serverside development, and well worth considering adding to your toolkit.

1 – for extra credit, it is really easy to keep a list of previous statements on the page, and use the javascript above to keep track of the history.

Oracle SID != SERVICE_NAME

… or how to setup your Oracle XE database so that JDBC database urls will work like they traditionally have.

For many years Oracle has been telling everyone Oracle SID != SERVICE_NAME.

Java Developers have always smiled and nodded with this statement as the oracle thin client jdbc url would always be something like:

jdbc:oracle:thin:@server:1521:SERVICE_NAME, or
jdbc:oracle:thin:@server:1521:SID 

which would work because databases always either had the SID and SERVICE_NAME as the same, or there was some magic that the Oracle listeners did to make this work.

Somewhere in the past couple of releases of Oracle this transparent mapping has ceased to exist. In particular Oracle 10G XE out of the box doesn’t support this sort of option.

It seems the new preferred way of doing Oracle JDBC URLs is:
jdbc:oracle:thin:@server:1521/SERVICE_NAME.

I discovered this information while working with making a WebSphere Portal instance use Oracle as it’s data repository. The documentation for doing this suggested using the old school SID style of url.  I tried this first as it felt familiar, but things didn't work.  Changing the url to use the PORT/SERVICE_NAME approach looked to mostly work, but didn’t quite spread the changes to everywhere required. I needed to run WPSConfig connect-database to update all the hidden bits of configuration around the portal installation. Unfortunately, this failed as the wpsconfig script calls out to programs that expect the url to follow the old school syntax (the stack trace show an index-out-of-bounds exception while parsing connect srings). I then had to go in and update the configuration on the database to enable connection via the old SID style approach.

This was done by working on the listener.ora file
(C:\oraclexe\app\oracle\product\10.2.0\server\NETWORK\ADMIN) to add in a SID_LIST_LISTENER entry for the database.

This was simply to add:
(SID_DESC =
    (GLOBAL_NAME = XE)
    (SID_NAME = XE)
    (ORACLE_HOME = C:\oraclexe\app\oracle\product\10.2.0\server)
)

to the SID_LIST of the SID_LIST_LISTENER

I ended up with a SID_LIST_LISTENER that looks like this:

SID_LIST_LISTENER =
    (SID_LIST =
        (SID_DESC =
            (SID_NAME = PLSExtProc)
            (ORACLE_HOME = C:\oraclexe\app\oracle\product\10.2.0\server)
            (PROGRAM = extproc)
        )
        (SID_DESC =
            (SID_NAME = CLRExtProc)
            (ORACLE_HOME = C:\oraclexe\app\oracle\product\10.2.0\server)
            (PROGRAM = extproc)
        )
        (SID_DESC =
            (GLOBAL_NAME = XE)
            (SID_NAME = XE)
            (ORACLE_HOME = C:\oraclexe\app\oracle\product\10.2.0\server)
        )
    )

This configuration works with an out of the box Oracle XE installation, and lets me use the jdbc:oracle:thin:@server:1521:SID.

Howto Keep Version History in a Single Table Using Hibernate

Published by Rob on December 7th, 2007 - in Development, Hibernate, Java

 

Problem:

Keeping a history of data in a single table in hibernate, where all versions are treated as first class objects.

That is to say that every record in the database will be immutable. New records will be created with a new version number, but the old versions will never change. They are kept as the history of changes.

Solution:

When querying objects from hibernate ensure that they are initialised, and then detach them from the session:

1
Hibernate.initialize(result);

1
session.evict(result);

Then when saving the objects, ensure that the ID is null, and call the

1
session.save(businessObject)

to save them.

1
//set the id to null to ensure that a new record is stored in the database.

1
businessObject.setId(null);

1
incrementVersion(businessObject);

1
session.save(businessObject);

The final part of the puzzle is to ensure that the class has it’s persistence properties set correctly, either by setting the annotations or configuring the xml file.

The important settings are:

  1. To help prevent updates, and improve performance set the mutable attribute on the class to false.
  2. The collection objects on the class have the lazy attribute set to false (ensuring that they are loaded when the object is retrieved, and the evicted object will work).

Following these steps will provide you with a versioned business object. It will be possible to retrive, and store the object to the database using the Hibernate search functionality.

Storing Large Strings with Hibernate 3

Published by Rob on November 15th, 2007 - in Hibernate, Java

If you want to store a large string in hibernate 3 in a relatively transparent way, the

1
text

 mapping type is your friend.  This will allow you to map a java String type to a database clob type, and store arbitary length large content items.  In previous versions (according to google) this took a fair bit of fiddling around, and required custom types (which required much magic to work in a cross platform manner).  Thankfully the

1
text

mapping type exists, making life much easier.

 

So in the old school pre 1.5 annotation world, we can take the class with a long string and map it using the code and mapping below:

1
class Profile {

    

1
...&#160;&#160;

1
&#160;&#160;private String bio;

    

1
...&#160;&#160;

1
2
3
&#160;&#160;public String getBio(){<br />
       &#160;&#160;&#160;&#160;return bio;<br />
       &#160;&#160;}

1
2
3
&#160;&#160;public void setBio(String bio){<br />
       &#160;&#160;&#160;&#160;this.bio=bio;<br />
       &#160;&#160;}

1
}

and the mapping file:

1
2
&#60;?xml version=&quot;1.0&quot;?&#62;<br />
       &#60;!DOCTYPE hibernate-mapping PUBLIC

    

1
&quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;

    

1
2
3
4
5
&quot;http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd&quot;&#62;<br />
       &#60;hibernate-mapping&#62;<br />
       &#160;&#160;&#160;&#160;&#60;class name=&quot;Profile&quot; table=&quot;profiles&quot;&#62;<br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;...<br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#60;property name=&quot;bio&quot; type=&quot;text&quot; length=&quot;

10485760

1
2
3
&quot;/&#62;<br />
       &#160;&#160;&#160;&#160;&#60;/class&#62;<br />
       &#60;/hibernate-mapping&#62;

This will let you store a large string in hibernate, automatically handling the mapping between clob data and string data.  The important parts of the xml snippet above are the type and length attributes.  Both are required to ensure that the clob will be created of the right length.  The default for the length is 255, which is great when you are storing shortish strings in a varchar, but not so good when you want a large string in a clob.

Todays Cool Keyboard Shortcut In IntelliJ…

Published by Rob on October 24th, 2007 - in Development, Java

..is cmd-opt-F7 (in Mac)

CMD OPT-F7 displays a list of  usages of the currently item (var/method) throughout the project, listing them in a nice inline dropdown list.  This is great for navigating around the usages of a method finding how they are used in context, before applying an aggressive refactoring.  I discovered the short cut recently while performing one of the other find usages options in Intellij (cmd-F7) and getting fat fingered and hitting CMD-OPT F7.

Why Use a Sledgehammer When There is a Bulldozer Lying Around

Published by Rob on October 17th, 2007 - in Java, WebSphere Portal

Or finding which websphere portal shared jar contains the class you need.

When trying to find which WebSphere portal jar file contains the PortletServiceHome class (for the unitiated there are 225 jars in the portal shared directory), I was pushing my very rusty shell scripting skills.  I pulled in Doug for some advice, and we were working through various sledgehammer approaches and talking to google about what to do.  We were playing with various combinations of pipes and shell commands to get the output of the find (or ls) to play with the jar tvf and grep.

AJ then piped in with just use grep, and he really meant.. just use grep, and take advantage of the fact that the contents of the jar will be listed in plain text in the jar.

The command I ended up with was (running in PortalServer/shared/app):

grep PortletServiceHome *.jar

Which returns:
Binary file wp.pe.api.standard.jar matches
Binary file wp.pe.rt.impl.jar matches

Then running jar -tvf across the first jar shows that this is what I need to compile against and work with when developing.

The bulldozer of a grep across jars is the best way to find which jar contains a class that you are looking for1.

1 – While this feels really inefficient, it is actually better than having to spawn off multiple the jar processes which other approaches would involve.

An AJAX Iteration

Published by Rob on September 21st, 2007 - in Development, Ephox, Java

I'm currently unwinding after only just managing to see all the cards1 move from the "this iteration" column2 to the "done" column3 on our XP storyboard.  While the estimates had us coming in at the same spot as earlier iterations, it ended up being quite a bit tighter than the earlier ones had been.  

There are a few reasons why, but the biggest cause would be the fact that we were moving from the comfortable world of Java actions into world of some heavy ajax ui.  I spent some time working with libraries and researching how to do things like JSON in Java, and AJAX popups.  It was a fun and stretching iteration, with plenty of prettiness to show for the work.

I learnt a lot including:

 AJAX with Prototype is great.  Prototype provides all the methods you want in the library.  I would consistently find the methods I would want in the library such as $('id') (to find an element with an id), and the serialize methods for forms are really nice. Kudos to the prototype team for a great library.

Scriptaculous provides nice effects that build on prototype.  The Autocompleter is worth the price of admission.

The popup library by Stuart Rackham works really nicely.  He doesn't have access to Safari so hasn't tested it there, but now I have, and while not all the features work, it is possible to produce nice popups that work in Safari with this library.  He has built this on top of Scriptaculous and prototype and he has done a great job.

JSON for passing around data is cool.  The whole eval on the client thing is a bit scary, but (almost as I expect now) at least prototype provides a nice evalJSON method to help alleviate a little my security paranoia. Of the millions of JSON libraries available, the standard java one works ok. (what's not to like about a library with a license that includes the text "The Software shall be used for Good, not Evil."  Mightier JSON libraries exist that do all kinds of magic, but the simple approach is working to start us off.

 

The biggest missing piece in my JavaScript Development is the fact that it isn't TDD. There seem to be bits and pieces out there, but nothing that is jumping out and saying "use me I just work.". The main reasons I am wanting to do Test Driven Javascript are for quality and pleasure. Doug spent some time researching options for acceptance testing of E2 front to back (including the javascript), which will definately help out the quality, but it still would be great to have a viable TDD option.

I never would have expected to say this, but it has been fun doing the JavaScript and AJAX in the past iteration. It really is much better than in the past, and I can see it only getting better.

1 – The card contains a unit of work

2 – The list of cards we are planning on getting done in the iteration, but haven't started working on yet

3 – The list of things we have finished this iteration.

Deploying A Portlet to Jetspeed 2

Published by Rob on August 3rd, 2007 - in Java

This page exists primarily as a note to self regarding deployment of Portlets to a development version of Jetspeed.

The key is simple. 

When deploying a webapp to Jetspeed you need to put it into ${TOMCAT_HOME}/webapps/jetspeed/WEB-INF/deploy/. This information is available in the article on Jetspeed-2 Deploy tools jetspeed site, which (when I looked) wasn't on the google search for jetspeed deployment, which is instead littered with many references to maven (which still ranks as one of the most disappointing open source projects in my mind).

Knowing this information is important because (for someone who has experience in hacking with webapps), it is tempting to just drop a war into ${TOMCAT_HOME}/webapps and assume that things will just work.  This really doesn't work, and gives errors like:

"Title Error: Cannot pass a null PortletDefinition to a PortletEntity" 

Unfortunately (again at time of develpment) google didn't really make it clear that this error can be caused by the fact that Jetspeed can't load your class.

After getting the portlet deployed, you'll also want to hack up a psml file and put it in ${TOMCAT_HOME}/webapps/jetspeed/WEB-INF/pages.  

Something like this forms a half decent starting point.

<page id="simple"
    xmlns="http://portals.apache.org/jetspeed"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://portals.apache.org/jetspeed http://portals.apache.org/jetspeed-2/2.1/schemas/psml.xsd">

    <defaults skin="orange" layout-decorator="tigris" portlet-decorator="tigris"/>
    <title>This is the simplest portlet in the world</title>
    <metadata name="title" xml:lang="fr">La plus simple portlet du monde</metadata>

    <fragment id="simplest" type="layout" name="jetspeed-layouts::VelocityTwoColumns">
        <fragment id="simplest-first" type="portlet" name="E2::PortletWrapper">
            <property layout="TwoColumns" name="row" value="0" />
            <property layout="TwoColumns" name="column" value="0" />
        </fragment>
    </fragment>

    <security-constraints>
        <security-constraints-ref>public-view</security-constraints-ref>
    </security-constraints>
</page>

Of course if you want you can use various jetspeed tools to try and make this bit easier, but for the old skool guys who want to be in control, this is a pretty decent option.

© Rob@Rojotek