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

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>