Saturday 30 August 2014

JAXB Tutorial for Java XML binding with annotations

JAXB

JAXB stands for JAVA Architecture for XML bindings. We will come to it in some time. Before that we need to understand a even basic concept - Serialization. Simply speaking Serialization is the process of turning an object in memory into a stream of bytes so that it can be transferred over a network or stored in a persistent storage.


There are various ways to serialize an object. For eg.

  1.  Serialization via XML. Convert the Object to XML transfer it and then convert it back to XML. 
  2. You can do the save via JSON (Javascript object notation).
  3. Or you can use the Serialization provided by Java i.e by making your class implement Serilizable interface.
In this post we will see how can we serialize/de-serialize Objects to and from xml.

Note : The verbs Marshal and Unmarshal are synonymous with Serialize and Deserialize. Specially in XML serialization you will hear the words   Marshal and Unmarshal very frequently.


Dependencies ??

If you are using JDK 6 or higher version you don't have to worry about any dependencies as JAXB comes bundled with it. If you are using a lower version that Java 6 then you can download the jars from here. Download and add “jaxb-api.jar” and “jaxb-impl.jar” on your project classpath.

 To the code....

First Create a model class file in package opensourceforgeeks.model. Lets call it Employee.java . Add the following code to it.
package opensourceforgeeks.model;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * 
 * @author athakur
 *
 */

@XmlRootElement
public class Employee {

    private String name;
    private int age;
    private int id;
    
    public String getName() {
        return name;
    }
    
    @XmlElement
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    
    @XmlElement
    public void setAge(int age) {
        this.age = age;
    }
    public int getId() {
        return id;
    }
    
    @XmlAttribute
    public void setId(int id) {
        this.id = id;
    }
    
    @Override
    public String toString() {
        return "Employee [name=" + name + ", age=" + age + ", id=" + id + "]";
    }

}
 


Next lets create a class to test this out.  Create class JAXBTest.java under package opensourceforgeeks.test. Add the following code to it and run - 

package opensourceforgeeks.test;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import opensourceforgeeks.model.Employee; 
/**
 * 
 * @author athakur
 *
 */ 
 public class JAXBTest {

    public static void main(String args[]) {

        Employee employee = new Employee();
        employee.setName("Aniket");
        employee.setAge(23);
        employee.setId(101);

        try {

            File file = new File("C:\\Users\\athakur\\Desktop\\data.txt");
            JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            
            System.out.println("Serializing Data");
            //Serilization
            //put the xml in the file
            jaxbMarshaller.marshal(employee, file);
            //also print it on standard ouput
            jaxbMarshaller.marshal(employee, System.out);
            
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            
            System.out.println("Deserializing Data");
            //Deserialization
            //read the xml to get the Object
            Employee newEmployee = (Employee) jaxbUnmarshaller.unmarshal(file);
            System.out.println(newEmployee);

        } catch (JAXBException e) {
            e.printStackTrace();
        }


    }

}
Your project structure should look like below - as per above conventions of-course. You are free to put the code anywhere you like :)


Output : 

On the standard output console you can see - 

Serializing Data
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee id="101">
    <age>23</age>
    <name>Aniket</name>
</employee>
Deserializing Data
Employee [name=Aniket, age=23, id=101]

You can also verify the data in the file whose path you have provided. XML should be generated in that file. It will have same XML as shown on standard output. For more details you can refer to the tutorial provided on the JAXB official site.

NOTE1 : In the model classes that you annotate you need to have default constructor (If you do not have any constructors Java compiler will put one default no arg constructor for you). If you do not provide default constructor you will get following exception -

JAXBException occurred : 1 counts of IllegalAnnotationExceptions.

NOTE2 :  If you get following exception - 
No message body writer has been found for response class
Make sure you are annotating root element with @XmlRootElement

Summary

Its quite self explanatory - the bindings and the conversion. Got the following diagram from the web that explains it in simplest way -



Related Links

Sunday 24 August 2014

Enabling telnet client on Windows 7

Background

A terminal emulation or telnet is a program for TCP/IP networks such as the Internet. The Telnet program runs on your computer and connects your PC to a server on the network. You can then enter commands through the Telnet program and they will be executed as if you were entering them directly on the server console. (More on Wiki)

If you type telnet in your windows console you will see command not found


That's because telnet client is not enabled by default on Windows. Lets see how can we enable it.

