Sunday, 17 May 2020

Difference between class level and object locking and static object lock


1) Class level locking will lock entire class, so no other thread can access any of other synchronized blocks.

2) Object locking will lock the block but more than one thread can enter this block simultaneously , but only if they are objects of different Threads

3)  In case of static object locking, Only one thread can enter this block of any class. But other threads can access 2nd synchronized blocks


public class SyncEx{

 private static final Object ststicLock = new Object();
 private final Object lock = new Object();

 public void lockingEx() {

    synchronized (ststicLock) {
        // Only one thread can enter this block of any class.. But other threads can access 2nd synchronized blocks
    }

    synchronized (lock) {
        // More than one thread can enter this block simultaneously , but only if they are objects of different Threads
    }

synchronized (SyncEx.class) {
        // Only one thread can enter this block of any class. No other thread will be allowed to access any of the other synchronized blocks
    }
 }
}

Monday, 23 September 2019

DROP or TRUNCATE all tables from a database

To DROP or TRUNCATE all the tables of a database




SET SERVEROUTPUT ON 

BEGIN
FOR tables IN (SELECT table_name as tname FROM user_tables)
LOOP
DBMS_OUTPUT.PUT_LINE('DROP TABLE ' || tables.tname || ' CASCADE CONSTRAINTS PURGE;')
EXECUTE IMMEDIATE('DROP TABLE ' || tables.tname || ' CASCADE CONSTRAINTS PURGE;');
END LOOP;
END;
/



SET SERVEROUTPUT ON 

BEGIN
FOR tables IN (SELECT table_name as tname FROM user_tables)
LOOP
DBMS_OUTPUT.PUT_LINE('TRUNCATE TABLE ' || tables.tname  || ';')
EXECUTE IMMEDIATE('TRUNCATE TABLE ' || tables.tname || ' ;');
END LOOP;
END;
/

Tuesday, 16 July 2019

Nginx configuration for load balancer

Create a conf file under location /etc/nginx/conf.d with any name and extension .conf :


server {
    listen       80;
    server_name  <server>;

    location /ctx01 {
        proxy_pass     http://host:port/ctx01/;
    }

    location /ctx02 {
        proxy_pass     http://host:port/ctx02/;
    }

    location / {
                root   /usr/share/nginx/html;
                index  index.html;
        }
}


This will redirect the request with /ctx01 to  http://host:port/ctx01.
Request with /ctx02 to  http://host:port/ctx02.

For e.g.  http://<server>:80/ctx01 will redirect to http://host:port/ctx01


If you want to have a home page, configure index.html under /usr/share/nginx/html.

Friday, 29 March 2019

How to replace a string in a CLOB file in SQL


We can replace the key with respective value in a CLOB in sql with replace function.

test_clob     CLOB;      // Type of test_clob is CLOB


test_clob := replace(test_clob, 'http', 'https');

This command will replace all occurrence of http with https from test_clob and store that in same file.


Note:  Don't change this in buffer while writing data to CLOB. With this it will replace but after last occurrence of the key it will not append data to the file.

test_lob.writeappend(test_clob, length(buffer), REPLACE(buffer,'http','https'));

Oracle wallet for connecting to a SSL(HTTPS) enabled application by SQL/PL-SQL query


If we want to connect to an application having SSL configuration (running over https) then it will throw error of certificate.

To solve this issue we need to create a wallet in DB and use that wallet in code.

Steps to create wallet:

1)   orapki wallet create -wallet ${ORACLE_HOME}/<wallet_name> -pwd <password>
      For e.g. orapki wallet create -wallet ${ORACLE_HOME}/https_wallet -pwd ****

2)    orapki wallet add -wallet https_wallet -cert <certificate.cer> -trusted_cert -pwd ******

     After adding certificate you can verify the certificate is present in wallet. You are done with server side changes of wallet.

In client side code you need to set the wallet before connecting to application.

     utl_http.set_wallet( 'file:/oravl01/oracle/12.1.0.1/owm/https_wallet' , '****' );

  After this you can connect to the application

req  := utl_http.begin_request( 'https://<user>:<pass>@<host>:<port>/test' );
resp := utl_http.get_response(req);

Sunday, 24 March 2019

