Monday, November 4, 2013

Chain of Responsibility, aka CoR




What it means


CoR UML Digram
Processing or handling the request in a chained fashion, where all the objects are either equally capable or capable of executing the request based on certain conditions. If one object can not process, the request is passed on to the next available handler in the chain, and so on. And, the request is finally processed by one of them.



Applications of CoR

  • Rule engine to apply n number of rules
  • A parsing engine, similar to XML Parsing
  • Assigning the responsibility to an available executor, for example, in a chat based support application

And..many more if you look around. My imagination is compilers and interpreters too are based on this design pattern.

References


How it is similar or different from other patterns?

Often, the question arises, why CoR is different from Strategy, decorator and command etc. Below are the links where we can find some useful discussion:

A pop window (row editor) example for JTable

Have been working on web application for quite sometime. But, I must say, a taste of how desktop applications are built using Swing and JavaFx is not bad either. 

Recently, one of my friend came up with a question, how to get a pop up editor for a row in JTable the way we get for a grid in web application. Honestly, I never worked on Swing based app in my career, my first hand experience on swing programming lasted only till college. So, I decided to take this opportunity to learn a bit of swing. 

After doing a bit of study on oracle's swing docs & tutorial and going thru a few similar questions on stack overflow, I found a close answer to the problem.

I thinks, couple of tricks to achieve this was:
  1. First, to instantiate JTable, use the constructor that accepts TableModel (that extends AbstractTableModel), so that, the default cell editing can be disabled, otherwise the double click event will enable the cell editing instead of popping up an editor JFrame.
  2. Second, use a JInternalFrame as a popup editor box.

And, here is the sample code:


package com.myapp.test;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

public class JTableFrame extends JFrame {

 private static final long serialVersionUID = 1L;
 public static final boolean DEBUG = false;

