Solution Architecture

I have made a formal commitment towards developing myself as a Solution Architect. This blog post will have the links to everything which i have found useful.

http://scn.sap.com/community/career-center/blog/2014/05/25/from-developer-to-solution-architect-essential-skills-for-climbing-the-it-professional-ladder

http://scn.sap.com/community/technology-innovation/blog/2013/01/03/so-you-want-to-be-an-architect-let-s-talk-about-street-credibility

Advertisement

Ramblings 2 @ Java

This post is a continuation of ramblings of the past.

Javascript frameworks:

Step back jQuery, it is now Backbone.js , Ember.js and Angular.js. It has to be seen who is winning, though. I have not used any of these in my project but each one of it, looks an interesting prospect. Check out this link to understand the differences. Underscore.js is a powerful complement to Backbone.js/jQuery.

Updated: React is a new javascript framework which looks simpler in implementation and is going to give serious competition Angular.

Security frameworks:

Spring security, Apache Shiro and JBoss PicketBox are in contention here. Spring security has the first mover advantage. But, Apache Shiro is easier to learn and implement. JBoss PicketBox is good for JBoss fans.

Single Sign-on:

It is always good to understand what each framework offers before embarking on a solution implementation. OAuth2 seems to be a preferable option if you are starting now and don’t have a SAML infrastructure already.  For enterprise, SAML still seems to be holding the ground. Learn more here and here too.

Shibboleth provides an open-source SAML implementation of SSO capability.

Source-control, Continuous Integration and Automated Deployment:

There seems to be a new stack for this GitHub + Jenkins + Puppet. A quick reality check of the competitors in each of the spheres is listed below.

GitHub/GitLab/Gitorious: For source control management, GitHub is leading the pack.For a quick differentiation, Check here.

Hudson/Jenkins : Continuous integration server which integrates with a git repository. Jenkins has taken over Hudson, Check here.

Chef/Puppet : Automated application deployment on servers. Puppet seems to be ahead of the race marginally, Check here.

Read this for a typical implementation.

Ramblings @ Java

It was a saturday evening and this led to skimming through my spam mails. It was here, i stumbled upon the release of Spring 4. This led to discovery a whole gamut of terms, concepts and many more. This post is just a collection of the same.

STOMP – Streaming Text Oriented Messaging Protocol. We can build interactive web applications using this protocol.

Micro Service Architecture – Extending Single Responsibility Principle to service level [or] Java Architecture, the UNIX way. To know more, go through PresentationBlog-1 and Blog-2.

CQRS – Command and Query Responsibility Segregation.

YAML – YAML ain’t a mark-up language. A standard for data serialization for all programming languages.

Stove pipe architecture – An anti-pattern; also means legacy software with undesirable qualities. Also, read Stove pipe architecture vs Enterprise Architecture 

CAP Theorem – A distributed system cannot guarantee all three aspects Consistency, Availability and Partition tolerance together.

A treasure-chest for design-patterns, anti-patterns and many more Sourcemaking

Bootstrap – It is a sleek, intuitive, fluid and powerful mobile first front-end framework for creating faster and easier web-development provided by Twitter. It is better than JQuery UI in many aspects and for a few differences check here.

Single page architecture – is a web application or web site that fits on a single web page with the goal of providing a more fluid user experience akin to a desktop application.

Dropwizard – A java framework to build ops-friendly, high-performance RESTful web-services. This helps in building Micro-service architecture.

JDBI  – A SQL convenience library for java to access relational database.

FreeMarker Vs Velocity – Freemarker wins hands-down due to its powerful api

 

 

 

 

Mediocre Life and Luck factor

It was supposed to be a lazy sunday after a long week in the office. To my surprise, i chanced upon an article on lifehacker which talked about seven effective habits of mediocre life. I could relate to it as a fish takes to water. In this article, James Alutcher concludes, “Being mediocre doesn’t mean you won’t change the world. It means being honest with yourself and the people around you. And being honest at every level is really the most effective habit of all if you want to have massive success.”

All of us, once in a while say “touch-wood”, have you wondered where does it originate?. The reasoning goes something like this: “Touching (“Knocking on”) wood dates back to pagan rituals that were designed to elicit the help of benign and powerful tree gods.” . Richard Wiseman has a wonderful article about luck and the luck factor here. He concludes saying “much of the good and bad fortune we encounter is a result of our thoughts and behavior”.

Handy Linux Commands

