Saturday, March 21, 2009

A simple light weight Event Logger

A very simple light weight logger is required some times where you don’t need to utilize Log4j or other loggers.

This logger creates a separate log file for every Java class, so that when your are running a heavy process coupled with different java components (classes) read here for Java Source code for Event logger

DES Encryption and Decryption Java

The Data Encryption Standard (DES) is a block cipher (a method for encrypting information) that was selected by NBS as an official Federal Information Processing Standard (FIPS) for the United States in 1976 and which has subsequently enjoyed widespread use internationally. It is based on a Symmetric-key algorithm that uses a 56-bit key. The algorithm was initially controversial with classified design elements, a relatively short key length, and suspicions about a National Security Agency (NSA) backdoor. DES consequently came under intense academic scrutiny which motivated the modern understanding of block ciphers and their cryptanalysis.
DES is now considered to be insecure for many applications. This is chiefly due to the 56-bit key size being too small;

Implementation in Java




import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.io.UnsupportedEncodingException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;
public class DesEncryption {
Cipher ecipher;
Cipher dcipher;
// 8-byte Salt
byte[] salt = {
(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
(byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03
};
// Iteration count
int iterationCount = 19;
public DesEncryption(String passPhrase) {
try {
// Create the key
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
ecipher = Cipher.getInstance(key.getAlgorithm());
dcipher = Cipher.getInstance(key.getAlgorithm());
// Prepare the parameter to the ciphers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
// Create the ciphers
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
} catch (java.security.InvalidAlgorithmParameterException e) {
} catch (java.security.spec.InvalidKeySpecException e) {
} catch (javax.crypto.NoSuchPaddingException e) {
} catch (java.security.NoSuchAlgorithmException e) {
} catch (java.security.InvalidKeyException e) {
}
}
public String encrypt(String str) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
} catch (javax.crypto.BadPaddingException e) {
} catch (IllegalBlockSizeException e) {
} catch (UnsupportedEncodingException e) {
} catch (java.io.IOException e) {
}
return null;
}
public String decrypt(String str) {
if (str == null)
return null;
try {
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
}
catch (javax.crypto.BadPaddingException e)
{
e.printStackTrace();
}
catch (IllegalBlockSizeException e)
{
e.printStackTrace();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
try {
// Create encrypter/decrypter class
DesEncryption encrypter = new DesEncryption("My Private Key");
// Encrypt
String encrypted = encrypter.encrypt("bi20009");
System.out.println(encrypted);
String decrypted = encrypter.decrypt(encrypted);
System.out.println(decrypted);
} catch (Exception e) {
}
}
}

Hope this helps

Submit Form through Windows Modal Dialog

Yes, you can submit form on Windows modal dialog.

Summary

Set the name of the modal dialog to let say ‘myModal’

The form inside the window modal dialog should have the target set to _self or name ‘MyModal’ and submit the form.

Details with Code

We will have three jsps,

1) The window modal opener (modalTest.jsp)

2) Window Modal Form Page (modelD.jsp)

3) Modal Form Submit page (modelSubmit.jsp)

modalTest.jsp

<html>
<body>
<head>
<script language="javascript">
function openModal(url, width, height)
{
var features = "";
if (width != "")
{
features = features + "dialogWidth:" + width + "px;";
}
if (height != "")
{
//features = features + "dialogHeight:" + height + "px;help:no;edge:sunken;status:no;unadorned:yes";
features = features + "dialogHeight:" + height + "px;";
}

return window.showModalDialog(url, "myModal", features);

}

function showWindow()
{
var modalWin;
modalWin = openModal('modelD.jsp', 545, 500);
}

</script>
</head>
<a href="#" onclick="showWindow();" > Click </a>
</body>
</html>

modelD.jsp

<html>
<head>
<title>My Modal Window</title>
<script language="javascript">

window.name = 'myModal';
</script>
</head>
<body>
<form name="frm" action="modalSubmit.jsp" target='myModal'>

<input type ="text" name="test1">
<input type="button" value="Submit Modal Window" onclick="document.frm.submit();" >

</form>
</body>
</html>

modelSubmit.jsp

<html>
<head>
<title>Modal Dialogue Submitted</title>
</head>
<body>
<%
String param = request.getParameter("test1");
if(param == null)
param="empty text box sent";
%>
<h1>
<%=param.toUpperCase() %>
</h1>
<br>
Hence you can submit a modal dialoague........... I want a pizza...
<br>
<input type="button" value="Close me" onclick="window.close();" />
</body>
</html>

Save all these jsp to you JSP Containers (Tomcat, JBOSS or any)

