XmlSlurper in Groovy

This class belongs to the  package groovy.util.XmlSlurper. The XmlSlurper is very useful in groovy to handle XML related operations


The constructor XmlSlurper() can be used to create a very loose (non-validating and namespace-aware) instance.

XmlSlurper parsers
The parse method has many flavors. The parse method returns a GPathResult object.

parse(File file) : XML file can be parsed directly
parse(InputSource input) : Parse the content of input source
parse(InputStream input) : Any binary input stream which refers to XML
parse(Reader in): Using a Reader instance
parse(String uri): URI eg: http://www.helicaltech.com/test.xml
parseText(String text): A helper method to parse the given text as XML


This is the key object which helps in navigating the xml object parsed by XmlSlurper

Some important method of the GPathResult
breadthFirst(): Provides an Iterator over all the nodes using a breadth-first traversal.
childNodes(): Returns an iterator over the child nodes
children(): Returns the children of this GPathResult as a GPathResult object.
declareNamespace(Map newNamespaceMapping): Adds the specified map of prefix to namespace mappings to this GPathResult.
depthFirst() :Provides an Iterator over all using depth-first traversal.
find(Closure closure) :Returns the first child matching the condition(s) specified in the passed closure.
name(): Returns the name of element
parent(): Returns as parent nodes
size() : Returns the size of this GPathResult.
text(): Returns the text of this GPathResult as a String.





What is Groovlet?

A Groovlet is a Servlet in Groovy script or in other word Servlets in Groovy

What it does?

The groovlet jar helps us  to automatically compile  .groovy source files into bytecode.
It load the Class and cache it until you change the source file.

What can be done using Groovlet?
Similar to JSP/Servlet in java we can use of implicit variables to access the session, output and request and many other variables

What is MarkupBuilder?
MarkupBuilder(out) binds html
Using the html.html we can output as html

