Here i’m going to write down about how to deal with XML by using Java, especially with JAXB as you can see from the subject title.
Why JAXB ?
Of course, there are many ways to handle XML in Java. You could use JAXP API (Java API for XML Processing) which comprises DOMParser and SAXParser (old fashion style and complicated in my opinion ^_^), or XMLBeans which convert and transform your XML file to POJO class or vice versa. I thought the last one (i.e. XMLBeans) is much better compared to JAXP (this is my opinion, maybe you have different opinion). But…, nowadays, i think i would like to choose JAXB rather than XMLBeans. Why ? because (in my private opinion again ^_^), configure JAXB is much simpler than XMLBeans. Because its already become standard of Java EE. Starting from Java 6, its already bundled inside the JDK. So, you don’t need to include external libraries/APIs to use this API. That’s the reason why i choose JAXB instead of XMLBeans.
However, in some IDE (e.g. Oracle Weblogic Workshop), you can see there, that the IDE has already bundled XMLBeans as its standard API to dealing with XML. That’s why, i’m still using XMLBeans for some of my projects that using Oracle Weblogic Workshop as the IDE. However, i’m not talking about XMLBeans here. I will talk about JAXB here. Yes, sure you could find anywhere from the internet (just need to ask uncle google ^_^) about this JAXB. But, I will show you here by example (a simple example), about how easily configure and play with JAXB in order to handling your XML stuffs.
I am writing this just to share a little bit about my findings while playing with Grails. Hopefully, this could be a good note for me too.
By the way, I’m using Grails 2.0.4 at the time of this writing. You could download it from Grails official site.
Ok, lets start then.
There are 3 errors/exceptions that I’ve just found while playing with the Grails as you can see from the following:
1. java.lang.SecurityException occured while running grails command
I was trying to create new Grails app with
grails create-app command, and i’ve got such an error. See the image below:
Click the image to enlarge
Java is getting bigger and popular now. I think the OpenSource is one of the reason that influence it. People always look for something that are free and powerful. One of my friends who’s currently working as PHP developer told me that he’s trying to switch to Java. He came to me yesterday and has asked me about Generic.
In order to answer his question, i’m gonna write some explanation here about Generic. Hopefully this could help him and the others (including me :-)).
What is Generic ?
Generic is one of the java’s feature/extension that starting from Java 5 (Tiger). In my opinion, Generic helps your code more stable by trying to reduce as many as possible any exception that could be raised in runtime. This could be done by trusting the compiler to check our code in the compile time. Generic helps you to detect your program bugs at compile time as many as possible. So, it’s guarantee that our running code is less of any error.
Generic allows you to abstract over types. The most common examples are container types, such as those in the Collection hierarchy. Overall, Generics are heavily uses by these classes (i.e. Collection classes).
Since 2 months ago, I’ve got my second remote job as Java EE Developer for Telco Company in Singapore named Hub9. So, currently now I’m working for two Telco’s Company in Singapore (i.e. CoreSDP and Hub9) as Java EE Developer. Both of them are remote jobs (which means working from home :-D), and I’ve got monthly payment for these jobs. This is what i’ve been dreaming for a long time :-).
Start from here I’ll write some notes about anything what i’ve been doing for over the last 2 months with Hub9.
As my first assignment, they’ve asked me to develop facebook chat (IM/Instant Messaging) application via SMS (known as Facebook SMS Chat). As you’ve already known, there are many chat (IM) application now, such as Yahoo Messenger, GoogleTalk, eBuddy, Jabber, etc. Even Facebook has provided it’s application with IM service, so every user on facebook could have a conversation via this chat (IM) service.
Every IM service need communication protocol in order to be working correctly. There are many protocols around can be used for this purpose. Some of them are under commercial (propietary) license, and some of them are not (means ‘free’, and this is what i’m going to use :-D).
One of the protocol that is free to be used is XMPP.
XMPP (stands for eXtensible Messaging and Presence Protocol) is an open-standard communication protocol for message oriented middleware based on XML (eXtensible Markup Language). This protocol was originally named Jabber, and was developed by the Jabber open source community in 1999 for near real-time, extensible instant messaging (IM), presence information, and contact list maintenance. The software implementation and client application based on this protocol are distributed as free and open source software (looks nice to me ^_^).
I think, you should read by your self later about XMPP, so I won’t give you the detail explanation about XMPP here (b’cause I’m not the kind of person who’s enjoy to talking to much about the ‘philosophy’ behind the ‘theory’ #:-s). So please googling or read the “RTFM” :-).
One thing that I think is important for us to know before we jump into the XMPP implementation is: basically, there are 2 implementations of XMPP. i.e. XMPP Server and XMPP Client.
In order to work, we should have those 2 implementation in our software.
I thought we’ve already heard about Dependency Injection (DI). I knew it when I learned Spring framework in Java. So far as I know, Spring framework has come with it’s 2 popular concepts which known as IoC (Inversion of Control) and AOP (Aspect Oriented Programming). When you learned about IoC concept in Spring, you often refers to DI as the other name of IoC. And there are many of Java developers equate IoC to DI. But, actually DI is not exactly the same as IoC. IoC is more wide than DI. On simplistic term, we can say that DI is a part of IoC, or one of the implementation of IoC.
IoC/DI has become popular with Spring. But, did you know that the main idea behind IoC/DI concept started from around 1994 ? In the year of 1994, Robert C.Martin wrote in his paper about “The Dependency Inversion Principle”, which also known as DIP for short. This is eventually known as IoC/DI’s principle, just like now.
On his paper, Robert states the three defining factors of bad code, shown as below:
- Rigidity, that it’s hard to change part of the code, because every change to the code often affects too many other parts of the system.
- Fragility, that when we make a change to our code, there is an unexpected parts of the system broken.
- Immobility, that it’s hard to reuse our application or application’s code, because it cannot be disentangled from current application