Sunday, August 7, 2016

HashMap Implementation Understanding

HashMap Implementation

Bucket is an array of entry in which index is based on hashing of keys.
In every index Bucket maintain linkedlist of entry.


/**
     * The default initial capacity - MUST be a power of two.
     */
    static final int DEFAULT_INITIAL_CAPACITY = 16;

    /**
     * The maximum capacity, used if a higher value is implicitly specified
     * by either of the constructors with arguments.
     * MUST be a power of two <= 1<<30.
     */
    static final int MAXIMUM_CAPACITY = 1 << 30;

    /**
     * The load factor used when none specified in constructor.
     */
    static final float DEFAULT_LOAD_FACTOR = 0.75f;

    /**
     * The table, resized as necessary. Length MUST Always be a power of two.
     */
    transient Entry[] table;

    /**
     * The number of key-value mappings contained in this map.
     */
    transient int size;



/**
     * Constructs an empty <tt>HashMap</tt> with the default initial capacity
     * (16) and the default load factor (0.75).
     */
    public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR;
        threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
        table = new Entry[DEFAULT_INITIAL_CAPACITY];
        init();
    }

public V put(K key, V value) {
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key.hashCode());
        int i = indexFor(hash, table.length);
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        addEntry(hash, key, value, i);
        return null;
    }

/**
     * Adds a new entry with the specified key, value and hash code to
     * the specified bucket.  It is the responsibility of this
     * method to resize the table if appropriate.
     *
     * Subclass overrides this to alter the behavior of put method.
     */
    void addEntry(int hash, K key, V value, int bucketIndex) {
        Entry<K,V> e = table[bucketIndex];
        table[bucketIndex] = new Entry<K,V>(hash, key, value, e);// here its //maintaining linkedlist of entry
        if (size++ >= threshold)
            resize(2 * table.length);
    }


static class Entry<K,V> implements Map.Entry<K,V> {
        final K key;
        V value;
        Entry<K,V> next;
        final int hash;

        /**
         * Creates new entry.
         */
        Entry(int h, K k, V v, Entry<K,V> n) {
            value = v;
            next = n;
            key = k;
            hash = h;
        }

        public final K getKey() {
            return key;
        }

        public final V getValue() {
            return value;
        }

