Saturday, 22 March 2025

OpenVPN commands - As alias

 When using openvpn3 I normally create aliases that would help me easily managing my vpn.

# Alias for importing VPN configuration persistently

alias vpn-import='openvpn3 config-import --persistent --name solofunds --config /home/shahzeb/vpn/openvpn_config.ovpn'


# Alias for starting the VPN session

alias connectvpn='openvpn3 session-start --config solofunds'


# Alias for stopping the VPN session

alias stopvpn='openvpn3 session-manage --config solofunds --disconnect'


# Alias for restarting the VPN session

alias restartvpn='openvpn3 session-manage --config solofunds --restart'


# Alias for listing active VPN sessions

alias vpnstatus='openvpn3 sessions-list'


# Alias for viewing VPN session statistics

alias vpn-stats='openvpn3 session-stats --config solofunds'


# Alias for checking VPN logs in real time

alias vpn-log='openvpn3 log --config solofunds --log-level 6'


#stop a given sessions

#openvpn3 session-manage --session-path /net/openvpn/v3/sessions/<session-path> --disconnect


Thursday, 22 August 2024

Docker startup issue - wsl.exe update


After updating docker to latest version 4.33.1 on windows 10 pro, I started getting the following error. 

wsl update failed: update failed: updating wsl: exit code: 4294967295: running WSL command wsl.exe C:\Windows\System32\wsl.exe --update --web-download

The popup box details ended with the following error code

: exit status 0xffffffff

Read our policy for uploaded diagnostic data⁠



 

The issue was fixed after re-installing the docker version 

4.28.0


I also installed the WSL version 2.3.17 

Thursday, 16 May 2024

Fixing Integration tests failure due to Order


The Maven Failsafe Plugin is used for running integration tests in a Maven project. It is designed to complement the Maven Surefire Plugin, which is used for running unit tests. The Failsafe Plugin allows for a separate lifecycle phase for integration tests, which typically run after the application has been packaged and deployed.

<plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-failsafe-plugin</artifactId>

    <version>3.0.0-M5</version>

    <executions>

        <execution>

            <id>integration-test</id>

            <goals>

                <goal>integration-test</goal>

            </goals>

        </execution>

        <execution>

            <id>verify</id>

            <goals>

                <goal>verify</goal>

            </goals>

        </execution>

    </executions>

</plugin>

To configure the maven-failsafe-plugin to run integration tests in alphabetical order, Add the following configuration. <execution> <id>integration-test</id> <goals> <goal>integration-test</goal> </goals> <configuration> <runOrder>alphabetical</runOrder> </configuration> </execution> Helpful content from the article. First, we would like to know why a specific test failed. By default, Maven runs the tests in the order determined by our file system. This is the default setting for the runOrder property. This can be different on my Windows machine from the one on the Linux build system. Again frustration. A good aid could be if the build produces an easy-to-use output to tell, what was the real order of the tests. 


Some of the content was generated by ChatGPT after I asked some questions.

Monday, 29 April 2024

Understanding Case Sensitivity in Liquibase Changelogs

When working with Liquibase to manage database schema changes, developers often encounter issues related to case sensitivity, especially when deploying changes across different environments. In this blog post, we'll explore the concept of case sensitivity in Liquibase changelogs and how it can impact database deployments.

What is Liquibase?

Liquibase is an open-source database schema migration tool that allows developers to manage and version database changes in a structured and repeatable manner. It uses changelog files to define database changes applied to the target database.

Understanding Case Sensitivity

One common issue that developers face when using Liquibase is case sensitivity. Unlike Windows, Linux file systems are typically case-sensitive, meaning file and directory names are treated as distinct based on case. This can lead to inconsistencies when working with Liquibase changelogs, especially when referencing database objects such as tables and columns.

Example Changelog

Let's consider the following example Liquibase changelog snippet:

<changeSet id="add_full_name_on_userprofile" author="shahzeb">

<addColumn tableName="userprofile"> <column name="full_name" type="VARCHAR(50)" defaultValue="regex"> <constraints nullable="false"/> </column> </addColumn> <comment>add full_name column</comment> </changeSet>

In this changelog, we're adding a new column named full_name to the userprofile table. Note that the table and column names are specified in lowercase, which may work fine on Windows but can lead to issues on Linux due to case sensitivity.






Wednesday, 24 April 2024