To enable Telnet command line utilities:

  1. Click Start > Control Panel.
  2. Click Programs and Features.
  3. Click Turn Windows features on or off.
  4. In the Windows Features dialog box, check the Telnet Client check box.
  5. Click OK. The system installs the appropriate files. This will take a few seconds to a minute.

Screenshots





And telnet client should be enable in your windows. You can just type telnet and check. It should open Microsoft telnet client. Or you can try directly connection to a host.

Eg. telnet alt4.gmail-smtp-in.l.google.com 25



Related Links

Saturday 23 August 2014

TortoiseSVN icons not showing up under Windows 7

Problem

Not to brag about it but SVN is probably the most widely used versioning system. It is used in most of the top companies. Now here is the problem I faced not once but twice. Tortoise SVN  (download) is a widely used UI based svn client. It supports all basic svn operations and has a nice UI for looking at the history, diffs and merge.  It even has nice overlay images to denote if you file is updated , modified or has some conflict. It so happens in windows 7 that after installing the software the overlay images do not appear. I have used it for almost a year now without any problem but today when I tried to configure it same problem appeared (overlay images not showing up). So below I will provide the simplest way to resolve this.



Solution

Once you have installed the software and overlay icons do not show up simply repair the installation.

From the TortoiseSVN lead developer (2010-04-17) : "You might have to do a repair install (start the installer again, then choose 'repair') after you've updated to 1.6.8. Otherwise the overlay icons might not show up properly. Sorry for the inconvenience."

You can do it the following way

  1. Launch the setup again and select repair.

  2. On next screen select next.

  3. In the next screen you will see the applications that the repair wizard will attempt to restart. Avoid selecting second option as you will unnecessarily have to reboot your machine. Two important applications that will restart are
    1. Svn status cache and
    2. Windows Explorer


    Other applications may include any svn processes that may be still in progress like checkout of a branch.

That should do it. But if it is still not resolved I suggest go through the links in "Related Links" sections. There are some useful answer on Stack overflow. But mind most of them include playing with system registry. Unless you are sure do not mess with it :).






Related Links

Sunday 17 August 2014

Reading password from command line in Java using java.io.Console

Background

Sometime we need to get passwords from the user. Passwords is a very crucial and secret information. We generally show the text while entering password as '*' characters or not display the characters at all. Java provides in built facility to enter passwords that are not echoed while entering. We are going to see this example.

java.io.console was introduced in Java 6 and has far more convenient methods than plain streams  - System.in and System.out. Also note that this class is Singleton. The Console class provides access to an instance of Reader and PrintWriter using the methods reader() and writer(), respectively

Documentation

We are going to use readPassword() method of Console class is java.io package.

Method documentation is as follows - 

   /**
    * Reads a password or passphrase from the console with echoing disabled
    *
    * @throws IOError
    *         If an I/O error occurs.
    *
    * @return  A character array containing the password or passphrase read
    *          from the console, not including any line-termination characters,
    *          or <tt>null</tt> if an end of stream has been reached.
    */
    public char[] readPassword() {
        return readPassword("");
    }


Code Example


/*
 * author : athakur
 */

public class HelloWorld {
    
    public static void main(String args[]) throws IOException {
        Console console = System.console();
        if(console == null) {
            //This is a bug in eclipse IDE
            //password will be shown on console
            System.out.println("Could not get Console. Falling back to default input mechanism");
            System.out.println("Enter password : ");

            String password = new BufferedReader(new InputStreamReader(System.in)).readLine();

            System.out.println("Password : " + password); 

            //process password

        }

        else {

            //intended behavior
            System.out.println("Enter password : ");

            char[] password = console.readPassword();

             System.out.println("Password : " + Arrays.toString(password)); 

            //process password

        }

    }

}

Generally we would store the password in DB or validate. In above code I am printing it just to demonstrate it's working. Note how the password is not printed when user types it in.



When will it fail?

