Posts Tagged Intensity

Object Persistence in Java

Introduction

At the time of development, sometimes it is necessary to store the state of the object in the file system. Some objects may or may not be stored in the file system depending upon the structural intensity of the object graph. In this article, I will focus on two major aspects of the object persistence. Before going to this subject, I would like to tell you about the significance of the object persistence. Object persistence presupposes the state of the object in the file system. In this matter you can make argument regarding object persistence in the database, which hibernate does. But so far this article is concerned I will give glimpse on persistence in the file system for all convenience. The state of object signifies the attributes or properties of the object in the broader sense. The object graph represents the internal morphological structure of the object. So persisting object means, you are going to store all the internal changed structure of the object.

Technicalities

There are several ways you can persist the state of the object. You can take help from Java IO system to store the object in the file system. However there are convenient approaches you can meet your expectations in this regard. One way is the textual representation of the object graph in the file system and another way is the binary representation of the object graph. These ways are very much convenient and easy from the view point of development. You can achieve the textual representation of the object graph using XMLEncoder and you can achieve the binary representation of the object graph using java object serialization process. Let me explain the two approaches below.

Persistence using XMLEncoder

XMLEncoder class is an approach to persist the object graph in an XML document or simply in an XML file. It provides the flexibility of storing the object as a textual approach. In this approach you can see the XML file and you can easily understand the attributes of the object. Similarly to obtain the object graph from the XML file, you can use XMLDecoder. All these classes have been defined in the java.beans package. Let me clarify all the aspects by citing the complete example.

Create a normal java bean or class having the following structure.

Let us see the class called Emp.java which is a normal java bean.

There is another test harness class called TestPersistence.java which exposes the use of XMLEncoder and XMLDecoder.

The following is the Emp.java.

package com.core.persist;

/**

* This is a simple java bean.

* @author Debadatta Mishra(PIKU)

*

*/

public class Emp

{

private String name = null;

private int age = 0;

private String empId = null;

public Emp()

{

super();

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getEmpId() {

return empId;

}

public void setEmpId(String empId) {

this.empId = empId;

}

}

The following is the TestPersistence.java

package com.core.persist;

import java.beans.XMLDecoder;

import java.beans.XMLEncoder;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

/**

* This is a test harness class to display the

* use of XMLEncoder and XMLDecoder.

* @author Debadatta Mishra(PIKU)

*

*/

public class TestPersistence

{

public static void main(String[] args)

{

Emp emp = new Emp();

emp.setName(”John”);

emp.setAge(23);

emp.setEmpId(”A123″);

try

{

/*

* The following codes are used to persist the Emp object graph

*/

XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(

new FileOutputStream(”C:/emp.xml”)));

encoder.writeObject(emp);

encoder.flush();

encoder.close();

/*

* The following codes are used to obtain the Emp object graph

* from the XML document

*/

XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(

new FileInputStream(”C:/emp.xml”)));

Emp emp1 = ( Emp )decoder.readObject();

decoder.close();

System.out.println(”Emp Name=>”+emp1.getName());

System.out.println(”Emp Age=>”+emp1.getAge());

System.out.println(”Emp Id=>”+emp1.getEmpId());

}

catch( Exception e )

{

e.printStackTrace();

}

}

}

The following is the output of the above example.

If you run the above classes, an XML document called emp.xml will be created in the specified location. The xml document will look like the following.

23

A123

John

So you have stored the state of the Emp object in the xml document. It is also required to load the Emp object from the xml document. For this purpose you have use XMLDecoder which has been used in the test harness class. If you want to test the above two classes, you can copy the classes and change the package structure and you can run it. In case of loading the object using XMLDecoder, it takes help from java’s reflection system.

Advantages of XMLEncoder and XMLDecoder

• Since it is a textual representation of the object graph, anybody can see the XML file and it helps in the portability to any other system.

• If you want to change the value of a particular property of an object, you can do it directly in the XML document so that while using XMLDecoder, you will get your modified value.

• If the object’s variables are declared transient, still you are able to store the complete object graph along with the transient variable’s value. This case is not possible in case of java object serialization.

