Saturday, October 24, 2009

Using variable with sed inside a shell script

For using sed inside a shell script, The variable should be in "double quotes" and the command in 'single quotes'. Following is the example

#!/bin/bash

usage () {
echo "$0 -s < name
> -r <replace name> -f <file name>"
exit
}

while getopts s:r:f: option
do
case "$option" in
s) search="$OPTARG";;
r) replace="$OPTARG";;
f) filename="$OPTARG";;
\?) usage
esac
done

sed -i 's/'"$search"'/'"$replace"'/g' "$filename"

Saturday, October 3, 2009

https redirection handling in httpunit

In httpunit when you are requesting a http page and if the page internally redirects to a https page, httpunit throws following error and response is not received:

sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed

To resolve this error, add following code in your httpunit program.

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.*;

try{
SSLContext context = SSLContext.getInstance("SSLv3");
TrustManager[] trustManagerArray = { new NullX509TrustManager() };
context.init(null, trustManagerArray, null);
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new NullHostnameVerifier());
}catch(Exception e) {
e.printStackTrace();
}


class NullX509TrustManager implements X509TrustManager {
/* Implements all methods, keeping them empty or returning null */
public X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(X509Certificate[] chain, String authType) {

}

public void checkServerTrusted(X509Certificate[] chain, String authType) {

}
}

class NullHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}



Getting thread dump for java application

When an java application server freezes or becomes non-responsive, it is recommended to generate a thread dump for the application. A thread dump is a user-friendly snapshot of the threads and monitors in a Java Virtual Machine (JVM). A thread dump can range from fifty lines to thousands of lines of diagnostics depending on how complex your application is.

On UNIX platforms you can send a signal to a program by using the kill command. This is the quit signal, which is handled by the JVM. On Solaris you can use the command kill -QUIT process_id, where process_id is the process id of your Java program.

run the command kill -QUIT <pid> or kill -3 <pid> on shell which will cause the thread dump to be displayed your application console. Alternatively you can enter the key sequence <CTRL> \ in the window where the Java program was started. This is why it's critical to redirect standard output & standard error to a log file so it is captured when you need a thread dump.

On Windows platform, enter the key sequence <CTRL> <break> for the thread dump of your application.

Thread States :: The key used for the thread states in Thread dump is:
R ==> Running or runnable thread
S ==> Suspended thread
CW ==>Thread waiting on a condition variable
MW ==> Thread waiting on a monitor lock
MS ==> Thread suspended waiting on a monitor lock