How to get all table names in a schema in Oracle database

Metadata of tables are maintained in USER_TABLES table.


We can get all table names in a Oracle DB by :

 SELECT TABLE_NAME FROM USER_TABLES;

To get the number of rows present in a table :

select NUM_ROWS from user_tables WHERE TABLE_NAME='<TABLE_NAME>'

We can get all information related to tables in a schema by

select * from user_tables

To get a table information : 

desc <table_name>

Name        Null?              Type         
-------      --------              -------------
TITLE   NOT NULL VARCHAR2(80) 
VALUE                      VARCHAR2(80) 
PARRENT                 VARCHAR2(80) 
URL                          VARCHAR2(400)
NAME                      VARCHAR2(80) 
ICON                        VARCHAR2(80) 
STATUS                   VARCHAR2(80) 
ID      NOT NULL    NUMBER(38) 

Wednesday, 20 March 2019

JSON parsing with conditional filter with jq + shell


JSON parsing with conditional filter


For e.g. We have a json as below. We want to get the "format" field value when name is "Test".

{
  "name": "maven-snapshots",
  "format": "maven2",
  "type": "proxy",
  "url": "http://<host>:<port>/repository/maven-snapshots"
}
{
  "name": "Test",
  "format": "yum",
  "type": "hosted",
  "url": "http://<host>:<port>/repository/Test"
}
{
  "name": "Test_Group",
  "format": "maven2",
  "type": "group",
  "url": "http://<host>:<port>/repository/Test_Group"
}


We can get  it by:      jq -r '.[] | select(.name=="Test") | .format' 
 This will filter the "name" field and if it matches then return the "format" field value for that array element.

We can parse this from URL by below command.

curl -s -u admin:admin123 -X GET 'http://<host>:<port>/service/rest/v1/repositories' | jq -r '.[] | select(.name=="Test")|.format'


To get the number of elements in JSON array

 jq length 

To get the type of object 

jq type 

To get values with comma separated in same line 


jq  -r '.[] | [.date, .count, .word] | @csv'
"2011-01-12 13:14",17,"This"
"2011-01-13 21:30",4711,"That"

Saturday, 20 October 2018

How to find max release version from DB by custom sql function


Normal MAX function inn sql will get the maximum based on mathematical calculation. 

For e.g.  Max between  
        1.9 and 1.10 will return 1.9
But in case of release highest should be 1.10. 

If we want to sort based on release numbers and get the maximum then we have to create a custom sql function and get the maximum. For this we will LPAD the release number by replacing each number after/replacing each "." with some specific number of 0. After that we can check for max number and return it.


CREATE OR REPLACE FUNCTION MAX_RELEASE_VERSION(release_version IN VARCHAR2) 
RETURN VARCHAR2 AS
sub_string VARCHAR2(50);
BEGIN
FOR char_value IN 
(SELECT LPAD(regexp_substr(release_version,'[^.]+', 1, level),4,'0') as splited_num FROM DUAL CONNECT BY regexp_substr(release_version, '[^.]+', 1, level) IS NOT null)
LOOP
sub_string:=sub_string || char_value.splited_num;
END LOOP;
RETURN sub_string;
END;
/

Sunday, 14 October 2018

Read Manifest file of a package without extracting the file in python

We can read the MANIFEST.MF or any file from a archive file without extracting the file.
With below code we can read the manifest file and retrieve the values for the key 'Plugin-Dependencies'. We can add more keys and if that key matches then it will return the values for that key.



import argparse
import os
import sys
import zipfile
import re
import logging

global manifestkeys = []
manifestkey = 'Plugin-Dependencies'

def getManifestkeys():
        manifestkeys.append(manifestkey.lower().strip().replace('-',''))
        return manifestkeys


