Categories
Java Java Bean Validation

Dependencies

We are going to use Maven as build tool, we will discuss this in its context.

  1. As per JSR380 specification of set of APIs provided by following
<dependency>

    <groupId>javax.validation</groupId>

    <artifactId>validation-api</artifactId>

    <version>2.0.1.Final</version>

</dependency>

2. Hibernate validator is reference implementation for above specification.

<dependency>
	    <groupId>org.hibernate.validator</groupId>
	    <artifactId>hibernate-validator</artifactId>
	    <version>6.0.13.Final</version>
</dependency>

3. Expression language dependencies to be able to write expressions in validation messages.

<dependency>
	    <groupId>org.glassfish</groupId>
	    <artifactId>javax.el</artifactId>
	    <version>3.0.0</version>
</dependency

Categories
Java Java Bean Validation

Why java bean validation

Validation is often a basic requirement for any kind of application, be it user input over web page, desktop application, mobile application or be it schematic constraints at database level or compliance of XML with respect to XSD.

Validations are implemented in several ways, at various levels in technology stack from User interface to database schema. We may restrict that “User Name” field must not be empty on login form, we may restrict that “userName” column not be empty in database table, we may restrict that in XSD that input xml tag “UserName” shouldnt be empty. Apart from this several frameworks provide their own ready to use configurable mechanisms, struts provides Validator interceptors, Hibernate provide its own annnoation over bean properties corresponding to reflect database schema.

Java bean validation can be just another approach, for several reasons. Because It is easy, because POJO java beans are used in almost all frameworks so portability ensured, or because it reduces boilerplate codes which need not be written, or because it can be looked upon as alternative to avoid to database schema, XSD xml schema or User Interface, to give performance improvement choice.

Java bean validation needs Java8 or higher version. This specification is defined by JSR380. There can be several implementations for this specification.

Categories
Ant Build Script

Documentation

1 . ant -help

This command executed in command prompt gives helpful information about what are all parameters present to execute the ant command.

2. Ant official Documentation : https://ant.apache.org/manual/index.html

Above link provides detailed developers documentation about all capabilities of ant build. It can give detailed documentation about syntax, procedures, what are all in built tasks available, how to create customized tasks, how to configure Ant with various development IDEs etc.

Categories
Ant Build Script Uncategorized

Setting Properties

In previous tutorial, we have set several paths hardcoded in buildConfig configuration file. However Ant provides capability to either declare those values as properties in configuration file, load values from resource file or pass resource key-value pairs from command line. We will use same buildConfig file to modify it.

1.Declare Values as Properties in configuration file:

We have cleanLogs target in buildConfig file, which is receiving directory path to delete files within. Lets declare that path as property value in beginning of path, like below

<property name="log.dir" value="d:/logs"/>

and target definition would become

    <target name="deleteLogs" depends="stopOracle" 
		description="Clean Logs directory">
      <delete>
         <fileset dir="${log.dir}">
            <include name="*"/>
         </fileset>
      </delete>
	</target>
  
     
     

Lets run the ant target

2. Read values from build.properties

Lets prepare a file named build.properties, which would host key value pairs. We have compileSource target, which receives several paths as hardcoded

<target name="compileSource">
	<javac
    srcdir="D:/Learning/11JAVA XML/1. javax.xml.bind/workspace/01HelloWorld/src"
    destdir="D:/Learning/11JAVA XML/build/classes"/>
	
	<jar destfile="D:/deployModule/lib/util.jar"
			basedir="D:/Learning/11JAVA XML/build/classes"
			includes="*"
	>
	</jar>
	</target>

Lets move all paths in build.properties in form of key-value pairs.

src.dir=D:/Learning/11JAVA XML/1. javax.xml.bind/workspace/01HelloWorld/src
build.class=D:/Learning/11JAVA XML/build/classes
jar.file=D:/deployModule/lib/util.jar

build.properties has to be declared at beginning as property file like below

<property file="build.properties"/>

Target definition “compileSource” would become like below

<target name="compileSource">
 <javac
    srcdir="${src.dir}"
    destdir="${build.class}"/>
	
 <jar destfile="${jar.file}"
			basedir="${build.class}"
			includes="*"
	>
	</jar>
</target>

3.Property passed from command line :

