Showing posts with label Scripts. Show all posts
Showing posts with label Scripts. Show all posts

Monday, October 13, 2008

Random Password Generator

A lot of places we need to create and apply Random Password upon granting a resource or creation of OIM user via code. Here is a code that would generate one and will also allow you to specify the length of random password.

================================
RandomPasswordGenerator.java
================================

import java.util.Random;

public class RandomPasswordGenerator {

public static int DEFAULT_PASSWORD_LENGTH=8;
public static char[] Special_Character = {'!','@','#','$','%','^','&','*','(',')' };
public static char getSpecialCharacter(){
Random rand = new Random();
int randInt = rand.nextInt(10);
return Special_Character[randInt];
}

public static String getPassword(int n) {
if(n <=8 ){
n=DEFAULT_PASSWORD_LENGTH;
}
char[] pw = new char[n];
int c = 'A';
int r1 = 0;
int i=0;
String tempString = new String();
while(i< n){
r1 = (int)(Math.random() * 4);
l1: switch(r1) {
case 0: c = '0' + (int)(Math.random() * 10); break l1;
case 1: c = 'a' + (int)(Math.random() * 26); break l1;
case 2: c = 'A' + (int)(Math.random() * 26); break l1;
case 3: c = getSpecialCharacter(); break;
}

char c1 = (char)c;
boolean isExisting = false;
l2: for(int j=0; j < i; j++){
if(c1 == pw[j]){
isExisting = true;
break l2;
}
}
if(!isExisting){
pw[i] = (char)c;
i++;
}

}
return new String(pw);
}
public static void main(String args[]){
System.out.println(RandomPasswordGenerator.getPassword(8));
}
}

===============================
Output would be something like:
===============================
&3FVZfxb

Friday, October 10, 2008

Searching Jar Files in Unix / Linux

Lot of times we get the following errors:
Exception in thread "main" java.lang.NoClassDefFoundError: server

And we just don't know which jar file is missing from the classpath. And we need to know the correct jar file name(s) to fix the problem. So, here I create a shell script that would allow you to search through all the jar files by specifying a specific keyword. You may modify this script to search in all files (*) or whatever your criteria may be. This script works recursively for all sub-folders as well. So, you can keep this script on the root level of search and simply execute it.

=====================================
searchjars.sh
=====================================


#!/bin/sh

if [ $# -ne 1 ];
then
echo "Usage: ./searchjars.sh <keyword>"
exit
fi

LOOK_FOR="$1"

for i in `find . -name "*jar"`
do
#echo "Looking in $i ..."
jar tvf $i | grep $LOOK_FOR > /dev/null
if [ $? == 0 ]
then
echo "==> Found \"$LOOK_FOR\" in $i"
fi
done


After saving this file, don't forget to give the execute permissions to this script.

[jboss@lin01 xlclient]$chmod +x searchjars.sh

Now you are ready to execute as follows:

If you don't specify any value, the script shows you the usage command:
[jboss@lin01 xlclient]$ ./searchjars.sh
Usage: ./searchjars.sh <keyword>

When you specify the value to be searched, you will see the files that have that value in it.
[jboss@lin01 xlclient]$ ./searchjars.sh server
==> Found "server" in ./java/lib/rt.jar
==> Found "server" in ./ext/jdbcpool-0.99.jar
==> Found "server" in ./ext/nexaweb-nfc-api.jar
==> Found "server" in ./ext/jai_core.jar
==> Found "server" in ./ext/javagroups-all.jar
==> Found "server" in ./ext/jbossall-client.jar
==> Found "server" in ./ext/jboss-client.jar
==> Found "server" in ./ext/nexaweb-common.jar
==> Found "server" in ./ext/soap.jar
==> Found "server" in ./lib/xlVO.jar
==> Found "server" in ./lib/XellerateClient.jar
[jboss@lin01 xlclient]$

If you need to see what files are being looked at, uncomment (remove the #) the following line in the script:
#echo "Looking in $i ..."

Thursday, January 24, 2008

Installing OIM as a Windows Service

On Production, if you want Xellerate to run as a Windows Service so that it can be restarted automatically on a server restart, you can do the following things.

1. Get JavaService.exe from the web (open source project) and deploy it in your JBOSS\bin directory.
2. Use the following script to install this as a web serivce. The following script will need some tweaking of parameters depending on your environment:
================================================
Here is the script
================================================

@echo off
setlocal

rem
rem -------------------------------------------------------
rem This script sets the JBoss instance for OIM to run
rem as a windows service. You may need to tweak the
rem variables below to properly define your installation.
rem
rem
rem
rem -------------------------------------------------------
rem

rem This sets the memory usage settings passed to the JVM
set MEM_ARGS=-Xmx1024m

rem This is the directory where OIM is installed in
set XL_HOME=C:\xellerate\xlserver\xellerate

rem This is the directory where JBoss is installed in
set JB_HOME=C:\xellerate\jboss-4.0.3SP1

rem This is the base directory of the JDK installation for JBOSS
set JAVA_HOME=C:\java\j2sdk1.4.2_13\jre

rem This is the name of the Windows service
set SERVICE_NAME=JBoss-OIM

rem
rem -------------------------------------------------------
rem End of common variables you may need to tweak
rem -------------------------------------------------------
rem

echo Attempting to stop and remove old OIM / JBoss service
net stop %SERVICE_NAME% /y
javaservice.exe -uninstall %SERVICE_NAME%
echo Done.

echo Attempting to install and start OIM / JBoss service
mkdir %JB_HOME%\logs
copy /f javaservice.exe %JB_HOME%\bin
cd %JB_HOME%\bin
javaservice.exe -install %SERVICE_NAME% %JAVA_HOME%\bin\client\jvm.dll %MEM_ARGS% -Djava.class.path="%JAVA_HOME%\lib\tools.jar;%JB_HOME%\bin\run.jar" -DXL.HomeDir=%XL_HOME% -Djava.awt.headless=true -start org.jboss.Main -stop org.jboss.Main -method systemExit -out %JB_HOME%\logs\console.log -err %JB_HOME%\logs\console.log -current %JB_HOME%\bin -path "%PATH%" -auto

net start %SERVICE_NAME%
echo Done.
endlocal



================================================
Here is how it will get installed.
================================================


================================================
Here is how it looks after getting installed as a Windows Service.
================================================


Notes:
================================================
1. Also copy javaservice.exe & jvm.dll to C:\<WindowsInstallFolder>\system32 (say C:\Windows\system32) as after restart if these files are not in path, the windows service will fail to start.