Saturday, August 29, 2015

CyclicBarrier Continued

In our previous series of CyclicBarrier we learned that CyclicBarrier can be constructed in two ways.
Like 
  1. CyclicBarrier cb=new  CyclicBarrier (int nosOfPArallelThread,Runnable BarrierAction);
  2.  CyclicBarrier cb=new  CyclicBarrier (int nosOfParallelThread);  
In our previous series we saw an example of first constructor, which takes two parameters numberofParallelThreads and a Runnable as argument.
In this series we look for the use of the second constructor ,  which takes only one integer argument,the number of parallel threads.

package com.brainatjava.test;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierConstructorWithOneArgument {

    private static int matrix[][] =
    {
        { 1 ,1,1,1},
        { 2, 2 ,2,2},
        { 3, 3, 3 ,3},
        { 4, 4, 4, 4 },
        { 5, 5, 5, 5 } };

    private static int results[];

    private static class Adder extends Thread
    {
        int row;

        CyclicBarrier barrierWhereAllThreadsWillWait;

        Adder(CyclicBarrier cb, int row)
        {
            this.barrierWhereAllThreadsWillWait = cb;
            this.row = row;
        }

        public void run()
        {
            int columns = matrix[row].length;
            int sum = 0;
            for (int i = 0; i < columns; i++)
            {
                sum += matrix[row][i];
            }
            results[row] = sum;
            System.out.println("Results for row " + row + " are : " + sum);
           int arrivalIndex=0;
            try
            {
                arrivalIndex= barrierWhereAllThreadsWillWait.await();
            } catch (InterruptedException ex)
            {
                ex.printStackTrace();
            } catch (BrokenBarrierException ex)
            {
                ex.printStackTrace();
            }
            if(arrivalIndex==0){
                System.out.println(Thread.currentThread().getName()+" is executing the combined result.");
                 int total = 0;
                 for (int i = 0; i < results.length; i++)
                 {
                     total += results[i];
                 }
                 System.out.println("Results are: " + total);
            }
        }
    }

    public static void main(String args[])
    {
        final int rows = matrix.length;
        results = new int[rows];
        CyclicBarrier barrier = new CyclicBarrier(rows);
        for (int i = 0; i < rows; i++)
        {
            new Adder(barrier, i).start();
        }
       
    }
}

In the above code snippet ,Please note the line highlighted with red color.Here the await() method of cyclicbarrier class is returning an int value.That signifies the arrival index of the thread at the barrier.
If the arrival index is zero ,it means that thread is the last thread to arrive at the barrier.

Here we choose the last thread that reaches at the barrier to execute the final action that is to calculate the total sum.Since we have taken the arrival index of the thread as zero.

Reset Method of CyclicBarrier(): 

If reset() method is called by any thread, then all the other threads waiting at the barrier will awake by throwing a BrokenBarrierException.So we can say that the reset() method is used when we want to break the barrier forcibly.Remember that if one thread will call reset method when no other threads are waiting on barrier,then reset method has no  effect.

Let's see the reset method in action with the help of an example.

package com.brainatjava.test;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

    class CyclicBarrierResetExample
    {
        public static void main(String args[])
        {
            CyclicBarrier cb=new CyclicBarrier(2,new Master());
            Thread slave1=new Thread(new Slave1(cb));
            slave1.start();
            Thread slave2=new Thread( new Slave2(cb));
            slave2.start();
        }
    }
    class Slave1 implements Runnable
    {
    CyclicBarrier cb;
        public Slave1(CyclicBarrier cb)
        {
            this.cb=cb;
        }
        public void run()
        {
            System.out.println("Slave1 has performed some work.");
            System.out.println("Slave1 is going to wait.");
            try
            {
                cb.await();
            }catch(BrokenBarrierException e){
                System.out.println("Slave1 can't wait as it is getting BrokenBArrier exception "+e.getMessage());
                e.printStackTrace();
            } catch (InterruptedException e) {
                 System.out.println("Slave1 can't wait as it is getting interupted exception  "+e.getMessage());
                e.printStackTrace();
            }
            System.out.println("Anyway !!!! Woo Slave1's waiting is finsihed.Slave1 is going home now.");
        }
    }
    class Slave2 implements Runnable
    {
    CyclicBarrier cb;
        public Slave2(CyclicBarrier cb)
        {
            this.cb=cb;
        }
        public void run()
        {
            System.out.println("Slave2 has performed some work.");
            System.out.println("Slave2 is going to wait.");
            try
            { Thread.sleep(1000);
                cb.reset();
                cb.await();
            }catch(BrokenBarrierException e){
                System.out.println("Slave2 can't wait as it is getting brokenbarrier exception "+e.getMessage());
            } catch (InterruptedException e) {
                 System.out.println("Slave2 can't wait as it is getting nullpointer exception "+e.getMessage());
                e.printStackTrace();
            }
           
            System.out.println("Anyway !!!! Woo Slave2's waiting is finsihed.Slave2 is going home now.");
        }
       
    }
    class Master implements Runnable
    {
        public void run()
        {
            System.out.println("Master is going to complete all his work as two slaves have already reached at the barrier.");
            System.out.println("Thank you slaves you completed all your work on time.");
        }
       
    }