We have target definition to start the oracle database service “startOracle”, wherein database instance service name is hardcoded.

  <target name="startOracle">
		<exec executable="cmd">
			<arg value="/c"/>
			<arg value="net start OracleServiceORCLDB"/>
		</exec>
   </target>

Here we can configure such that we receive service name from command line. Hence target definition would become like this

<target name="startOracle">
		<exec executable="cmd">
			<arg value="/c"/>
			<arg value="net start ${service.name}"/>
		</exec>
    </target>

Now we can pass service.name value as command line parameter like below

cmd>ant -buildfile buildConfig.xml -Dservice.name=OracleServiceORCLDB startOracle

Hence we will receive output as

Now we have completely modified file buildConfig.xml as below

<?xml version="1.0"?>
   <project name="AntDemo" default="hello">
   
   <property name="log.dir" value="d:/logs"/>
   <property file="build.properties"/>
   
   <target name="hello">
      <echo>Hello World - Welcome to Apache Ant!</echo>
   </target>
   
  <target name="stopOracle">
		<exec executable="cmd">
			<arg value="/c"/>
			<arg value="net stop ${service.name}"/>
		</exec>
   </target>
   
	<target name="startOracle">
		<exec executable="cmd">
			<arg value="/c"/>
			<arg value="net start ${service.name}"/>
		</exec>
    </target>
   
   
    <target name="deleteLogs" depends="stopOracle" 
		description="Clean Logs directory">
      <delete>
         <fileset dir="${log.dir}">
            <include name="*"/>
         </fileset>
      </delete>
	</target>

	<target name="compileSource">
	<javac
    srcdir="${src.dir}"
    destdir="${build.class}"/>
	
	<jar destfile="${jar.file}"
			basedir="${build.class}"
			includes="*"
	>
	</jar>
	</target>
</project>

we have additional build.properties file below

src.dir=D:/Learning/11JAVA XML/1. javax.xml.bind/workspace/01HelloWorld/src
build.class=D:/Learning/11JAVA XML/build/classes
jar.file=D:/deployModule/lib/util.jar

Categories
Ant Build Script Uncategorized

Ant Target

Target can be seen as module of tasks in ant configuration file. We can request ant to execute specific target, by passing command line input. Each target may have one or more tasks. Following is sample buildConfig.xml

<?xml version="1.0"?>
   <project name="AntDemo" default="hello">
   <target name="hello">
      <echo>Hello World - Welcome to Apache Ant!</echo>
   </target>
   
  <target name="stopOracle">
		<exec executable="cmd">
			<arg value="/c"/>
			<arg value="net stop OracleServiceORCLDB"/>
		</exec>
   </target>
   
	<target name="startOracle">
		<exec executable="cmd">
			<arg value="/c"/>
			<arg value="net start OracleServiceORCLDB"/>
		</exec>
    </target>
   
   
    <target name="deleteLogs" depends="stopOracle" 
		description="Clean output directories">
      <delete>
         <fileset dir="d:/logs">
            <include name="**/*.class"/>
         </fileset>
      </delete>
	</target>

	<target name="compileSource">
	<javac
    srcdir="D:/Learning/11JAVA XML/1. javax.xml.bind/workspace/01HelloWorld/src"
    destdir="D:/Learning/11JAVA XML/build/classes"/>
	
	<jar destfile="D:/deployModule/lib/util.jar"
			basedir="D:/Learning/11JAVA XML/build/classes"
			includes="*"
	>
	</jar>
	</target>
</project>

Exec as Target : please refer target named “stopOracle” having exec as task tag inside. It contains executable command to stop oracle service. if we pass this as target it will stop oracle databse service named “OracleServiceORCLDB”. Similarly we have “startOracle” to start the oracle database instance service.

Dependent Target : Target “deleteLogs” is having dependency on “stopOracle” target. Hence if we execute “deleteLogs” target, it will first invoke “stopOracle”, then its own tasks are executed.

Multiple Tasks : If we want two or more tasks to be executed together as single module, multiple tasks can be mentioned in single target. Target “compileSource” contains the two tasks compile .java files and also create the jar of compiled classes.

Categories
Ant Build Script Uncategorized

Execute Ant

It is xml based configuration file.

Following is HelloWorld configuration file “buildConfig.xml”. File can have any name. Open command prompt and copy file in current directory.

