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.api.debugger.jpda.test;

import org.netbeans.spi.debugger.jpda.SourcePathProvider;
import org.netbeans.spi.debugger.ContextProvider;
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
import org.netbeans.api.debugger.Session;
import org.netbeans.api.java.classpath.GlobalPathRegistry;
import org.netbeans.api.java.classpath.ClassPath;
import org.openide.filesystems.*;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.File;

/**
 * An engine context suited for automatic test environment. 
 *
 * @author Maros Sandor
 */
public class TestEngineContextProvider extends SourcePathProvider {

    private Session         session;
    private ClassPath       sourcePath;
    private String[]        originalSourceRoots;
    private PropertyChangeSupport pcs = new PropertyChangeSupport(this);

    public TestEngineContextProvider (ContextProvider ctxProvider) {
        this.session = (Session) ctxProvider.lookupFirst(null, Session.class);
        sourcePath = (ClassPath) session.lookupFirst (null, ClassPath.class);
        if (sourcePath == null) {
            Set s = GlobalPathRegistry.getDefault().getPaths(ClassPath.SOURCE);
            sourcePath = ClassPathSupport.createProxyClassPath((ClassPath[]) s.toArray (new ClassPath [s.size ()]));
        }
        HashSet s = new HashSet (Arrays.asList (getRoots (sourcePath)));
//        s.addAll (Arrays.asList (getRoots (JavaPlatform.getDefault ().getSourceFolders ())));
        originalSourceRoots = (String[]) s.toArray (new String [s.size ()]);
    }

    /**
     * Translates a relative path (java/lang/Thread.java) to url.
     *
     * @param relativePath a relative path (java/lang/Thread.java)
     * @return url
     */
    public String getURL (String relativePath) {
        ClassLoader cl = this.getClass().getClassLoader();
        URL url = cl.getResource(relativePath);
        if (url == null) return null;
        return url.toString();
    }


    /**
     * Returns relative path for given url.
     *
     * @param url a url of resource file
     * @param directorySeparator a directory separator character
     * @param includeExtension whether the file extension should be included
     *        in the result
     *
     * @return relative path
     */
    public String getRelativePath(String url, char directorySeparator, boolean includeExtension) {

        try {
            URL u = new URL(url);
            String path = u.getPath();
            int idx = path.indexOf("org/netbeans/api/debugger/jpda");
            if (idx == -1) {
                idx = path.indexOf("basic/");
            }
            path = path.substring(idx);
            if (!includeExtension) {
                idx = path.lastIndexOf(".");
                if (idx != -1) path = path.substring(0, idx);
            }
            path = path.replace('/', directorySeparator);
            return path;
        } catch (MalformedURLException e) {
            return null;
        }
    }

    /**
     * Returns set of original source roots.
     *
     * @return set of original source roots
     */
    public String[] getOriginalSourceRoots () {
        return originalSourceRoots;
    }

    /**
     * Returns array of source roots.
     *
     * @return array of source roots
     */
    public String[] getSourceRoots () {
        return getRoots (sourcePath);
    }

    /**
     * Sets array of source roots.
     *
     * @param sourceRoots a new array of sourceRoots
     */
    public void setSourceRoots (String[] sourceRoots) {
        int i, k = sourceRoots.length;
        FileObject[] fos = new FileObject [k];
        for (i = 0; i < k; i++)
            fos [i] = getFileObject (sourceRoots [i]);
        Object old = sourcePath;
        sourcePath = ClassPathSupport.createClassPath (fos);
        pcs.firePropertyChange (PROP_SOURCE_ROOTS, old, sourcePath);
    }

    /**
     * Adds property change listener.
     *
     * @param l new listener.
     */
    public void addPropertyChangeListener (PropertyChangeListener l) {
        pcs.addPropertyChangeListener (l);
    }

    /**
     * Removes property change listener.
     *
     * @param l removed listener.
     */
    public void removePropertyChangeListener (
        PropertyChangeListener l
    ) {
        pcs.removePropertyChangeListener (l);
    }

    private static String[] getRoots (ClassPath cp) {
        FileObject[] fos = cp.getRoots ();
        int i, k = fos.length;
        List l = new ArrayList ();
        for (i = 0; i < k; i++) {
            File f = null;
            try {
                if (fos [i].getFileSystem () instanceof JarFileSystem)
                    f = ((JarFileSystem) fos [i].getFileSystem ()).
                        getJarFile ();
                else
                    f = FileUtil.toFile (fos [i]);
            } catch (FileStateInvalidException ex) {
            }
            if (f != null)
                l.add (f.getAbsolutePath ());
        }
        String[] fs = new String [l.size ()];
        return (String[]) l.toArray (fs);
    }

    private FileObject getFileObject (String file) {
        File f = new File (file);
        FileObject fo = FileUtil.toFileObject (f);
        if ( file.endsWith (".jar") ||
             file.endsWith (".zip"))

            fo = FileUtil.getArchiveRoot (fo);
        return fo;
    }
}
... 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.