call modalTest.jsp

The first screen you gonna see is aa simple click screen that you will click to open the windows modal dialog

m1

On windows modal dialog you will see a form with a text field,

m2

write your name in the text field in lower caps and submit the form… and yes the form submits and return the result with your name in Capital letters

m3

That it, you can submit Windows Modal dialog.

Thursday, March 19, 2009

Saving images in MySQL with PHP

Here is the small code to save the image in MySQL with the help of PHP.
Images are saved in MySQL as BINARY data. BINARY data can not be saved in varchar or char data types, for this purpose we need a data type which can handle binary data. BLOB columns are treated as binary strings (byte strings). The following table is fulfilling our requirements for a simple test with a BLOB field.


CREATE TABLE `images` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`image` BLOB NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

Now we have a MySQL table ready to store the image. Next steps are easy i.e.



  1. Read the image

  2. Encode the image data

  3. Save binary data in DB


These 3 steps are performed with the following PHP code


$image = chunk_split(base64_encode(file_get_contents("image.jpg")));
$query = "INSERT INTO images (image) VALUES('$image')";
mysql_query($query) or die(mysql_error());
echo "Image id is ".mysql_insert_id();

Now we have saved the image in database successfully. The next step is to display the image.


// showimage.php
header('Content-type: image/jpeg');
$query = "SELECT image from images where id=1";
$rs = mysql_fetch_array(mysql_query($query));
echo base64_decode($rs["image"]);


Now to display this image



img src="showimage.php" />





I have copied this valuable code from my friend's blog

( where he had answered many user queries in reqponse for this posting.

http://www.einfobuzz.com/2010/04/saving-images-in-mysql-with-php.html

 
 
 

Monday, March 16, 2009

Some past moments

Following are the list of points that I asked my each developer of our BI product Sapience to follow while developing this ultimate product…I posted here as I was deleting my old stuff and I suddenly went through this document and I smiled remembering our olden golden days when we were developing our Generic BI suite in Java. … ahhh  posted just for my ex-team may be they too will smile….

Rules of Thumb

1) Every one must perform some kind of unit testing on their side as well; relying solely on QC department is not going to help us in this regard.

2) Every one who will perform code audit of his respective module that he is responsible, which can save us from Null pointer exception and other errors. (Please do handle for nulls after every method call that returns an object especially for Strings).

3) Every one should fill his daily report as well as regression report daily.

4) The number of bugs produced in ones module will definitely help me to evaluate his skills. High buggy modules will not be appreciated rather considered to be a black rock in the path of stabilization.

5)Reopened bugs are not acceptable at any moment.

6) For multiple pages forms (e.g. wizards), rely on sessions rather on request and hidden form variables.

7) Do evaluate your self at the end of the day, what did you learn, what you have achieved.

8) Do think about your parents and your family at least once a while, and think what deed of yours can make them smile.

Remember the more bugs your module produce, the lower your performance will be counted.

Important things to remember while Programming:

 

  • Good programs are
    • Flexible
    • Robust
    • Extensible
    • Easy to understand
    • Easy to maintain

 

  • An object oriented program is a collection of interacting objects.  

  • Good OO programs are characterized by
    • Encapsulation
      • information hiding
      • modular design
      • Reliance on public method contracts not internal implementation
    • Loose coupling between objects.
    • High levels of abstraction -- polymorphic behavior

  • A program starts by creating one object whose job it is to create all objects that are initially needed. 
  • Objects can create other objects. 
  • Objects can be disposed and cease to exist.
  • Objects should only know the minimum of what they need to know and should do only the minimum of what they need to do.  This is called "loose coupling".  Don't make "god objects" i.e they can do every thing.
  • Objects should expose the minimum information and methods needed by the rest of the program.   This is another aspect of loose coupling.
  • When using an object, do not concern yourself with how its methods are implemented, only on what that class defines as the function of that method.   Thus a method involves more than just code, but also a "contract" with the rest of the program.

Object Memory Model (Java Part 1)

Object Memory Modal:

When you create a class, you are creating a new data type. By creating a class, you are creating a new data type that defines both the nature of the data being manipulated and the routines used to manipulate it. You can use this type to declare objects of that type. However, obtaining objects of a class is a two-step process. First you must declare a variable of the class type. This variable does not define an object. Instead, it is simply a variable that can refer to an object. Second, you must acquire an actual physical copy of the object and assign it to the variable, by using the new operator. The new operator dynamically allocates memory for an object and returns a reference to it. This reference is the address in memory of the object allocated by new. This reference is then stored in the variable.

E.g.