html.html {
head {
body {
p(“Yeh, ${request.remoteHost}: ${session.counter}! ${new Date()}”)

How to Set up groovylets
Add the following to your web.xml:



Sources Groovy Docs

Log File using system.out

Log File using system.out

If you want your System.out.print() output to be logged in the file rather than console, then that could also be possible by little trick.


Before we start,

 :: System - Java Main Class
 :: out - Instance of PrintStream
 :: print() - A public Method

To achieve this we need to change the property of out using System-class.

System.setOut(PrintStream p);

PrintStream  : The PrintStream class provides methods to write data to another stream. The PrintStream class automatically flushes the data so there is no need to call flush() method. Moreover, its methods don’t throw IOException.


package com.helical;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class SystemOutINFile {
    public static void main(String arr[]) throws FileNotFoundException
        // Creating a log file object
        PrintStream printObject = new PrintStream(new File("log.txt"));

        // Storing current System.out in PrintStream before assigning some new value
        PrintStream console = System.out;

        // Assign printObject to output stream
        System.out.println("Whatever will be here in this function, it will go to text file");

       // Reassign the value for output stream
       System.out.println("console output");

Hurray , That’s it….

Thanks for your time , Have a good Day 🙂

Pushpraj Kumar(Helical IT Solutions)



What is ThreadLocal?
There are different scope of a variable in java.
1. Local Scope : This scope includes the variable declared inside the methods.
2. Instance Scope: This scope is also known as instance variable. This is created one per instance.
3. Class Scope: The variable is created one per class. They are generally static
4. Thread Scope: The variable is available in thread scope using ThreadLocal.

ThreadLocal allows the java developer to store any variable or data in Thread scope. The treadlocal will take care that the data is only visible to thread. ThreadLocal thus helps to manage threadsaftey for an object/pojo across the thread without using the synchronous keyword.

How to keep data in ThreadLocal?
ThreadLocal provides set() method to store the data, and get() method to retrive the data. Using the getter and setter the manipulation can be done quite easily.

Generally ThreadLocal instances are private static fields in classes (e.g.,we may associate a user ID or Transaction ID etc).
Frameworks generally use threadlocal to store context related data.
Using Generics in ThreadLocal.
After java 1.5 we can use Generic notation in the ThreadLocal declaration.
eg. ThreadLocal<String> threadLocal = new ThreadLocal<String>();

Mehods in ThreadLocal.
1. protected T initialValue() : This can be used to set the intial value of threadlocal other than null.
2. public T get() : This is the getter method
3. public void set(T value): Setter method to set the value
4. public void remove(): This method clears the data from the thread local



Sample Code to illustrate the ThreadLocal

package com.helical;

import com.helical.model.User;

public class ThreadLocalExample {

    private static ThreadLocal threadContext = new ThreadLocal();

    public User getUser() {
        User user = threadContext.get();
        if (user == null) {
            throw new UserDoesNotExistsException("There is no user inside the ThreadLocal");
        } else {
            return user;


    public void setUser(User user) {
        User localUser = threadContext.get();
        if (localUser != null) {
            throw new UserExistsException("There is already  a user in ThreadLocal");
        } else {

    public void clearThreadLocal() {

    private class UserExistsException extends RuntimeException {
        public UserExistsException(String s) {


Java Naming and Directory Interface (JNDI)

Using JNDI, applications running in distributed environment, helps to look up services independently.

How JNDI is useful?

  1.  During deployment sequence, the same JNDI name can be used in different servers.
  2.  It helps to provide security of credential leakage to the user groups like developer, testers, stagers etc, because the credentials are configured in the server not in the application.
  3.  Connection Pooling can be achieved

How to use JNDI?
In order to use, JNDI classes and service providers should be there
The service provider may be :

  1. Lightweight Directory Access Protocol (LDAP)
  2. Common Object Request Broker Architecture (CORBA)
  3. Common Object Services (COS) name service
  4. Java Remote Method Invocation (RMI) RegistryThe objects needs to be created and registered so that , they can be further used for lookup.

How to configure JNDI in tomcat?
In tomcat environment we can configure JNDI in 3 different places
1. Inside the application using context.xml in META-INF directory.
2. Inside the tomcat server context.xml : Inside the apache-tomcat/conf directory we can configure. This can be helpful when multiple application needs the same database.
3. Both in server.xml and context.xml

NB: The database driver jar should be present in the tomcat lib directory

<Resource name="jdbc/lookupName" 


In order to use it in java program we have to use the followng
DataSource ds = (DataSource) ctx.lookup(“java:/comp/env/jdbc/lookupName”);

Lucene Search

Introduction  to lucene api

  1. Lucene is a text search engine java api library.
  2.  It majorly helps in indexing and searching.
  3.  It searches by calculating a score for each document and gives the result based on the score that is more accurate.
  4. Document can be searched using the search phrase, wildcard characters , range query.
  5. Lucene is high-performance and scalable.

Some of the important classes in Lucene API are

Analyzer:- This abstract class main task is to extract tokens for indexing.
Some implementing classes are StandardAnalyzer.

IndexWriter:- The index-writer helps in creating indexes and write the indexes to file inside a directory that is passed as parameter.

Fields: A document can be broken down into multiple fields. The fields are attribute that can store some values.
Document:- This class contains collection of Fields. It helps in creating a virtual document. When looping through a directory whose files have to be indexed, we can construct the many fields object for a single file  and then put in the document object. This document object is then passed to the IndexWriter, which helps indexWriter to create indexes for that specific file.
IndexReader: This class can be used to open an existing indexes from indexDirectory and helps in searching with a user provided search pharase.
QueryParser: The query parser uses the file content as input and find a match.

Query : QueryParser returns a Query object after parsing the query-string.

To execute the query and find the actual document contents we pass the query object to IndexSearcher.search() method.

TopDocs object is returned from the IndexSearcher.search() method, it contains collection of the searched docs (scoreDocs)
ScoreDoc.:The scoreDoc can be used to convert the searched document to Indexed Doc.
The pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">





import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

public class LuceneIndexingAndSearchingExamples {

    private static final String indexStoreDirectory = "E:/search/indexes";
    private static final String directoryToIndex = "E:/samples/documents";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your search phrase\n");
        String searchText = scanner.nextLine();

    private static void indexDirectory() {
        try {
            Directory directory = FSDirectory.open(Paths.get(indexStoreDirectory));
            IndexWriterConfig configuration = new IndexWriterConfig(new SimpleAnalyzer());
            IndexWriter indexWriter = new IndexWriter(directory, configuration);

            //Delete all indexes if exits
            File directoryToIndexFile = new File(directoryToIndex);
            processEachFile(indexWriter, directoryToIndexFile);
        } catch (FileNotFoundException fne) {
            System.out.println("The sample folder is not found");
        } catch (IOException ioe) {
            System.out.println("There was some problem in i/o operation");

    private static void processEachFile(IndexWriter indexWriter, File directoryToIndexFile) throws IOException {

        File[] files = directoryToIndexFile.listFiles();
        if (files == null) {
            throw new RuntimeException("Directory " + directoryToIndex + " is empty/not found");
        for (File eachFile : files) {
            Document virtualDocument = new Document();
            virtualDocument.add(new TextField("path", eachFile.getName(), Store.YES));
            StringBuilder builder = getContents(eachFile);
            virtualDocument.add(new TextField("contents", builder.toString(), Store.YES));

    private static StringBuilder getContents(File eachFile) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(eachFile)));
        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
        return builder;

    private static void searchFromIndexes(String text) {
        try {
            Path path = Paths.get(indexStoreDirectory);
            Directory directory = FSDirectory.open(path);
            IndexReader indexReader = DirectoryReader.open(directory);
            IndexSearcher indexSearcher = new IndexSearcher(indexReader);
            QueryParser queryParser = new QueryParser("contents", new StandardAnalyzer());
            Query query = queryParser.parse(text);
            TopDocs topDocs = indexSearcher.search(query, 10);
            printOutput(indexSearcher, topDocs);
        } catch (IOException ioe) {
        } catch (ParseException e) {

    private static void printOutput(IndexSearcher indexSearcher, TopDocs topDocs) throws IOException {
        System.out.println("Total Matches found " + topDocs.totalHits);
        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            Document virtualDocument = indexSearcher.doc(scoreDoc.doc);
            System.out.println("Path of document : " + virtualDocument.get("path"));
            System.out.println("\n=================\nContents \n=================\n" + virtualDocument.get("contents"));

Apache Lucene 5.1.0 indexing and searching java example

Groovy Helpful Tips

Groovy Helpful Tips

Here are some useful information when using groovy

Single Line comment in Groovy
// We can comment using // in groovy as in Java.

Multi Line comment in Groovy
/* Multi-line comment style is same as in Java */

Documentation comment in Groovy
* Similar to Java commenting Style

Shebang style is helpful when running Groovy script directly from linux system
#!/usr/bin/env groovy

as and trait are keyword in Groovy

”’ Triple quoted string help to write strings in multiple line eg.
def multiLineString = ”’
line one
line two
line three

String interpolation

def name = ‘Helical’ // a plain string
def greet = “Hello ${name}” //This will result in Hello Helical

List in groovy.

by default list are ArrayList
def numbers = [1, 2, 3]

To define a Linked List
def linkedList = [2, 3, 4] as LinkedList //Note the usage of as keyword

To define map

def capitalInfo = [state: ‘Delhi’, district: ‘South Delhi’, pin: ‘110019’]

capitalInfo[‘metroStation‘] = ‘Nehru Place’ //adds a new key in the map

Elvis operator
The “Elvis operator” is a shortening of the ternary operator.
userName = user.name ? user.name : ‘Guest’ //classic way
userName = user.name ?: ‘Guest’ //elvis way

Ways to execute python code from Java

There are many ways to execute Python code from with in Java. In case if your project has requirement to
execute Python code from Java, here are few code samples that I have collected from Internet.

Ways to use Python in Java Code

1. Using Jython ScriptContext:

Python code that is written in a file which can be executed using the Jython ScriptContext.
The following snippet uses the file named numbers.py and gets the output printed to the standard out put in python.

     StringWriter writer = new StringWriter();
     ScriptEngineManager manager = new ScriptEngineManager();
     ScriptContext context = new SimpleScriptContext();
     ScriptEngine engine = manager.getEngineByName("python");
     engine.eval(new FileReader("/home/numbers.py"), context);

2. Second way of using Jython:

In this approach PythonInterpreter object using which we can execute Python code.

        Properties properties = System.getProperties();
	PythonInterpreter.initialize(properties, properties, new String[0]);
	PythonInterpreter interp = new PythonInterpreter();
	interp.set("firstName", args[0]);
	interp.set("lastName", args[1]);

Another way of using PythonInterpreter

Using python code directly in the Java Programs. Here the python code is executed using the python interpreter written in Java.

        PythonInterpreter python = new PythonInterpreter();
	int number1 = 10;
	int number2 = 32;
	python.set("number1", new PyInteger(number1));
	python.set("number2", new PyInteger(number2));
	python.exec("number3 = number1 + number2");
	PyObject number3 = python.get("number3");
	System.out.println("Val : " + number3.toString());

3. Invoking native python interpreter using Java:

Using Runtime class or ProcessBuilder class of Java we can invoke the python interpreter directly
and pass the file that consists of the python code as an argument.
Here one should have Python installed on their machine and be available in the PATH.

      Process process = Runtime.getRuntime().exec("python numbers.py " + number1 + " " + number2);//OR
      ProcessBuilder builder = new ProcessBuilder("python", "test1.py", " " + number1, " " + number2);

Using ProcessBuilder API is more preferred approach than the Process.

Hope the samples have helped you in getting some insights.




What is..?

Jenkins is a tool (web application ) that helps in continuous integration of projects. It is open source and has capability to manage any types of build software. Jenkins can be seamlessly integrated with any testing and deployment environment.

Continuous Integration.

In software developement life cycle, there are various pahses of the software developement. In developement there may be checking in the code, modifying the code, testing, deployment , bug fixes etc. Continous Integration is a practise that requires developers to integrate code into a shared repository at regular intervals. The code can be checked in from various central repository such as GIT, SVN, CVS. They may be modified as per the requirements. Continious Integration is a practise that whenever a code commit occurs, the build process should be triggered.

How to get jenkins.

Jenkins can be downloaded from jenkins.io. Then it may be run from the command line using the java -jar command. It may be put in the application container such as tomcat, jboss, weblogics etc.

Once the Jenkins application is deployed in the machine, it has to be configured with the project repository, build tool (maven, ant etc) build time, testing configuration , email configuration etc.

After the above configuration is set , Jenkins create a disk-space to store all the projects, dependencies etc in a directory ~/.jenkins (default location). We may also change the location by setting the JENKINS_HOME environment variable before launching the servlet container.


Email Notification.

Jenkins also sends the email notification when any deployment event occurs. These email feeds can be managed in the email notification area. There is a provision to configure the SMTP settings for sending out emails.



Jenkins ships in with many plugins to build project, manage project, add security, add user and organization level profile, etc We may also add plugins build for jenkins systems.

Manage Plugins screen helps to install, update and remove plugins.


Groovy Language

  • Groovy can run on JVM
  • Groovy scripts can be executed using groovy jar file.
  • Annotations are supported in groovy.

Groovy installation.

  • Binary download link http://www.groovy-lang.org/download.html
  • The binary can be run from command prompt
  • Set GROOVY_HOME, GROOVY_HOME\bin variable as enviroment variable.
  • After installing and running in the command line you get the groovy shell(groovysh).
  • groovy> println ” hello helical”

Source code in groovy.

  • The extension for groovy source file is .groovy
  • We can write either script or we may also encapsulate the code as class definition inside this file.
  • By default groovy class is subclassed by java.lang.Object.
  • Java class and its objects can access groovy script class and object and vice versa.
  • Using groovyc we can compile the source code.
  • Plain Old Groovy Objects (POGO).

Intersting facts about groovy.

  • Groovy doesnot require ; at the end of statement.
  • Last expression of the method is returned by default. So we may not explicitly use return keyword.
  • Maps, list and regular expression readily available in groovy.
  • The following package are readily available. (groovy.lang.*, groovy.util.* , java.lang.*, j ava.util.*, java.net.*, java.io.*, java.math.BigInteger, java.math.BigDecimal)
  • Its classes and methods are by-default public
  • Automatic getter and setter is created for the fields in groovy class.
  • Unlike java == operator in groovy checks for contents.
  • The is() may be used to check if two variables/object refer the same.
  • The parameters inside a function are optional.
  • Gpath is expression language to evaluate nested structure.

Data types in Groovy.

    • The keyword def is used to define variable. We can also use specific type for variable declaration. Example int marks, String company. etc
    • Range data type is a Collection. Ranges consists of two values separated by two dots.
for (i in 0..9) {
  println ("Hello $i")

assert 'L'..'P' == ['L', 'M', 'N', 'O','P']

String & Gstring.

  • ‘Strings may be enclosed inside single quote ‘.
  • “Mehod() calls and ${variable} substitution inside a string is known as Gstring”.
  • ”’Tripple single quotes can be used for multi line string”’
  • “”” Multi line Gstring can be enclosed in tripple quotes”””
  • /regularexpression/

Methods in Groovy.

  • Groovy supports operator overloading. – operator can be used to substract string, << can be used for string concatination.
  • Groovy string has .toUrl() method that can be directly used to convert a string to Url encoding string.



package packageName
class ClassName {
  static void main(def args){
    def hetroList= [1,2,"hi","45"]
    hetroList.each{ println it }