def getAttributes(manifestFile):
        logging.debug('Parsing attributes from %s'%(manifestFile))
        manifestkeys = getManifestkeys()
        logging.debug('Manifest Key : ' + str(manifestkeys))
        manifest = []
        current = {}
        lines = manifestFile.split('\n')
        idx = 0
        while idx<len(lines):
                line = lines[idx].strip()               
                if len(line) > 0:
                        splits = line.split(':')
                        key = splits[0].lower().strip().replace('-','')
                        if key in manifestkeys:
                                logging.debug('Key matched for ' + key)
                                value = ':'.join(splits[1:]).strip()
                                current[key] = value
                                while len(line.encode('utf-8')) >= 70:
                                        if idx<len(lines)-1:
                                                next_line = lines[idx+1]
                                                if next_line[0] == ' ':
                                                        current[key] += next_line.strip()
                                                        idx += 1
                                                        line = next_line
                                                        continue
                                        break
                elif len(current)>0 :
                        count = len(current.values())
                        # We need to weed out name only entries
                        # We can manually interrogate packages
                        if count > 1 or (count==1 and 'name' not in current.keys()):
                                manifest.append(current)
                        current = {}
                idx += 1
        if current != {}:
                manifest.append(current)
        return manifest

def readManifest(jar_file_name):
  _MANIFEST_NAME = 'META-INF/MANIFEST.MF'
  with zipfile.ZipFile(jar_file_name) as jar:
    try:
      manifest_string = jar.read(_MANIFEST_NAME)
      return getAttributes(manifest_string)
    except KeyError:
      return None


if __name__ == "__main__":
    logging.basicConfig(level=logging.WARNING)
    parser = argparse.ArgumentParser(description='Provide arguments to script as below')
    parser.add_argument('artifact_name',help='A String positional argument as artifact name for which the manifest to be read.')
    args = parser.parse_args()
    readManifest(args.artifact_name)

Saturday, 22 September 2018

Convert an integer column value from AlphaNumeric values in SQL

For e.g. We have the column value  ' 1.11+build.201701152243 '

Here we have combination of alphabets and numeric and special characters like + and .
Suppose we need to convert it to integer for comparison purpose.

We can convert like below.


CAST(REGEXP_REPLACE(<COLUMN_NAME>,'[^0-9]','') AS INTEGER )

This will first replace everything except digits with empty. After that it will convert that as Integer.
Then we can use that as int for aggregate function.

Friday, 24 August 2018

How to sort content of a file based on multiple columns in UNIX


For e.g.

We have below data in a file and we want to sort based on the date.
We can sort this by :
 1st sort based on YYYY   ( -k 4n ) Sort 4th column based on number
 2nd sort based on MON    (-k 3M)  Sort 3rd column based on Month
 3rd sort based on DD
 4th sort based on TIME

test.txt
--------
Sun,  20  Aug 2018 23:11:10 GMT   1.0.0-MASTER-20180801-171
Wed, 22  Mar 2019 22:11:10 GMT  1.0.0-20180823-263
Mon, 22  Apr 2018 21:11:10 GMT  1.0.0-20180823-265
Wed, 28  Feb 2018 21:11:09 GMT  1.0.0-20180823-263
Tue,  28  Feb 2018 21:11:10 GMT   1.0.0-20180823-263
Wed, 21  Jan 2020 23:11:10 GMT  1.0.0-20180823-243


sort -k 4n -k 3M -k 2n -k 5n test.txt > sortedTest.txt


Thursday, 23 August 2018

How to deploy an external artifact to repository in pom.xml


We can use "maven-deploy-plugin" to upload any artifact to repository from a location by pom.xml.
By this we can upload any artifact without uploading the generated artifacts, also without install/deploy goal.

....
</plugins>
....
<plugin>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.8.2</version>
    <executions>
        <execution>
            <id>deploy-file</id>
            <phase>clean</phase> <!-- The phase in which you want to upload the file to Repository . In this case "mvn clean " goal will upload the artifact.-->
            <goals>
                <goal>deploy-file</goal>
            </goals>
            <configuration>
<repositoryId>maven-public</repositoryId>  <!-- This is the id which contains the user/pass to connet to Nexus. This will be under "servers" section in settings.xml -->
                <file>C:\Users\bismayam\Desktop\poc.jar</file>
                <url>http://<host>:<port>/nexus/content/repositories/test-releases/</url>
                <groupId>testGroup</groupId>
                <artifactId>testArtifact</artifactId>
                <version>1.0</version>
                <packaging>jar</packaging>
            </configuration>
        </execution>
    </executions>
</plugin>
</plugins>

Thursday, 21 June 2018

How to test private constructors in Junit test by reflection in Java

