BaseMapDrive.java 3.21 KB
Newer Older
Naung1 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
package com.csc.library.drive;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.csc.library.session.InitialEnvironment;
import com.csc.library.utilities.MyLog;
/***
 * 
 * @author odragonfireo
 * Base Map drive on windows
 */
public class BaseMapDrive implements DriveImpl {

	public static String user="";
	public static String pwd="";
	public static String path="";
	public static String drive="";
	public static boolean havedrive=false;
	public static BaseMapDrive mapDrive=null;
	/**
	 * Ǩͺ öҹ
	 */
	public boolean canRead(String drive) throws Exception {
		long millisec=System.currentTimeMillis();
		File f=new File(drive);
		if(f.canRead()){
			millisec=System.currentTimeMillis()-millisec;
			MyLog.info("OK["+millisec+"]!!!!");
			return true;
		}else{
			MyLog.info("FAIL TO CONNECT MAP DRIVE!!!!");
			return false;
		}
	}

	/**
	 * ӡź map drive
	 */
	public void disConnectMapDrive(String drive) throws Exception {
		String command = "net use " + drive + ": /delete";
		excuteCommand(command,"");
	}

	/**
	 * ӡ map drive ҧ
	 * net use x: \\\\192.168.1.212\\d$\\EnterprisePT\\Programmer /user:administrator abc
	 */
	public boolean mapDrive(String drive, String path, String user, String pwd)
			throws Exception {
		
		String command ="net use " + drive + ": " +path + " /user:" + user;
		return excuteCommand(command,pwd);		
	}

	   
    public boolean excuteCommand(String param,String pwd){
		try{
			boolean flag = false;
			MyLog.info("Running: " + param);
			param+=" "+pwd;
			StringBuffer sb = new StringBuffer();
			Process process = Runtime.getRuntime().exec(param);
			InputStream standardInput = process.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(
			standardInput));
			InputStream standardError = process.getErrorStream();
			BufferedReader brError = new BufferedReader(new InputStreamReader(
			standardError));
			String s;
			while ((s = br.readLine()) != null){
				sb.append(s + "\n");
				flag = true;
			}
			while ((s = brError.readLine()) != null){
				sb.append(s + "\n");
				flag = false;
			}
			standardInput.close();
			standardError.close();
			return flag;
		}catch (Exception e){
			System.out.println(e);
			return false;
		}
	}    
	
    public boolean scanDrive() throws Exception {
		return true;
	}
    
	public DriveImpl init() {
			DriveImpl mapnetwork=null;
		try {
			InitialEnvironment inv = new InitialEnvironment("GLOBAL");
			String ClassName=inv.getValue("mapdrive-classname");

			if(ClassName.length()==0){
					mapnetwork = (DriveImpl)Class.forName("com.csc.library.drive.WindowsMapDrive").newInstance();
				}else{
					mapnetwork=(DriveImpl)Class.forName("com.csc.library.drive."+ClassName).newInstance();
			}
		} catch (Exception e) {
			MyLog.error(this, e);
		}
		return mapnetwork;
	}   
	
	public void initEnv(){
		if(!havedrive){
			InitialEnvironment env=new InitialEnvironment("GLOBAL");
			user  = env.getValue("mapdrive-user");
			if(user==""){
				havedrive=false;
				return ;
			}
			pwd   = env.getValue("mapdrive-password");
			path  = env.getValue("mapdrive-path");
			drive = env.getValue("mapdrive-drive");
			havedrive=true;
		}
	}
}