        public final V setValue(V newValue) {
            V oldValue = value;
            value = newValue;
            return oldValue;
        }

Thursday, May 28, 2015

Cell , Node , Cluster , Deployment manager Websphere Application Server‏


All application servers have an Admin Service. The Admin Service does just what the name implies. In most application servers, the admin service does a really good job of system administration for one application server. When you try to run five or ten application servers in parallel, things get flakey. WebSphere Application Server is really a network of servers connected by a network of management. This is what IBM means by "network deployment" or the initials ND. At any moment, some servers will be running, some servers will be stopped, and some servers will be in some in between state. Large, distributed networks are like that. If the network is large enough -- we are talking tens of thousands of computers spread across several continents -- just knowing what is running on any given part of your network becomes a chore. The admin service for WebSphere Application Server is meant to control everything in an extended network of thousands of servers. It is possible to control a network that large from one laptop computer located anywhere in the world. Everything means
  • configuration -- the way the internal parts of WebSphere Application Server come to life when we start them
  • control -- the way those internal parts behave after we start them
  • monitoring -- reports and alerts we get
In order to understand how IBM administers the large network of servers that compose WebSphere Application Server, you have to understand some terms
IBM goes to great lengths to separate the actual code that executes from the settings that configure and control how that code behaves. Anything information that influences the way WebSphere Application Server behaves is isolated in something that IBM calls a profile. There is one profile for every node in the network of servers that we call WebSphere Application Server. Every time you create a profile, you have created a node. That node and the servers it contains might stand alone or it might be part of a collection of nodes that IBM calls a cell.
A node contains a collection of application servers and web servers that all happen to live on the same physical computer. One physical computer can contain many nodes as long as it has enough memory and enough CPU to power all the servers in all the nodes it contains. Every server in a node is guaranteed to live on the same physical computer.
An application server in the network of servers and services that we call WebSphere Application Server is Java Enterprise Edition platform code running on a Java Virtual Machine. That code produces dynamic content. This article describes how WebSphere Application Server produces dynamic content. Each application server has its own separate Java Virtual Machine. Sometimes, application servers are just called servers. Notice that any one application server is just a very small part of the product we call WebSphere Application Server. In the competing products, that application server is the entire product. Notice the difference in scale
A web server is a computer program that serves up static content. Sometimes, web servers are just called servers.
A cell is a collection on nodes. All the nodes could be on the same physical computer, but more likely, they will be on a network of physical computers
Configuration determines how all the application servers, web servers, message queues, and various internal services of WebSphere Application Server come to life. The configuration information that system administrators are allowed to modify is stored in a collection of XML files and properties files in the config directory of a profile.
XML files and properties files in the config directory of a profile define how the various servers and services in a node come to life. But those configuration files only get read once. They get read when a server or service is initializing. The moment initialization is complete, the XML files and the properties files are ignored. The only way to get a server to pay attention to changes you make in an XML file or a properties file is to stop the server and then start it again.
A network of JMX beans called MBeans control the way the network servers and services behave once they are running. Those same JMX beans also monitor the behavior of the servers and services while they execute.
Two kinds of specialized application servers enable us to configure, control, and monitor the behavior of the distributed network of servers and services that IBM calls WebSphere Application Server
A deployment manager is an application server that runs exactly one application -- an application that manages the configuration and behavior of an entire cell. There is only one deployment manager in any cell. The deployment manager holds the official configuration records for every server and service in the entire distributed network that is part of a cell. When a deployment manager orders a node agent to change the configuration of its servers and services to match this master record, we say the deployment manager has performed a synchronization. Deployment managers only talk to node agents
A node agent controls, configures and monitors all the servers and services in a node. Node agents talk to a deployment manager and node agents talk to the servers in their node. They relay administrative traffic from the deployment manager to the individual servers in their node. They relay status from the servers in their node to the deployment manager.
MBeans do several things
  • They simple pieces of Java code that can represent an application, a service, a server, or almost anything else in your enterprise.
  • They can report the status of anything in the enterprise
  • They can generate alerts concerning anything in the enterprise
  • They can be created automatically by WebSphere Application Server on behalf of anything installed in a cell
This thing we call WebSphere's admin service is really a network consisting of the various MBeans, the node agents, and the deployment manager. There are three fairly thin facades to the admin service
  • a web browser based graphic user interface called the admin console
  • a command line shell called wsadmin. This shell has a very powerful scripting language
  • any JMX clients that you might choose to write
If you have never used WebSphere Application Server before, the web browser based admin console is a very good way to learn basic setup and administration. There are limits to what you can do from the admin console. Depending on what you want to do, navigation can either be very easy or very hard. The admin console page that allows you to change JVM heap size is burried deep in the navigation tree of each server. If you want to change heap size for ten servers, you must navigate that tree for each of your ten servers. That can be a lot of tedious mouse clicks.
Once you know what you are doing, wsadmin becomes more attractive than the admin console for many administrative tasks. For one thing, the script language of wsadmin allows you to automate tasks. Once your network reaches a certain size, the admin console becomes a source of errors because of the large number of mouse clicks you must make and the amount of text entry you must provide. Again, the script language provides ways to automate tasks
The most powerful way to control the network of servers and services we call WebSphere Application Server is to write a JMX client. JMX clients have complete unfettered access to the entire admin service and everything that happens across the entire network.

Friday, July 11, 2014

OCR with Tss4J (wrapper for Tesseract OCR API) - Reading Text (English and Kannad) from Scanned Image and PDF

Recently I got a task in which I had to read text(English and Kannad) from scanned document(Image and PDF), I was searching for JAVA API implementing OCR , finally I found the API Tess4J.
After a lot of R&D(For support of both 32 and 64 bit system), I implemented a solution.

I am sharing the code here.

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import net.sourceforge.vietocr.PdfUtilities;


public class OCRKannad {

