Sunday, 28 January 2018

How To + SpringBoot + Embedded Tomcat + JNDI + URL + File Resources

If you are building your application in SpringBoot with Embedded Tomcat and looking for reading property files using JNDI, this post may help you.

Let say you have an environment property file which you want to keep out of your web package  and it should be referred/read in your application using JNDI URL resource. You can follow below steps to achieve same.

1. Create the property file which you want to read using JNDI URL resource.

EnvPropFile
mykey=myvalue

2. Create URLFactory class which should implement ObjectFactory interface.

URLFactory.java
package com.xxx.example;

import java.net.URL;
import java.util.Hashtable;
import javax.naming.*;
import javax.naming.spi.ObjectFactory;

public class URLFactory implements ObjectFactory {
    public Object getObjectInstance(Object obj, Name name,
       Context nameCtx, Hashtable environment) throws Exception {
       Reference ref = (Reference) obj;
       String urlString = (String) ref.get("file").getContent();
       return new URL(urlString);
     }
}


3. Add below code in your spring boot application class that should initialize TomcatEmbeddedServletContainerFactory bean. Here in this bean, under postProcessContext() method, you can define JNDI file resources as mentioned below.  Factory class should be same that you created in step #2.

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
            
  return new TomcatEmbeddedServletContainerFactory() {

     @Override
     protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                             Tomcat tomcat) {
         tomcat.enableNaming();
         return super.getTomcatEmbeddedServletContainer(tomcat);
     }

     @Override
     protected void postProcessContext(Context context) {
       ContextResource fileRes = new ContextResource();
       fileRes.setName("url/EnvPropFile");
       fileRes.setType(URL.class.getName());
       fileRes.setProperty("factory","com.xxx.example.URLFactory.java");
       fileRes.setProperty("file","file:///C:/your-path/EnvPropFile");
       context.getNamingResources().addResource(evolutionEngineConfigResource);                                         
      }
   };
}

4. Define the URL bean which should return URL using resource name lookup. Keep 'java:comp/env' as it is and add your resource name as mentioned above.


import java.net.MalformedURLException;
import java.net.URL;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;

@Configuration
public class ConfigFileConfiguration {

 @Bean(name="envPropFile", destroyMethod = "")
  public URL envPropFile() throws MalformedURLException, NamingException{
                              InitialContext context = new InitialContext();
                              return (URL) context.lookup("java:comp/env/url/EnvPropFile");
   }
}


5. Now autowire this URL bean in other spring component where you want to access the properties. Note that, in actual production code, file initialization should happen only once. 

@Component
public class MyService {
  
    @Autowired
    private URL envPropFile;

    public   void pringEnvPropFileKey() throws Exception{
    
      Properties configHolder = new Properties();
      
      final URLConnection conn = envPropFile.openConnection();
      final InputStream inputStream = conn.getInputStream();
      configHolder.load(inputStream);
   
      String value = configHolder.getProperty("mykey"); // This is the key mentioned in prop file
   
      System.out.println(value);

   }
}

I think that's pretty much what you need to do to read file using JNDI in spring boot embedded tomcat application.To get more detail specific to tomcat and it's JNDI resources you can refer this link.

Tuesday, 26 December 2017

TechTip - SVN to GIT Migration - Renaming/Refactoring Projects

During SVN to GIT Migration, I have faced one challenge that is renaming/restructuring projects while migration.

Let say, you have following maven project in SVN

old-project
-- src
-- pom.xml

Now you want this project to be renamed while migrating to GIT and it should look like below:

new-project
-- src
-- pom.xml

If you try changing this name while cloning projects from SVN to GIT just like below. You will loose history after migration. But you must think about solution where history remain as it is but your project can be renamed or restructured.

git svn clone --trunk=/trunk --branches=/branches --branches=/bugfixes --tags=/tags --prefix=origin/ --authors-file= svn-committer.txt  svn://xyz.com/SRC/Trunk/old-project new-project

How to do it? 

Actually, there is no rocket science I have applied here. Its pretty simple. You just need to refactor/rename your projects in SVN itself. The good part is, SVN still maintains full history even after renaming the folder names while copying from one place to another. This is really one of the great features in SVN that I will miss in GIT for sure.

First copy your project in SVN itself with required name using below command

svn copy <source-svn-url>   <destination-svn-url> -m "Renamed project name"


Example:

svn copy svn://xyz.com/SRC/Trunk/old-project  svn://xyz.com/SRC/GIT-SRC/new-project -m "Renamed project name from old-project to new-project"

Do whatever refactoring you want to do with your project in SVN itself. Now once you are ready with structure of your  project which must be there in GIT as repository, you can use your refactored location to clone project.

git svn clone --trunk=/trunk --branches=/branches --branches=/bugfixes --tags=/tags --prefix=origin/ --authors-file= svn-committer.txt  svn://xyz.com/SRC/GIT-SRC/new-project new-project

