Wednesday, December 15, 2010

The great book on Core Java...

Finally, I came to know that the Thread.stop() methods generates the ThreadDeath object and how to use the interrupted flag...

The great! great! book on Core Java:

Core Java - Volume 1 - Fundamentals
  by: Cay S. Horstmann & Gary Cornell
  PEARSON Education, Sun Microsystems

Saturday, May 30, 2009

Remove gphone.exe virus by Java Program

import java.io.File;
import java.io.FilenameFilter;

/**
* This programs removes files created by the gphone.exe virus
*
* This virus creates duplicate copies of itself (gphone.exe) into every folder
* of your system with the name that folder.
* The common behavior is that all virus files share the same size
* and the same lastModified date.
*
* Notes:
* + You may have to change the virusFileSize and lastModified variables below.
* + un-comment the "files[i].delete()" below to enable the deletion.
* + on a unix machine, following command is also useful
* + find . -name "*.exe" -printf "%s\t\t%f\n"
*
* @author ujariya
* @version 1.0 (30th May 2009)
*/

public class VirusExeDelete
{
static int virusCounter = 0;
// 1232372178000 => Mon Jan 19 19:06:18 IST 2009
// This time is in milliseconds
static long lastModified = new Long("1232372178000").longValue();
static int virusFileSize = 349184;

public static void main(String[] args)
{
System.out.println("Searching and Removing");
//File root = new File("/mnt/S_VIDEO");

// Run the search into every partition / PEN drives
findAndDeleteVirusFiles(new File("/mnt/S_DATA"));
findAndDeleteVirusFiles(new File("/mnt/S_MISC"));
findAndDeleteVirusFiles(new File("/mnt/S_MOVIES"));
findAndDeleteVirusFiles(new File("/mnt/S_OPSYS"));
findAndDeleteVirusFiles(new File("/mnt/S_SANGEET"));
findAndDeleteVirusFiles(new File("/mnt/S_TEMP"));
findAndDeleteVirusFiles(new File("/mnt/S_VIDEO"));

System.out.println("virusCounter: "+ virusCounter);
}

static void findAndDeleteVirusFiles(File dir)
{
File[] files = dir.listFiles();
for(int i=0; i
{
// Iterate recursively
if(files[i].isDirectory())
{
findAndDeleteVirusFiles(files[i]);
}

// Search / Destroy the virus
else if(files[i].getName().toLowerCase().endsWith(".exe")
&& files[i].length() == virusFileSize)
{
System.out.println(files[i].length()+ " "+ files[i].lastModified()+" " +files[i].getPath());
// delete viruses
// files[i].delete();
virusCounter++;
}

// Searching other files having same last modified date (within one minute) / file size
/* else if(Math.abs(files[i].lastModified() - lastModified) <>
{
System.out.println(files[i].length()+ " "+ files[i].lastModified()+" " +files[i].getPath());
virusCounter++;
}
*/
}
}
}

Friday, September 14, 2007

the describe method of the DataObjects...

Joy ho!

I written and tested a Java code recently... (yes, it works :-) )
Its a generic describe method for Data Objects which uses reflect API provided in Java.
This works even if you have a hierarchy of Data Objects.

Here is the code:

// Yeeeah! a code by UJ... the PowerBuilder.
public String describe()
{
// Output Format: ClassName [field1 = value1, field2 = value2, ...]
try
{
StringBuffer sbReturn = new StringBuffer();
// get the associated 'Class' object
Class theClass = this.getClass();

// appending the classname
sbReturn.append(theClass.getName().substring(theClass.getName().lastIndexOf(".")+1));
sbReturn.append(" [");

// Exclude the Object class...
while(theClass.getSuperclass() != null)
{
// Get all methods
Method methods[] = theClass.getDeclaredMethods();
for(int i=0; i
{
String methodName = methods[i].getName();
// if its a getter method...
if(methodName.startsWith("get"))
{
// get the field name
sbReturn.append(methodName.substring(3));
sbReturn.append(" = ");
// get value by invoking getter method
sbReturn.append(String.valueOf(methods[i].invoke(this, new Object[0])));
sbReturn.append(", ");
}
}
// get the parent class
theClass = theClass.getSuperclass();
}
// finally, remove the last comma & space appended
sbReturn.setLength(sbReturn.length() - 2);
// done.
sbReturn.append("]");
return sbReturn.toString();
}
catch(Exception ex)
{
return null;
}
}


And here is the sample output:

SpecDetailFormGateway [P4Path = xyz, P4Changelist = null, DataFormat = edi, DataFormatVersion = null, StrAction = null, SpecID = null, Editable = true, XPath = null, StrDetailType = GATEWAY, StrProductName = null, StrLECName = null, StrVersionID = null, GatewayName = ASR, Comments = Animesh, Servlet = null, MultipartRequestHandler = null]


knowledge shared is knowledge squared.
~ Jariya

PS: make sure to import java.lang.reflect.Method