Monthly Archives: June 2010

Profissao Java 2010

Hi!

Talking again about events: Profissao Java 2010 (something like Java Profession 2010 in english). This event happened yesterday, Saturday, June 26th, 2010 (obviously :P ). Was an one-day event, right to the point filled with information regarding a lot of different topics, from JavaEE to Digital TV to entrepreneurship.

Talking about market opportunities and the right frame of mind when approaching career decisions, Bruno Sousa suggested a few books that we should read:

Bruno Sousa and a few book suggetions

Bruno Sousa and a few book suggetions

One other topic covered by the event was mobile platforms. Including some Java ME, Android and, despite the fact of not being Java, iPhone. I liked it. Here is a picture of Helder da Rocha talking about iPhone development:

Helder da Rocha on iPhone development

Helder da Rocha on iPhone development

Overall, I liked the event. The technical parts were interesting but what I liked the most was the focus on entrepreneurship that a lot of the presentations had. Hearing how others are building their business in our market is always interesting and fun – and help us avoiding some mistakes, when our time comes. The event run a little bit late, having ended after 8 pm, and we left really tired, but it was worth it.

Congratulations to the Globalcode team for one more great event =)


KeyStore Explorer and Digital Certificates

Ok, one more post aimed at my memory, but that can end up helping others as well ;)

I’ve been playing around with digital certificates in Java, because I’ll have to implement some stuff here that requires secure calls to web services using them.

One of the requirements for the secure connection is that our server trusts the Web Service’s server. To do this we have to, somehow, install a certificate chain provided by the Web Service provider. Sounds pretty simple, but can be a little bit troublesome if you are not familiar with how Java works with digital certificates and also with how to handle the keystore tool that comes with the JVM.

At first, I tried to import the certificate chain using the keystore directly, without success. The chain provided is a .pb7 file, which I discovered later that follows the PKCS #7 format / standard / whatever. Having never dealt with such a file before, I had no idea of what to do with it. I just knew I had to import it in my local (or server, when in production) trusted certificate store. But I couldn’t find the proper parameters to pass to keystore so that it would do the right thing…

I gave up this approach for a while and started to google around for some solution. This is when I found the KeyStore Explorer application. It is a free tool that really helps visualizing digital certificates, both installed and the ones available in specific certificate files. I installed the tool, and with some guessing I found how to visualize the .pb7 file mentioned before. Strangely, I had to do some manual labour to import the chain of certificates: I visualized, one by one, all of the certificates in the chain, and exported each one to .cer files. After that, I opened the trusted store and imported all of those certificates into it. Done.

Of course, this solution is not ideal, but works. The ideal would be to import the chain directly using the keystore tool, which is probably possible, I just could find exactly how. If you do know how to do this, please leave a comment =)


DSL in Scala for Date Calculation

I’ve been attending a course about Java Architecture in Caelum these last weeks. In one of those Saturdays, the instructor mentioned a little bit about DSLs. Better yet, he gave us an example using Scala =)

Now, the example was intriguing and interesting, at least for a Scala beginner like me, so I decided to translate it to English (the original was in Portuguese) and post it here. If you want, you can see the original in Portuguese, created by Sergio Lopes, here.

Before seeing the translated version of the DSL implementation, lets take a look on how you would use it – which is the most interesting part:

Tomorrow minus 1 month and plus 10 years and plus 1 day

Although the code above looks like a (almost?) proper sentence, it is valid Scala source code. That’s the beauty of writing DSLs in Scala =)

One thing that took me a while to understand regarding this code is the Conjunction part. Represented by the and instance in this case, its purpose is simple (at least after you understand it): pass a partial result to the next part of the calculation, when necessary. Notice in the code how months, years and days have one overloading that receives a Conjunction. This is what makes possible to yield a result that will be passed to the next calculation step.

Here is the full implementation code of the DSL:

import java.util.Calendar

class Date(val data: Calendar) {
 data.clear(Calendar.HOUR)
 import Date.Conjunction

 private var last = 0;

 def plus(num: Int) = { last = num; this }
 def minus(num: Int) = { last = -num; this }

 def +(num: Int) = plus(num)
 def -(num: Int) = minus(num)

 def months = { data.add(Calendar.MONTH, last); this }
 def months(and: Conjunction): Date = months
 def month = months
 def month(and: Conjunction): Date = months

 def years = { data.add(Calendar.YEAR, last); this }
 def years(and: Conjunction): Date = years
 def year = years
 def year(and: Conjunction): Date = years

 def days = { data.add(Calendar.DAY_OF_MONTH, last); this }
 def days(and: Conjunction): Date = days
 def day = days
 def day(and: Conjunction): Date = days

 override def toString = "%1$Td/%1$Tm/%1$TY" format data
}

object Date {
 class Conjunction
 val and = new Conjunction

 def Today = new Date(Calendar.getInstance)
 def Tomorrow = Today + 1 day
 def Yesterday = Today - 1 day

 def today = Today
 def tomorrow = Tomorrow
 def yesterday = Yesterday
}

The only thing I added to the code was operator overloading, so that the usage can be even more interesting, allowing stuff like this:

Today + 2 months

So, the def + and the def - are not present in the original code. I just added those as an exercise to understand how to use operator overload in Scala, which ended up being ridiculously simple. If you want to learn more about operator overloading, Joey Gibson has a nice blog entry about this here.


Follow

Get every new post delivered to your Inbox.