 JTableFrame() {

  MyTableModel myMyTableModel = new MyTableModel();

  JTable table = new JTable(myMyTableModel);
  
  table.addMouseListener(new MouseAdapter() {
   public void mouseClicked(MouseEvent e) {
    if (e.getClickCount() == 2) {
     JTable target = (JTable) e.getSource();
     int row = target.getSelectedRow();
     int column = target.getSelectedColumn();
     System.out.println("row=" + row + " - " + "col=" + column);
     System.out.println("value here is:"
       + target.getValueAt(row, column));
     
     // use JInternalFrame as pop up editor
     JInternalFrame editorPopup = new JInternalFrame(
       "Edit Record", true, true, true, true);

     GridLayout gridLayout = new GridLayout(0, 2);
     gridLayout.setHgap(5);
     gridLayout.setVgap(5);

     editorPopup.setLayout(gridLayout);
     
     // creating the popup form elements and setting the values
     for (int i = 0; i < target.getColumnCount(); i++) {
      System.out.print(target.getValueAt(row, i) + ":");

      editorPopup.add(new JLabel(getColumnTitle(i)));
      JTextField txtFname = new JTextField(20);
      txtFname.setText(target.getValueAt(row, i).toString());
      editorPopup.add(txtFname);

     }

     JButton okButton = new JButton("Ok");
     okButton.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {

       JOptionPane.showMessageDialog(((JButton) e
         .getSource()).getParent(), "Record Saved!");
      }
     });

     editorPopup.add(okButton);
     editorPopup.add(new JButton("Cancel"));

     editorPopup.pack();

     target.add(editorPopup);
     editorPopup.setVisible(true);
    }

   }
  });

  table.setPreferredScrollableViewportSize(new Dimension(1000, 700));
  table.setFillsViewportHeight(true);

  // Create the scroll pane and add the table to it.
  JScrollPane scrollPane = new JScrollPane(table);

  add(scrollPane);
  this.setSize(1000, 600);
  // pack();
  setVisible(true);
 }

 class MyTableModel extends AbstractTableModel {

  private static final long serialVersionUID = 1L;

  private String[] columnNames = { "First Name", "Last Name", "Sport",
    "# of Years", "Vegetarian" };
  private Object[][] data = {
    { "Kathy", "Smith", "Snowboarding", new Integer(5),
      new Boolean(false) },
    { "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
    { "Sue", "Black", "Knitting", new Integer(2),
      new Boolean(false) },
    { "Jane", "White", "Speed reading", new Integer(20),
      new Boolean(true) },
    { "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };

  public int getColumnCount() {
   return columnNames.length;
  }

  public int getRowCount() {
   return data.length;
  }

  public String getColumnName(int col) {
   return columnNames[col];
  }

  public Object getValueAt(int row, int col) {
   return data[row][col];
  }

  /*
   * JTable uses this method to determine the default renderer/ editor for
   * each cell. If we didn't implement this method, then the last column
   * would contain text ("true"/"false"), rather than a check box.
   */
  public Class getColumnClass(int c) {
   return getValueAt(0, c).getClass();
  }

  /*
   * Overriden this to disable the default editing of cell 
   */
  public boolean isCellEditable(int row, int col) {

   return false;

  }

  /*
   * Don't need to implement this method unless your table's data can
   * change.
   */
  public void setValueAt(Object value, int row, int col) {
   if (DEBUG) {
    System.out.println("Setting value at " + row + "," + col
      + " to " + value + " (an instance of "
      + value.getClass() + ")");
   }

   data[row][col] = value;
   fireTableCellUpdated(row, col);

   if (DEBUG) {
    System.out.println("New value of data:");
    printDebugData();
   }
  }

  private void printDebugData() {
   int numRows = getRowCount();
   int numCols = getColumnCount();

   for (int i = 0; i < numRows; i++) {
    System.out.print("    row " + i + ":");
    for (int j = 0; j < numCols; j++) {
     System.out.print("  " + data[i][j]);
    }
    System.out.println();
   }
   System.out.println("--------------------------");
  }

 }
 
 private String getColumnTitle(int index) {

  String title = null;

  switch (index) {
  case 0:
   title = "First Name";
   break;
  case 1:
   title = "Second Name";
   break;
  case 2:
   title = "Sport";
   break;
  case 3:
   title = "No of Year";
   break;
  case 4:
   title = "is Vegitarian?";
   break;
  case 5:
   title = "--";
   break;
  }

  return title;
 }

 public static void main(String args[]) {
  new JTableFrame();
 }
}

Maven FAQs

Maven, as a build tool replacement for ANT, has been on my Radar for last few months now. Although, we as team are little late to adopt it. However, for late adopters, there is always an added advantage of availability of resources.
 
There are plenty of resources and books available online at the cost, it uses your network bandwidth. However, for a build tool, usually we go by learning only what we need to do and mostly, "learning by doing" approach. Hence, maintaining this FAQ of a some of the interesting find outs of Maven, while trying to learn it, following the reverse engineering way. 

Please Note: Many of us, including me, would agree that Maven is not just the build tool. But, it always wise to learn walking first, rather than learning how to run, isn't it? :)

What does it mean if the maven dependency is of type war?

http://maven.apache.org/plugins/maven-war-plugin/overlays.html

What is transitive dependency?


What does different dependency scopes mean?

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

How to test a webapp using Jetty?

http://maven.apache.org/plugins/maven-war-plugin/examples/rapid-testing-jetty6-plugin.html

How to force maven to update local maven repository?

http://stackoverflow.com/questions/15673431/how-to-force-maven-to-update-local-repo

How to enable compilation at a particular Java version, say JDK1.5?

By default, if you run the maven build from eclipse, it takes the build JDK version set up in the Eclipse's project environment. So, to enable the build at higher or lower JDK level,  add this to your plugin list, so that the correct version is being taken for java compiler:


<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>2.0.2</version>
 <configuration>
  <source>1.5</source>
  <target>1.5</target>
 </configuration>
</plugin> 
 

Eclipse fails to copy resources, results in error something like below:

org.apache.maven.lifecycle.LifecycleExecutionException: Internal error in the plugin manager executing goal 'org.apache.maven.plugins:maven-resources-plugin:2.2:resources': Mojo execution failed.

We faced this issue while running maven build from eclipse, after bit of goggling and connecting some dots, we realized that the default maven-resources-plugin version used by eclipse, which is by the way defined in some pom.xml somewhere in your eclipse installation folder, was 2.1. This is a bit buggy when it is used with certain eclipse variants. So, in order to resolve this problem, point it to a higher version plugin. To do this, add the maven-resources-plugin, a plugin entry in your <plugins> list of pom.xml, precisely, like this:


<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-resources-plugin</artifactId>
 <version>2.5</version>
</plugin>
 
In my case, pointing it to 2.5 resolved this issue.
 

Using Maven, compiling and Running a Java File, with main() method?

 
mvn compile exec:java -Dexec.mainClass="com.myapp.App"  

 
  

Monday, August 19, 2013

ANT Deploy Tasks using Cargo

I wrote a post some time back on how to stop and start tomcat container using ANT.

Well, while trying to find something about Maven, I found this CARGO thing which can help you achieving the same, in fact better results. 

Interestingly, it has support to do similar task from Maven as well from JUnit as well.

Here is a link to  a sample example for Ant task

http://cargo.codehaus.org/Ant+support#Antsupport-examples

A similar example for doing it via Maven :

http://cargo.codehaus.org/Maven2+plugin

Friday, August 16, 2013

How to prevent JVM from getting terminated or shutdown, even if System.exit(0) is invoked?

Question


Normally, if System.exit(0) is executed, the JVM which is is running the code is stopped/terminated. How to prevent JVM from getting terminated or shutdown, even if System.exit(0) is called?

This was a question asked to one of my fellow developer in some interview. I did a bit of google and this is what I found after my small research on some of the websites.

So, The below code example will help you understand how to do it conceptually.

Another Question..


But, the another question I asked myself why would we ever need to alter the System.exit() sequence? What I could figure out is, some how if some of the existing code (which may be present by mistake in the code base) or some hacker tries to execute such code on your JVM, your JVM will be stopped and you application will be dead and will be a victim of DOS ( Denial of Service) attack. In web application context, this could be a very huge impact and can result in nightmare for production support team first and development team later.

And..the Answer...


Coming back to the example, the trick in below code example is, creating your own SecurityManager class and overriding checkExit() to throw an exception. Please note, when shutdown sequence is executed after System.exit() is executed, checkExit() method is called to check if the caller code is allowed to call System.exit() or not. So, this code example uses this fact to prevent JVM Shutdown sequence to execute properly and there by preventing JVM from getting terminated.

I am not sure though that this is the only way. If you know some other way to do the same, please do share it. :)

Demo Example

package com.test.util;

import java.security.Permission;

public class NoExitTest{

     
    private static class MySecurityManager extends SecurityManager 
     {
              
         public void checkExit(int status) 
         {
             System.out.println("check exit called..");
          super.checkExit(status);
             throw new SecurityException();
         }
    }   

 /**
  * @param args
  */
 public static void main(String[] args) {
    
  NoExitTestCase mngr = new NoExitTestCase();
  
    
  System.setSecurityManager(new MySecurityManager()); 
   // If you comment the code in above line you can see how exit works as it is expected to work normally
   
  try 
   {
    System.out.println("calling exit..");
    System.exit(0);
    
  } catch (SecurityException e) 
  {
    System.out.println("exit exception..exit did not execute properly");
    e.printStackTrace();
  
  }
    
  System.out.println("Exiting Normally...");

 }

}



Tuesday, August 13, 2013

How to display/print current time using Ant


At times, while juggling between lots of things a time, you may forget when the deployment happended or did you actually deployed since you did the last code change!

This small 4 liners can facilitate you with that feature. You can know the last deployed time by looking at the console/terminal.


<tstamp>
    <format property="IST_TIME" pattern="HH:mm:ss:sss zzz" locale="en,In"/>
 </tstamp>
<echo> Build Completed At: ${IST_TIME}</echo>


Add it the last target of your build script that is going to be executed, change the locale to your respective locale, to time in your timezone and you are good to go.

A similar example can be found in my previous post.

       

How to restart (stop/start) the tomcat using Ant

In case you are using tomcat on your local machine and using ant script to deploy your project war to tomcat webserver, this build script can come handy.

Please note, this may not work on your machine just by doing a copy paste. You may need to do a bit of change in the build.properties file to change the parameters there and also little bit of change in the build.xml to suit your exact requirement.

When I wrote this script, this build.xml was kept in the project home directory, which is normally the case for most of the web projects.

Our Goal:


  • Our goal here is to stop the tomcat if already running, then copy the war file to tomcat's webapps directory and after that start the tomcat again.
  • In addition, skip the tomcat stop, if it is not running already. Just copy the war file to tomcat's webapps directory and after that start the tomcat again.

Steps this script does:


  • The ant script, first start executing the target named, tomcat-start (which is meant to start the tomcat).
  • As you can see the depends attribute,  tomcat-start, depends on  deploy-local, so it executes the deploy-local
  • deploy-local in turn depends on tomcat-stop.
  • tomcat-stop depends on the check-port.
  • what check-port does is, it sets the property, "tomcat.running". Sets this parameter to true if it the tomcat is running on localhost, port 8080.
  • Please note that the port can differ depending on your tomcat configuration/installation. So, you might need to change this port to the one on which your tomcat runs.
  • Once this is done, the tomcat-stop, stops the tomcat, if "tomcat.running" sets to true.
  • deploy-local, copies the war file to tomcat's webapps directory.
  • Once the war is copied, tomcat-start starts the tomcat, and you app is ready to be tested on your local machine.

build.properties:


 project.name=MyProject  
 project.dir=C\://My_WorkSpace//MyProject  
 #Tomcat Local setting  
 tomcat.home=C\://Program Files (x86)//Apache Software Foundation//Tomcat 5.0  

 

build.xml



<?xml version="1.0" encoding="UTF-8"?>
<project name="${project.name}" default="tomcat-start" basedir=".">
 <!-- previous default task : tomcat-start -->
 <property file="build.properties" />
 <property environment="env" />


  
 <!--stopping tomcat  , if it is found running -->
 
 <target name="tomcat-stop" if="tomcat.running" depends="check-port" >
  <echo message="Tomcat is running...stopping it"/> 
     <java jar="${tomcat.home}/bin/bootstrap.jar" fork="true" >
         <jvmarg value="-Dcatalina.home=${tomcat.home}"/>
         <arg line="stop"/>
     </java>
 </target>
 

 <!-- Check whether Tomcat is running -->

   <target name="check-port" description="Check whether Tomcat is running">
     <echo message="Checking whether Tomcat is running"/>
     <condition property="tomcat.running">
       <socket server="localhost" port="8080"/> 
     </condition>
   </target>


 <!--    deploy-local: delete and copy ear --> 
 
 <target name="deploy-local" depends="tomcat-stop">
  
  <delete>
   <fileset dir="${tomcat.home}/webapps">
       <include name="${project.name}.war"/>
   </fileset>
  </delete>
  
  <delete dir="${tomcat.home}/webapps/${project.name}"/>
  
  <copy todir="${tomcat.home}/webapps">
   <fileset dir="${dist.dir}">
    <include name="**/${project.name}.war" />
   </fileset>
  </copy>
  
  <tstamp>
   <format property="TODAY_INDIA" pattern="HH:mm:ss:sss zzz" locale="en,In"/>
  </tstamp>
  <echo> Build Completed At: ${TODAY_INDIA}</echo>
  
      
 </target>
 
 <!-- start tomcat, it depends on deploy-local -->
 
 <target name="tomcat-start" depends="deploy-local">
       <java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
           <jvmarg value="-Dcatalina.home=${tomcat.home}"/>
           <arg line="start"/>
       </java>
 </target>
  
 
</project>

Related Links:

 


Monday, August 12, 2013

replace vs replaceAll in Java (Difference between replace and replace All)



Sometimes we do some silly mistake unintentionally.

Assuming replace() will replace first instance of String found in the entire String, and replaceAll() will replace all the instances of the Strings, is one of them.

When I realized this mistake of mine few years back, I curiously wanted to know what my fellow developers think about it.Interestingly, most of them were living with the same misconception.In fact they are really good Java programmers.
 
Hence, don't go by the name, Java has some other meaning tagged to it.

Both replace() and replaceAll() replaces all the occurrence found. However, there is difference in the type of arguments they accept.

replace() works on CharSequence, where as replaceAll(), accepts regex, so you can do a regular expression based pattern matching. The later is more flexible, but might result in a bit of performance overhead of compiling the regex and then do a matching. For smaller String replacements, you might not need to think much.  


 Quoting Java Doc:


String replace(CharSequence target, CharSequence replacement)
          Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
 String replaceAll(String regex, String replacement)
          Replaces each substring of this string that matches the given regular expression with the given replacement.
 String replaceFirst(String regex, String replacement)
          Replaces the first substring of this string that matches the given regular expression with the given replacement.
          



PS: CharSequence is one of the interface implemented by String class.

If you see the Java Doc for String class, String implements Serializable, CharSequence and Comparable<String>.
So, dont get confused by CharSequence, normally you can pass String as input to replace() function because of above reason.

And yes, for repacling only the first occurance of a char sequence, you can use replaceFirst(String regex, String replacement) method of String class.
           

Prototype

Prototype is another creation pattern. Intent:  - Intent is to create objects by cloning existing instance  - Specify the kinds of obj...