Docker build crashing with error : failed to receive status: rpc error: code = Unavailable desc = error reading from server: EOF

 On Windows 10, Docker desktop, I received the following error when building an image. The build will take more than 30 minutes when reaches the following command.

RUN poetry config virtualenvs.create false && poetry update && poetry install --no-interaction --no-ansi

and throw the following error. 

failed to receive status: rpc error: code = Unavailable desc = error reading from server: EOF

I had to increase the allocated resources to the docker desktop to resolve the issue.



Sunday, 7 May 2023

Running Spring boot app with MySQL in docker container

Create a Dockerfile and add the following contents.


 FROM openjdk:11
 LABEL AUTHOR="shahzeb"
 ARG BUILD_FILE=target/pos-1.0.jar
 COPY ${BUILD_FILE} pos.jar     
 ENTRYPOINT [ "java", "-jar", "/pos.jar" ]
 EXPOSE 8080
 

To build the docker image, run the following command.

        
            docker build -t [docker image name] .
            docker build -t shahzeb/pos .
        
    

To run the container from the image created from above command, run the following command

                
            

docker run -p 8080:8080 shahzeb/pos --network=host

        
            

docker container run --network pos-app-env --name pos-app -p 8080:8080 -d shahzeb/pos

The --network parameter is use to connect the spring boot application to the mysql database on the host machine. Since we are already using the EXPORT 8080 for exposing the port. we can skip passing -p 8080:8080 to the docker run command.

docker network create [network name.] #Create a network on which the app will run

        
            

docker network create pos-app-env

        
            

docker container run --network [netowrk name] --name [container name] -p 8080:8080 -d [docker image name]

        
            

docker container run --network pos-app-env --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=str0ng -d mysql:8.0

        
            

# Running pos in docker environment

To run the application in docker env, run the following 4 commands.

Create a network in docker

        
            

docker network create pos-app-env

Start MySQL in same network

        
            

docker container run --network pos-app-env --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=str0ng -d mysql:8.0

Build Docker Image container POS app

        
            

docker build -t shahzeb/pos .

Run Container for the app

        
            

docker container run --network pos-app-env --name pos-app -p 8080:8080 -d shahzeb/pos

Using docker-compose file.


version: '3.9'
services:
  mysqldb:
    image: mysql:8.0
    ports:
      - "3306:3306"
    networks:
      - pos-app-env
    environment:
      - MYSQL_ROOT_PASSWORD=str0ng
      - MYSQL_DATABASE=pos
    restart: always
    healthcheck:
      test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ]
      timeout: 20s
      retries: 10

  pos-app:
    image: shahzeb/pos
    ports:
      - "8080:8080"
    networks:
      - pos-app-env
    depends_on:
      - mysqldb

networks:
  pos-app-env:


Thursday, 8 December 2022

Using Composite Key in Hibernate With Spring Boot

 This post contains an example showing how we can use a composite key in hibernate with spring.


We have an employee that has multiple tasks. I am not posting the DB, the reason is that I have turned on the ddl-auto to true. If you need to automatically update the DB when spring loads the entities, add the following property to the application.properties file.

spring.jpa.hibernate.ddl-auto=update

Coming back to the code part. Here is an Employee class.

import com.fasterxml.jackson.annotation.JsonManagedReference;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import lombok.Data;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor
@Entity
public class Employee implements Serializable {
@Id
private long employeeId;
private String name;
private String dept;
@JsonManagedReference
@OneToMany(mappedBy = "employee")
List<Task> taskList;

}
and the following section contains the Task class


import com.fasterxml.jackson.annotation.JsonBackReference;
import java.io.Serializable;
import java.sql.Date;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class Task implements Serializable {
@EmbeddedId
private CompositeTaskId taskId;

@JsonBackReference
@MapsId("employeeKey")
@ManyToOne
private Employee employee;

private String taskName;
private Date date;
}
and CompositeTaskId

import java.io.Serializable;
import javax.persistence.Embeddable;
import lombok.Data;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor
@Embeddable
public class CompositeTaskId implements Serializable {
private long employeeKey;
private long taskId;

}
Running the code with controllers will create DB  given table and composite key.


P.S. I have re-used code from some example online as well but don't remember the reference link.

Saturday, 11 September 2021

xmls

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE foo [
   <!ELEMENT foo ANY >
   <!ENTITY xxe SYSTEM "file:///c:/boot.ini" >]>
