Sunday, December 23, 2007

Happy Holidays!

Christmas gift suggestions:
  • To your enemy, forgiveness;
  • To an opponent, tolerance;
  • To a friend, your heart;
  • To a customer, service;
  • To all, charity;
  • To every child, a good example;
  • To yourself, respect."
(by Oren Arnolds)

I whish you all a merry Christmas and a really wonderful, healthy, happy and successful 2008.

Wednesday, November 7, 2007

How to setup Java Logging in OC4J

by Eduardo Rodrigues

Sometime ago I had a great idea for a web application logger that would basically log events as a feed (RSS or ATOM). After a lot of thinking of the best way to implement my idea, I decided to follow the Java Logging Framework. So, basically, all I had to do was to create my own FeedHandler extending java.util.logging.Handler and also create a new java.util.logging.Formatter extension for each specific standard feed format. I started implementing RSS20Formatter based on RSS 2.0 specification.

It took only 2 days for me to be completely satisfied with my own little Java Feed Logging library (which came to work really well, by the way) but then I had to make it work with my J2EE applications running in OC4J 10.1.3.x. And I wondered what would be the best way to do that.

I had read OC4J’s documentation on logging and saw that it is possible to define a log handler in OC4J’s logging configuration file j2ee-logging.xml declaring its class as a subclass of java.util.logging.Handler, but that’s all the information available on how to configure OC4J loggers using the Java Logging Framework because the focus seems to be on the Oracle Diagnostic Logging Framework instead.

I already use (and recommend) the ODL Framework in my J2EE applications running in OC4J mainly for its integration with web-based Oracle Enterprise Manager 10g Application Server Control Console which turns log viewing and analysis into a much more easy and comfortable experience. So, my first attempt was to add a new <log_handler> to <log_handlers> element in my j2ee-logging.xml file, basically copying my existing <log_handler> and changing its attributes and properties like that:

Original j2ee-logging.xml file

<?xml version = '1.0' encoding = 'iso-8859-1'?>
<logging_configuration>
   <log_handlers>
      <log_handler name="contaweb-handler"
                   class="oracle.core.ojdl.logging.ODLHandlerFactory"
                   formatter="oracle.core.ojdl.logging.ODLTextFormatter">
         <property name="path"
                   value="../application-deployments/log/ContaWeb"/>
         <property name="maxFileSize" value="10485760"/>
         <property name="maxLogSize" value="104857600"/>
         <property name="encoding" value="ISO-8859-1"/>
         <property name="useSourceClassAndMethod" value="true"/>
         <property name="supplementalAttributes"
                   value="J2EE_APP.name,J2EE_MODULE.name"/>
      </log_handler>
   </log_handlers>
   <loggers>
      <logger name="tim.contaweb" level="ALL" useParentHandlers="false">
         <handler name="contaweb-handler"/>
      </logger>
   </loggers>
</logging_configuration>


New j2ee-logging.xml file

<?xml version = '1.0' encoding = 'iso-8859-1'?>
<logging_configuration>
   <log_handlers>
      <log_handler name="contaweb-handler"
                   class="oracle.core.ojdl.logging.ODLHandlerFactory"
                   formatter="oracle.core.ojdl.logging.ODLTextFormatter">
         <property name="path"
                   value="../application-deployments/log/ContaWeb"/>
         <property name="maxFileSize" value="10485760"/>
         <property name="maxLogSize" value="104857600"/>
         <property name="encoding" value="ISO-8859-1"/>
         <property name="useSourceClassAndMethod" value="true"/>
         <property name="supplementalAttributes"
                   value="J2EE_APP.name,J2EE_MODULE.name"/>
      </log_handler>
      <log_handler name="contaweb-rss-handler"
                   class="oracle.br.logging.feed.FeedHandler"
                   formatter="oracle.br.logging.feed.RSS20Formatter">
         <property name="path" value="../applications/ContaWeb/ContaWeb/rss"/>
         <property name="level" value="INFO"/>
         <property name="title" value="ContaWeb Log Events"/>
         <property name="link" value="http://localhost:8888/em/"/>
         <property name="limit" value="100"/>
      </log_handler>

   </log_handlers>
   <loggers>
      <logger name="tim.contaweb" level="ALL" useParentHandlers="false">
         <handler name="contaweb-handler"/>
         <handler name="contaweb-rss-handler"/>
      </logger>
   </loggers>
</logging_configuration>