Output Of the Above Programme

Slave1 has performed some work.
Slave1 is going to wait.
Slave2 has performed some work.
Slave2 is going to wait.
Master is going to complete all his work as two slaves have already reached at the barrier.
Thank you slaves you completed all your work on time.
Anyway !!!! Woo Slave2's waiting is finsihed.Slave2 is going home now.
Slave1 can't wait as it is getting BrokenBArrier exception null
java.util.concurrent.BrokenBarrierException
    at java.util.concurrent.CyclicBarrier.dowait(Unknown Source)
    at java.util.concurrent.CyclicBarrier.await(Unknown Source)
    at com.brainatjava.test.Slave1.run(CyclicBarrierResetExample.java:30)
    at java.lang.Thread.run(Unknown Source)
Anyway !!!! Woo Slave1's waiting is finsihed.Slave1 is going home now.

Monday, August 24, 2015

CyclicBarrier:Java Concurrency

In our previous series we have learned about different synchronizers like Semaphore,CountdownLatch etc.
Similarly CyclicBarrier is another synchronizer.We will try to understand it with the help of an example.

Let's assume we have a project.Now in initial stage assume there are 3 developers are working on it.And the project manager is there to review the status of the project.After working for some days the developers completed there code and the project manager reviewed the status.And he is happy now.All is well.Now he decided to move to the next phase.Assume there are 3 QA guys are there.Now the code is submitted to QA team for quality testing.After working some days , the QA team has completed the process.Again project manager reviewed the status of the project and he is happy with the progress.Then he decided to move to the next step ie. deployment in staging server  for  SIT(System Integration Test).Assume there are 3 support guys working on this and after few days,they completed there work.The manger reviewed the status and he is happy with it.Now he decided to move to the next step ie. for UAT(User Acceptance Test).Suppose there are 3 guys who is handling this process and he completes his work after some efforts.Now the project manager reviewed the status of the project and he is happy with it and gave green signal to go to the next step ie. to live the project.

Here Remember one thing that when one developer completes his work,he will wait for fellow developers to complete their work.That is they will wait for each other,unless all the  developers complete their work.Similarly all QA guys will wait for each other to complete their work.And same policy will apply for
the deployment and UAT guys.

Here one important thing to note is that after each stage the project manager is reviewing code and  giving go ahead after doing necessary corrections.Here we have 4 stages.They are development stage,QA stage,deployment stage and UAT stage.But after each stage the job of the project manger is  same ie. to review the project status and give go ahead.

And let's try to understand CyclicBarrier with the help of  the above example.

CyclicBarrier:

  • it is a  synchronizer that allows a set of threads that all wait for each other to reach a common barrier point.
Here in our example the developers will wait for each other till the completion of all the developers in stage1.
Similarly in stage 2 QA guys will start their work and wait for each other to complete.
Similarly in stage 3 the deployment guys will start their work and wait for each other to complete.
And in stage 4 the UAT guys will start their work and wait for each other to complete.
  •  It is helpful for for multithreaded operations that occur in stages with intermediate results from the different threads need to be combined between stages.
 Here in our example in stage 1 developers threads  are completing  their work and project manager is reviewing the work by combining them and gave go ahead.Again in stage 2 the QA threads take those output of stage 1 and start their work.In stage 3 the deployment threads take the output from stage 2 and start their work.And in stage 4 the UAT threads take the output from stage 3 and start their work.
  • It explicitly allow one thread to tell others to stop waiting at the barrier point ,if any interruption or error occurs by throwing InterruptedException or BrokenBarrierException.
Here in our example if any exception occurs in any developer thread in stage 1 ,then all other developer threads waiting at barrier will also leave the barrier point by throwing exception.
Java Doc syas:The CyclicBarrier  uses an all-or-none breakage model for failed synchronization attempts: If a thread leaves a barrier point prematurely because of interruption, failure, or timeout, all other threads waiting at that barrier point will also leave abnormally via BrokenBarrierException(or InterruptedException if they too were interrupted at about the same time).

  • A single threaded operations required between different stages to combine the result from the different threads in each stage.  
 Here in our example the project manager is there to combine and review the codes generated by each thread in each stages.Here we can think of project manager thread as  the single thread required to join the results of each thread.
Java Doc Says:A CyclicBarrier supports an optional Runnable command that is run once per barrier point, after the last thread in the party arrives, but before any threads are released.

This is called barrier action thread.And this thread is called after all the parties arrived at the barrier point.

CyclicBarrier Construction:

We can construct  the CyclicBarrier with the following parameters
  • The number of threads that will take part in parallel processing.
  • And/or the barrier action thread which is called at the end of each stage to combine the result of all the parallel threads.But this parameter is optional. 

CyclicBarrier cb=new CyclicBarrier(int nosOfParallelThreads);
 CyclicBarrier cb=new CyclicBarrier(int nosOfParallelThreads,Runnable barrierActionThread);

1.CyclicBarrier  class is used as a barrier for a set of threads, to keep them waiting until all the other  threads have reached it.We said a thread reaches the barrier if it calls the await() method of the CyclicBarrier  class.

2.The barrier is said to be tripped once all the predefined number of threads reached the barrier.

3.The last thread to reach the barrier(or calls the await() method) executes this Runnable  action before the other waiting threads are released.

4.The threads will keep waiting until the number of waiting threads is equal to the number passed at the time of CyclicBarrier construction or the waiting thread is interrupted by some other thread or the barrier is broken or reset.

5.If any thread is interrupted while waiting at the barrier, then all other waiting threads will throw BrokenBarrierException  and barrier is broken.

6.The Runnable action supplied in constructor will not be executed if barrier is broken.

7.The getNumberWaiting() method returns the number of threads waiting or blocked at the barrier for the barrier to be tripped.

8.The getParties() method returns the number of threads required to trip the barrier.

Working of CyclicBarrier:

  • Each thread performs its own module.
  • After completion of the module , each thread calls the await() method.
  • The await() method returns only when the predefined number of threads in constructor have called await() method.
  • if any of the threads is interrupted or times out while waiting for the barrier, then the barrier is broken and all other waiting threads receive a   BrokenBarrierException.

Code Sample:

 DeveloperThread:

package com.brainatjava.test;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class DeveloperThread implements Runnable{
    private CyclicBarrier cyclicBarrier;
    public void run() {
    System.out.println(Thread.currentThread().getName()+" has finished his work and waiting on barrier for other developer threads to reach at the barrier.");
    try {
        cyclicBarrier.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (BrokenBarrierException e) {
        e.printStackTrace();
    }
       
    }
    public void setCyclicBarrier(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }
    public CyclicBarrier getCyclicBarrier() {
        return cyclicBarrier;
    }
   }

QAThread  :


package com.brainatjava.test;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class QAThread implements Runnable{
    private CyclicBarrier cyclicBarrier;

    public void run() {
        System.out.println(Thread.currentThread().getName()+" has completed his work and waiting for other QA threads to reach at the barrier");
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }

    public void setCyclicBarrier(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    public CyclicBarrier getCyclicBarrier() {
        return cyclicBarrier;
    }
}

DelploymentThread :


package com.brainatjava.test;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class DelploymentThread implements Runnable{
    private CyclicBarrier cyclicBarrier;
    public void run() {
        System.out.println(Thread.currentThread().getName()+" has completed his work and waiting for other deployment thread to reach at the barrier.");
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
    public void setCyclicBarrier(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }
    public CyclicBarrier getCyclicBarrier() {
        return cyclicBarrier;
    }
}

UATThread :


package com.brainatjava.test;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class UATThread implements Runnable{
    private CyclicBarrier cyclicBarrier;

    public void run() {
        System.out.println(Thread.currentThread().getName()+" has completed his work and waiting for other UAT threads to reach at the barrier");
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }

    public void setCyclicBarrier(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    public CyclicBarrier getCyclicBarrier() {
        return cyclicBarrier;
    }
}

ManagerBarrierActionThread:


package com.brainatjava.test;

public class ManagerBarrierActionThread implements Runnable{
    int stageCount;
    public int getStageCount() {
        return stageCount;
    }
    public void setStageCount(int stageCount) {
        this.stageCount = stageCount;
    }
    public void run() {
        stageCount++;
        System.out.println("\nProject manager  reviewed the code and gave go ahead.And this completes stage "+stageCount+"\n");
       
    }
}



MainThread which will invoke all this thread:

package com.brainatjava.test;

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierTest {
public static void main(String[] args) {
    final int Thread_COUNT=3;
    final int DEVELOPEMENT_COMPLETED=1;
    final int QA_COMPLETED=2;
    final int DEPOYMENT_COMPLETED=3;
    boolean flag =true;
    ManagerBarrierActionThread managerBarrierActionThread=new ManagerBarrierActionThread();
    CyclicBarrier cb=new CyclicBarrier(Thread_COUNT,managerBarrierActionThread);
    for(int i=0;i<3;i++){
    DeveloperThread developers=new DeveloperThread();
    developers.setCyclicBarrier(cb);
    Thread developerThread=new Thread(developers,"DeveloperThread"+(i+1));
    developerThread.start();
}
       
    while(flag){
        int stageCount=managerBarrierActionThread.getStageCount();
        if(stageCount==DEVELOPEMENT_COMPLETED){
        flag=false;
        for(int i=0;i <3;i++){

        QAThread testers=new QAThread();
        testers.setCyclicBarrier(cb);
        Thread qaThread=new Thread(testers,"TesterThread"+(i+1));
       
qaThread.start();
    }
    }
       
}
    flag=true;
    while(flag){
        int stageCount=managerBarrierActionThread.getStageCount();
        if(stageCount==QA_COMPLETED){
        flag=false;
       for(int i=0;i<3;i++){

        DelploymentThread deployers=new DelploymentThread();
        deployers.setCyclicBarrier(cb);
        Thread deploymentThread=new Thread(deployers,"DeploymentThread"+(i+1));
        deploymentThread.start();
    }
    }
       
}
    flag=true;
    while(flag){
        int stageCount=managerBarrierActionThread.getStageCount();
        if(stageCount==DEPOYMENT_COMPLETED){
        flag=false;
       for(int i=0;i<3;i++){

        UATThread uat=new UATThread();
        uatThreads.setCyclicBarrier(cb);
        Thread uatThread=new Thread(
uat,"UATThread"+(i+1));
       
uatThread.start();
    }
    }
       
}
}
}

The output of the above code is :

DeveloperThread1 has finished his work and waiting on barrier for developer threads to reach at the barrier.
DeveloperThread3 has finished his work and waiting on barrier for developer threads to reach at the barrier.
DeveloperThread2 has finished his work and waiting on barrier for developer threads to reach at the barrier.

Project manager  reviewed the code and gave go ahead.And this completes stage 1

TesterThread1 has completed his work and waiting for other QA threads to reach at the barrier
TesterThread2 has completed his work and waiting for other QA threads to reach at the barrier
TesterThread3 has completed his work and waiting for other QA threads to reach at the barrier

Project manager  reviewed the code and gave go ahead.And this completes stage 2

DeploymentThread1 has completed his work and waiting for other deployment thread to reach at the barrier.
DeploymentThread2 has completed his work and waiting for other deployment thread to reach at the barrier.
DeploymentThread3 has completed his work and waiting for other deployment thread to reach at the barrier.

Project manager  reviewed the code and gave go ahead.And this completes stage 3

UATThread1 has completed his work and waiting for other UAT threads to reach at the barrier
UATThread2 has completed his work and waiting for other UAT threads to reach at the barrier
UATThread3 has completed his work and waiting for other UAT threads to reach at the barrier

Project manager  reviewed the code and gave go ahead.And this completes stage 4

If you notice the output we will see in  stage 1 or iteration 1 3 developers threads run parallely and completed their work ,then manger reviews the progress and gave go ahead.
Similarly in stage 2 or iteration 2 Tester threads have done their work .
In Stage 3 or iteration 3 Deployment Threads have done their work .
In Stage 4 or iteration 4 UAT Threads have done their work.

Note:

Here one thing need to observe that we are using the same cyclic barrier again and again for different stages/iterations without resetting it.So the barrier is called cyclic.We will dig deep about it in next series.We will also discuss one more variant of CyclicBarrier in part 2 of this series and will know some other important properties of  it.

Tuesday, August 18, 2015

CountDownLatch:Java concurrency

In our previous series of Mutex and Semaphore , we learn about the different Synchronizers.Now we discuss about one of them that is CountDownLatch.Let's take the understanding of CountDownLatch With the help of an example.

        Let's take a real life scenario.Suppose we get a new project from a client.We have one architect and 10 developers.First the architect will design the project  and decide the technologies to use.He will decide about the project flow and create the different modules.But till then all the developers need to wait for the architect to finish all his work.It means developers are coming to office but they are not doing anything useful.Because they have dependency on the  architect.Just think they are waiting for a lock to be released by architect.After the lock is released by the architect the developers will get go ahead.Then they will start to work on there assigned module.Assume till the time the architect is waiting for completion of the task which is going on by developers.Just think that he is waiting for the lock to be released by developers.Remember that he can not proceed until all the developers have completed their work.If 5 developers have completed their work,then he still has to wait for remaining 5 developers.Then after the architect will proceed to review all the pieces of code and integrate them.This scenario is perfectly suitable for use of a CountDownLatch.


Definition:CountDownLatch  is a synchronization aid or a synchronizer  that allows one or more threads to wait for one or more other threads to complete. 

Implementation In Java:
 
To implement java provides a class know as CountDownLatch.We can create  a CountDownLatch object with integer argument like  
   CountDownLatch architectLatch = new CountDownLatch(1);
CountDownLatch developerLatch = new CountDownLatch(10);
  Let's consider our example that we have 10 developers all are waiting on architectLatch by calling architectLatch.await() method.Here await() method will cause the calling threads(i.e our developer threads) to wait until the latch has counted down to zero.Then after completing its work the architect thread will call architectLatch.countDown().The countDown() method decreases the count of the latch by 1.Now architect thread will wait by calling developerLatch.await().If the count of the latch reaches at zero , then all the threads waiting on this CountDownLatch (ie. architectLatch) will start.
 After completing their work each developer thread will call developerLatch.countDown().
When all the developer threads complete then the count of the developerLatch will be zero and architect thread will resume and start working.Take  a look at the implementation details.

import java.util.concurrent.CountDownLatch;

public class Architect {

        public static void main(String args[]) throws InterruptedException {
            final int totalNosOfLatchCount=10;
             CountDownLatch architectLatch = new CountDownLatch(1);
             CountDownLatch developerLatch = new CountDownLatch(
totalNosOfLatchCount);
             for (int i = 0; i < totalNosOfLatchCount; ++i)
               new Thread(new Developer(architectLatch, developerLatch)).start();

             completeModuleDesign();       
    
             architectLatch.countDown();   
  
             System.out.println("Architect has decreased the count of architectLatch and 
             going to wait on  developerLatch.");
             developerLatch.await();       
   
             System.out.println("Now all the developers have completed there work and architect is going to resume.");
           }
        public static void  completeModuleDesign(){
            System.out.println("Architect has completed the design of all his module. ");
         }
}
import java.util.concurrent.CountDownLatch;

public class Developer  implements Runnable {
           private final CountDownLatch architectLatch;
           private final CountDownLatch developerLatch;
           Developer(CountDownLatch startSignal, CountDownLatch doneSignal) {
              this.architectLatch = startSignal;
              this.developerLatch = doneSignal;
           }
           public void run() {
              try {
                  architectLatch.await();
                  completeDevloperModule();
                developerLatch.countDown();
                System.out.println("Each developer has decreased the count on developerLatch by       one.\nAnd number of avaialble count on developerLatch is "+developerLatch.getCount());
              } catch (InterruptedException ex) {} // return;
           }

           void completeDevloperModule() {
               System.out.println("Developer is completing his own module");
           }
         }



Reusability of CountDownLatch:

 One important thing we need to note here that , after reaching the count zero,the  CountDownLatch can't be reused.There is no method to increase the count of CountDownLatch.We can think of it as a benefit as no one can increase the count by mistake also.If we call again the await() method after the count reaches 0, there will be no result.Simply it will pass through that and will not throw any exception. Still if we want to reset the count for programming purpose, we can consider using CycclicBarrier. 

Comparison of CountDownLatch with wait(),notify() and join():


At first glance it seems like we can achieve this with the help of wait ,notify and join mechanism.But CountDownLatch is more forgiving in the case of missed signal.Consider a scenario , if one thread is waiting and another thread is notifying him,but just assume that the thread1, call it notifying thread, started first and it notified the the thread which is supposed to receive the notification signal,which is not started yet.But the thread which need to receive the notification signal started after that.Now the second thread which need to receive the notification signal will be in a stall state, because it missed the notification signal issued by the first thread.But in this case CountDownLatch will serve perfectly well.That is if count is down first and await method will invoked later,there will be no missed signal issue.

Monday, August 10, 2015

cvc-elt.1: Cannot find the declaration of element 'beans'-Part2

In our previous series of the exception cvc-elt.1: Cannot find the declaration of element 'beans' We saw what is xml namespace uri and what is xml namespace prefix.And we resolve a flavor of the exception.Here in this blog we will see other favors of the same exception and measures to avoid it.Before knowing all this we should know how the application context xml document is interpreted by JAVA xml Parser and what are the different components of the application context.

Sample xml configuration:


<xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:task="http://www.springframework.org/schema/task"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
                      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                      http://www.springframework.org/schema/task
                      http://www.springframework.org/schema/task/spring-task-3.1.xsd
                      http://www.springframework.org/schema/context
                      http://www.springframework.org/schema/context/spring-context-3.1.xsd ">
Have a look at xsi:schemaLocation.This is used by JAVA xml Parser to validate the xml file.Suppose we have a tag defined in application context  like "context" .For this we have to declare the context namespace.Now what Java xml parser will  here.It will read the  schemaLocation of "context" element as http://www.springframework.org/schema/context/spring-context-3.1.xsd.And try load them from internet to validate the xml file.And Spring intercepts these load requests  and try to find the same xsd version from its own classpath.
Let's do a little bit more analysis.When we build a war or jar file to deploy in production machine, a spring.schemas property  file is created inside the META-INF folder.The contents of the spring.schemas look like this.


Sample Spring.schemas:


http\://www.springframework.org/schema/context/spring-context-2.5.xsd=
org/springframework/context/config/spring-context-2.5.xsd

http\://www.springframework.org/schema/context/spring-context-3.0.xsd=
org/springframework/context/config/spring-context-3.0.xsd

http\://www.springframework.org/schema/context/spring-context-3.1.xsd=
org/springframework/context/config/spring-context-3.1.xsd

http\://www.springframework.org/schema/context/spring-context.xsd=
org/springframework/context/config/spring-context-3.1.xsd

http\://www.springframework.org/schema/jee/spring-jee-2.0.xsd=
org/springframework/ejb/config/spring-jee-2.0.xsd

http\://www.springframework.org/schema/jee/spring-jee-2.5.xsd=
org/springframework/ejb/config/spring-jee-2.5.xsd

http\://www.springframework.org/schema/jee/spring-jee-3.0.xsd=
org/springframework/ejb/config/spring-jee-3.0.xsd

http\://www.springframework.org/schema/jee/spring-jee-3.1.xsd=
org/springframework/ejb/config/spring-jee-3.1.xsd

http\://www.springframework.org/schema/jee/spring-jee.xsd=
org/springframework/ejb/config/spring-jee-3.1.xsd

http\://www.springframework.org/schema/lang/spring-lang-2.0.xsd=
org/springframework/scripting/config/spring-lang-2.0.xsd

http\://www.springframework.org/schema/lang/spring-lang-2.5.xsd=
org/springframework/scripting/config/spring-lang-2.5.xsd

http\://www.springframework.org/schema/lang/spring-lang-3.0.xsd=
org/springframework/scripting/config/spring-lang-3.0.xsd

http\://www.springframework.org/schema/lang/spring-lang-3.1.xsd=
org/springframework/scripting/config/spring-lang-3.1.xsd

http\://www.springframework.org/schema/lang/spring-lang.xsd=
org/springframework/scripting/config/spring-lang-3.1.xsd

http\://www.springframework.org/schema/task/spring-task-3.0.xsd=
org/springframework/scheduling/config/spring-task-3.0.xsd

http\://www.springframework.org/schema/task/spring-task-3.1.xsd=
org/springframework/scheduling/config/spring-task-3.1.xsd

http\://www.springframework.org/schema/task/spring-task.xsd=
org/springframework/scheduling/config/spring-task-3.1.xsd

http\://www.springframework.org/schema/cache/spring-cache-3.1.xsd=
org/springframework/cache/config/spring-cache-3.1.xsd

http\://www.springframework.org/schema/cache/spring-cache.xsd=
org/springframework/cache/config/spring-cache-3.1.xsd
When  we start our application by starting our application server (tomcat/jetty etc) or by simply running the main method,then it takes a schemaLocation value from our application context xml document and try to find the mapping for it in spring.schemas property file.If it finds one in the spring.schemas property file ,then it uses the value associated with this mapping.Usually the value is the location of the corsponding  xsd file.

Let's analyze some rows in spring.schemas property file.

http\://www.springframework.org/schema/context/spring-context-2.5.xsd
=org/springframework/context/config/spring-context-2.5.xsd

http\://www.springframework.org/schema/context/spring-context-3.0.xsd
=org/springframework/context/config/spring-context-3.0.xsd

http\://www.springframework.org/schema/context/spring-context.xsd=
org/springframework/context/config/spring-context-3.0.xsd
Here the left side represents the schemaLocation values as declared in application context xml document.And the right side represents the excat location of the corresponding xsd file.That is if we un jar the spring-context.jar file we will see the corresponding xsd file inside  the package
org.springframework.context.config
After getting the xsd file in the classpath(inside the spring-context.jar),the xml parser validates the tags in the application context document with the corresponding xsd file.

Note:If you are curious about the extra backslash(\) before the : in 
http\://www.springframework.org/schema/context/spring-context-2.5.xsd
Then note that the ':' character is a valid delimiter in the Java properties format, and so the ':' character in the URI needs to be escaped with a backslash.

Now come back to the contents of the spring.schemas property file

http\://www.springframework.org/schema/context/spring-context-2.5.xsd
=org/springframework/context/config/spring-context-2.5.xsd

http\://www.springframework.org/schema/context/spring-context-3.0.xsd
=org/springframework/context/config/spring-context-3.0.xsd

http\://www.springframework.org/schema/context/spring-context.xsd=
org/springframework/context/config/spring-context-3.0.xsd
This means that (in xsi:schemaLocation)   of application context xml document

http://www.springframework.org/schema/context/spring-context-2.5.xsd   
will be validtaed against
org/springframework/context/config/spring-context-2.5.xsd  
in the classpath
And
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
and
http://www.springframework.org/schema/context/spring-context.xsd
will be validated against org/springframework/context/config/spring-context-3.0.xsd
in the classpath

Rule Of Thumb :

Always make sure the xsd version in application context xml document matches with xsd version availble in spring.schemas property file(which is inside META-INF).If we use in application context xml document a higher version  of xsd file and our application contains a lower version of the corresponding  jar file,then mismatch will be there.

But how it resolves the same when there is internet in the machine where application is deployed?
After searching the  class path,if Java xml Parser can't find the same version of the xsd file in the classpath, then it tries to look it over internet.So we get the exception in the machine where internet connection is not there.

Issue with jar creation in Eclipse:

Recently I was creating a jar for a spring application in STS 3.6.Here my application context xml document contains the same version of the xsd file as with my jar file.I matched everything .But still I was getting the same exception cvc-elt.1: Cannot find the declaration of element 'beans'.After digging a little I came to find that,the spring.schemas property file didn't contain any schemaLocation and it's corrsponding xsd file location mapping for 'bean' element.You can see this in sample spring.schemas header.So I copy paste the mapping for the same from another file in spring.schemas property file.Then it started working.So be careful when creating a runnable jar in eclipse. 


 


Saturday, August 8, 2015

cvc-elt.1: Cannot find the declaration of element 'beans'-Part1

Generally when working with spring applications,we get the exception cvc-elt.1: Cannot find the declaration of element 'beans'.There are various reasons for this.Usually we get the exception when we run this application in a machine where internet connection is not there.Now we identify some of the cases here and find out the root cause for this.And steps to take to avoid this exception.

To know what the error exactly is we need to know some more terms and definitions.Let's starts with an sample configuration that is a part of the application context.

Sample xml configuration:


<?xml version="1.0" encoding="UTF-8"?>

&ltbeans xmlns="http://www.springframework.org/schema/beans" 

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

    xmlns:task="http://www.springframework.org/schema/task" 

    xsi:schemaLocation="http://www.springframework.org/schema/beans 

                    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

                    http://www.springframework.org/schema/task

                    http://www.springframework.org/schema/task/spring-task-3.1.xsd

                  http://www.springframework.org/schema/context

                  http://www.springframework.org/schema/context/spring-context-3.1.xsd ">

Xml Namespace:

We can say an XML namespace declaration is a key-value pair.The key part of  the  XML namespace is declared using the reserved XML attribute xmlns or xmlns:prefix  and the value part is a URI(Uniform Resource Identifier). Here key part is called namespace prefix and value part is called namespace URI.
Let's analyze some examples of  XML namespace.

Let's consider the second line of the above xml snippet.

 xmlns:beans="http://www.springframework.org/schema/beans"
The above line defines a XML namespace.In our example here  "bean" is the namespace prefix which is a key part and  "http://www.springframework.org/schema/beans" is the namespace URI , which is the value part.

Similary in line

xmlns:context="http://www.springframework.org/schema/context"

"context" is the namespace prefix which is a key part and  "http://www.springframework.org/schema/context"  is the namespace URI , which is the value part.

Similarly in line

xmlns:security="http://www.springframework.org/schema/security"

 "security" is the namespace prefix which is a key part and  "http://www.springframework.org/schema/security"   is the namespace URI , which is the value part.

Ideas behind use of xml namespace:

1.XML namespace is used to group together elements having a common idea.
2.And to distinguish between different  elements  having the same name featuring different semantics.

For example consider a student having name and roll no and some other fields and the college  having name and location and some other fields.How can we distinguish here the name element,as name is available for both the tags student and college?For example

<student>
    <name>Alok</name>
    <rollno>17</rollno>
    <college>
        <name>DKL</name>
        <location>Delhi</location>
    </college>
</student>

We can distinguish between them by creating two unique namespace declaration for each.

Here

<studentxml:student 
xmlns:studentxml="http://www.my.student.com/xml/student" 
xmlns:collegexml="http://www.my.college.com/xml/college">
    <studentxml:name>Alok </studentxml:name>
    <studentxml:rollno>17 </studentxml:rollno>
    <collegexml:college>
        <collegexml:name>DKL</collegexml:name>
        <cityxml:locatio>Delhi</collegexml:location>
        </collegexml:college>
</studentxml:student>
Now there is no ambiguity between two name elements.So in our example xmlns:beans="http://www.springframework.org/schema/beans"  the namespace prefix is beans and namespace URI is http://www.springframework.org/schema/beans.
We can think of namespace prefix  is a short name(or can say an alias) for the complete namespace uri.
Any element or attribute whose name starts with the prefix "beans:" is considered to be in the beans namespace.
Also it is possible to declare a default namespace. This default namespace is like to declare a complete namespace uri without a namespace prefix as follows.
xmlns="http://www.springframework.org/schema/beans"
you can see this line is same as the second line of  the heading sample xml configuration.
In this case, any element without a namespace prefix is considered to be in the beans namespace.

Attributes are never come under the umbrella of  the default namespace. An attribute without an explicit namespace prefix is considered not to be in any namespace.

 By the time we have got a fair understanding of namespace prefix and namespace uri.Now we proceed  to the exception of type 1.

Exception Category 1:

 If we define the namepace prefix and namespace uri in below manner like

<?xml version="1.0" encoding="UTF-8"?>
&ltbeans xmlns:beans="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:task="http://www.springframework.org/schema/task"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                   http://www.springframework.org/schema/task
                  http://www.springframework.org/schema/task/spring-task-3.1.xsd
                 http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-3.1.xsd ">

                   <!-- some more bean declarations goes here -->
       </beans>
We will get the below exception.

Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 11 in XML document from class path resource [META-INF/spring/app-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 78; cvc-elt.1: Cannot find the declaration of element 'beans'.
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:527)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:441)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
    at com.one97.EsmeSimulator.Main.MainClass.main(MainClass.java:8)
Caused by: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 78; cvc-elt.1: Cannot find the declaration of element 'beans'.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)
    ... 14 more

Solution to the Exception Category 1:

Notice here in line no 2 of the namespace declaration, we declare the namespace prefix like
beans xmlns:beans="http://www.springframework.org/schema/beans"
Here we have bounded the relevant namespace uri to a prefix.But we are using the beans tag like.
  </beans>.Here we are using an unprefixed name for the beans element.

This problem has two solutions

solution:1 

Since the uri here is prefixed with namespace prefix bean,so we need to prefix the beans element as 
</beans:beans>.And also we need to do this for all the child elements of bean elements.

solution:2 

As  discussed above , we can declare the beans element as default namespace without any namespace prefix as follows
 beans xmlns="http://www.springframework.org/schema/beans" 
Then no need to add the prefix for beans element and for all it's child elements.

If you find it useful, please follow  the part 2 of the series  to find solutions for this kind of exceptions

Tuesday, August 4, 2015

Auto Login in Spring Security

Once was working in a project having requirement to redirect the user from one web application to other web application , which was deployed altogether in a different server but in same LAN.But both the applications have implemented spring security.Consider we have two web applications. Web application A is deployed in Server A and Web application B is deployed in server B.Two applications have separate log in module implemented in spring security.But each user has same credential details for both the applications. But the requirement here is,  if one user has logged in to application A and wants to redirect to a url in application B,
then application B should not ask for  credentails of the user again.And it should directly redirect to the user to the desired page.

So our requirement here is to transfer user credential  from  application A to application B.That is the user should be logged in to application B automatically with the same username and password without user intervention.We can do it with following configuration.

<sec:authentication-manager alias="authManager">
    <sec:authentication-provider
        ref="userDAOProvider" />
</sec:authentication-manager>

<bean id="daoAuthenticationProvider" 
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider> 

<property name="userDeatilsService" ref="userservice"/>
</property>
</bean>


< bean id="userService" class="com.brainatjava.test.service.impl.UserServiceImpl" > 
< /bean >
This is the configuration manager for spring security.Now we will see the controller in Application B,which will receive the redirect request from Application A.And how it will use the username and password to directly authenticate the user.Please look the code snippet below.

@Controller
Public class AuthenticationController{
 
@Autowired
private AuthenticationManager  authManager;
 
 @RequestMapping(value = "/goToHome", method = RequestMethod.GET)
 public void  goTohome(@RequestParam String username,@RequestParam 
String password,HttpServletRequest request,HttpServletResponse response) 
throws IOException {
  try{
 
  authenticateUser(username,password,request);
 
  }
  catch(Exception e){
   e.printStackTrace();
  }
  response.sendRedirect("home");
 }
private void authenticateUser(String username,String password,
 HttpServletRequest request) {
        
        UsernamePasswordAuthenticationToken authToken = new 
 UsernamePasswordAuthenticationToken(username, password);
        request.getSession();
        authToken.setDetails(new WebAuthenticationDetails(request));
        Authentication authentication= authenticationManager.authenticate
 (authToken);
        SecurityContextHolder.getContext(). 
        setAuthentication(authentication);
    }
}
} 
And as /goToHome is in application B and it will get a hit from application A which is already deployed in a different server,so we will permit it to do so.The same can be done by adding the following line in application context of application B.

  <security:http use-expressions="true" auto-config="true">
                     <security:intercept-url pattern="/goToHome"
                        access="permitAll" />
</security:http>

Here we can see our /goToHome url is a get request.As, we cannot send a POST request using sendRedirect().We can consider encrypting our query string parameters for security issue.