Monday, December 31, 2012

Eclipse author ${user} variable



Here is a small “hack” which can put anything you want in this ${user} variable.

Add one more variable in the eclipse.ini file with the following.
-vmargs 
-Duser.name="your name here..." 

or
Rather than having to change the template manually, add 
-vmargs -Duser.name="your name here" to the shortcut you use to run Eclipse ( by right clicking on it, selecting properties and editing the target input field) :
C:/java/eclipse/eclipse.exe -vmargs -Duser.name="your name here.."

Sunday, December 30, 2012

Force overwrite on git pull

How do I force an overwrite of local files on a git pull?


git fetch --all
git reset --hard origin/master
git fetch downloads the latest from remote without trying to merge or rebase anything. 
Then thegit reset resets the master branch to what you just fetched

Thursday, December 27, 2012

Image decorator






Here is the simple snippet to add a decorator to the image.



@Override
public Image getImage(Object element) {


if (resource instanceof Column)
{
 return decorateImage(SImageRegistry.getImage(SImageConstants.column), element);
}

}


public Image decorateImage(Image image, Object element) {
if (element instanceof Column) {
if (((Column) element).isPrimaryKeyPart()) {
DecorationOverlayIcon decorationOverlayIcon = new DecorationOverlayIcon(image,
SImageRegistry.getImageDescriptor(SImageConstants.primary_key), IDecoration.TOP_LEFT);
return decorationOverlayIcon.createImage();
}
return image;
}
return null;
}

Wednesday, December 5, 2012

Increase heap size to your eclipse

Here is the example to specify the eclipse vm arguments in eclipse.ini file to avoid eclipse memory issues during your development.

-startup
plugins/org.eclipse.equinox.launcher_1.0.100.v20080501.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.0.100.v20080428-1330
-showsplash
org.eclipse.platform
-vm
/usr/lib/jvm/java-1.5.0-sun/jre/bin/java
-vmargs
-Xms512m
-Xmx1024m
-XX:+UseParallelGC
-XX:PermSize=512M
-XX:MaxPermSize=1024M

If you are working with eclipse plug-in development, while launching your eclipse application you have to specify the vm arguments so that it will be available for your target platform as well.
Please find the screenshot below to configure it.









Wednesday, November 28, 2012

Collections emptyList()

Can you guess the output for the below program ?
import java.util.Collections;
import java.util.List;

public class CollectionsTest {

private List<String> pool = Collections.<String> emptyList();

public List<String> call1() {
pool.add("Hello");
return pool;
}

public static void main(String[] args) {
CollectionsTest collectionsTest = new CollectionsTest();
List<String> call1 = collectionsTest.call1();
System.out.println(call1);
call1.add("one more added..");
System.out.println(call1);
}
}


Output:

Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:131)
at java.util.AbstractList.add(AbstractList.java:91)
at CollectionsTest.call1(CollectionsTest.java:9)
at CollectionsTest.main(CollectionsTest.java:15)


In short:
Collections.emptyList() returns an immutable list, i.e., a list to which you cannot add or remove elements.

Then, why should we use at all ? What is the purpose of this ?


Best Regards,
Kondal Kolipaka


My JAXB Learnings - Part 2


As part of this post, you will be understanding on the following concepts in the JAXB through an example.

1. Defining the name space
2. Defining the wrapper elements in the XML
3. Setting the name for entities in the list.
4. Defining the order for elements

This example is based on the book store and book model objects.

Book.java


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

@XmlRootElement(name = "myBook")
// If you want you can define the order in which the fields are written
// Optional
@XmlType(propOrder = { "author", "name", "publisher", "isbn" })
public class Book {

private String name;
private String author;
private String publisher;
private String isbn;

// If you like the variable name, e.g. "name", you can easily change this
// name for your XML-Output:
@XmlElement(name = "titleName")
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getPublisher() {
return publisher;
}

public void setPublisher(String publisher) {
this.publisher = publisher;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

}



2. BookStore.java


import java.util.ArrayList;

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

//This statement means that class "Bookstore.java" is the root-element of our example
@XmlRootElement(namespace = "com.kondal.bookstore.model")
public class BookStore {

// XmLElementWrapper generates a wrapper element around XML representation
@XmlElementWrapper(name = "bookList")

// XmlElement sets the name of the entities
@XmlElement(name = "book")
private ArrayList<Book> bookList;
private String name;
private String location;

public void setBookList(ArrayList<Book> bookList) {
this.bookList = bookList;
}

public ArrayList<Book> getBooksList() {
return bookList;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}
}


3. BookMain 


import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

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

public class BookMain {

private static final String BOOKSTORE_XML = "./BookStore-jaxb.xml";

public static void main(String[] args) throws JAXBException, IOException {

ArrayList<Book> bookList = new ArrayList<Book>();

// create books
Book book1 = new Book();
book1.setIsbn("978-0060554736");
book1.setName("The Game");
book1.setAuthor("Neil Strauss");
book1.setPublisher("Harpercollins");
bookList.add(book1);

Book book2 = new Book();
book2.setIsbn("978-3832180577");
book2.setName("Feuchtgebiete");
book2.setAuthor("Charlotte Roche");
book2.setPublisher("Dumont Buchverlag");
bookList.add(book2);

// create BookStore, assigning book
BookStore BookStore = new BookStore();
BookStore.setName("Fraport BookStore");
BookStore.setLocation("Frankfurt Airport");
BookStore.setBookList(bookList);

// create JAXB context and instantiate marshaller
JAXBContext context = JAXBContext.newInstance(BookStore.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

// Write to System.out
m.marshal(BookStore, System.out);

// Write to File
m.marshal(BookStore, new File(BOOKSTORE_XML));

// get variables from our xml file, created before
System.out.println();
System.out.println("Output from our XML File: ");
Unmarshaller um = context.createUnmarshaller();
BookStore bookstore2 = (BookStore) um.unmarshal(new FileReader(
BOOKSTORE_XML));
ArrayList<Book> list = bookstore2.getBooksList();
for (Book book : list) {
System.out.println("Book: " + book.getName() + " from "
+ book.getAuthor());
}
}
}


Output on console:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:bookStore xmlns:ns2="com.kondal.bookstore.model">
    <bookList>
        <book>
            <author>Neil Strauss</author>
            <titleName>The Game</titleName>
            <publisher>Harpercollins</publisher>
            <isbn>978-0060554736</isbn>
        </book>
        <book>
            <author>Charlotte Roche</author>
            <titleName>Feuchtgebiete</titleName>
            <publisher>Dumont Buchverlag</publisher>
            <isbn>978-3832180577</isbn>
        </book>
    </bookList>
    <location>Frankfurt Airport</location>
    <name>Fraport BookStore</name>
</ns2:bookStore>

Output from our XML File:
Book: The Game from Neil Strauss
Book: Feuchtgebiete from Charlotte Roche




Tuesday, November 27, 2012

Get started with JAXB

If you are a beginner for JAXB. Here is the simple example will help you to get started.

All you need to have is JDK environment. This comes along with JAXB implementation.

This particular example is based upon the customer data, which deals with marshaling and unmarshaling.

Work flow:
1. Define your customer class, and identify the elements and attributes as part of  it.

Customer.java


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

@XmlRootElement
public class Customer {

String name;
int age;
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;
}

}

2. Using JAXB Context do the marshaling.

CustomerJAXBMarshal.java

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class CustomerJAXBMarshal {
public static void main(String[] args) {

Customer customer = new Customer();
customer.setId(100);
customer.setName("Raj");
customer.setAge(20);

try {

File file = new File("C:\\Customer.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

jaxbMarshaller.marshal(customer, file);
jaxbMarshaller.marshal(customer, System.out);

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

}
}


Output on console:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100">
    <age>20</age>
    <name>Raj</name>
</customer>


3.  Unmarshal the Customer XML file to build the Customer run time object.

CustomerJAXBUnmarshal.java

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class CustomerJAXBUnmarshal {
public static void main(String[] args) {

try {

File file = new File("C:\\Customer.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer);

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

}
}

Output on console:
Customer@1feca64