After migration in GIT, you can see full change history of old-project including message 'Renamed project name from old-project to new-project' that you just did while renaming the project in SVN itself from old to new location.

Renaming/Refactoring was a big challenge but with this simple approach I could successfully renamed/refactored projects while SVN to GIT migration.

If you are looking for post about how to migrate from SVN to GIT. You can refer my another post.


TechTip - How to embed html report within the Jenkins email notifications?

If you would like to send html report through Jenkins email notifications and looking for how to embed html report within the Jenkins email notifications, you can follow below steps:

Prerequisite

Ensure that your Jenkins has email-ext-plugin installed. If not, refer this and install it first.

Steps

Follow below steps to embed html file in email content:
  1. Go to your jenkins job and click on Configure.
  2. Go to Post Build Actions and then select Editable Email Notifications
  3. Select Content Type as HTML(text/plain)
  4. In the default content section replace $DEFAULT_CONTENT with following
           ${FILE,path=”relative path to html file”}

Example

For example, below is the absolute path for your html file. So you just need to pick bold marked as relative path and mention in path.

/app/jenkins/jobs/myjob/workspace/my-maven-module/target/site/surefire-report.html


${FILE,path="my-maven-module/target/site/surefire-report.html”}


This will place the surefire-report.html content in your email body.


Hope this tip is helpful for you.




Monday, 18 December 2017

SVN to GIT Migration

In this post, I will be describing about how to migrate projects from SVN to GIT. You can follow below steps to migrate your projects into GIT from SVN.

Prerequisites 
You need following softwars to be available on your local machine before proceeding further:
  • Java runtime, version 6 or newer
  • Git, with git-svn included, version 1.7.7.5 or newer
  • Subversion, version 1.6.17 or newer.
Instructions 
Step-1: First download svn-migration-scripts.jar from Atlassian’s bitbucket account.

Step-2: You need to verify required software on your local machine. Execute below command to do the same:

java -jar svn-migration-scripts.jar verify

If you are moving your projects from SVN to GIT, you must want to maintain SVN history in GIT too. But it must be possible where committer names in SVN and GIT are different. For this you need to map committer's names with GIT committer email.

Step-3: Extract SVN committer’s names and store in text file. Execute below command to do same: 
java -jar svn-migration-scripts.jar authors <SVN-URL> > svn-committer.txt 
Step-4: Map every SVN committer’s information with their respective mail id's on GIT. 
For example, while extracting we got below file:

NarendraV = NarendraV < NarendraV@mycompany.com>
AbcX = AbcX <AbcX@mycompany.com>


Now change this file like below. Here, we need to ensure that given email id is configured in GIT. 

NarendraV = NarendraV <NarendraV@abc.com>
AbcX = AbcX AbcX@abc.com

Step-5: Execute below command for converting SVN project to GIT in local:

git svn clone --trunk=/trunk --branches=/branches --branches=/bugfixes --tags=/tags --prefix=origin/ --authors-file= svn-committer.txt <svn-repository-url> <git-repository-name>

Ensure to replace <svn-repository-url> with your SVN repository URL and <git-repository-name> with repository name in GIT.

Step-4: Now create remote repository using BitBucket and push your local SVN to GIT converted project (done in step#5). 

First add remote repository to your local:

git remote add <GIT-Repository>

Now push project to remote repository using below command:

git push <remote-name> <remote-branch>

Example: git push origin develop

If you would like to get more detail, you can refer this link.

Challenges Faced
Here I would like to list out some challenges that we faced during SVN to GIT migration.
  • SVN branch did not follow the branch/trunk/tag standard structure
  • Restructuring/Refactoring projects in GIT. Please refer this link to get detail. 
  • Empty sub-folder migration from SVN to GIT
Hope this post helps you in SVN to GIT migration.

TechTip - Oracle SQL query to replace strings in clob column

If you are looking for oracle query to find and replace a string in clob column in bulk. You can find same tip below:

Create table having clob column.

create table MyClobTable ( column1 int, clob_column clob );


Create procedure to perform insertion in table

create or replace procedure MyProc( proc_column1 in int, proc_text in varchar2 )
as
begin
insert into MyClobTable values ( proc_column1, proc_text );
end;


Insert below two records in table.

exec MyProc(1, 'I am Narendra Verma and currently living in Atlanta. I visited a lot of places in Atlanta. Atlanta is a nice city in US.' );

exec MyProc(2, It is a great time to be in the City of Atlanta' );

commit;

Check if rows are inserted.

select * from MyClobTable;


Output:
1
I am Narendra Verma and currently living in Atlanta. I visited a lot of places in Atlanta. Atlanta is a nice city in US.

2
It is a great time to be in the City of Atlanta

Execute below to replace ‘Atlanta’ with 'Alpharetta' in all rows.

MERGE INTO MyClobTable A
     USING (SELECT column1,
                   TO_CLOB (REPLACE (clob_column, 'Atlanta', 'Alpharetta'))
                      AS updated_string
              FROM MyClobTable
             WHERE clob_column LIKE '%Atlanta%'
            )  B
        ON (A.column1 = B.column1)
WHEN MATCHED
THEN
   UPDATE SET A.clob_column = B.updated_string;


Check if 'Atlanta' word is replaced with 'Alpharetta' in all rows.

select * from MyClobTable;


Output:
1
I am Narendra Verma and currently living in Alpharetta. I visited a lot of places in Alpharetta. Alpharetta is a nice city in US.
2
It is a great time to be in the City of Alpharetta

Hope this tech tip helped you.

Monday, 26 June 2017

Generating and Reading QR Code (Two-Dimensional Barcode)

If you are looking for solution to generate and read QR code in your java application, I think you are visiting right post. In this post I am going to demonstrate how to generate and read bar code in Java.

About QR (Quick Response) Code 

QR Code is a two-dimensional barcode that is readable by smartphones. It allows to encode over 4000 characters in a two dimensional barcode. 

From Wikipedia: A QR code (abbreviated from Quick Response code) is a specific matrix barcode (or two-dimensional code) that is readable by dedicated QR barcode readers, camera telephones, and to a less common extent, computers with webcams. The code consists of black modules arranged in a square pattern on a white background. The information encoded may be text, URL, or other data.

QR codes are plastered on advertisements, billboards, business windows, and products. Now a days, these are being so popular and being utilized by different technical solutions. Paytm is one of the great example which has gained tremendous popularity where you can just scan QR code and pay. With the help of  QR code you can reduce typing effort for your app users. 

Open Source Lib for Barcode Image Processing 

ZXing ("zebra crossing") is an open-source, multi-format 1D/2D barcode image processing library implemented in Java. To get more detail refer this

Using ZXing, its very easy to generate/read QR code. If you are interested to generate/read QR code in your java code, you need to add below dependency in your maven java project:


<dependency>
               <groupId>com.google.zxing</groupId>
               <artifactId>javase</artifactId>
               <version>2.0</version>
</dependency>


How to Generate QR Code?
Here you will find the java example where you can generate QR code for your given string. In this example I am using my blog URL 'http://nverma-tech-blog.blogspot.com/' for which I want to generate QR code.

import java.io.File;
import java.io.FileOutputStream;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public class QRCodeGenerator {

       public static void main(String[] args) throws Exception {

// this is the text that we want to encode          
String text = "http://nverma-tech-blog.blogspot.com/";
      
int width = 400;
       int height = 300; // change the height and width as per your requirement

       // (ImageIO.getWriterFormatNames() returns a list of supported formats)
// could be "gif", "tiff", "jpeg"
       String imageFormat = "png";

       BitMatrix bitMatrix =
new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height);

MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, new FileOutputStream(new File("MyBlogQRCode.png")));
      
}
}


Once you execute this program, it will generate QR code image named 'MyBlogQRCode.png' at the location where your program is executed. If you open QR code image, it will be like below:

Since I have encoded my blog URL 'http://nverma-tech-blog.blogspot.com/' and generated this QR code, if you scan this QR code from your mobile's camera, my blog URL will be opened on browser. 

During writing this post, I did same on my mobile (Motorola X Play) and sharing same flow with you. Ensure that QR code scanner is enabled on your mobile. 

As you scan above QR code on your mobile camera, you will see red marked icon displayed. 




Now click on this icon, two options will be displayed.


Now if you click on View Website, browser is opened and you will see my blog is accessed as per decoded URL 'http://nverma-tech-blog.blogspot.com/' in QRCode.



Now you can read my blog on your mobile too without typing actual URL on your mobile browser :). 

How to Read QR Code?

Here you will find the java example where you can decode your generated QR code and get the same string back which is encoded in QRCode.

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class BarCodeReader {
      
       public static void main(String[] args) throws Exception {

InputStream barCodeInputStream = new FileInputStream("MyBlogQRCode.png");
            
BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);

LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
            
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
            
Reader reader = new MultiFormatReader();
             Result result = reader.decode(bitmap);

             System.out.println("Decoded barcode text is - " + result.getText());
            
       }
}


Output of this program is


Decoded barcode text is - http://nverma-tech-blog.blogspot.com/


Others
You can find other good references below on QR code:

Hope this post help you. If you have any question or feedback do write comment. I will try to assist you.


Sunday, 28 May 2017

Technical Events

GIDS established itself as the gold standard conference and expo for the software practitioner ecosystem. Over 45,000 attendees have benefited from the platform since its founding in 2008. I have got an opportunity to attend world class summit GIDS-2017 first time. I was able to meet lot of enthusiastic developers from different parts of the country. Overall I have experienced great learning, wonderful speakers and cool products showcased in this summit. I would like to share my GIDS learning experience in this blog. Read more...