You must have notices the null check in the beginning of the code. That is because there is a know bug (bug #122429) in Eclipse IDE. We cannot get Console instance. So we have added a null check. However the mechanism will work just fine in normal console.




Related Links

Saturday 16 August 2014

Using Groovy to send Emails

Background

In this post we will see how can we write a groovy script to send emails. I am going to use javax.mail and javax.activation for this. To setup groovy environment you can refer to one of my earlier posts - 




Code

/**
 * @author athakur
 *
 */

@Grapes([
    @Grab(group='javax.mail', module='mail', version='1.4.7'),
    @Grab(group='javax.activation', module='activation', version='1.1.1'),
    @GrabConfig(systemClassLoader=true)
])

import javax.mail.internet.*;
import javax.mail.*
import javax.activation.*

class EmailSender {
    
    public static void main(def args){
        //to test the script
        EmailSender emailSender = new EmailSender();
        emailSender.sendmail();
    }
    
    def static message = "Test Message";
    def static subject = "Test Message Subject";
    def static toAddress = "test1@opensourceforgeeks.com";
    def static fromAddress = "test2@opensourceforgeeks.com";
    def static host = "test.email.opensourceforgeeks.com";
    def static port = "25";
    
    public EmailSender() {
        
    }

    def static sendmail() {
        sendmail(message, subject, toAddress, fromAddress, host, port);
    }
    
    def static sendmail(String message ,String subject, String toAddress, String fromAddress, String host, String port){
        Properties mprops = new Properties();
        mprops.setProperty("mail.transport.protocol","smtp");
        mprops.setProperty("mail.host",host);
        mprops.setProperty("mail.smtp.port",port);

        Session lSession = Session.getDefaultInstance(mprops,null);
        MimeMessage msg = new MimeMessage(lSession);

        StringTokenizer tok = new StringTokenizer(toAddress,";");
        ArrayList emailTos = new ArrayList();
        while(tok.hasMoreElements()){
            emailTos.add(new InternetAddress(tok.nextElement().toString()));
        }
        InternetAddress[] to = new InternetAddress[emailTos.size()];
        to = (InternetAddress[]) emailTos.toArray(to);
        msg.setRecipients(MimeMessage.RecipientType.TO,to);
        msg.setFrom(new InternetAddress(fromAddress));
        msg.setSubject(subject);
        msg.setText(message)

        Transport transporter = lSession.getTransport("smtp");
        transporter.connect();
        transporter.send(msg);
        
        println("Message sent");
    }
}



 Then you can simply run it with groovy EmailSender.groovy.

Note :  you can import all packages you need using the grab syntax mentioned in the top of the script.
 To know the group, module and version you can search the Maven repository.  In the link there is a separate tab for grape like there is for ivy, maven or gradle. So you can directly pick up the syntax from there. For example if you search for javax.activation you can see the following syntax under grape tab - 


@Grapes(

        @Grab(group='javax.mail', module='mailapi', version='1.4.3')

)



Friday 15 August 2014

How to capture arguments passed to a Groovy script?

It is very much similar to Java and you can use the same java syntax. For eg.

class TestExecutor {

    public static void main(def args) {
        println("Printing arguments");
        for(String arguments : args) {
            println (arguments);
        }
    }
} 


Run it and you should see the arguments printed



Also note if you do not provide main method or provide one like in above example then you can get arguments as args[i] but you can change the name of the array (again same as java). So you can have something like -

public static void main(def argsNew) {

    println("Printing arguments");

    for(String arguments : argsNew) {

        //using args in above for loop will throw error

        println (arguments);

    }

}


Point being it's not something that is hard-coded. Finally as suggested in other answer you can always use CliBuilder for smart parsing. But again in that too it internally used def options = cli.parse(args).

Finally there is always the hard way to do things (Just putting here for code coverage and additional information - no need to use this code for fetching arguments) -

import groovy.lang.Binding;
Binding binding = new Binding();
int x = 1
for (a in this.args) {
  println("arg$x: " + a)
  binding.setProperty("arg$x", a);
  x=x+1
}
println binding.getProperty("arg1")
println binding.getProperty("arg2")
println binding.getProperty("arg3") 

Important Links

Wednesday 13 August 2014

Executing Shell commands in Groovy

Background

Groovy is a dynamic java language that runs on JVM. If you are familiar with Java that you will find it very easy to understand and code in Groovy. Will not go into much details now. You can refer to my earlier posts - 

Goal of this post is to show how easy it is to execute shell commands from Groovy. I am using Windows for demonstrating this but should be irrespective of OS you are using. All you have to do is make sure cURL is installed on your system which I will be using to demonstrate this example. You can refer to following posts on cURL


To the code....

 Put the following code in a file name GroovyTest.groovy and run it

class GroovyTest {
    public static void main(def args){
        def url = 'http://mail.google.com';
        println("Output : " + executeCurlCommand(url));
    }    
    
    def static executeCurlCommand(URL){
        
        def url = "curl " + URL;
        def proc = url.execute();
        def outputStream = new StringBuffer();
        proc.waitForProcessOutput(outputStream, System.err)
        return outputStream.toString();
        
        
    }
}

After running you can see the output same as executing the command on your console. If you need all outputs on standard output you can do

proc.waitForProcessOutput(System.out, System.err);


You can see the output by executing the same command in console

Using HTTP POST and GET using cURL

Background

cURL is a command line utility to perform GET and POST requests. It comes really handy for testing REST APIs. For debian based Linux systems you can simply install it

  • sudo apt-get install curl

But you can install  it in windows as well. You can use it from cygwin as well (select the appropriate package while installing). For installing cURL in windows I had written a post some time back. You can refer the same -


That's about installation. In this post we will see the usage if cURL command.



Using cURL command

As we know there are two majorly used REST APIs - GET and POST. Lets see how to perform these actions using cURL - 

GET

  1. With json :

    curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource

  2. With XML :

    curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource

POST


  1. For posting data :

    curl --data "param1=value1&param2=value2" http://hostname/resource

  2. For File Upload :

    curl --form "fileupload=@filename.txt" http://hostname/resource

  3. RESTful HTTP post :

    curl -X POST -d @filename http://hostname/resource

  4. For logging into a site (auth):

    curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
    curl -L -b headers http://localhost/

POST with some JSON data

You can use -H to set the header while using cURL command 

Example  - 
  • -H "Content-Type: application/json"
So your complete command would look something like -

curl -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/api/login

Difference between GET and POST


Related Links


Saturday 2 August 2014

Installing Groovy plugin in Eclipse

Background

You may want to go through the previous post on what is groovy and how to set the environment required to run Groovy programs. In this post I will show how to install it's Eclipse plugin.


Getting Started

  1. In Eclipse go to Help -> Install new Software .


  2. For the plugin site refer to the Groovy page for Eclipse plugin. The site depends on which version on Eclipse you are using. I am using Juno. So link for me is http://dist.springsource.org/release/GRECLIPSE/e4.2/.


  3. Select "Groovy-Eclipse" and click on next.



  4. Check install Details and Click on Next. Accept the agreement and click install. It will take some time to install the plugin depending on your internet bandwidth. You can see the progress at the bottom of Eclipse or from Progress tab.

Create a new groovy Project

  1.  Create a new Groovy project named GroovyTestProject.



  2. Create a groovy class named GroovyTest.groovy. Enter the following code and then run it as Groovy Script.

    class GroovyTest {
        public static void main(def args){
            println("Hello World!")
        }
     }
    


  3.  You should see the output in console.

Spurious wakeups in Java and how to avoid them.

Spurious wakeups

If you haven't heard this word now would be a good time for it. A very interesting topic. I was not aware of this problem some time back  and when I did some research on it turns out the concept does exist and must be taken care of in production level code. It is in fact a well thought design decision (Read more in Interesting Explanation section).

What are Spurious wakeups?

As per Wiki

Spurious wakeup describes a complication in the use of condition variables as provided by certain multithreading APIs such as POSIX Threads and the Windows API.

Even after a condition variable appears to have been signaled from a waiting thread's point of view, the condition that was awaited may still be false. One of the reasons for this is a spurious wakeup; that is, a thread might be awoken from its waiting state even though no thread signaled the condition variable. For correctness it is necessary, then, to verify that the condition is indeed true after the thread has finished waiting.

Why do they occur?


The pthread_cond_wait() function in Linux is implemented using the futex system call. Each blocking system call on Linux returns abruptly with EINTR when the process receives a signal. ... pthread_cond_wait() can't restart the waiting because it may miss a real wakeup in the little time it was outside the futex system call. This race condition can only be avoided by the caller checking for an invariant. A POSIX signal will therefore generate a spurious wakeup.

Interesting Explanation


Just think of it... like any code, thread scheduler may experience temporary blackout due to something abnormal happening in underlying hardware / software. Of course, care should be taken for this to happen as rare as possible, but since there's no such thing as 100% robust software it is reasonable to assume this can happen and take care on the graceful recovery in case if scheduler detects this (eg by observing missing heartbeats).

Now, how could scheduler recover, taking into account that during blackout it could miss some signals intended to notify waiting threads? If scheduler does nothing, mentioned "unlucky" threads will just hang, waiting forever - to avoid this, scheduler would simply send a signal to all the waiting threads.

This makes it necessary to establish a "contract" that waiting thread can be notified without a reason. To be precise, there would be a reason - scheduler blackout - but since thread is designed (for a good reason) to be oblivious to scheduler internal implementation details, this reason is likely better to present as "spurious".

How to avoid a spurious wakeup?

A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:


     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait(timeout);
         ... // Perform action appropriate to condition
     } 


Important Links

Setting up Groovy environment

What is Groovy?

Groovy is a dynamic language that runs on the JVM and is tightly integrated with the Java language.  Groovy provides lots of simplifications compared to standard Java language features and advanced language features as properties, closures, native support for lists, maps and regular expressions, duck typing and the elvis operator.


Setting up groovy environment

Groovy requires Java, so you need to have a version available (while groovy 1.6 supported JDK 1.4 or greater, for groovy 1.7 onwards, minimum JDK 1.5 is needed)

Download the latest version from Groovy.  Unzip it and put it in some folder on your hard drive.
Then you have to set the GROOVY_HOME environment variable and add %GROOVY_HOME%/bin  to your PATH variable (Similar to JAVA_HOME. See above link on how to set up java environment).

Open groovyConsole.bat by double clicking on the icon in the bin directory of the Groovy distribution. If that does not work you may want to open the command line, navigate to bin directory and execute the bat file. Even if it does not open you will see the errors responsible for not starting the bat file.

Type in println("Hello World from yourName!")  and you should see it printed on the console (screenshot attached). After typing do Ctrl + R to execute the code.



Loading external jars in Grrovy

If you see your groovy-2.3.6\conf folder you will see groovy-starter.conf file which looks like - 


And if you notice it loads jars from
  1. {groovy.home}/lib/*.jar or
  2. !{user.home}/.groovy/lib/*.jar
You can put your jars in any of the above folder. Also .gradle folder should be automatically be created in your user directory. For me it's C:\Users\athakur\.groovy.

Difference between Groovy and Java

  •  In Java 'java.lang' package is imported by default, but in groovy some others general purpose packages and classes are imported by default. Eg   
    • groovy.lang.*,
    • groovy.util.*,
    • java.io.*,
    • java.net.*,
    • java.util.*,
    • java.lang.*
  • In Java '==' operator is used for comparing primitive types and .equals is used to compare Objects but in grooby we can use '==' to compare both primitive and objects.
  • Semicolon is optional in groovy
  • In groovy all classes and methods and public by default.
  • Array is initialized as int[] array = [1,2,3] in groovy unlike Java where you do int[] array = { 1, 2, 3}.
  • In groovy you need to specify a variable of it is primitive or instance of some class. All you have to do is use keyword 'def' and groovy is smart enough to understand the instance type. Eg. def str = "Hello World!"
  • The return keyword is optional.
  • You can use the this keyword inside static methods (which refers to this class).
     

Reference to more information 

Setting up your Java environment

Goal

This is the most basic task that needs to be done before writing Java programs. I had written a couple of basic posts on Java to explain the basics which also includes downloading and installing Java.  Goal of this post is to list down steps to do so. This will be kept as a reference for all future posts.


Previous related posts

Steps to set up Java on your machine

  1. Download suitable Java distribution from  oracle site.
  2. Run the installer. 
    1. For Windows simple run the .exe file.
    2. For debian based system you can simply
      execute sudo apt-get install oracle-java7-installer
      from command line. 
  3. Next we need to add JAVA_HOME environment variable. Though not required for running standalone java programs. Many other software's use this to locate Java installation.   To do so follow the steps 
    1. Windows
      1. Open the System control panel
      2. Open Advanced tab
      3. Click on the Environment Variables button
      4. Add a new System variable with the name JAVA_HOME. Value should be the directory Java was installed in (something like C:\Program Files\Java\jdk1.7 
    2. Linux
      1. Refer How to permanently set $PATH on Linux?
      2. Path is above post is different, You should add your Java installation path (Something like /home/user/java/jdk1.7/bin)
  4. For windows when you run the installer java will be automatically added to %PATH% variable. You can easily verify that by executing following in cmd - java -version. If not added you can do so by adding %JAVA_HOME%\bin to your system path (assuming you followed step 3 else provide complete path).
t> UA-39527780-1 back to top