• It is also very easy and convenient in case of object inheritance. There is no need to bother about the super class and sub class. Some of the limitations of normal java object serialization can be over come in this approach.

Persistence using Serialization

Serialization is a java’s default mechanism to save the state of the object or simply the object graph in the file system. In this case your object will be persisted in the file system where the file is not human readable. It means that you are going to store the binary representation of the object graph in the file system. This object serialization can be achieved using the writeObject() method of the class ObjectOutputStream. The main thing you have to remember is that the object you are going to persist must implement Serialization interface which is called as marker interface. In next article I will explain you the use and the beauty of the marker interfaces. Similarly deserialization means retrieval of object from the saved state. You can achieve the deserialization using readObject() method of the ObjectInputStream class.

Please refer below the following piece of code to achive serialization.

The following is class called Emp. It implements Serializable interface. There is another class called TestSerialization. This class performs both serilization and deserilization. This is the normal way of serilization concept from java.

package com.core.persist;

import java.io.Serializable;

/**

* This is a simple java bean.

* @author Debadatta Mishra(PIKU)

*

*/

public class Emp implements Serializable

{

private static final long serialVersionUID = -164971138528601769L;

private String name = null;

private int age = 0;

public Emp()

{

super();

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

Class TestSerialization.java

package com.core.persist;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

public class TestSerialization

{

public static void main(String[] args)

{

Emp emp = new Emp();

emp.setAge(23);

emp.setName(”John”);

try

{

/*

* Code to serialize or persist the object

*/

ObjectOutputStream ous = new ObjectOutputStream( new FileOutputStream(”D:/test.ser”));

ous.writeObject(emp);

ous.close();

/*

* Code to deserialize the object

*/

ObjectInputStream oin = new ObjectInputStream( new FileInputStream(”D:/test.ser”));

Emp emp1 = ( Emp )oin.readObject();

System.out.println(”Emp age—-”+emp1.getAge());

System.out.println(”Emp Name—-”+emp1.getName());

}

catch( Exception e )

{

e.printStackTrace();

}

}

}

You can run the above code in your editor to test the functionalities relating to serialization.

Now I put forward some cases for seriliazation.

Case-1:

If your object does not implement Serializable interface,

In order to serialize an object, it is a must that the class must implement seriliazable interface. This is the required principle of serilization. Oterwise it will throw NotSerializationException.

There is another way, if your class does not implement Serilizable interface, you have to declare the object as transient. So that that object state will not be persisted.

Case-2:

In case of inheritance, your super class does not implement Serializable interface and sub class also does not.

In this case serilization will not happen . If you are interested to store the all the properties of the object you can go for XMLEncoder and XMLDecoder as I have alredy explained you.
Case-3:

In case of inheritance, your class implements Serializable interface and sub class does not.

In this case, you should not worry about it, seriliazation happens.

Case-4:

In case of inheritance, your super class does not implement Serializable interface but your sub class does.

Seriliazation will happen but with a lemon falvour. Here no exception will be thrown but your super class data members or object properties of your super class will be not be persisted. When you deserialize object, you will get the default values of your super class object.

Case-5:

This is the best case. You super class and sub class implement serializable interface.

Everything is fine here, serilization happens.

Case-6:

If your object uses transient modifiers inside the objects,

You have to remember that transient objects or variables will not be persisted in case of serialization.

Case-7:

If your object uses volatile modifiers inside the object,

There is nothing to worry about, serialization will happen normally and data will be persisted.

Case-8:

If your object uses static modifiers inside the object,

You have to remember that,since static is not a part of object, so static variables or static object reference will not be persisted in case of seriaization.

Case-9:

It is a very special case I am going to focus on. You may encounter the following situations at the time of serialization.

• You are not sure whether your super class does implement serializable interface.

• You do not have access to the source code of your super class.

• Your super class may be a final class.

• Your super class may contain noe-serializable object reference.

In this case, if you feel frustration and disappointment, you can go for XMLEncoder and XMLDecoder as I have already explained.

If you want to persist the object using java’s serialization concept and mechanism, you have to do it little bit intelligently and manually.

Please refer to the following piece of code.

The following class name is Emp.java

package com.core.persist;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

/**

* This is a simple java bean.

* @author Debadatta Mishra(PIKU)

*

*/

public class Emp implements Serializable

{

private static final long serialVersionUID = -164971138528601769L;

private String name = null;

private int age = 0;

private String empId = null;

private transient Project proj = null;

public Emp()

{

super();

proj = new Project();

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getEmpId() {

return empId;

}

public void setEmpId(String empId) {

this.empId = empId;

}

public Project getProj() {

return proj;

}

public void setProj(Project proj) {

this.proj = proj;

}

/**You are proividing a default callback method

* for manual seriallization process.

* @param os of type {@link ObjectOutputStream}

* @throws Exception of type {@link Exception}

*/

private void writeObject( ObjectOutputStream os ) throws Exception

{

try

{

os.defaultWriteObject();

os.writeInt( proj.getProjectId());

os.writeObject( proj.getPojectName() );

}

catch( Exception e )

{

e.printStackTrace();

}

}

/**You are providing default desrialization with

* some manual twik.

* @param oin of type {@link ObjectInputStream}

* @throws Exception of type {@link Exception}

*/

private void readObject( ObjectInputStream oin ) throws Exception

{

try

{

oin.defaultReadObject();

proj = new Project();

proj.setProjectId(oin.readInt());

proj.setPojectName( (String) oin.readObject() );

}

catch( Exception e )

{

e.printStackTrace();

}

}

}

The following class name is Project.java

package com.core.persist;

/**

* @author Debadatta Mishra(PIKU)

*

*/

public class Project

{

private int projectId = 0;

private String pojectName = null;

public Project()

{

super();

}

public String getPojectName() {

return pojectName;

}

public void setPojectName(String pojectName) {

this.pojectName = pojectName;

}

public int getProjectId() {

return projectId;

}

public void setProjectId(int projectId) {

this.projectId = projectId;

}

}

The following class name is TestSerialization.java which is test harness class.

package com.core.persist;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

/**

* @author Debadatta Mishra(PIKU)

*

*/

public class TestSerialization

{

public static void main(String[] args)

{

Emp emp = new Emp();

emp.setAge(23);

emp.setEmpId(”A1″);

emp.setName(”John”);

Project proj = new Project();

proj.setProjectId(5555);

proj.setPojectName(”XYZ”);

emp.setProj(proj);

try

{

ObjectOutputStream ous = new ObjectOutputStream(

new FileOutputStream(”D:/test.ser”));

ous.writeObject(emp);

ous.close();

ObjectInputStream oin = new ObjectInputStream(new FileInputStream(

“D:/test.ser”));

Emp emp1 = ( Emp )oin.readObject();

System.out.println(”Emp age—-”+emp1.getAge());

Project proj1 = emp1.getProj();

System.out.println(”Proj Id—–”+proj1.getProjectId());

System.out.println(”Proj Name—-”+proj1.getPojectName());

}

catch( Exception e )

{

e.printStackTrace();

}

}

}

Please see the two methods writeObject() and readObject() inside the class Emp. These two methods are significant in the sense that you are going to achieve serialization with your default object as well as manual serialization with the non serializable object with some data. When you call the methods writeObject() and readObject() for a particular object these methods will be invoked automatically and will persist some default data. In these particular methods you are persisting data manually and thereby making the whole object serializable.

Conclusion

I hope that you will enjoy my article. If you find any problems or errors, please feel free to send me a mail in the address debadattamishra@aol.com . This article is only meant for those who are new to java development. This article does not bear any commercial significance. Please provide me the feedback about this article.

Subscribe to My Newsletter
The benefits your subscribers are going to receive from your newsletter
Name:
Email:
 
Powered by Optin Form Adder

Popularity: 1% [?]

Post to Twitter

Tags: , , , , , , , , ,

The Different Types Flowers

What do these lexis have in frequent: flower, florist, flower store? Apart from all containing the search of the word \’flower\’, there is much more that they should disclose. Naturally, coupled by the being intensity of the florist, the plants and the flower store are intricately linked. Beyond eloquent that they\’re related, it\’s important to discover that the connection is a good one that you, as a client, will want to engross manually in. What to look for will be a combination of the three. Make assured that on top of providing the flora you darling, the flora are also of top eminence. The florist should be an eager accomplice in ensuring that you get just what you ask for, exceeding your own expectations. The flower stored is necessary to exceed the expectations. If the flower store is , you will not only get a scenic spray, but it will enter really crisp and on time. Be definite the florist you decide workings mutually well with both the flora and the flower store. This will guarantee a blissful buyer with a product that is better than imagined. Flowers, which have been and remain to be the mark of expected beauty throughout the ages, are one realm of vogue that has not dramatically evolved. This speaks keenly in advance of plants, indicating their timelessness and perfection that wishes no improvement.

Thus, when ordering plants, what you should to respect is not the style of flora, but the preference of the receiver. Once you have firm winning the brand of flower, you must find a flower store that provides your beloved strain at optimal trait. A flower\’s quality depends very much on the mind the florist gives, but past the florist\’s heed, it is optimal that the flora come level from the growers or from a flower sale. This ensures their flavor, as the time useless in travel is diminished when flora make fewer trips to spread their flower store destination. Flowers are known to positively weight the being mind, so be sure to go all the way with this definite pressure by retail the freshest flora you can find on the promote. Knowing of this explicit cause of flora on people, you should presume your florist to be an optimistic and open persona – in sharp, the convinced outcome of the plants should not be mystified in the doubts of their job. A talented florist is not only skilled of creating a lovely arrangement, but also disposed and interested in making arrangements with you to guarantee the arrival of the spray you predict. While the florist is an artist, who must use creativity as a tool of the job, the florist has many more roles to complete. One of the most important of these additional roles is the sensitivity of the florist: the florist is not only putting plants together, but understanding what affair you indigence the plants for, what mode of character the flora are directed to – the florist must have the facility to understand and sympathize with the difficulties and joys in life of every incident, demonstrating this post in the end upshot: the flower arrangement.

If the florist deals speedily with the farmer, each at the farmer\’s position or through a flower sale, this is a good indicator of the florist, viewing superstar who cuts the hound frankly to the resource, lowering prices for himself and his client, by being more focus. The florist, as aforementioned, is a persona who creates the connection between flowers and flower store, between grower and consumer. A character of central importance, the florist is a person who is impressive and establishes a good bond with you, his client. The flower store is the last, but not the slightest important, of our floral trinity. The flower shop completes the picture. The flower shop is the unit which makes the talent and efficiency of the florist impending; it is the florist\’s workshop and tool bench, if you will, while also plateful to keep the flowers in moist and good order. It is the flower shop that ensures approach of flowers to their closing destination not purely as a superb bouquet, but as moist, green, and promising living creations – as breathing as a rainforest. In addition, the flower shop acting a function in influencing the consumer and shaping his ruling as regards the entire wrap of the shop. Online photographs of vacant bouquets do more than imply the variety of flowers and ability of the florist. The photographs, as well as the total website, are indicators of the organization and professionalism of the party. The person-expression of a company speaks noisily. Reading testimonials on websites is also an indicator of the combination of the flowers, the florist, and the flower shop. It takes a fanatical performance from each of the participants to make a client delighted.

Flowers are, lacking query, a classic, and yet beautiful gift. Unique and living, they are different from all other gifts, and manage to convey emotions at the same time as they are plateful a position as a existing. Thus, to optimize and attain this potential of emotion that flowers can induce, it is important that you buy your flowers from a flower shop that is professional and sincere, with a operate of florists who love their work and respect their flowers as alive as we are, and finally, with flowers that are as crisp and expressive as you are, to disclose precisely your belief through their understated and bright beauty.

Popularity: 1% [?]

Post to Twitter

Tags: , , , , , , , , , , ,

Powered by Yahoo! Answers