alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.javacore.scanning;

import java.io.File;
import java.io.IOException;
import java.util.*;
import org.netbeans.jmi.javamodel.Codebase;
import org.netbeans.jmi.javamodel.JavaModelPackage;
import org.netbeans.jmi.javamodel.Resource;
import org.netbeans.mdr.NBMDRepositoryImpl;
import org.netbeans.modules.javacore.ClassIndex;
import org.netbeans.modules.javacore.ExclusiveMutex;
import org.netbeans.modules.javacore.JMManager;
import org.netbeans.modules.javacore.ProgressPanel;
import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
import org.openide.ErrorManager;


/**
 *
 * @author  Pavel Flaska
 */
public class FileScanner {
    
    private ClassUpdater classUpdater;
    private JavaUpdater javaUpdater;
    private File root;
    private Codebase codebase;
    private JavaModelPackage mofPackage;
    private Collection resources;
    private Collection resourcesToScan;
    private boolean zipFile;
    private final ExclusiveMutex mutex;
    private static HashSet ignoredPackages;
    private static HashSet eagerlyParse;
    
    private static final boolean DEBUG = false;
    
    public FileScanner(File _root, String sourceLevel, Codebase cb) {
        synchronized (FileScanner.class) {
            if (ignoredPackages == null) {
                ignoredPackages = parseSet("org.netbeans.javacore.ignorePackages", "sun sunw");
                eagerlyParse = parseSet("org.netbeans.javacore.eagerlyParse", "javax/swing/JFrame.java");
            }
        }
        
        String name=_root.getName();
        
        root = _root;
        codebase = cb;
        mofPackage=(JavaModelPackage)codebase.refImmediatePackage();
        mutex = JMManager.getTransactionMutex();
        if (name.endsWith(".jar") || name.endsWith(".zip")) {    //NOI18N
            zipFile=true;
        }
        javaUpdater=new JavaUpdater(mofPackage,sourceLevel, this);
        classUpdater=new ClassUpdater(mofPackage);
    }
    
    private static HashSet parseSet(String propertyName, String defaultValue) {
        StringTokenizer st = new StringTokenizer(System.getProperty(propertyName, defaultValue));
        HashSet result = new HashSet();
        while (st.hasMoreTokens()) {
            result.add(st.nextToken());
        }
        return result;
    }
    
    void checkParseEagerly(Resource resource) {
        if (eagerlyParse.contains(resource.getName())) {
            if (resourcesToScan == null) {
                resourcesToScan = new ArrayList(eagerlyParse.size());
            }
            resourcesToScan.add(resource);
        }
    }
    
    public Resource[] scan() {
        NBMDRepositoryImpl repository = (NBMDRepositoryImpl)JavaMetamodel.getDefaultRepository();
        FileInfo rootInfo;
        repository.disableEvents();
        resourcesToScan = null;
        if (zipFile) {
            long oldTime=codebase.getTimestamp();

            if (oldTime!=0) {
                ClassIndex index=ClassIndex.getIndex(mofPackage);
                long jarTime=root.lastModified();

                if (oldTime>=jarTime && index.getTimestamp()>=jarTime) {
//                        fail = false;
                    return null; // Jar is up to date
                }
            }
            try {
                rootInfo=new ZipArchiveInfo(root).getRootFileInfo();
            }catch (IOException ex) {
                ErrorManager.getDefault().notify(ex);
                return null;
            }
        } else
            rootInfo=new FileEntry(root,"");
        resources = new HashSet(mofPackage.getResource().refAllOfClass());
        // get all subfolders (recursively) of filesystem root
        scanPackage(rootInfo,"");
        if (zipFile) {
            codebase.setTimestamp(root.lastModified());
            codebase.setLibrary(true);
        }
        removeFromRepository();
        return resourcesToScan == null ? null : (Resource[]) resourcesToScan.toArray(new Resource[resourcesToScan.size()]);
    }
    
    private void removeFromRepository() {
        Iterator i = resources.iterator();
        
        while (i.hasNext()) {
            ((Resource) i.next()).refDelete(); 
        }
    }
    
    private static ProgressPanel progress;
    
    private void scanPackage(FileInfo directory,String pack) {
        if (ignoredPackages.contains(pack)) return;
        
        FileInfo files[] = directory.listFiles();
        Map javaFiles=new HashMap();
        Map classFiles=new HashMap();
        
        for(int i=0;i
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.