<?xml version="1.0"?>
   <project name="AntDemo" default="hello">
   <target name="hello">
      <echo>Hello World - Welcome to Apache Ant!</echo>
   </target>
</project>

Execute following Ant command

>ant -buildfile buildConfig.xml hello

As seen in command, we are invoking ant.bat having two command line parameters -buildFile and target to execute in the build file.

In the buildConfig.xml target “hello” is mentioned and invoked. Target “hello” consists of single in-built task echo to print the input message in config file.

It prints output on console by default. Each target is executed & printed on console. Here “hello:” target is executed & printed on console its output that is echo message. In the end it says “BUILD SUCCESSFUL” with return code 0.

Ant has several optional input parameters, that can be seen using following command

cmd>ant -help

Categories
Ant Build Script Uncategorized

Install Ant

  • Download Ant distribution from https://ant.apache.org/bindownload.cgi
  • Check that compatible java version is already installed on local machine & environment variable JAVA_HOME is already set. Following commands can help to check that.
  • Unzip the Ant downloaded distribution package
  • Set environment variable ANT_HOME pointing to base directory of unzipped ant distribution.
  • Add new entry in PATH environment variable, to add path of ant bin directory.
  • To check if ant is successfully installed, open command prompt and type command “ant -version”, it should show installed version of ant.
Categories
Ant Build Script

What is Ant

Ant is open source build framework provided by Apache Foundation, based on Java. At can help us to automate day to day tasks mainly for software build procedure. It can also serve as developer tool to automate day to day mundane tasks like start-stop services, delete logs, update database, codebase etc.

Ant takes XML configuration file as input build File. Build file contains the tags based instructions on what & how to perform certain tasks.

Build file may mention more than one target definitions. Each target may have one or more tasks to perform. Target can be passed as another input to Ant script, to instruct which target to perform. Target may have dependency on some other targets. In such case, dependency targets are executed first, followed by tasks in current target.

Each target may have one or more tasks to perform like copy files, delete files, compile, execute etc. Tasks are in built tag definitions of Ant. Ant provides rich set of inbuilt tasks viz copy, delete, echo, exec etc. Those in built tasks can be used passing attributes as their inputs. Also customized ant tasks can also be developed.

XML based build file may be viewed as static configuration. For dynamic configurations, input values can be passed through command line arguments or resource files.

Categories
Scripts Windows Batch

Functions

In general, Function is a block of code which can be called multiple times from any point in program. Function may receive input and it may return output after processing.

  1. Function with No input and no output returned
::To demonstrate the functions

@echo off


::call to function
call :helloworld
::return after function call
exit /B %ERRORLEVEL% 



::function definition starts
:helloworld
echo Hello World
pause 
exit /B 0
::function definition ends




Output

2. Function that receives input but returns no output

::To demonstrate the functions

@echo off


::call to function with 2 parameters
call :WishMessage Peter Indiana
exit /B %ERRORLEVEL%


::Function definition starts
:WishMessage
::%~n is used to refer input parameters
echo Hello Good Morning %~1 %~2 
pause 
exit /B 0
::Function definition ends


output

3. Function that receives input and returns some output

::To demonstrate the functions

@echo off



call :WishMessage Peter Indiana value1
echo today is %value1%
exit /B %ERRORLEVEL%



:WishMessage
echo Hello Good Morning %~1 %~2
set value1=Monday 
pause 
exit /B 0

Output

Categories
Scripts Windows Batch

Return Code

10ReturnCodes.bat

::This program is used to demonstrate the return statement
::Every comamnd or batch file returns an integer to indicate result of program
:: if returned code is XERO, in general it is agreed as succesful execution
:: in case of error it supposed to return NON-ZERO integer, to indicate error specific

@echo off

:: ERRORLEVEL is in-built variable which is popped up with return code from last executed command or program
ping foo.com
if %ERRORLEVEL%==0 (echo foo ping was successful) else (echo foo ping was having issue)
 
ping google.com
if %ERRORLEVEL%==0 (echo google ping was successful) else (echo google ping was having issue)


echo "program will exit with 0 return code, after you press enter"
pause

::at time of return, program should return with error code
exit 0

Output

Design a site like this with WordPress.com
Get started