We can not test private constructors directly by JUnit test. We need to use reflection API to call the constructors.


For e.g we have below class which have a private constructor.

package com.company.main;
import java.lang.reflect.InvocationTargetException;
public class MainClass {
private MainClass(){
System.out.println("From private constructor");
}
}


We have the below test case to use reflection to call the private constructor.  If there are any explicit checking is being done by the developer to restrict the calling of private constructor then it may throw exception. 

package com.company.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.jupiter.api.Test;
import com.company.main.MainClass;

class MainTest {

@Test
void test() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

Constructor<MainClass> constructor = MainClass.class.getDeclaredConstructor(new Class[0]);
constructor.setAccessible(true);
MainClass mainClass = constructor.newInstance(new Object[0]);
}
}

How to restrict a private constructor from being called by reflection API in Java


To restrict the private method from being called by reflection API we have to write a method that will check the caller of the method. If the caller is from any other class than the current class then we have to throw the error.

We can get the caller class from the caller stack of Reflection API.

getCallerClass(0) will return class "sun.reflect.Reflection"
getCallerClass(1) will return class "com.company.main.MainClass" // Same class

getCallerClass(1) will return class "jdk.internal.reflect.NativeConstructorAccessorImpl" // If it is being called from any other calss.

check if caller class of 1 and 3 are not the same then throw exception.

package com.company.main;

public class MainClass {

private MainClass(){
checkCallerClass();
System.out.println("From private constructor");
}


private void checkCallerClass() {
               Class self = sun.reflect.Reflection.getCallerClass(1);
               Class caller = sun.reflect.Reflection.getCallerClass(3);
               if (self != caller) {
                    throw new java.lang.IllegalAccessError();
               }
        }

}

Wednesday, 13 June 2018

How to check if a jar file is corrupted

Sometimes jar files get corrupted and we will get errors like below when we will run the file.

 $ java -jar test.jar
Error: Invalid or corrupt jarfile test.jar

When we try to see the content with jar -tvf test.jar.

java.util.zip.ZipException: error in opening zip file
        at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(ZipFile.java:127)
        at java.util.zip.ZipFile.<init>(ZipFile.java:88)
        at sun.tools.jar.Main.list(Main.java:977)
        at sun.tools.jar.Main.run(Main.java:222)
        at sun.tools.jar.Main.main(Main.java:1147)

Run below command. If this is giving proper output like below then it is not corrupted.
It should give information about the classes present in that jar file and few others.
This jar file may work with -cp command. This usually happens if you use prefix with /META-INF/MANIFEST.MF
testuser@indl123!:testuser> unzip -lv RunDbSttm.jar
Archive:  RunDbSttm.jar
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
       0  Defl:N        2   0% 07-20-2016 11:49 00000000  META-INF/
      68  Defl:N       67   2% 07-20-2016 11:49 0a41c062  META-INF/MANIFEST.MF
    3653  Defl:N     2022  45% 07-20-2016 11:48 e8c11d5d  RunDbSttm.class
--------          -------  ---                            -------
    3721             2091  44%                            3 files


How to start a Java program (jar) from another Jar


We can start a jar inside another jar and get the result by below code.

Process proc = new ProcessBuilder("java", "-XMx8192M", "-jar", "test.jar").start();
int result = proc.waitFor();

You can get details of ProcessBuilder from below.

https://docs.oracle.com/javase/10/docs/api/java/lang/ProcessBuilder.html

Tuesday, 8 May 2018

Download any file from Database