MyClass obj;

Obj = new MyClass

Or it can be written as

E.g. MyClass obj = new MyClass();

In the first step obj is declared as variable of type MyClass. Now this obj variable contains a Null,

Which indicates that it does not yet point to an actual object. In the next step an actual object is created in the memory and its reference is passed to the obj variable. Actually the obj variable only contains the reference of the object which resides in the memory.

The statement “ new MyClass() “ actually runs the constructor of class MyClass. The new operator allocates memory for the object at runtime and the MyClass() executes the constructor.

What are constructors?? Hossla, Hossla, we are coming to it.

// Displays the function of refernce variable and the object.

image

After the constructor is executed a real instance of the object is created and its reference is passed to the variable. Now you can use the object methods and can perform different operations on the object, which the class offers.

MyClass Obj1 = new MyClass();

MyClass Obj2;

Obj2=Obj1;image

Two different objects can refer to the same object as shown above.

Constructors:

They are simple functions, which are executed whenever a new object is created. A constructor initializes an object immediately upon its creation. It has the same name as the class. Constructor is automatically called before the new operator completes. They don’t have any return type nor void. It is the constructor’s job to initialize the internal state of an object. The can be overloaded according to the arguments passed to them.

An interesting Scenario

Qs:

If, when defining a class, we don't define its constructor, the compiler implicitly assigns one. However, if we do define a constructor, the compiler interprets this as function overriding and does not flash an error. But if this is the case, why do I receive a compiler error in example 3 below?

Example 1:

class x
{
   int x,y;
   public void x( ) // The compiler considers this method overriding.
   {
   }
}
public static void main(String arg[])
{
   x x1=new x() // No error.
}

Example 2:

class x
{
   int x,y;
   public x( ) // The compiler considers this method overloading.
   {
   }
  public x( int a, int b ) // The compiler considers this method overloading.
   {
       x = a;
       y= b;
   }
}
public static void main(String arg[])
{
   x x1=new x() // No error
  x x1=new x( 5, 6 ) // No error
}

Example 3:

class x
{
   int x,y;
   public x( int a, int b )
   {
       x = a;
       y= b;
   }
}
public static void main(String arg[])
{
   x x1=new x() // Why is there an error here?
  x x1=new x( 5, 6 ) // No error
}

Ans:

The behavior that you point out is correct and expected. To understand the behavior, you need to understand a few rules about constructors.

First, if you do not define any constructors, Java will insert a default no-arguments constructor of the form:

public \()
{
   super();
}

Second, if you declare constructors, what you see is what you get. This means that if you don't explicitly declare a no-arguments constructor but declare some other constructor, you will only get the constructor that you do explicitly define. This is why you see an error in example 3. Since you've never declared a no-arguments constructor but did define another type of constructor, Java does not insert a no-arguments constructor.

For example 3 to work you must also explicitly declare the no-arguments constructor:

public x()
{
   super();
}

Finally, subclasses do not inherit constructors. You must explicitly define each constructor that you need. Further, you can only call superconstructors that actually exist in the base class, in accordance with the rules that I outlined above.

this:

When an object’s method needs to refer the object that invoked it the this operator is used to refer that object. this can be used inside any method to refer the current object.

Garbage Collection:

Objects are stored in memory, in C++, dynamically allocated objects must be manually released by use of delete operator. Java de allocates the unused objects from the memory with the help of Garbage collector. The technique in which when no references to an object exists, that object is no longer to be needed and the space is freed is called Garbage Collection. There is no explicit method to destroy un referenced objects, while if necessary you can call the garbage collector else the garbage collector is automatically executed in its low priority thread.

Finalize:

Like in constructors, you initialize object variables or allocate resource to the object, you can perform some actions, or free up resources when the object is being destroyed. Java provides a mechanism called finalization. By using the finalize method you can define specific actions that will occur when an object is just about to reclaimed by the garbage collector. To add a finalizer to a class, you simply define a finalize() method.

The garbage collector runs periodically, checking for objects that are no longer referenced by any running state. Right before the resource is freed, the Java run time calls the finalize method.

The signature for the method is

protected void finalize()

The protected keyword restricts the method to be called by code outside tha class. It is important to note that finalize is just called before the garbage collector destroys it. It is not called when an object goes out of scope.

In C++ destructors also do the same thing except they are executed when an object goes out of scope.

Object Oriented Principles (Part 2)

Polymorphism:

Polymorphism is a Greek word that means “many forms”. Polymorphism is a phenomena in which same name is given to different implementations or same command name given to different commands.

“Same interface for multiple methods”

