Thursday, March 19, 2015

STRUTS2 CUSTOM VALIDATORS DEMO CODE EXAMPLE

GreaterThanZeroValidator.java

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

public class GreaterThanZeroValidator extends FieldValidatorSupport {

    @Override
    public void validate(Object object) throws ValidationException {

String fieldName = getFieldName();
Integer fieldValue = (Integer) getFieldValue(fieldName, object);

if (fieldValue != null && fieldValue <=0 ) {
   addFieldError(fieldName, object);
}  
    }
    
}

Validators.xml (also put all default validator in this file)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator Definition 1.0//EN"
        "http://struts.apache.org/dtds/xwork-validator-definition-1.0.dtd">

<validators>
    <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
    <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
    <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
    <validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
    <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
    <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
    <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
    <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>
    <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>
    <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>
    <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>
    <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
    <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>
    <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
    <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>
    <validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>

    <validator name="greaterThanZero" class="com.validator.GreaterThanZeroValidator" />

</validators>

ABCAction-validation.xml
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">

<validators>

<field name="pkgData.validityPeriod" >
<field-validator type="greaterThanZero" short-circuit="true">
<message>${getText("error.greaterthanzero", {getText("pkg.validityperiod")})}</message>         </field-validator>
<field>

ABCAction.java

public class ABCAction extends ActionSupport implements   ServletRequestAware{

private static final long serialVersionUID = 1L;
private static final String MODULE  = PkgCTRL.class.getSimpleName();
private static final String PACKAGE = " Package ";

private PkgData pkgData = new PkgData();
.......
.......
.......
.......

}

LIBS (keep the required libs only)

validators.xml file location (means it should come in classpath)

helpful link : 

http://struts.apache.org/docs/validation.html#Validation-RegisteringValidators
(read above link content fully with patience )

Tuesday, March 10, 2015

CSS to center aling text of last and cecond last TD of all rows in a table

td:nth-last-child(-n+2) {       
             text-align: center;

}

CSS to center aling text of last and first TD of all rows in a table

        tr>td:last-child {                       
            text-align: center;
        }
       
        tr>td:first-child {                       
            text-align: center;
        }

Monday, March 9, 2015

JAVA CODE TO GET ALL THE DETAIL OF JVM AND O.S

import java.util.*;
class JavaProps {
public static void main(String[] args) {
Properties p = System.getProperties();
p.setProperty("TestKey", "TestValue");
p.list(System.out);
}

}

COMPILE AND RUN

D:\TestWork\JavaCode>javac JavaProps.java

D:\TestWork\JavaCode>java -Draj=sonofking JavaProps

-- listing properties --
raj=sonofking
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=C:\Program Files\Java\jre8\bin
java.vm.version=25.0-b70
java.vm.vendor=Oracle Corporation
java.vendor.url=http://java.oracle.com/
path.separator=;
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg=sun.io
user.script=
user.country=IN
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=Service Pack 1
java.vm.specification.name=Java Virtual Machine Specification
user.dir=D:\TestWork\JavaCode
java.runtime.version=1.8.0-b132
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=C:\Program Files\Java\jre8\lib\endorsed
os.arch=amd64
java.io.tmpdir=C:\Users\KIRPAL~1.RAJ\AppData\Local\T...
line.separator=

java.vm.specification.vendor=Oracle Corporation
user.variant=
os.name=Windows 7
sun.jnu.encoding=Cp1252
java.library.path=C:\windows\system32;C:\windows\Sun\Ja...
java.specification.name=Java Platform API Specification
java.class.version=52.0
sun.management.compiler=HotSpot 64-Bit Tiered Compilers
os.version=6.1
user.home=C:\Users\kirpalsinh.raj
TestKey=TestValue
user.timezone=
java.awt.printerjob=sun.awt.windows.WPrinterJob
java.specification.version=1.8
file.encoding=Cp1252
user.name=kirpalsinh.raj
java.class.path=.
java.vm.specification.version=1.8
sun.arch.data.model=64
java.home=C:\Program Files\Java\jre8
sun.java.command=JavaProps
java.specification.vendor=Oracle Corporation
user.language=en
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode
java.version=1.8.0
java.ext.dirs=C:\Program Files\Java\jre8\lib\ext;C:...
sun.boot.class.path=C:\Program Files\Java\jre8\lib\resour...
sun.stderr.encoding=cp437
java.vendor=Oracle Corporation
file.separator=\
java.vendor.url.bug=http://bugreport.sun.com/bugreport/
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.stdout.encoding=cp437
sun.desktop=windows
sun.cpu.isalist=amd64

Valid declarations for main() method


1) static public void main(String[] args)
2) public static void main(String... x)
3) static public void main(String bang_a_gong[])

Note: One can overload the main method but cannot override it.

Sunday, March 8, 2015

Java Threads key points to Remember

1) Thread do not come out of a waiting pool just because a lock has been released.

2) Thread must own the lock on the object that wait() is being invoked on. 

3) notifyAll() notifies all the threads waiting on a particular locked object,
    not all threads waiting on any object.

4) when synchronized instance methods are called on the same instance, they block each 
    other.   

5) synchronized static methods in the same class block each other, regardless of which      
    instance was used  to call the methods.

6) synchronized instance methods called on different instances do not block each other

7) The join() must be placed in a try/catch block.

8) wait(), notify(), notifyAll() must be call from within the synchronized area

9) A thread is done being a thread when its target run() method completes.

10) A thread can call the start() method only once in its lifetime. If start() is called
      more than once on a Thread object, it will throw a RuntimeException

11) sleep(), join() and wait() method throws InterruptedException

12) join(), setPriority(), wait(), notify() and notifyAll() are final method.

13) sleep() and yield() are static methods.

14) thread states are new, runnable, running, waiting/block/sleeping, dead

15) yield() method is supposed to do is make the currently running thread back to runnable 
      to allow other threads of  the same priority to get their turn.

16) A yield() won't ever cause a thread to go to the waiting/sleeping/ blocking state.

17) If possible then the synchronized code should be kept minimum.

18) A static synchronized method and a non-static synchronized method will not block each 
      other, ever.

19) Threads calling static synchronized methods in the same class will always block each 
      other—they all lock on the same Class instance.

20) Threads calling non-static synchronized methods in the same class will only block each 
      other if they're invoked using the same instance.

What will Happens when a Thread Can't Get the Lock on the required Object ?

When a thread tries to enter into a synchronized method and if the lock is already taken by another thread , then the thread trying to get the lock will be blocked on the object's lock. 
and the thread goes into a kind of pool for that particular object and sits there until the lock is released and the thread CAN again become runnable/running thread when the lock released on the object. but if there are many thread waiting for the objects lock then we cant say which thread will get a chance to occupy the lock on  the object

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...