<foo>&xxe;</foo>


<!DOCTYPE root [<!ENTITY test SYSTEM 'file:///etc/passwd'>]>  
<vehicle>   
   <type>Car</type>   
   <brand>&test;</brand>  
</vehicle>

Tuesday, 24 August 2021

Hibernate associations

The following post is for playing with the assiocations.


Uni-directional

 public class SourceEntity {

@Id

private Long id;

private String name;

@OneToMany

private List<TargetEntity> targets;

}

public class TargetEntity {
@Id
private Long id;
private String description;
}

Both tables have two columns each. No FK column is added.

For the able table, the third table is "source_entity_targets" is created with two columns "source_entity_id" and "target_id".

Bi-directional

If I add convert it to bi-directional, still it will create a third table. Following changes are added for bi-directional and Target Entity looks like. 

public class TargetEntity {

@Id
private Long id;
private String description;
@ManyToOne(fetch = FetchType.LAZY)
    private SourceEntity sourceEntity;
}

This time the target entity table has "source_entity_id" column as well. It is "MUL" key.

Adding @JoinColumn annotation in Source table


public class SourceEntity {
@Id
private Long id;
private String name;
        @OneToMany(cascade = {CascadeType.ALL})
@JoinColumn
private List<TargetEntity> targets;
}

public class TargetEntity {

@Id
private Long id;
private String description;

@ManyToOne(fetch = FetchType.LAZY)
private SourceEntity sourceEntity;

}

This creates two tables. The source table has 2 columns, Target tables have 4 columns out of which 2 columns are: source_entity_id and targets_id. 

On saving the object, the source_entity id was saved in target_ids.

Join Column name

When I added the name to the Join column annotation as following in the source entity class.

Two tables were created.

@OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name="source_id")
private List<TargetEntity> targets;

Target table's column name "targets_id" is converted to "source_id".

When I added the @JoinColumn annotation in targetClass as 
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="source_id")
private SourceEntity sourceEntity;

Three tables were created.
source_entity has 2 columns.
The target entity has now 3 columns. The third column is "source_id".
And source_entity_targets table with two columns as discussed above.

When saved an object, the source_id was set to null and the source_entity_targets columns had values.

After adding the following to source
@OneToMany(cascade = {CascadeType.ALL}, mappedBy = "sourceEntity") 
private List<TargetEntity> targets;

2 tables were created but the source id was not saved in the target table. the source_id column value was set to null.

To fix this, a new sourceEntity should be added to the target entity before saving the source entity. Example: 

SourceEntity src = new SourceEntity();
src.setName("source");
List<TargetEntity> targetEntities = new ArrayList<>();
TargetEntity target = new TargetEntity();
target.setDescription("description");
target.setSourceEntity(src); // <<<<< this line adds the source to target.
targetEntities.add(target);
src.setTargets(targetEntities);
dataService.persistData(src);









Sunday, 22 August 2021

Junit - Parameterized Unit Testing - CsvFileSource

Sometimes we need to execute a single test case multiple times.

In Java, For this purpose using JUnit's Parameterized testing is a good approach. 

It allows us to provide test data in an iterable format and code runs for all the records. In this series, I will try to cover different ways of providing iterable data to unit tests. 


In this tutorial, we will read the data from a CSV file with the annotation @CsvFileSource. It will use a comma-separated file as an input data source.

In Junit, We use the @Test annotation to mark a method as a test case. In case of making a test Parameterized, We need to add the annotation `@ParameterizedTest` instead of @Test and mention the input file as a parameter in `@CsvFileSource`.

@ParameterizedTest

 @CsvFileSource(resources = "/input_test_data.csv", numLinesToSkip = 1)

You can see another parameter named 'numLinesToSkip' which is used to skip these lines for the testing. In this case, I have added 1 to skip the header name.

To start the example, First, create a CSV file under the directory 'src/test/resources' and name it whatever you want. For this example, the file name is "input_test_data" with two columns. i.e. First Name and Last Name. Following are the contents of our CSV file.

First Name, Last Name

Ragnar, Lothbrok

Now create a Java class in the test folder and put the following contents in the file.\


import org.junit.jupiter.params.ParameterizedTest;

import org.junit.jupiter.params.provider.CsvFileSource;

class ParameterizedUnitTest {

    @ParameterizedTest