Let’s take an example of a dog, a dogs sense of smell is polymorphic. If the dog smells a cat, it will bark and run after it. If the dog smells its food, it will salivate and run to its bowl. The same sense of smell is at work on both situations. The difference is what is being smelled, that is, that type of data being operated upon the dogs nose.

We can assume that, polymorphism is a phenomena in which, a standard interface is given to more than one method or any implementation, and they will differ on the basis of the data being operated by them.

Many languages including Java, C++, PL/SQL, and Visual Basic implement polymorphism.

These languages implement polymorphism, from there different features, example method overloading is the technique by which many languages implement polymorphism. We will discuss method overloading but for now, Hossla, Hossla.

There is a confusion for method overloading and operator overloading that they are a part of Object Oriented technology, although that is not true because they are the features provided by any language to implement polymorphism.

Like Java implements polymorphism in three ways:

1) Polymorphism by Inheritance:

If any method accepts an object of a super class, the object of its sub class can also be accepted. The reason is that a subclass inherit all the properties and methods from the super class, the method which accepts the object of a super class may be using one of the properties or a method from that object, so those properties are also contained by the object of the sub class.

2) Overloading:

Implementing identically named methods in a same class that differ according to the arguments passed to them. It means that the same name will be given to different functions, and they will be called according to the type and number of arguments.

3) Interfacing:

Implementing identically named methods that take identical arguments in different classes.

C++ also implements polymorphism by operator overloading and the three defined above.

Inheritance:

Inheritance is one of the cornerstones of object oriented programming, because it allows creating class hierarchy. In terminology of Java the class which is inherited is called the

super class” and the class that does the inheriting is called “sub class”.

When a sub class is inherits a super class, it inherits the state and the behavior from all of its ancestors

opp1

In the above following example, the object of A has var1 and M1. The class A is inherited by class B, that why the object of class B contains the properties and methods of class A. The class is inherited by class C, so the object of class C contains the methods and properties of class B.

Object Oriented Principles (Part 1)

Object:

In the reality every thing is an object. Your pen, PC, cup, car etc are all objects.

Any thing that has physical existence is called an object. Even though you and me are also objects.

Every object has two distinct aspects:

1) Behavior

2) Characteristics

For an easy example, car is an object, it has methods to change gear, to accelerate, to break etc, these methods are simply called behavior of the car and it has properties like color, size, weight, model etc are all characteristics of the car.

Thus you saw that every object has properties which can be accessed by its methods.

In programming concepts every object has variables (properties) and functions (methods). Both of them are concealed in an object.

The basic idea behind the OOP languages was to combine both data and its member functions into a single unit called Object.

Object is the logical association between program variables and program methods.

Object contains variables that are only accessed by its member functions.

In other words an object is an instance (occurrence) of a class.

Class:

Continuing with the car example, the company which made the car has first designed the car model and its other aspects, they decided the number of gears, they decided how to change the gear, how to accelerate and other methods and properties of the car on just a piece of paper, there is no physical existence of the car yet,

They only designed the car architecture on the paper, which include car properties and car methods, and this architecture of the car is simply called its Class.

Class is a blue print of an object.

Class defines the architecture of an object, it defines the variables and the methods contained in an object.

Class does not have any physical existence, it just defines the object.

Encapsulation:

The word Encapsulation seems to have any biological meaning or it is derived from the medial dictionaries, a new hunter to Oops will think like that, but it is not true.

Data encapsulation and data hiding are the key terms in Object Oriented Programming.

The term Encapsulation means, “The data contained in a variable of an object is only accessed by its member functions”.

If a user wants to access certain data in the object, it will call its member function and that function will read the data and will return the value.

Thus we can assume that the data and its member function is encapsulated in a single unit called object and the data is only accessed by the functions defined in its class.

The Following Diagrams can give you a better approach:

 

opp1

Encapsulation

opp2

Design Patterns

Definition

In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. Algorithms are not thought of as design patterns, since they solve computational problems rather than design problems.

  • A design pattern is to design is what a class library is to coding
  • Documentation of expert software engineers' "behavior"
  • Documentation of specific reoccurring problems (and solutions)
  • Abstraction of common design occurrences
  • Large range of granularity -- from very general design principles to language-specific idioms
  • A fully realized form, original, or model accepted or proposed for imitation[dictionary]
  • Describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice [Alexander]
  • the abstraction from a concrete form which keeps recurring in specific non-arbitrary contexts [Riehle]
  • both a thing and the instructions for making the thing [Coplien]
  • A literary format for capturing the wisdom and experience of expert designers, and communicating it to novices