Tuesday, August 31, 2010

Windows Vista Speech Recognition

If you would like to smile a bit...

Wednesday, August 18, 2010

XML To Beans with XMLBeans & Maven Integration

If you have the xsd of the xml you are dealing with (if you do not have it, you have to create it; which is an interface between you and the party giving or consuming the xml) you can easily build java beans from it with xml-beans. Or you can use it to parse easily.

In a project I am dealing, I created a separate artifact which has dependencies to xmlbeans artifacts and I have a plug-in which automatically builds the related beans from xds's under a given directory.

Here are the dependencies for xml-beans:

After adding dependencies as described above add xmlbeans plugin so that related xml's will be created automatically when "mvn install" is executed:

Now you have an artifact which you can add as a dependency. If you add/change an xsd under "src/main/resources/xsd" path with each "mvn install" you will have a jar with beans representing your xmls.

With created beans you can create or parse xml documents easily. First here is an xml how to read a file:

And here is an example for creating a document:

opt stands for the options:

Please note that package name is determined from name space. As you might noticed in creation bean from file example package name was com.yamanyar.ish. This means xsd has following declaration at beginning:

And corresponding xml will like:

I hope this helps.

Thursday, August 12, 2010

CanonicalSet: Weak References from Set, Shared Instances

Today I needed to work with an approach that I think it's worth to share.

We were running out of memory issues due to many instances of a class. So I decided to make use of shared instances. Instead of creating the instance every time, it is better to check if it was created before and return it in that case.

In order to achieve that we have to have a collection for the objects we had created to check if it was created already.We need to put them in a set; however putting them in a set is a bit problematic: if we put them in a set there will be a strong reference from the set which prevents it to be garbage collected ever. We need a set which we can add objects with weak references so that if there are no more strong references from outside, it will be garbage collected.

Before starting development I goggled if it was already implemented. And of course it was: http://docs.geotools.org/stable/javadocs/org/geotools/util/CanonicalSet.html

You can download sources from http://maven.geotools.fr/repository/org/geotools/gt2-metadata/2.5-SNAPSHOT/ and check it too.

Before this development; I had nearly 300.000 instances and now I have only 1040 instance. Of course you need to be sure that your object is immutable. Also be sure with your hashcode() and equals() methods.

The most notable method will be unique() method on CanonicalSet. Create your regular object and pass to this method. It will return you the one in the set if exists and drop the newly created object.