Usage Command
Check for the Version of Linux uname -mrs  or
Open file using cat /proc/version or
uname -a [All information]
Check for the Machine architecture uname -mrs
i386 and i686 = 32-bit; x86_64 = 64-bit
Check for the Server release details lsb_release -a
Check the processes running ps fuxxx , ps auxxx
Checking whether a port is open or not telnet <ip_address> <port_no>
Updating access rights to execute a program chmod +x ./<program_name>
Searching for specific text occurrences grep <search_term> *
Checking top 100 lines of a file tail -100f <file_name>
Running a program in back-ground nohup ./<program_name> &
Checking ip address /sbin/ifconfig or ip addr show
Checking which process is cosuming how much top
Checking how much space is available in current drive df -h
Killing a process kill -9
Copying file/directory from a linux server [File]scp <USER_NAME>@<HOST_NAME>:<USR_DIRECTORY>/sample.txt .
[Directory]scp -r <USER_NAME>@<HOST_NAME>:<USR_DIRECTORY>/ .
Clear the screen clear screen
Java Set-up in linux Click here

Jar with dependencies using maven

In the earlier post, though we had dependencies for our jar, we just used them while compiling them. Hence, when we execute our jar, it is required that, they be included in the class-path while executing our jar.

Though, this sounds reasonable enough, there are situations when it becomes tedious to include the dependencies in the class-path every-time you want to run the jar,especially, when there are more than a few dependencies and multiple main-classes. A simpler way is to bundle the other dependent jars in our jar.

Hmmm. This may sound a little off the mark. It is true you cannot bundle the dependent jars in your jar. Albeit, you can include the classes of your dependent jars in your jar. This will solve the problem. Whoa!!! how do i do it?

This can be done pretty easily using Maven in tandem with an assembly.xml. Following are the key points:

a. Use Maven Assembly plugin to assemble the class files in a jar and include their dependencies as well
b. Maven Assembly plugin requires an additional assembly.xml which in turn specifies format e.g. jar and depndency-set
c. Configure the assembly.xml as part of same project and use it from pom.xml using maven assembly plugin
d. Generate one single jar with all dependencies

The sample pom.xml looks like below:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.schoudari.blog</groupId>
  <artifactId>my-blog-reader</artifactId>
  <packaging>jar</packaging>
  <version>1.0.0-SNAPSHOT</version>
  <name>My Blog Reader</name>
  <properties>
    <log4j.version>1.2.16</log4j.version>
  </properties>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId><!--1.Use Maven assembly plugin to assemble the jar-->
        <configuration>
          <attach>false</attach>
          <descriptors>
            <descriptor>src/assemble/assembly.xml</descriptor><!--2.Assembly configuration-->
          </descriptors>
          <archive>
            <manifest>
              <mainClass>com.schoudar.blog.MyBlogReader</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>    
  </dependencies>

</project>

The sample assembly.xml is specified below:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
  <id>jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>

Useful Links:
http://www.ibm.com/developerworks/java/library/j-5things13/index.html

Creating a Jar using Maven

In the earlier post, we had seen how to create a jar, in this post we will see how to create a jar using maven. Following are key points in creating the jar using maven.

1. Specify the packaging type e.g. jar.

2. Compile the source code. Use  maven-compiler-plugin here.

3. Generate eclipse related files like .classpath, etc. Use maven-eclipse-plugin. [optional]

4. Bundle the java class files in a jar. Use maven-jar-plugin.

5. Provide the main-class[entry point] for your jar in the mainfest.

6. Execute the unit-tests of your jar before creating the jar. [optional]

Refer to the sample pom.xml below:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.schoudari.blog</groupId>
  <artifactId>my-blog-reader</artifactId>
  <packaging>jar</packaging> <!--1.Packaging Type -->
  <version>1.0.0-SNAPSHOT</version>
  <name>Blog Reader</name>

  <properties>   
    <log4j.version>1.2.16</log4j.version>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId> <!--2.Compiles the source-code -->
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId> <!--3.Generates the eclipse-related files -->
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId><!--4.Creates a jar -->
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.schoudari.blog.BlogReader</mainClass><!--5.Add main class -->
            </manifest>
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId><!--6.Executes the unit-test cases -->
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>       
  </dependencies>  
</project>

Basics of Jar

A few years back, i was asked why a class in jar was not running as expected from command prompt. The answer was quite simple, the dependencies of the class are not available.

There is a common misconception that you create a jar and everything runs out of the box for you when you run the main class. Now what is the main class in the jar. Let me give you some introduction to jars:

a. Jar – Java archive. As the name says, it archives a set of java files.

b. Common Jar Operations

Operation Command
To create a JAR file jar cf jar-file input-file(s)
To view the contents of a JAR file jar tf jar-file
To extract the contents of a JAR file jar xf jar-file
To extract specific files from a JAR file jar xf jar-file archived-file(s)
To run an application packaged as a JAR file (requires the Main-class manifest header) java -jar app.jar

c. In general, a java file uses some or the other open source libraries e.g. log4j.jar, etc. Hence, when we create a jar, it is required that it is available in the class-path when we are creating the jar, so the class files can be created and then, they can be bundled. The same principles applies when you execute the jar file as well.

d. Every jar should have at-least one Main-class. Main-class is a class, which is a public class, with the public static void main(String[] args) method. This is the entry-point for all the classes in your jar. P.S. There can be more than one main class as well. In such a case, it is preferable not to have a main-class specified in the manifest.