    @CsvFileSource(resources = "/input_test_data.csv", numLinesToSkip = 1)

public void testCategories(String firstName, String lastName) {

System.out.print("Full Name is :"+firstName+" "+lastName);

}

}

Now you can run the test by adding multiple records to your CSV file.


Happy coding.




Tuesday, 15 June 2021

Finding the longest subarray that starts and ends on the same digit

 package com.poc.basic;


import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


public class Test {


public static void main(String[] args) {

List<Integer> input=Arrays.asList(1, 2, 3, 7, 6, 1, 9, 8, 6, 1, 9, 8,  9, 8, 3);

List<Integer> largest = new ArrayList<Integer>();

int largestSqnce = 0;

Map<Integer, Integer> map = new HashMap();

for(int i=0; i<input.size(); i++) {

if(!map.containsKey(input.get(i))) {

map.put(input.get(i), i);

}else {

if(i - map.get(input.get(i)) > largestSqnce) {

largestSqnce = (i - map.get(input.get(i)))+1;

largest = input.subList(map.get(input.get(i)), i+1);

}

}

}

System.out.println(largest);

}

}


Tuesday, 21 April 2020

Java heap space issue on ubuntu 18.04 with Tomcat

Setting up Swap Memory:

Check Swap Memory:

1) sudo swapon --show

Creating a Swap File


2): sudo fallocate -l 1G /swapfile

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

3) sudo chmod 600 /swapfile

4) sudo mkswap /swapfile

5) sudo swapon /swapfile

Make the changes permanent, use following commands

6) sudo vi /etc/fstab

7) /swapfile swap swap defaults 0 0

8) Verify: sudo swapon --show

9) Check swam memory:
sudo free -h


In Tomcat setenv.sh add the xms and xmx parameters like following.

CATALINA_OPTS="-Djava.net.preferIPv4Stack=true -Xms256M -Xmx1024M"

Tuesday, 19 February 2019

Using JsPdf to generate pdf from html

Today i felt a need to save my html data to pdf. For the purpose jsPdf with jsPdf-auto is a good combination.
Here is the code that is working with me now.


//base function that i called on button click.

function exportReportTable() {
    toDataURL($("#imgUrl").val(), function(dataUrl) {
        exportReportAsPdf(dataUrl)
    });
}

// This function converts the image to data which can be use to add as logo.

function toDataURL(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.onload = function() {
        var reader = new FileReader();
        reader.onloadend = function() {
            callback(reader.result);
        }
        reader.readAsDataURL(xhr.response);
    };
    xhr.open('GET', url);
    xhr.responseType = 'blob';
    xhr.send();
}



// function using jspdf and jspdf-auto.

function exportReportAsPdf(imgData){
    var doc = new jsPDF();

    // To display header and log on single page, uncomment the line and remove from header section.

    /*doc.addImage(imgData, 'png', 12, 5, 20, 20);
    doc.setFontSize(12);
    doc.text($("#companyName").val(), 60, 10);
    doc.text("Payment Vouchers", 80, 15);
    doc.text("Date From: " + $("#fromDate").val() + " To: " + $("#toDate").val(), 65, 20);*/

    var header = function (data) {
        doc.setTextColor(40);
        doc.setFontStyle('normal');
        doc.addImage(imgData, 'png', 12, 5, 20, 20);
        doc.setFontSize(12);
        doc.text($("#companyName").val(), 60, 10);
        doc.text("Payment Vouchers", 80, 15);
        doc.text("Date From: " + $("#fromDate").val() + " To: " + $("#toDate").val(), 65, 20);
        doc.setFontSize(10);
    };

    doc.autoTable({
        margin: {
            top: 30
        },
        html: '#reportTable',
        didDrawPage: header,
        bodyStyles: {valign: 'top'},
        styles: {overflow: 'linebreak', cellWidth: 'wrap'},
        columnStyles: {2: {cellWidth: 'auto'}} // 2 represents the 2nd column
    });
    // To add water mark
    doc = addWaterMark(doc, imgData);
    doc.save("Payment_Voucher_Report_"+ $("#fromDate").val()+"_"+$("#toDate").val()+".pdf");
}

// This function add water mark text at bottom. You can change the location and text.

function addWaterMark(doc,imgData) {
    var totalPages = doc.internal.getNumberOfPages();

    for (i = 1; i <= totalPages; i++) {
        doc.setPage(i);
        //doc.addImage(imgData, 'PNG', 40, 40, 75, 75);
        doc.setTextColor(150);
        doc.setFontSize(8);
        doc.text(140, doc.internal.pageSize.height - 10,"Report has total "+totalPages+" pages. This is "+ i+" of "+totalPages);
        //doc.text(30, 30, 'Report Text', null, 46);
    }

    return doc;
}

Tuesday, 29 November 2016

Printf works in CUDA

In order to execute the printf("something") in cuda code, we need to compile the code with an extra parameter i.e. [-arch=sm_20] which enables us to print our output inside cuda kernel.
for example i have my hello.cu file. The following command is use to compile the code.

nvcc -arch=sm_20 -o hello hello.cu

for complete documentation you can follow the link






Friday, 28 October 2016

Alfresco : ObjID already in Use Issue

While working on alfresco today, I encountered an issue which took almost 2 hours to debug.

Issue :
While starting the alfresco, the process starts fine but in the end the following error logs were popping out.

ERROR [web.context.ContextLoader] Context initialization failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.remoting.rmi.RmiServiceExporter' defined in class path resource [alfresco/emailserver/email-service-context.xml]: Invocation of init method failed; nested exception is java.rmi.server.ExportException: internal error: ObjID already in useCaused by: java.rmi.server.ExportException: internal error: ObjID already in use

On googling one of the suggestion was that the alfrsco port is blocked by firewall. I disabled the firewall but issue was still there. At the end the issue was turned out to be weird.  The solution was...

Solution :
There was wrong entry for my machine inside /etc/hosts file. To fix the issue i had to fixed it by assigning my machine ip (the correct ip, as it was changed) to node name. To fix the issue, add entry 127.0.0.1 localhost.

  

Tuesday, 11 October 2016

Playing with tensorflow

import tensorflow as tf
import numpy as np

#Declare variable in tensorflow

#constant(value, dtype, shape, name)

strng = tf.constant("hello tensor");
session = tf.Session();
print session.run(strng);


#shape means the shape of output. In this case it will be 3x4 matrix. Where the missing values will be filled
#using last value i.e 4

cont_tensor = tf.constant([1,2,3,4],dtype=tf.int32,shape=[3,4],name="SimpleTensor");
print session.run(cont_tensor);

cont_tensor2=tf.constant(1, dtype=tf.float16, shape=[4,4], name="TensorWithAllValues");
result = session.run(cont_tensor2);
#print tensor's name
print cont_tensor2.name

#Define Place holder
m1PH = tf.placeholder(tf.int32, shape=[3,3], name="Matrix1PlaceHoler")
m2PH = tf.placeholder(tf.int32, shape=[3,3], name="Matrix2PlaceHolder")

#Define operations
#addOperation =  tf.add(m1PH, m2PH, name="Add Operation");
mulOperation = tf.matmul(m1PH, m2PH);

#create a matrix of random values and feed it to both place holders
randomMatrix = np.random.random_integers(1,10, [3,3])

print session.run(mulOperation, feed_dict={m1PH: randomMatrix, m2PH: randomMatrix})

#saving graph for tensorboard
tf.train.SummaryWriter("/home/shahzeb/tensorflow/tensorboardlogs",session.graph)

#Once code is executed, launch tensorboard to view the graph generated.
#execute the following command
#$> tensorboard --logdir=/path/to/log/dir #in my case its /home/shahzeb/...

Thursday, 3 September 2015

Today i just got curious about Numbers so i asked my self, how much i can count? I stuck at trillion... Really. So did a little search on Google and found a very good table. I updated the table a little bit.

I would like to share it if someone else need it. (like me .. ) :D


Name
number of zeros
groups of (3) zeros
Thousand
3
1 (1,000)
Million
6
2 (1,000,000)
Billion
9
3 (1,000,000,000)
Trillion
12
4 (1,000,000,000,000)
Quadrillion
15
5
Quintillion
18
6
Sextillion
21
7
Septillion
24
8
Octillion
27
9
Nonillion
30
10
Decillion
33
11
Undecillion
36
12
Duodecillion
39
13
Tredecillion
42
14
Quatttuor-decillion
45
15
Quindecillion
48
16
Sexdecillion
51
17
Septen-decillion
54
18
Octodecillion
57
19
Novemdecillion
60
20
Vigintillion
63
21
Centillion
303
101