       /**
        * @param args
        */
       public static void main(String[] args) {
              // TODO Auto-generated method stub
              System.out.println("----11111" );
         System.out.println("---->" + args[0]);
                  String str1 = args[0];
                  String textFileFolder = args[1];
                  Tesseract localTesseract = Tesseract.getInstance();
                  File[] arrayOfFile1 = PdfUtilities.convertPdf2Png(new File(str1));
                                localTesseract.setLanguage("eng+kan");
                  for (File localFile : arrayOfFile1)
                    try
                    {
                      String str2 = localTesseract.doOCR(localFile);
                      Writer out = new BufferedWriter(new OutputStreamWriter(
                                new FileOutputStream(textFileFolder), "UTF-8"));
                            try {
                                out.write(str2);
                            } catch(Exception ex){
                                   System.out.println("Exception ex "+ex);
                            }finally {
                                try {
                                                out.close();
                                         } catch (IOException e) {
                                                System.err.println(e.getMessage());
                                         }
                            }
                     
                      System.out.println(str2);
                    }
                    catch (TesseractException localTesseractException)
                    {
                      System.err.println(localTesseractException.getMessage());
                    } catch (UnsupportedEncodingException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                     } catch (FileNotFoundException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                     }
                    finally
                    {
                      localFile.delete();
                    }
               
             
       }

}

API is available at http://sourceforge.net/projects/tess4j

For Complete dependency and dll please write to me saurabh.ranu@hotmail.com 

Hope it"ll be helpful , please comment if have any issue.






Thursday, May 29, 2014

Synchronizing a static method in java- Thread Communication - wait notify



Static methods synchronize on the class lock. Acquiring and relinquishing a class lock by a thread in order to execute a static synchronized method is analogous to that of an object lock for a synchronized instance method.

A thread acquires the class lock before it can proceed with the execution of any static synchronized method in the class, blocking other threads wishing to execute any static synchronized methods in the same class. 

This does not apply to static, non-synchronized methods, which can be invoked at any time. 
A thread acquiring the lock of a class to execute a static synchronized method has no effect on any thread acquiring the lock on any object of the class to execute a synchronized instance method.
In other words, synchronization of static methods in a class is independent from the synchronization of instance methods on objects of the class. A subclass decides whether the new definition of an inherited synchronized method will remain synchronized in the subclass.


Waiting and Notifying
Waiting and notifying provide means of communication between threads that synchronize on the same object . The threads execute wait() and notify() (or notifyAll()) methods on the shared object for this purpose. 
These final methods are defined in the Object class and, therefore, inherited by all objects. These methods can only be executed on an object whose lock the thread holds (in other words, in synchronized code), otherwise, the call will result in an IllegalMonitorStateException.

final void wait(long timeout) throws InterruptedException
final void wait(long timeout, int nanos) throws InterruptedException
final void wait() throws InterruptedException

A thread invokes the wait() method on the object whose lock it holds. The thread is added to the wait set of the current object.

final void notify()
final void notifyAll()

A thread invokes a notification method on the current object whose lock it
holds to notify thread(s) that are in the wait set of the object.
Communication between threads is facilitated by waiting and notifying, as illustrated
by Figure below.



A thread usually calls the wait() method on the object whose lock it holds because a condition for its continued execution was not met.
The thread leaves the Running state and transits to the Waiting-for notification state. There
it waits for this condition to occur. The thread relinquishes ownership of the object lock.

Transition to the Waiting-for-notification state and relinquishing the object lock are completed as one atomic (non-interruptible) operation. The releasing of the lock of the shared object by the thread allows other threads to run and execute synchronized code on the same object after acquiring its lock. Note that the waiting thread relinquishes only the lock of the object on which the
wait() method was invoked. It does not relinquish any other object locks that it might hold, which will remain locked while the thread is waiting. Each object has a wait set containing threads waiting for notification. Threads in the Waiting-for-notification state are grouped according to the object whose wait() method they invoked.


Figure above shows a thread t that first acquires a lock on the shared object, and afterward invokes the wait() method on the shared object. This relinquishes the object lock and the thread t awaits to be notified. While the thread t is waiting, another thread t can acquire the lock on the shared object for its own purposes. A thread in the Waiting-for-notification state can be awakened by the occurrence of any one of these three incidents:

1. Another thread invokes the notify() method on the object of the waiting
thread, and the waiting thread is selected as the thread to be awakened.
2. The waiting thread times out.
3. Another thread interrupts the waiting thread.

notify() does not release lock, but when  block will complete lock will be released by thread.


Why can't Java constructors be synchronized?

Because synchronized guarantees that actions on the same objects are not to be performed by multiple threads. And when the constructor is called you still don't have the object. It is logically impossible for two threads to access the constructor of the same object.






Wednesday, May 28, 2014

Sorting XML response based on more than one field - Web Experience Factory (Portlet Factory)


I was facing a problem to sort xml response based on more than one field.
Tried to use Transform Sort Builder but unable to implement it.
Finally implemented a solution of writing my custom comparator to sort the XML response.

My Sample XML Response :

<?xml version="1.0" encoding="utf-8"?>
<Invoices>
  <Invoice>
    <Date>06/10/2013 16:14:26</Date>
    <InvoiceId>str1234</InvoiceId>
    <Client>str1234</Client>
    <BillingEntity>str1234</BillingEntity>
    <UserAccess>str1234</UserAccess>
    <Reports>
      <Report>str1234</Report>
    </Reports>
  </Invoice>
</Invoices>

// Sort Method

public  IXml sort(WebAppAccess webAppAccess, IXml sourceData) {
       System.out.println("!!!!!!In SortInvioceReportLJO.sort 111111111!!!!!!!!!!!!!!");
         IXml result = sourceData.cloneElement();
        IXml parent = result;
        // find the elements to be sorted
        List elements = XmlUtil.findElements(parent, "Invoices/Invoice");
        // loop through each element to be sorted
        Iterator iterator = elements.iterator();
        while (iterator.hasNext()) {
            IXml element = (IXml)iterator.next();
            // remove the element from its parent element
            parent.removeChildElement(element);
        }
        // sort the list of elements
       System.out.println("!!!!!!In SortInvioceReportLJO.Collection Sort!!!!!!!!!!!!!!");
        Collections.sort(elements, new CustomComparator());
        // loop through each element again - in the sorted order now
        iterator = elements.iterator();
        while (iterator.hasNext()) {
            IXml element = (IXml)iterator.next();
            // add the element back to its parent element
            parent.addChildElement(element);
        }
        System.out.println("!!!!!!Returning from SortInvioceReportLJO.sort== "+result);
        return result;
    }


// Custom Comparator Class

public class CustomComparator implements Comparator {

              public int compare(Object o1, Object o2) {
                     System.out.println("In CustomComparator compare !!  ");
                     IXml e1;
                     IXml e2;
                     SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
                     int i = 0;
                     try{
                     if((o1 instanceof IXml) && (o2 instanceof IXml)){
                          
                           e1 = (IXml)o1;
                           e2 = (IXml)o2;
                           System.out.println("In Comparator o1!!! "+o1);
                           System.out.println("In Comparator o2!!! "+o2);
                          
                           i = sdf.parse(e2.getText("Invoice/Date")).compareTo(sdf.parse(e1.getText("Invoice/Date")));
                           if(i==0){
                                  i = (Integer.valueOf((e1.getText("Invoice/InvoiceId")))).compareTo(Integer.valueOf((e2.getText("Invoice/InvoiceId"))));
                           }
                           System.out.println("In Comparator i === "+i);
                    
                     }
              }catch(ParseException ex){
                     System.out.println("CustomComparator Exception in Parsing !!  "+ex);
              }
                    
                     return i;
              }

       }


Hope it'll be helpful.