⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 directorymodel.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
/*
 *  SSL-Explorer
 *
 *  Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
			
package com.sslexplorer.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.sslexplorer.boot.Util;

/**
 * @author Brett Smith <brett@3sp.com>
 */
public class DirectoryModel implements Serializable {
    
    static final long serialVersionUID = 1014394281853915842L;

    public final static int NEW_FILE = 0;
    public final static int REMOVED_FILE = 1;
    public final static int CHANGED_FILE = 2;

    private Map files;
    private File rootFile;

    /**
     *  
     */
    public DirectoryModel() {
        super();
    }

    public void load(File rootFile) throws IOException {
        files = new TreeMap();
        this.rootFile = rootFile;
        recurseDir(rootFile, files);
    }

    public List compare(DirectoryModel model) {
        List changes = new ArrayList();

        //  First get a list of files that exist in this model
        Map.Entry entry = null;
        FileItem thisItem = null;
        FileItem oppositeItem = null;
        for (Iterator i = files.entrySet().iterator(); i.hasNext();) {
            entry = (Map.Entry) i.next();
            thisItem = (FileItem) entry.getValue();
            oppositeItem = (FileItem) model.files.get(entry.getKey());
            if (oppositeItem != null) {
                if (oppositeItem.getLastModified() != thisItem.getLastModified()) {
                    changes.add(new Change(CHANGED_FILE, thisItem.getFile()));
                }
                // File exists in this model and
            } else {
                changes.add(new Change(NEW_FILE, thisItem.getFile()));
            }
        }

        //  Now get a list of files that exist in the opposite model
        for (Iterator i = model.files.entrySet().iterator(); i.hasNext();) {
            entry = (Map.Entry) i.next();
            thisItem = (FileItem) entry.getValue();
            oppositeItem = (FileItem) files.get(entry.getKey());
            if (oppositeItem == null) {
                changes.add(new Change(REMOVED_FILE, thisItem.getFile()));
            }
        }

        return changes;
    }
    
    public void dump() {
        //  First get a list of files that exist in this model
        Map.Entry entry = null;
        FileItem thisItem = null;
        System.out.println("Model for " + rootFile.getAbsolutePath());
        for (Iterator i = files.entrySet().iterator(); i.hasNext();) {
            entry = (Map.Entry) i.next();
            thisItem = (FileItem) entry.getValue();;
            System.out.println("    " + thisItem.getFile().getAbsolutePath() + " [" +
                            thisItem.getLastModified() + "]");
        }        
    }

    //    private void iterate(FileList list, DirectoryModel model) {
    //        for(Iterator i = rootList.iterator(); i.hasNext(); ) {
    //            Object o = i.next();
    //            if(o instanceof FileList) {
    //                iterate((FileList)o);
    //            }
    //            else {
    //                
    //            }
    //        }
    //        
    //    }

    private void recurseDir(File file, Map map) throws IOException {
        if (!file.isDirectory()) {
            throw new IOException(file.getAbsolutePath() + " is not a directory.");
        }
        files.put(file.getAbsolutePath(), new FileItem(file));
        File[] children = file.listFiles();
        if (children != null) {
            for (int i = 0; i < children.length; i++) {
                if (children[i].isFile()) {
                    files.put(children[i].getAbsolutePath(), new FileItem(children[i]));
                } else {
                    recurseDir(children[i], map);
                }
            }
        }

    }
    
    public void dumpChanges(List changes) {
        System.out.println("Changes ...");
        for(Iterator i = changes.iterator(); i.hasNext(); ) {
            Change c = (Change)i.next();
            String t = c.getChange() == NEW_FILE ? "New File" :
                ( c.getChange() == CHANGED_FILE ? "Changed File" : "Removed file");
            System.out.println("    " + t + " [" + c.getFile().getAbsolutePath() + "] ");
        }
    }
    
    
    public static void main(String[] args) throws Exception {
        File cacheFile = new File("/home/brett/CACHE");
        File dir = new File("/home/brett/Desktop/Temp/Xtra");
        if(!cacheFile.exists()) {
            DirectoryModel model = new DirectoryModel();
            model.load(dir);
            ObjectOutputStream out = null;
            try {
                out = new ObjectOutputStream(new FileOutputStream(cacheFile));
                out.writeObject(model);
            }
            finally {
                Util.closeStream(out);
            }
            System.out.println("Cache file created, now change the directory " + 
                            dir.getAbsolutePath() + " in some way and re-run " + 
                            "this class to get a comparison.");
        }
        else {
            DirectoryModel model = new DirectoryModel();
            model.load(dir);
            
            ObjectInputStream in = null;
            DirectoryModel oppositeModel = null;
            try {
                in = new ObjectInputStream(new FileInputStream(cacheFile));
                oppositeModel = (DirectoryModel)in.readObject();
                System.out.println("Comparing");
                model.dumpChanges(model.compare(oppositeModel));                
            }
            finally {
                Util.closeStream(in);
            }
            
        }
    }

    public class FileItem implements Serializable {
        private static final long serialVersionUID = -4063581789279571280L;
        private File file;
        private long lastModified;

        public FileItem(File file) {
            this.file = file;
            this.lastModified = file.lastModified();
        }

        public long getLastModified() {
            return lastModified;
        }

        public File getFile() {
            return file;
        }
    }

    public class Change {
        private int change;
        private File file;

        public Change(int change, File file) {
            this.change = change;
            this.file = file;
        }

        public int getChange() {
            return change;
        }

        public File getFile() {
            return file;
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -