Friday, January 27, 2006

To Get Machine Name , IP Address , User Name in the network

The following program gives the Machine Name , IP Address , User Name of the windows network in which we run this program

package NET;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.TreeMap;


class TestNet {

TreeMap objTreeMap;

TestNet() {

objTreeMap = new TreeMap();

getAllNamesIPAddress();
printAll();

}

public void getAllNamesIPAddress(){
try {
Process objProcess = Runtime.getRuntime().exec("net view");

InputStreamReader objInputStreamReader = new InputStreamReader(objProcess.getInputStream());

BufferedReader objBufferedReader = new BufferedReader(objInputStreamReader);

String strLine;
String[] strSysUsrName;

while ((strLine = objBufferedReader.readLine()) != null) {

if ((strLine.length() > 0) && (strLine.charAt(0) == '\\')) {
strSysUsrName = strLine.trim().split(" ");
if (strSysUsrName.length > 1) {

String strIPAddress = getIPAddress(strSysUsrName[0].substring(2,strSysUsrName[0].length()));
StringBuffer strBuffUserName = new StringBuffer("");
for(int intIterStrLine=1;intIterStrLine
if(intIterStrLine>1){
strBuffUserName.append(" ");
}

strBuffUserName.append(strSysUsrName[intIterStrLine]);
}
objTreeMap.put(strLine,strIPAddress);
//objTreeMap.put(strBuffUserName.toString().trim().toLowerCase(),strIPAddress);
//System.out.println(strIPAddress + ":" + strBuffUserName.toString().trim());

}
}
}


} catch (IOException eIOException) {
eIOException.printStackTrace();
}

}
private String getIPAddress(String strSystemName) {
String strIPAddress = "";

try {
strIPAddress = InetAddress.getByName(strSystemName).getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}

return strIPAddress;
}
private void printAll(){
//System.out.println("Total Users : " + objTreeMap.size());
Iterator objIterator = objTreeMap.keySet().iterator();

while(objIterator.hasNext()){

String strKey =(String)objIterator.next();
System.out.println(strKey + " : " + objTreeMap.get(strKey));
}
}

public static void main(String[] args) {
TestNet objTestNet = new TestNet();
}
}

Monday, January 23, 2006

Cricket Field Settings


e h




2 j
43 1 d
5
6 # i c
#
7 # b
8

9 a




f g



1 wicket keeper
2 first slip
3 second slip
4 third slip
5 gully +
6 point +*~
7 cover +
8 extra cover +
9 mid-off +*
a mid-on +*
c square leg +~
d leg slip
e third man
f long off
g long on
h fine leg
i bat-pad

+ deep (near boundary)
* silly (near batsman)
~ backward (more 'up')
# Track

j deep backward square leg

source : http://www.cricinfo.com/link_to_database/ABOUT_CRICKET/EXPLANATION/EXPLANATION_OF_CRICKET.html

For Searching the existence of a Class file in .Jar & .War Files

The following program searches for the exitence of a class file in the Jar and War files located under given folder and subfolders.

-------
package JarFileSearch;

import java.io.File;
import java.io.IOException;

import java.util.Enumeration;
import java.util.Vector;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;


class ClassFileSearch {
Vector objJarFilesVector;
String strFolderName;
String strClassName;
Vector objVector;

ClassFileSearch(String strFolderName, String strClassName) {
objJarFilesVector = new Vector();
this.strFolderName = strFolderName;
this.strClassName = strClassName;
this.objVector = new Vector();
}

public static void main(String[] args) {
String strInFolderAndSubFolders = "C:/srikanth";
String strClassName = "DateUtility.class";

long l_startTime = System.currentTimeMillis();
ClassFileSearch objClassFileSearch = new ClassFileSearch(strInFolderAndSubFolders,
strClassName);
System.out.println("searching for " + strClassName +
" file in .jar and .war files available under " +
strInFolderAndSubFolders);
objClassFileSearch.findAllJarFiles(objClassFileSearch.getFile());
objClassFileSearch.findExitenceOfClass();

long l_endTime = System.currentTimeMillis();
System.out.println("Time Taken (in millis)" +
(l_endTime - l_startTime));
}

public File getFile() {
return new File(this.strFolderName);
}

public void findExitenceOfClass() {
FindRun[] objFindRun = new FindRun[objJarFilesVector.size()];

for (int i = 0; i < objJarFilesVector.size(); i++) {
objFindRun[i] = new FindRun(strClassName,
(String) objJarFilesVector.get(i));
objFindRun[i].start();

//System.out.println("searching --> " + (String)objJarFilesVector.get(i));
try {
objFindRun[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public void findAllJarFiles(File objFile) {
File[] objFileArray = objFile.listFiles();

for (int i = 0; i < objFileArray.length; i++) {
if (objFileArray[i].isDirectory()) {
findAllJarFiles(objFileArray[i]);
} else {
String[] strArray = objFileArray[i].getName().split("\\.");

if ((strArray != null) && (strArray.length > 0)) {
if (strArray[strArray.length - 1].equalsIgnoreCase("jar") ||
strArray[strArray.length - 1].equalsIgnoreCase(
"war")) {
objJarFilesVector.add(objFileArray[i].getAbsolutePath());

//objVector.add(new Thread(new FindRun(strClassName,objFileArray[i].getAbsolutePath())));
//((Thread)objVector.get(objVector.size()-1)).start();
}
}
}
} // end of: for
}
} // end of : ClassFileSearch class


class FindRun extends Thread {
String strClassName;
String strJarName;

FindRun(String strClassName, String strJarName) {
this.strClassName = strClassName;
this.strJarName = strJarName;
}

public void run() {
try {
JarFile objJarFile = new JarFile(new File((String) strJarName));
Enumeration objEnumeration = objJarFile.entries();

while (objEnumeration.hasMoreElements()) {
JarEntry objJarEntry = (JarEntry) objEnumeration.nextElement();

if (objJarEntry.getName().indexOf(strClassName) > 0) {
System.out.println(strJarName + " * " +
objJarEntry.getName());
}
}

//System.out.println("searching ..." + strJarName);
} catch (IOException eIOException) {
eIOException.printStackTrace();
}
}
} // end of : FindRun Class
------

Wednesday, January 18, 2006

JavaScript Event Handling

The captureEvents() method doesn't have any effect until you assign a function to handle the desired events. To capture all click events on a page, for example, you would need to define a function that handles the event, and then assign the function reference to the event handler. Here's an example:


function processClicks(e) {
// statements to handle the click events
}

window.captureEvents(Event.CLICK); // click is an event
window.onclick = processClicks; // onclick is an event handler

Without specifying a function to handle click events at the window level, all click events would simply be captured at that level, but would not have any influence because a captured event does not proceed to the intended target event handler
------------------------
-- for cross browsers---
------------------------

//FIRST, TELL THE BROWSERS TO REACT TO THE EVENT
if( document.captureEvents ) {
//non IE
if( Event.KEYUP ) {
//NS 4, NS 6+, Mozilla 0.9+
document.captureEvents( Event.KEYUP );
}
}
/* this next line tells the browser to detect a keyup
event over the whole document and when it detects it,
it should run the event handler function 'alertkey' */
document.onkeyup = alertkey;

//NOW CREATE THE EVENT HANDLER FUNCTION TO PROCESS THE EVENT
function alertkey(e) {
if( !e ) {
//if the browser did not pass the event information to the
//function, we will have to obtain it from the event register
if( window.event ) {
//DOM
e = window.event;
} else {
//TOTAL FAILURE, WE HAVE NO WAY OF REFERENCING THE EVENT
return;
}
}
if( typeof( e.which ) == 'number' ) {
//NS 4, NS 6+, Mozilla 0.9+, Opera
e = e.which;
} else if( typeof( e.keyCode ) == 'number' ) {
//IE, NS 6+, Mozilla 0.9+
e = e.keyCode;
} else if( typeof( e.charCode ) == 'number' ) {
//also NS 6+, Mozilla 0.9+
e = e.charCode;
} else {
//TOTAL FAILURE, WE HAVE NO WAY OF OBTAINING THE KEY CODE
return;
}
window.alert('The key pressed has keycode ' + e +
' and is key ' + String.fromCharCode( e ) );
}

Tuesday, January 17, 2006

Java Tiger Enum Example

package LangBasics.Enum;

import java.util.Scanner;

class EnumWithMethod
{
//You can declare the method abstract in the enum type and override it
//with a concrete method in each constant.
//Such methods are known as constant-specific methods

public enum MathEnumWithMethods {
ADD {
void doCalc(int i,int j)
{
System.out.println("+ -->");
System.out.println(i + j);
}
},
SUB {
void doCalc(int i,int j)
{
System.out.println("- -->");
System.out.println(i - j);
}
},
MUL {
void doCalc(int i,int j)
{
System.out.println("* -->");
System.out.println(i * j);
}
},
DIV {
void doCalc(int i,int j)
{
System.out.println("/ -->");
if(j!=0)
System.out.println(i / j);
else
System.out.println("Not a valid input");
}
};

abstract void doCalc(int x, int y);

} // end of : MathEnumWithMethods method

public static void main(String[] args)
{
EnumWithMethod objEnumeration = new EnumWithMethod();
Scanner in = new Scanner(System.in);
System.out.println("Enter First Number");
int i = in.nextInt();
System.out.println("Enter Second Number");
int j = in.nextInt();
System.out.println("Enter Operation ADD/SUB/MUL/DIV");
String strOper = in.next();
if(strOper.equalsIgnoreCase("ADD")){

MathEnumWithMethods.ADD.doCalc(i,j);

}else if(strOper.equalsIgnoreCase("SUB")){

MathEnumWithMethods.SUB.doCalc(i,j);

}else if(strOper.equalsIgnoreCase("MUL")){

MathEnumWithMethods.MUL.doCalc(i,j);

}else if(strOper.equalsIgnoreCase("DIV")){

MathEnumWithMethods.DIV.doCalc(i,j);

}else{
System.out.println(strOper + " is not a right choice");
}

} // end of : main method

} // end of : EnumWithMethod Class

This page is powered by Blogger. Isn't yours?

Subscribe to Comments [Atom]