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-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.java.j2seproject.classpath;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.spi.java.classpath.ClassPathFactory;
import org.netbeans.spi.java.classpath.ClassPathProvider;
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
import org.netbeans.spi.project.support.ant.AntProjectHelper;
import org.netbeans.spi.project.support.ant.AntProjectListener;
import org.netbeans.spi.project.support.ant.AntProjectEvent;
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.WeakListeners;

/**
 * Defines the various class paths for a J2SE project.
 */
public final class ClassPathProviderImpl implements ClassPathProvider, PropertyChangeListener {
    
    private static final String SRC_DIR = "src.dir"; // NOI18N
    private static final String TEST_SRC_DIR = "test.src.dir"; // NOI18N
    private static final String BUILD_CLASSES_DIR = "build.classes.dir"; // NOI18N
    private static final String DIST_JAR = "dist.jar"; // NOI18N
    private static final String BUILD_TEST_CLASSES_DIR = "build.test.classes.dir"; // NOI18N
    
    private final AntProjectHelper helper;
    private final PropertyEvaluator evaluator;
    private final ClassPath[] cache = new ClassPath[8];

    private final Map/**/ dirCache = new HashMap();

    public ClassPathProviderImpl(AntProjectHelper helper, PropertyEvaluator evaluator) {
        this.helper = helper;
        this.evaluator = evaluator;
        evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator));
    }

    private synchronized FileObject getDir(String propname) {
        FileObject fo = (FileObject) this.dirCache.get (propname);
        if (fo == null ||  !fo.isValid()) {
            String prop = evaluator.getProperty(propname);
            if (prop != null) {
                fo = helper.resolveFileObject(prop);
                this.dirCache.put (propname, fo);
            }
        }
        return fo;
    }
    
    private FileObject getPrimarySrcDir() {
        return getDir(SRC_DIR);
    }
    
    private FileObject getTestSrcDir() {
        return getDir(TEST_SRC_DIR);
    }
    
    private FileObject getBuildClassesDir() {
        return getDir(BUILD_CLASSES_DIR);
    }
    
    private FileObject getDistJar() {
        return getDir(DIST_JAR);
    }
    
    private FileObject getBuildTestClassesDir() {
        return getDir(BUILD_TEST_CLASSES_DIR);
    }
    
    /**
     * Find what a given file represents.
     * @param file a file in the project
     * @return one of: 
*
0
normal source
*
1
test source
*
2
built class (unpacked)
*
3
built test class
*
4
built class (in dist JAR)
*
-1
something else
*
*/ private int getType(FileObject file) { FileObject dir = getPrimarySrcDir(); if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir, file))) { return 0; } dir = getTestSrcDir(); if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir, file))) { return 1; } dir = getBuildClassesDir(); if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir, file))) { return 2; } dir = getDistJar(); // not really a dir at all, of course if (dir != null && dir.equals(FileUtil.getArchiveFile(file))) { // XXX check whether this is really the root return 4; } dir = getBuildTestClassesDir(); if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) { return 3; } return -1; } private ClassPath getCompileTimeClasspath(FileObject file) { int type = getType(file); return this.getCompileTimeClasspath(type); } private ClassPath getCompileTimeClasspath(int type) { if (type < 0 || type > 1) { // Not a source file. return null; } ClassPath cp = cache[2+type]; if ( cp == null) { if (type == 0) { cp = ClassPathFactory.createClassPath( new ProjectClassPathImplementation(helper, "javac.classpath", evaluator)); // NOI18N } else { cp = ClassPathFactory.createClassPath( new ProjectClassPathImplementation(helper, "javac.test.classpath", evaluator)); // NOI18N } cache[2+type] = cp; } return cp; } private ClassPath getRunTimeClasspath(FileObject file) { int type = getType(file); if (type < 0 || type > 4) { // Unregistered file, or in a JAR. // For jar:file:$projdir/dist/*.jar!/**/*.class, it is misleading to use // run.classpath since that does not actually contain the file! // (It contains file:$projdir/build/classes/ instead.) return null; } else if (type > 1) { type-=2; //Compiled source transform into source } ClassPath cp = cache[4+type]; if ( cp == null) { if (type == 0) { cp = ClassPathFactory.createClassPath( new ProjectClassPathImplementation(helper, "run.classpath", evaluator)); // NOI18N } else if (type == 1) { cp = ClassPathFactory.createClassPath( new ProjectClassPathImplementation(helper, "run.test.classpath", evaluator)); // NOI18N } else if (type == 2) { //Only to make the CompiledDataNode hapy //Todo: Strictly it should return ${run.classpath} - ${build.classes.dir} + ${dist.jar} cp = ClassPathFactory.createClassPath( new ProjectClassPathImplementation(helper, DIST_JAR, evaluator)); // NOI18N } cache[4+type] = cp; } return cp; } private ClassPath getSourcepath(FileObject file) { int type = getType(file); return this.getSourcepath(type); } private ClassPath getSourcepath(int type) { if (type < 0 || type > 1) { // Unknown. return null; } ClassPath cp = cache[type]; if ( cp == null ) { if (type == 0) { cp = ClassPathFactory.createClassPath( new ProjectClassPathImplementation(helper, SRC_DIR, evaluator)); // NOI18N } else { cp = ClassPathFactory.createClassPath( new ProjectClassPathImplementation(helper, TEST_SRC_DIR, evaluator)); // NOI18N } cache[type] = cp; } return cp; } private ClassPath getBootClassPath() { ClassPath cp = cache[7]; if ( cp== null ) { cp = ClassPathFactory.createClassPath(new BootClassPathImplementation(helper, evaluator)); cache[7] = cp; } return cp; } public ClassPath findClassPath(FileObject file, String type) { if (type.equals(ClassPath.COMPILE)) { return getCompileTimeClasspath(file); } else if (type.equals(ClassPath.EXECUTE)) { return getRunTimeClasspath(file); } else if (type.equals(ClassPath.SOURCE)) { return getSourcepath(file); } else if (type.equals(ClassPath.BOOT)) { return getBootClassPath(); } else { return null; } } /** * Returns array of all classpaths of the given type in the project. * The result is used for example for GlobalPathRegistry registrations. */ public ClassPath[] getProjectClassPaths(String type) { if (ClassPath.BOOT.equals(type)) { return new ClassPath[]{getBootClassPath()}; } if (ClassPath.COMPILE.equals(type)) { ClassPath[] l = new ClassPath[2]; l[0] = getCompileTimeClasspath(0); l[1] = getCompileTimeClasspath(1); return l; } if (ClassPath.SOURCE.equals(type)) { ClassPath[] l = new ClassPath[2]; l[0] = getSourcepath(0); l[1] = getSourcepath(1); return l; } assert false; return null; } public void propertyChange(PropertyChangeEvent evt) { dirCache.remove(evt.getPropertyName()); } }
... 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.