Posts

Java JVM Shutdown Hook - tutorial

JVM shutdown hooks provide a clean and simple mechanism for registering application-specific behavior that performs the required cleanup work when a JVM terminates. It is a Java feature that let you have a piece of Java code run whenever the JVM terminates under one of the following conditions:   - The program exits normally, such as when the last non-daemon thread exits or when the                         Runtime.exit() method is invoked.   -  The virtual machine is terminated in response to a user interrupt, such as typing CTRL-C, or a                      system-wide event, such as user logoff or system shutdown (for example, the JVM receives               one of  the interrupt signals SIGHUP (Unix Only), SIGINT, or SIGTERM). A shutdow...

Java HashMap Example

The HashMap class uses a hash table to implement the Map interface. This allows the execution time of basic operations, such as get() and put() , to remain constant even for large sets. The following constructors are defined:   HashMap( ) HashMap(Map m ) HashMap(int capacity ) HashMap(int capacity , float fillRatio )   Hash map does not guarantee the order of its elements. Therefore, the order in which elements are added to a hash map is not necessarily the order in which they are read. Here is the code:   import java.util.HashMap; import java.util.Map; /**  *  * This Java HashMap example describes the basic operations performed on the HashMap  * @author abdul  *  */ public class HashMapExample {     public static void main(String args[]){                 // constructs a new empty HashMap with default initial capacity   ...

How to get free disk space of a drive in java ?

JDK 1.6 provides few new methods  getTotalSpace() , getFreeSpace() , bundled with java.io.File. These methods provides essential information regarding disk space. getFreeSpace() method -   Returns the number of unallocated bytes in the partition named by this abstract path name. getTotalSpace() method - Returns the size of the partition named by this abstract pathname. Here is the code:  import java.io.File; /**  * Program that returns the number of unallocated bytes , total space in the partition.  *  * @author abdul  *  */ public class DiskDetail {     public static void main(String[] args) {         File file = new File("C:");         long totalSpace = file.getTotalSpace(); // total disk space in bytes.         long freeSpace = file.getFreeSpace(); // unallocated / free disk space   ...

JAXB - II (jaxb.index file and ObjectFactory)

I was getting a Exception like, javax.xml.bind.JAXBException: "doesnt contain ObjectFactory.class or jaxb.index" while trying to create a JAXBContext using JAXBContext.newInstance(String contextPath). It took me a while to figure out what went wrong. So now that I've got things working correctly, I thought I'd post this example and solution to hopefully save you some time. When we create a marshaller, we first need to create a JAXBContext via its newInstance() factory method. You can create a context for a specific JAXB class  or you can create a context for a list of packages. There are two ways to resolve this issue :    - By creating ObjectFactory   - By adding jaxb.index file jaxb.index : The jaxb.index file is a text file contains a listing of the classes on the containing package that have JAXB annotations. Note : The name of the clases is their simple name not their classified name.   Rather than creating a ObjectFactory, I guess adding ...

Java Architecture for XML Binding (JAXB)

Overview:  - Java Architecture for XML Binding (JAXB) is a Java standard that defines how Java objects are converted to/from XML (specified using a standard set of mappings).  - It defines a programmer API for reading and writing Java objects to / from XML documents and a service provider which / from from XML documents allows the selection of the JAXB implementation  - It makes reading  and writing of XML via Java very easy.  - It allows Java developers to access and process XML data without having to know XML or XML processing  - It is used heavily by JAX-WS    - It provides ways to generate XML content from a Java representation , to generate a Java representation from XML file , to generate XML schema from Java Objects  - The JAXBContext class provides the client's entry point to the JAXB API. It provides an abstraction for managing the XML/Java binding information necessary to implement the JAXB binding framework operati...

OSGi for Beginners

Image
Open Services Gateway initiative framework (OSGi) 1. What is OSGi ?   OSGi is a specification. The core of the OSGi specification defines a component and service model for Java. The components and services can be dynamically activated, de-activated, updated and de-installed. A very practical advantage of OSGi is that every bundle must define its exported Java packages and its required dependencies. This way you can effectively control the provided API and the dependencies of your plug-ins. OSGi bundles : The OSGi specification defines the OSGi bundle as the unit of modularization. A bundle is a cohesive, self-contained unit, which explicitly defines its dependencies to other modules and services. It also explicitly defines its external API. Technically OSGi bundles are .jar files with additional meta information. This meta  information is stored in the "META-INF" folder in the "MANIFEST.MF" file. bundle = identity & dependency info + jar The "MANIFE...

Java Reflection: Annotations

What are Java Annotations? Annotations is a new feature from Java 5. Annotations are a kind of comment or meta data you can insert in your Java code. These annotations can then be processed at compile time by pre-compiler tools, or at runtime via Java Reflection. The annotation can be attached to Classes, Methods,Parameters,Fields etc. How to create custom Annotation? To create an annotation we use the interface keyword and add an @ symbol infront of it. The @ symbol will tell the compiler that it doing some business with an annotation. @Retention (RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value(); } The @ in front of the interface marks it as an annotation. Once you have defined the annotation you can use it in your code.Here is an example of class annotation: @MyAnnotation(value="Class Annotation") public class MyClass { } The two directives in the annotation definition, @Retention(RetentionPolicy.RUNTIME) and @Target(El...