Archive for March, 2011

Scanning directories for files in java – the iterator way

Posted on March 24th, 2011 by admin  |  No Comments »

This program I am sharing here can be used to get all files inside a specified root directory and its sub-directories and generate a list of files. There are other articles on this problems that generates file list using an recursive method call and. Function is called recursively each time a folder or folder is encountered. The problem with this approach is when number of files and directory level increases the efficiency becomes very poor, for cases like C:\WINDOWS or C:\Program Files that code failed and RAM /disk usage reaches to 100%.

The one here I am sharing is very cute code which will work like an iterator in collection framework, when you call the next() method all files in that root will be returned and root directory will be changed to sub-directory, a subsequent call will thus fetch files from next level directory. There is no question of overloading the computer here because now you are getting the files in step by step. I have tested this code with C:\WINDOWS and doing a right click property check for that folder the number from windows console turns out to be exactly the same.

======== code ======

package com.columnit.filenotify.scanner;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import org.apache.log4j.Logger;

import com.columnit.filenotify.solrj.SolrjServer;

/**
 * Scanning files recursively from a directory
 *
 * @author Abhishek Kaushik
 */
public class NewScanFiles{

	public  static boolean DOINDEX = true;
	public int level = 0; //Directory level
	public int count = 0; //Accumulative number of files
	public  static HashMap<Integer, HashMap<Integer, String>> schemaRoot = new HashMap<Integer, HashMap<Integer, String>>();
	public static int currentFolderIndex = 0; //Identifies folders in same level
	public static int countLevelFolders = 1; //For initial root directory
	private static Logger log = Logger.getLogger(SolrjServer.class);

	public static void main(String[] args){
		DOINDEX = true;
		try {
			NewScanFiles nsf = new NewScanFiles("C:\\Windows");
			while(nsf.hasNext()){
				//
				nsf.next();
			}
			System.out.println("count="+nsf.count);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public NewScanFiles(String folderUrl) throws FileNotFoundException{
		File file = new File(folderUrl);
		if(!file.exists()){
			throw new FileNotFoundException();
		}
		if(DOINDEX){
			HashMap<Integer, String> hm = new HashMap<Integer, String>();
			hm.put(0, folderUrl);
			schemaRoot.put(0, hm);
		}
	}
	public String getNextRoot(){
		return schemaRoot.get(level).get(currentFolderIndex);
	}
	public boolean hasNext(){
		return schemaRoot.get(level).size() != 0;
	}
	public Collection<String> next() {
		Collection<String> all = new ArrayList<String>();

		String folder = getNextRoot();
		HashMap<Integer, String> hm = new HashMap<Integer, String>();
		File file = new File(folder);
		try{
			File[] childs = file.listFiles();
			int folderCount = 0;
			for(int i=0; i<childs.length; i++){
				if(childs[i].isFile()){
					all.add(childs[i].getPath());
					count++;
				}else{
					hm.put(folderCount, childs[i].getPath());
					folderCount++;
				}
			}
			if(currentFolderIndex == 0){
				schemaRoot.put(level+1, hm);
			}else{
				HashMap<Integer, String> thm = schemaRoot.get(level+1);
				int existingCount = thm.size();
				for(int i=0; i<hm.size(); i++){
					thm.put(existingCount+i, hm.get(i));
				}
			}
			if(currentFolderIndex < countLevelFolders - 1){
				currentFolderIndex++;
			}else{
				currentFolderIndex = 0;
				countLevelFolders = schemaRoot.get(level+1).size();
				level++;
			}
			schemaRoot.remove(level - 1);
			log.info("folder="+folder+" level="+level+" currentFolderIndex="+currentFolderIndex);
//			System.out.println("folder="+folder+" level="+level+" currentFolderIndex="+currentFolderIndex);
			return  all;
		}catch(NullPointerException e){
//			e.printStackTrace();
			log.error("Failed to get content of : "+folder+e.getStackTrace());
			currentFolderIndex++;
		}catch(Exception e){
//			e.printStackTrace();
			log.error("Exception caught: "+ e.getStackTrace());
		}
		return null;
	}
}

Secure practices for ajax/jquery links

Posted on March 23rd, 2011 by admin  |  No Comments »

Web 2.0 has ajax capability which is today very fast adopted by major portion of web. Ajax technology is made child’s play by jquery libraries. Regular hyperlinks can be converted into ajax links just by giving id/class name to it and then attaching a event listener to that. While initial jquery developers tends to get excited with this feature and start utilizing this feature site wide not knowing the fallacy with the approach.

If you see ajax using sites like orkut and many others then you will find that no websites are creating a direct hyperlink with href information in them. Rather doing mouse over them shows void(0). This is important it is a better alternative than using “#” in the hyper link which redirects browser url by appending “#” to it and everything then becomes unclickable.

there are two approaches here set the href property of a anchor to void(0) and for geenrating proper event keep them inside onclick event or bind the class/id using jquery event listener.

<a href=”javascript:void(0)” onclick=”doSomeThing()”>click me</a>

or

<a href=”javascript:void(0)” class=”delete”>click me</a>

This is a good practice and advantages are that the information inside your links are hidden and no one can do “copy link URL” and past it on the browser window. Which makes ajax calls useless.