Wednesday, November 24, 2010

Capturing the parameters passed to expected methodof your mock item

We all use easymock to mock java objects to speed up developing our unit tests and focus on the code fragment.

I would like to point out a great feature of this framework which is not known very much. You need to be familiar with easymock already to get the point.

You can capture the object that is passed to your expectation. Please see the code below with comments.


As you might already noticed above, you can easily get the passed object from capturer object. Capturer object was used with static method "capture" of easymock.

Monday, November 15, 2010

Amazon App Store for Android

Rumors and reports of an Amazon competitor to Google’s own Android App Store were confirmed today with Amazon’s invitation letter to developers, posted here on Ubergizmo. The Amazon App Store, while not particularly shocking news, has several important implications that go way beyond just another source of Apps for Android phone users.

Read full article here.

Friday, October 8, 2010

How to add a certificate?

Do you recieve following error:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

That's perhaps the certifcate you requesting is not valid or self signed. If you really trust to the certificate (perhaps it is your development environment) and want to add it to your server, here is how:

Assume that you are trying to reach https://abc.def.com/someContent/

1) Execute this on your console:
openssl s_client -connect abc.def.com:443

(windows users can use FireFox to see the certificate and export it instead of step1 and step2)

2)Copy the certificate information and save it to a file like abc.pem. The part your are going to copy will look like this:

-----BEGIN CERTIFICATE-----
MssJjDC8sjshHN7hhshsGGGSGSVfstsg9w0BAQUFADCBizELMAkGA1UEBhMCQkUx
EDAOBgNVBAgTB0JlbGdpdW0xETAPBgNVBAcTCFphdmVudGVtMSQwIgYDVQQKExtT
b2334GUtU29sdXRpb25zIEV1cm9wZSBCLlYxDDAKBgNVBAsTA0lTRTEjMCEGA1UE
AxMaZXNwLnN0Zy5zcC5zb255LWV1cm9wZS5jb20wHhcNMTAwNTE5MDgxNjU3WhcN
MjAwNTE2MDgxNjU3WjCBizELMAkGA1UEBhMCQkUxEDAOBgNVBAgTB0JlbGdpdW0x
ETAPBgNVBAcTCFphdmVudGVtMSQwIgYDVQQKExtTb255IGUtU29sdXRpb25zIEV1
cm9wZSBCLlYxDDAKBgNVBAsTA0lTRTEjMCEGA1UEAxMaZXNwLnN0Zy5zcC5zb255
LWV1cm9wZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMt0TExB8o6g
OkikMgabQT/wI6N21BXbmP5JTRcs7tSknwxnneCl0Nc8LoEQ2pib5BLpyzQLg5Gg
kJdnskMQBJ35SMo8OiThRmuwOYb1rk/dFP8j924HUTbFaT6b8rJALYG2ljY62fj0
b+BTlzbZb+cl5sRBSIgX+Hzx9NTD64EZAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEA
bkTufSkz8BO1O9q37lGkwM8Vv/dAJ8bMeYUkLNW3n34frJ72Y26Vme9iVPl7kvdK
dYdkZ8iIQIiNQUasOfwhyOPcakvz2mx60vxFz4bykhzYAwAvM80d+2XC9xqWYf3A
Jgdhdg6hhdhlfeBSh9ih3JRNlYDub+IDbrgYPsDB2uA=
-----END CERTIFICATE-----

(You should include BEGIN CERTIFICATE and END CERTIFICATE lines too)

(Windows users, can export the file with firefox; just goto website and click on certificate at bottom right; choose details; and then export)

3) Add it to trusted certs:
sudo /opt/java/bin/keytool -import -alias abc.def.com -keystore $JAVA_HOME/jre/lib/security/cacerts -file abc.pem

(if you do not have an idea what is your keystore password, default is 'changeit')

(windows users should execute just keytool, they should not type (sudo /opt...)

When asked, reply "yes".

Please note that if you are developer; possibly you will want to update cacerts in
JDK_HOME/jre/lib/security/cacerts and JAVA_HOME/lib/security/cacerts

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.