Tuesday, June 23, 2015

JSch - Upload/download files from remote server

We will using JSch to connect our unix box. Once connected you should be able to upload or download the required files.

JSch is a pure Java implementation of SSH2. To know more click here.

For running below code you need to add jsh jar file (e.g. jsch-0.1.53.jar) in your applications classpath. Download jsch-0.1.53.jar from here.

Please see the self explanatory code below -


Tuesday, June 9, 2015

Serialization problem with Singleton

In previous example we have seen the basics of singleton. In this example we will see how Serialization can effect the Singleton design pattern.

Sometimes in distributed systems, we may need to implement Serializable interface in Singleton class so that we can store it's object state in a file system and retrieve it at later point of time. Let's take an exammple of Clipboard class which implements Serializable interface.

Clipboard.java


TestClipboard.java


Output :
c1 hashCode = 28117098
c2 hashCode = 13495805

From the above hashCode output we can see the intent of singleton pattern is simply blown up, to overcome this problem we need to provide the implementation of readResolve() method as shown below.


Now if u see the hashCode output, it will be same.

The readResolve method is invoked by serialization if the method exists and it would be accessible from a method defined within the class of the object being serialized. Thus, the method can have private, protected and package-private access. Subclass access to this method follows java accessibility rules.

Classes that need to designate a replacement when an instance of it is read from the stream should implement this special method with the exact signature - ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;

Thursday, October 30, 2014

Top Ten Reasons for learning Java Programming Language

Java is the best programming languages created ever as it has last 20 years by gaining popularity every passing day. Although there were occasions when Java development slowed down, but with path breaking changes in form of Enum, Autoboxing and Generics in Java 5, Google's choice of language for Android apps development and performance improvement with Java 6, kept Java as top programming language. Also In terms of Job opportunities and popularity Java outscore every one with lots of Jobs opportunity available. You can work on developing core Java based server side application, can even go for Android based mobile application development and J2EE web and enterprise applications.

Here are top 10 reason for Learning Java Programming Language

1) Java is Easy to learn

Java has fluent English like syntax which makes it easy to read Java program and learn quickly. Once you are familiar with initial hurdles with installing JDK and setting up PATH and understand How Classpath works, it's easy to write program in Java.

2) Java is an Object Oriented Programming Language

Java is an Object Oriented Programming language. Developing OOPS application is easier, and it also helps to keep system modular, flexible and extensible. You can use all key concepts like AbstractionEncapsulationPolymorphism and Inheritance. Java also promotes use of SOLID and Object oriented design principles in form of open source projects like spring, which make sure your object dependency is well managed by using dependency Injection principle.

3) Java has Rich API

Java provides API for networking, I/O, utilities, xml parsing, database connection, and almost everything. Whatever left is covered by open source libraries like Apache Commons, Google Guava and others.

4) Powerful development tools e.g. Eclipse, Netbeans

Eclipse and Netbeans has played huge role to make Java one of the best programming language. Coding in IDE is a treat, especially if you have coded in DOS Editor or Notepad. They not only help in code completion but also provide powerful debugging capability, which is essential for development and testing. Integrated Development Environment made Java development much easier, faster and fluent. Apart from IDE, Java platform also has several other tools like Maven and ANT for building Java applications, JConsole, decompilers, Visual VM for monitoring Heap usage etc.

5) Good collection of Open Source libraries

Open source libraries ensures that Java should be used everywhere. Apache, Google, and other organization has contributed lot of great libraries, which makes Java development easy, faster and cost effective. There are framework like Spring, Struts, Maven, which ensures that Java development follows best practices of software craftsmanship, promotes use of design patterns and assisted Java developers to get there job done.

Tuesday, October 7, 2014

Java : File Encryption and Decryption using Blowfish

The Java security APIs span a wide range of areas, including cryptography, public key infrastructure, secure communication, authentication, and access control. Java security technology provides the developer with a comprehensive security framework for writing applications, and also provides the user or administrator with a set of tools to securely manage applications.
Source : http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136007.html

javax.crypto.Cipher
This class provides the functionality of a cryptographic cipher for encryption and decryption. It forms the core of the Java Cryptographic Extension (JCE) framework.
Cipher API : http://docs.oracle.com/javase/6/docs/api/javax/crypto/Cipher.html

Cipher: initialized with keys, these used for encrypting/decrypting data. There are various types of algorithms: symmetric bulk encryption (e.g. AES, DES, DESede, Blowfish, IDEA)
http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html

Blowfish  
Blowfish has a 64-bit block size and a variable key length from 1 bit up to 448 bits
Blowfish Wiki : http://en.wikipedia.org/wiki/Blowfish_%28cipher%29

Here are the general steps to encrypt/decrypt a file in Java -

  • Create a Key from a given byte array for a given algorithm.
  • Get an instance of Cipher class for a given algorithm transformation.
  • Initialize the Cipher with an appropriate mode (encrypt or decrypt) and the given Key.
  • Invoke doFinal(input_bytes) method of the Cipher class to perform encryption or decryption on the input_bytes, which returns an encrypted or decrypted byte array.
  • Read an input file to a byte array and write the encrypted/decrypted byte array to an output file accordingly.


Please see the below java code