Monday, July 29, 2013

How to right click on a mac mouse

Initially I was puzzled on how do I right click on my new mac mouse. :-) If you have searched for this article, you also would have had the same question.

Navigate to System Preferences --> Mouse --> "Check" Seconday click --> Click on right side.

Once you are done with that you can right click on the mac mouse.

Thursday, July 25, 2013

How to convert XML to Java object and vice versa in runtime?

Well the obvious choice would be to use JaxB, due to its familiarity. Let us see it with a simple example. JAXB is governed by the same team as Glassfish. JAXB comes by default with JDK1.6+. JAXB stands for Java Architecture for XML Binding.

XML has become a common form of data storage in many applications and for webservices. There are two common operations that are required while dealing with XML from Java. Read an xml file and convert it to a Java object, and write a Java object to an xml file.

JAXB does both the above said operations and those operations are called as
  1. Marshalling – Convert a Java object into a XML.
  2. Unmarshalling – Convert a XML into a Java Object.
Let us see this with an example,
Marshalling: Converting Java object to XML
Its a simple process, lets see this with a sample program
Output
While running the above example you will run into the following exception,

Exception in thread "main" javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "com.ananth.samples.Employee" as an element because it is missing an @XmlRootElement annotation]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:317)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:243)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:75)
at com.ananth.samples.Java2XMLExample.main(Java2XMLExample.java:20)
Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "com.ananth.samples.Employee" as an element because it is missing an @XmlRootElement annotation
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:216)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:286)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:462)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:314)
... 3 more

To fix the above exception you have to add an @XmlRootElement annotation to your Employee Pojo.
Instead of System.out you can point to the File to which you want to write the output during jaxbMarshaller.marshal(employee, file);. Once make the change while rerunning the sample will give your the following output.
UnMarshalling: Converting XML to Java object
Its a simple process, lets see this with a sample program
Now, I had a question why do I should I have a @XmlRootElement annotation in my pojo.  Cant I do I deal with my existing POJOs of my application?
The answer is you cant do it with JAXB but there are other options like XStream. I have referred their two minutes tutorial and it really look me only 2 minutes to try it out.
Add the XStream maven dependency and after that it is as easy as calling toXML and fromXML methods of XStream object.


Friday, July 19, 2013

XML reader error: javax.xml.stream.XMLStreamException: ParseError at [row,col]

100/100 times I know that this exception happens while there is a parse error in your xml and there is something wrong at the specified row and column.

Knowing the following exception is due to an XML parse error, recently I was puzzled for a while trying to look into the actual content of the file.  I couldn't see any closing tag error at the row and column mentioned.

After looking closer, I figured out my xml is used by multiple threads where a few threads were trying to write the XML and the other was trying to read the same file. And the exception was due to thread safety and the file was not fully written before the read. :-)

Message: XML document structures must start and end within the same entity.
com.sun.xml.internal.ws.streaming.XMLStreamReaderException: XML reader error: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[8829,4]
Message: XML document structures must start and end within the same entity.
at com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil.wrapException(XMLStreamReaderUtil.java:256)
at com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil.skipTags(XMLStreamReaderUtil.java:146)
at com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil.skipElement(XMLStreamReaderUtil.java:119)
at com.sun.xml.internal.ws.wsdl.parser.WSDLParserExtensionFacade.definitionsElements(WSDLParserExtensionFacade.java:129)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parseWSDL(RuntimeWSDLParser.java:314)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:135)
at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:254)
at com.sun.xml.internal.ws.client.WSServiceDelegate.(WSServiceDelegate.java:217)
at com.sun.xml.internal.ws.client.WSServiceDelegate.(WSServiceDelegate.java:165)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:93)

Monday, June 17, 2013

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

I got this exception while I was trying to execute an open source application that I found from the web. I was trying to run the application from my eclipse managed tomcat 7 server. I noticed that my project's maven dependencies were not having the right jars.
This problem can be fixed by adding maven dependencies in the project's web deployment assembly.
  1. Open the project's properties (e.g., right-click on the project's name in the project explorer and select "Properties")
  2. Select "Deployment Assembly"
  3. Click the "Add..." button on the right margin
  4. Select "Java Build Path Entries" from the menu of Directive Type and click "Next"
  5. Select "Maven Dependencies" from the Java Build Path Entries menu and click "Finish".
You should see "Maven Dependencies" added to the Web Deployment Assembly definition after the following the above steps.

Friday, June 14, 2013

Wikipedia, RHEL ditches MySQL and moves to MariaDB

Prior to this Wikipedia has moved to MariaDB from MySQL recently.

According to news from the Red Hat summit which concludes in Boston on Friday, RHEL 7 will also include MongoDB.

The switch was expected to happen after Red Hat's community distribution Fedora announced earlier this year that it would be moving to MariaDB. But it will come as a major jolt to Oracle, the owner of MySQL. (openSUSE, the community distribution of SUSE, announced a switch to MariaDB at the same time as Fedora.)

Many changes in Fedora foreshadow changes in the enterprise distribution.

Once RHEL makes the switch, it will flow on to CentOS, a distribution that provides RHEL without the trademarks, hence making it free in terms of cost. CentOS is widely used by small businesses.

MySQL became Oracle's property after the company bought Sun Microsystems in 2010. There have been many complaints about its stewardship of what was once the last word in open-source database software.
MariaDB is a fork of MySQL that was begun by Ulf Michael Widenius under his new company, Monty Program. Widenius is one of the three co-founders of the original MySQL. The company was sold to Sun in 2008.

Earlier this year, Monty Program merged with SkySQL, a company set up in October 2010 by former employees of the original MySQL. Notable among SkySQL's ranks are former MySQL senior vice-president of global services, Ulf Sandberg, and adviser David Axmark, another of the three co-founders of MySQL.

Widenius and several others raised objections to Oracle's acquisition of MySQL but failed to stop the sale going through.

MySQL's first employee in Australia, Arjen Lentz, said Fedora and OpenSuSE were community driven, whereas RHEL's switch to MariaDB was a corporate decision with far-reaching implications.

"I presume there is not much love lost between Red Hat and Oracle (particularly since the "Oracle Linux" stuff started) but I'm pretty sure this move won't make Oracle any happier," said Lentz, who now runs his own consultancy, Open Query, from Queensland. "Thus it's a serious move in political terms."

He said that in practical terms, MariaDB should now get much more of a public footprint with people (people knowing about MariaDB and it being a/the replacement for MySQL), and direct acceptance both by individual users and corporates.

"Monty Program actually merged (aka was bought) by SkySQL a few months ago. SkySQL does not have significant activity in Australia and New Zealand, Open Query has. SkySQL is currently mainly active in Europe.

"So in terms of significance for AU/NZ, Open Query is definitely happy with this move, and is set to gain business for it. Which is nice in economic terms for Australia, it's not a foreign company with Australian clients."