Note that I just added my new log handler to my existing logger named “tim.contaweb” which was already used by my application. In other words, every single log event generated by this logger will be directed to both handlers in a transparent way. The problem was it didn’t work at all :((((

Making the long story short, after googling a lot, I first found that the <property> elements and the “formatter” attribute in a <log_handler> are effective only if its class is oracle.core.ojdl.logging.ODLHandlerFactory. Then I also found that my answer was inside the ODL JAR file (ojdl.jar): the logging configuration DTD/Schema.

If you look inside ojdl.jar (which is generally located at $ORACLE_HOME/diagnostics/lib) you’ll find files oracle.core.ojdl.logging.logging-config.dtd and oracle.core.ojdl.logging.logging-config.xsd and, after examining them, you’ll notice the non-documented element <logging_properties> which is the first optional element inside the root element <logging_configuration>. In fact, this <logging_properties> only takes <property name=”” value””/> elements and has the exact same behavior as those properties declared in file $JAVA_HOME/jre/lib/logging.properties, and those are the ones needed by Java Logging Framework. So, my working j2ee-logging.xml file is:

<?xml version = '1.0' encoding = 'iso-8859-1'?>
<logging_configuration>
   <logging_properties>
      <property name="oracle.br.logging.feed.FeedHandler.formatter"
                value="oracle.br.logging.feed.RSS20Formatter"/>
      <property name="oracle.br.logging.feed.FeedHandler.path"
                value="../applications/ContaWeb/ContaWeb/rss"/>
      <property name="oracle.br.logging.feed.FeedHandler.level"
                value="INFO"/>
      <property name="oracle.br.logging.feed.FeedHandler.title"
                value="ContaWeb Log Events"/>
      <property name="oracle.br.logging.feed.FeedHandler.link"
                value="http://localhost:8888/em/"/>
      <property name="oracle.br.logging.feed.FeedHandler.limit"
                value="100"/>
   </logging_properties>

   <log_handlers>
      <log_handler name="contaweb-handler"
                   class="oracle.core.ojdl.logging.ODLHandlerFactory"
                   formatter="oracle.core.ojdl.logging.ODLTextFormatter">
         <property name="path"
                   value="../application-deployments/log/ContaWeb"/>
         <property name="maxFileSize" value="10485760"/>
         <property name="maxLogSize" value="104857600"/>
         <property name="encoding" value="ISO-8859-1"/>
         <property name="useSourceClassAndMethod" value="true"/>
         <property name="supplementalAttributes"
                   value="J2EE_APP.name,J2EE_MODULE.name"/>
      </log_handler>
      <log_handler name="contaweb-rss-handler"
                   class="oracle.br.logging.feed.FeedHandler"/>

   </log_handlers>
   <loggers>
      <logger name="tim.contaweb" level="ALL" useParentHandlers="false">
         <handler name="contaweb-handler"/>
         <handler name="contaweb-rss-handler"/>
      </logger>
   </loggers>
</logging_configuration>


It’s also very important to say that all log handlers declared in file j2ee-loggin.xml will be automatically instantiated during OC4J’s startup process. Because of this, all needed classes must be reachable from OC4J’s boot class loader. There were some options to achieve that but, in my case, the easiest was to deploy my new logging library to a JAR file and just put it in $ORACLE_HOME/j2ee/home/lib/ext directory.

That’s all for now. I hope it’s useful.

Sunday, September 23, 2007

SQLDeveloper setup trick

by Eduardo Rodrigues

Yes. The title of this post really is SQLDeveloper. I did not write it wrong. But what it has to do with Java? Well, the answer is simple: Oracle SQLDeveloper is a very useful and interesting database tool for developers and is build in Java. It's like an IDE for Oracle DB and has somethings in common with JDeveloper. For those who are now curious, Oracle SQLDeveloper may be downloaded from Oracle Technology Network. I use it and certainly recommend it for developers (not for DBAs).

With all that said, let's go directly to the point...

One of the things SQLDeveloper has in common with JDeveloper is how its setup is done. However, in it's latest versions (1.2.1 was the latest version at the time this post was written), SQLDeveloper has a small setup trick.

As you will notice, SQLDeveloper does not come with an installer program. All you have to do is uncompress the downloaded zip archive and run sqldeveloper.exe located in its root directory. You may choose to download it with a bundled JDK 1.5.0_06 or without it and then set it up to use a more recent JDK (version 6 is already certified). Looking inside the expanded directory tree, you'll find an "<SQLDeveloper_Install_Dir>/SQLDeveloper/bin" subdirectory containing the files "sqldeveloper.conf" and "jdk.conf". The former is the setup starting point and the latter is empty when you download SQLDev without the bundled JDK.

Edit this file and notice the "IncludeConfFile" directives. The first one points to an "ide.conf" file located in "<SQLDeveloper_Install_Dir>/jdev/bin" subdirectory. This file works for SQLDev much like "jdev.conf" does for JDev. Some important things you may setup there is Java Heap maximum size and, specially for Windows users, the keepWorkingSetOnMinimize system property which avoids SQLDev's working memory to be paged out by Windows when you minimize it (believe me, you want this system property set). You may set those like this:

# setting maximum heap to 256 MB
AddVMOption -Xmx256M

#setting keepWorkingSetOnMinimize
AddVMOption -Dsun.awt.keepWorkingSetOnMinimize=true

If you look at the same subdirectory, you'll find another "jdk.conf" file, this time with the following content:

###############################################################
# Oracle IDE JDK Configuration File
# Copyright 2000-2006 Oracle Corporation.
# All Rights Reserved.
###############################################################
#
# Directive SetJavaHome is not required by default,
# except for the base install, since the launcher will
# determine the JAVA_HOME. On Windows it looks
# in ..\..\jdk, on UNIX it first looks in ../../jdk.
# If no JDK is found there, it looks in the PATH.
#
# SetJavaHome C:\Java\jdk1.5.0_04

Because SetJavaHome directive is commented out, this file has pretty much the same effect as the empty one located in "<SQLDeveloper_Install_Dir>/SQLDeveloper/bin" subdirectory. In this case, the application will look for the bundled Java Runtime Engine which should be located in "<SQLDeveloper_Install_Dir>/jdk" subdirectory. And this is the trick. Which of the "jdk.conf" files is the correct one? The answer is: the empty "jdk.conf" in "<SQLDeveloper_Install_Dir>/SQLDeveloper/bin" subdirectory. So, if want or need to specify what JDK should be used to run SQLDeveloper, you must edit this file and add the following:

# tipical setting for Windows
# (you don't need to enclose the path with double quotes)
SetJavaHome C:\Program Files\Java\jdk1.5.0_12

In order to confirm your settings, just open the "About" window and select the "Version" tab:



There you may check what Java is being used and, looking at the "Properties" tab, you may check all other settings.

That's it. Best regards to all!

Sunday, September 2, 2007

JavaOne 2007 - Performance Tips 2 - Finish the finalizers!

by Eduardo Rodrigues

Continuing from my last post about some lessons learned at JavaOne'07 on Java performance since JDK 1.5, there's something we usually do not pay much attention to but which can get us some trouble: object finalizers.

Every time we override the protected void finalize() throws Throwable method, we are implicitly creating a postmortem hook to be called by the Garbage Collector after it finds that the object is unreachable and before it actually reclaims the object's memory space. In general, we override finalize() with the best of the intentions which is to ensure that all necessary disposal of system resources and any other cleanup will be performed before the object is permanently discarded. So why is that an issue?

Well, we all should know that finalize() is an empty method declared in java.lang.Object class, therefore, inherited by any existing Java class. So, when it's overridden, the JVM can't assume the default trivial finalization for the object anymore which means that "fast allocation" won't happen here. In fact, "finalizable" objects have much slower allocation simply because the VM must keep track of all finalize() hooks. Besides, those objects also give much more work to the GC. It takes at least 2 GC cycles (which are also slower) to reclaim a "finalizable" object. The first is the usual one when the GC identifies the object as garbage. The difference is that now it has to enqueue the object on finalization queue. Only during a next cycle GC will dequeue and call the object's finalize() method and, if we're lucky, discard the object and reclaim its space, or else, it may take another cycle just to finally get rid of that object.

If we look closer, we'll notice that putting more pressure on the GC and slowing down both initialization and finalization processes are not the only problems here. Let's take a quick look at the J2SE 5.0 API Javadoc for the Object.finalize() method:

"(...) After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded. The finalize method is never invoked more than once by a Java virtual machine for any given object. Any exception thrown by the finalize method causes the finalization of this object to be halted (...)"

It is quite clear to me that there's a potential temporary (or even permanent) "memory leak" matter hidden in that piece of Javadoc. Since the JVM is obligated to execute the finalize() method before discarding any object overriding it, in fact, due to the additional GC cycles described above, not only that specific object will be retained longer in the heap but also any other objects that are still reachable from it. In the other hand, even after executing finalize(), the VM will not reclaim an object's space if, by any means, it may still be accessed by any object or class, in any living thread, even if they're also ready to be finalized. Like it isn't enough, if any exception is thrown uncaught during finalize() execution, the finalization of the object is halted and there's a good chance that, in this case, this object will be retained forever as garbage.

At last, the fact that the finalize() method should never be invoked more that once for any given object certainly implies the use of synchronization which is one more performance threatening element.

So, next time you consider writing a finalizer in a class, please, take a second look at it. And if you really have to do that, be really careful with the code you write and try to follow these tips:
  • Use finalizers only as a last resort!

  • Even if you do not explicitly override the finalize() method, library classes you extend may have done it. Look at the example bellow:

    class MyFrame extends JFrame {
    private byte[] buffer = new byte[16*1024*1024];
    (...)
    }

    In JDK 1.5 and earlier, the 16MB buffer will survive, at least, 2 GC cycles before any MyFrame instance is discarded. That's because JFrame library class does declare a finalizer. So, try to split objects in cases like this:

    class MyFrame {
    private JFrame frame;
    private byte[] buffer = new byte[16*1024*1024];
    (...)
    }

  • Even if you're considering to use a finalizer to dispose expensive and scarce resources, keep in mind that, being scarce, it's very likely that they will be exhausted before memory (assuming that memory is usually plentiful). So, in these cases, prefer to pool scarce resources instead.
To be continued...

Friday, August 3, 2007

Mastering ADF Faces <af:selectOneChoice> component

I've being trying to solve this issue for a long time. I've read many blogs, forums and articles about it but, none was a complete solution. Fortunately, now that I've finally managed to put it all together I decided to share with you all.

The problem is very simple and, as far as I noticed, very common also. I want to build a combo box based on a result set from my database within an ADF Faces page. Ok, I know, I should use the selectOneChoice component bound to my database through a PageDefinition XML. What if I also need to bind a specific attribute from my result set to the value attribute of each item in the selectOneChoice component? Well, believe me... it's not as easy as it may seem.

By definition, when a bound selectItems tag is used with the seleceOneChoice component, ADF will render each item like this:

<option value="N">bound resultset attribute as the option label</option>

"N" being a natural number varying from 0 (zero) to the total number of rows in the result set minus 1. In other words, an item's value will always be the index of it's corresponding row in the bound iterator's collection, no matter what. The problem is that, many times, it's really important to have more meaningful information as the item's value, specially when we need to make some client-side processing based on it. The question is: how? Well, here is the complete answer with very simple example.

Suppose we've defined a list binding identified by "myList" bound to an iterator for a collection of objects of the following bean class:

public final class ListItemBean {

   private String itemValue;
   private String itemLabel;

   public ListItemBean() {}

   public void setItemValue(String value) {
      this.itemValue = value;
   }

   public void setItemLabel(String label) {
      this.itemLabel = label;
   }

   public String getItemValue() {
      return this.itemValue;
   }

   public String getItemLabel() {
      return this.itemLabel;
   }
}


The most common use case of <af:seleconechoice> would be:

<af:selectonechoice id="myCombo" value="#{bindings.myList.inputValue}">
   <f:selectitems value="#{bindings.myList.items}"/>
</f:selectitems>


The label of each will be rendered according to myList definition in the PageDefinition XML file. So, if I want to use the bean attribute itemLabel, myList definition should be something like this:

<list listopermode="1" iterbinding="myListIterator" id="myList">
   <attrnames>
      <item value="itemLabel">
   </item>
</attrnames>


Now, if I want to take control of how each will be rendered:

<af:selectonechoice id="myCombo" valuepassthru="true" value="#{bindings.myList.inputValue}">
   <af:foreach items="#{bindings.myList.iteratorBinding.allRowsInRange}" var="row">
      <af:selectItem id="myItem"
         value="#{row.dataProvider.itemValue}"
         label="#{row.dataProvider.itemLabel}"/>
   </af:forEach>
</af:selectOneChoice>


It is very important to define an id for af:selectitem. If you don't, ADF runtime won't render the page correctly. Also notice the valuePassThru attribute defined to "true". It tells ADF to render each selectItem like this: <option value="the item's real value goes here">the item's label goes he</option>. That's important exactly because I need to make client-side processing using items real values. Without valuePassThru="true", the options values would continue to be rendered as corresponding indexes. Before you ask, know that simply adding valuePassThru="true" to the common use case above won't work since ADF ignores it if you aren't using af:selectItem.

Well, that should be all, but it isn't. There's one side effect which is a potentially unwanted empty element as your combo box's first option. That's because now, ADF doesn't have a default value for your combo box when it's first rendered. There are many different ways of solving this minor issue but, in my opinion, the easiest would be to add the following JavaScript to the page:

<script type="text/javascript">

function removeEmptyOption() {
   if (document.forms[0].myCombo.options[0].value=='') {
      document.forms[0].myCombo.options[0] = null;
      document.forms[0].myCombo.value = document.forms[0].myCombo.options[0].value;
   }
}

</script>


Now, just call it from the page body's onLoad event: <afh:body onload="removeEmptyOption()">

If you prefer, you may also use CSS to do the job (which I think is far more elegant). Just add the following style to the page:

<style type="text/css" media="screen">
   option[value=""] {
      display: none;
   }
</style>


The problem with this solution is that CSS Selectors are not compatible with all browsers, specially with IE6 : (

So, that's it. I hope you enjoy.