e. A jar generally comes with a MAINFEST file. This is like meta-information about a jar. e.g.

Manifest-Version: 1.0
Main-Class: MyClass
Class-Path: MyUtils.jar
Created-By: 1.6.0 (Sun Microsystems Inc.)

Useful links:
http://docs.oracle.com/javase/tutorial/deployment/jar/index.html

Difficult Java interview questions – Serialization

This article is a follow-up to my article here

a. Are static variables serializable?

Ans: Static and transient variables of a class are not serializable. 

b. Can a class implement both Serializable and Externalizable?

Ans: Yes, the class can implement both Serializable and Externalizable.

c. What happens to the non-serializable objects in a class while serializing which implements Serializable?

Ans: The class fails to serialize with exception  java.io.NotSerializableException.

d. What happens to the non-serializable objects in a class while serializing which implements Serializable, Externalizable?

Ans: The class can be serialized and de-serialized successfully.

e. How do we deal with serialVersionUID when a class implements Serializable?

Ans:  If we don’t provide a serialVersionUID,  java compiler generates a serialVersionUID based on the properties defined in the class. Hence, if we have modified the class with a new property after a class has been serialized, then it will fail while de-serializing as a new serialVersionUID is generated by java compiler again. So, to get around this issue, we can fix the serialVersionUID of a class to be 1L. 

f. If we have a Serializable class “MyClass” which extends “MySuperClass”. What happens to the properties of “MySuperClass”, when we serialize/de-serialize “MyClass”?

Ans: If the properties inside the “MySuperClass” are serializable like primitives,serializable classes then all these will be available in the sub-class during/after serialization. However, if there are non-serializable properties, then there should be a default constructor in “MySuperClass” provided.

g. How do we serialize the non-serializable properties of a super-class[non-serializable] in a serializable sub-class?

Ans: We will have to over-ride the default private writeObject() and readObject() methods of sub-class and provide values for the non-serializable properties. Otherwise, ensure that the non-serializable properties are set with some default/specific values while creating the sub-class it-self. Refer below for sample code:

import java.io.*;

public class TestSerialization {
    public static void main(String args[]) {
        // Object serialization
        try {
            MyClass object1 = new MyClass("Hello");
            System.out.println("object1: " + object1);
            FileOutputStream fos = new FileOutputStream("serial");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(object1);
            oos.flush();
            oos.close();
        } catch (Exception e) {
            System.out.println("Exception during serialization: " + e);
            System.exit(0);
        }
        // Object deserialization
        try {
            MyClass object2;
            FileInputStream fis = new FileInputStream("serial");
            ObjectInputStream ois = new ObjectInputStream(fis);
            object2 = (MyClass) ois.readObject();
            ois.close();
            System.out.println("object2: " + object2);
        } catch (Exception e) {
            System.out.println("Exception during deserialization: " + e);
            System.exit(0);
        }
    }
}

class FailSerilization{
    String y="fail serilization";
}

class MySuperClass{
    String x="not super";
    FailSerilization fs;

//    MySuperClass(FailSerilization fs){
//        this.fs=fs;
//    }
    MySuperClass(){
        fs=new FailSerilization();
    }

}

class MyClass extends MySuperClass implements Serializable {
    String s;

    public MyClass(String s) {
        //super(new FailSerilization());
        this.s = s;

    }

    public String toString() {
        return "s=" + s + ";x= "+x+"; fs= "+fs.y;
    }
}

More here:

class MyClass extends MyClass2 implements Serializable{

  public MyClass(int quantity) {
    setNonSerializableProperty(new NonSerializableClass(quantity));
  }

  private void writeObject(java.io.ObjectOutputStream out)
  throws IOException{
    // note, here we don't need out.defaultWriteObject(); because
    // MyClass has no other state to serialize
    out.writeInt(super.getNonSerializableProperty().getQuantity());
  }

  private void readObject(java.io.ObjectInputStream in)
  throws IOException {
    // note, here we don't need in.defaultReadObject();
    // because MyClass has no other state to deserialize
    super.setNonSerializableProperty(new NonSerializableClass(in.readInt()));
  }
}

/* this class must have no-arg constructor accessible to MyClass */
class MyClass2 {

  /* this property must be gettable/settable by MyClass.  It cannot be final, therefore. */
  private NonSerializableClass nonSerializableProperty;

  public void setNonSerializableProperty(NonSerializableClass nonSerializableProperty) {
    this.nonSerializableProperty = nonSerializableProperty;
  }

  public NonSerializableClass getNonSerializableProperty() {
    return nonSerializableProperty;
  }
}

class NonSerializableClass{

  private final int quantity;

  public NonSerializableClass(int quantity){
    this.quantity = quantity;
  }

  public int getQuantity() {
    return quantity;
  }
}

For further reading , refer to JCG website.