package com.edu.main;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class DownloadFileFromDB {

public static void main(String[] args) {

final String host = "jdbc:oracle:thin:@<host>:1521:<service_name>";
final String user = "username";
final String pass = "*******";
String SQL = "SELECT <COLUMN> FROM <TABLE> WHERE <COLUMN>=?";

Connection conn = null;
PreparedStatement smt = null;
InputStream input = null;
FileOutputStream output = null;
ResultSet rs = null;

try {

Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(host, user, pass);
System.out.println("DB Connection successful..");

smt = conn.prepareStatement(SQL);
smt.setString(1, "<VALUE_TO_BE_REPLACED_WITH_?>");
rs = smt.executeQuery();

output = new FileOutputStream(new File("C:\\Users\\bismaya\\Desktop\\<FILENAME>.<EXT>"));

while (rs.next()) {
input = rs.getBinaryStream("<COLUMN_OF_FILE_IN_DB>");
int r = 0;
while ((data = input.read()) != -1) {
output.write(data);
}
}
System.out.println("Success...");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
System.err.println("DB Connection failed!");
e.printStackTrace();
} catch (FileNotFoundException e) {
System.err.println("File not found in provided location!");
e.printStackTrace();
} catch (IOException e) {
System.err.println("Error while writing to file!");
e.printStackTrace();
} finally {
if (rs != null) {
try {
input.close();
output.flush();
output.close();
smt.close();
conn.close();
} catch (SQLException e) {
System.err.println("Error while closing the connecton!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

}

Wednesday, 2 May 2018

Apache httpd load balancer setup


Install httpd as service by following  http://httpd.apache.org/docs/2.4/install.html 

sudo yum install httpd
sudo systemctl enable httpd
sudo systemctl start httpd

1) Edit /etc/httpd/conf/httpd.conf and change the Listen port to configure the Load balancer port. By default port will be 80.

2) Add a file lb.conf under /etc/httpd/conf.d to configure members to be redirected. httpd service will load all *.conf files present under /etc/httpd/conf.d folder.

3) lb.conf content example as below.

ProxyRequests off
<Proxy balancer://Testbalancer>
BalancerMember <URL1>
BalancerMember <URL2>
BalancerMember <URL3>
ProxySet lbmethod=byrequests Or ProxySet lbmethod=bybusyness Or lbmethod=bytraffic
</Proxy>
<Location /balancer-manager>
SetHandler balancer-manager
</Location>
ProxyPass /balancer-manager !
ProxyPass / balancer://Testbalancer/ connectiontimeout=30 timeout=60


This will redirect all request http://<host>/ to either <URL1> or <URL2> or <URL3>

4) You can configure ProxyPass to redirect in case some specific string comes after the URL.

    ProxyPass "/test" balancer://Testbalancer/ connectiontimeout=30 timeout=60

    ProxyPassMatch "^/(.*\.gif)$" "http://backend.example.com/$1"

This will cause a local request for http://example.com/foo/bar.gif to be internally converted into a proxy request to http://backend.example.com/foo/bar.gif.

    ProxyPassMatch "^/(.*\.gif)$" "http://backend.example.com:8000/$1"


Start/Stop/Restart  service by : service httpd start/stop/restart






Wednesday, 25 April 2018

POST data to any endpoint via REST API




curl -v -X POST -u "user:password" --header "Content-Type: application/json" 'http://<host>:<port>/<endpoint>' -d @test.json


You can change the Content-Type based on your required type and supported by endpoint.

Type application 
================
application/javascript
application/octet-stream
application/ogg
application/pdf
application/xhtml+xml
application/x-shockwave-flash
application/json
application/ld+json
application/xml
application/zip

Type Audio
==========

audio/mpeg
audio/x-ms-wma
audio/vnd.rn-realaudio
audio/x-wav

Type image
==========

image/gif
image/jpeg
image/png
image/tiff
image/vnd.microsoft.icon
image/x-icon
image/vnd.djvu
image/svg+xml

Wednesday, 11 April 2018

Encode Decode error in python : UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 0

Error desc:

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 0:
                    ordinal not in range(128)

This type of error comes when some special character is not in range of selected encoding type.

Solution :

Find the default encoding type of python.
             python -c 'import sys; print(sys.getdefaultencoding())'
             o/p : ascii

We need to change the encoding type and try with different type of encoding. e.g.

latin-1
utf-8
utf-16-be


Create a file sitecustomize.py under this location : /usr/lib/python2.7/site-packages/sitecustomize.py

echo "import sys; sys.setdefaultencoding('latin-1')" > sitecustomize.py


Compile the python script and check the encoding type is changed by below command.
python -m py_compile sitecustomize.py

PYTHONPATH=".:$PYTHONPATH" python -c 'import sys; print(sys.getdefaultencoding())'

o/p : latin-1

Difference between class level and object locking and static object lock

1) Class level locking will lock entire class, so no other thread can access any of other synchronized blocks. 2) Object locking will lo...