All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.neo4j.codegen.source.ClasspathHelper Maven / Gradle / Ivy

There is a newer version: 5.26.0
Show newest version
/*
 * Copyright (c) "Neo4j"
 * Neo4j Sweden AB [http://neo4j.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j 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 3 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, see .
 */
package org.neo4j.codegen.source;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

final class ClasspathHelper
{
    private ClasspathHelper()
    {
        throw new AssertionError( "Not for instantiation!" );
    }

    static String javaClasspathString()
    {
        return System.getProperty( "java.class.path" );
    }

    static Set javaClasspath()
    {
        String[] classpathElements = javaClasspathString().split( File.pathSeparator );
        Set result = new LinkedHashSet<>();

        for ( String element : classpathElements )
        {
            result.add( canonicalPath( element ) );
        }

        return result;
    }

    static String fullClasspathStringFor( ClassLoader classLoader )
    {
        Set classpathElements = fullClasspathFor( classLoader );
        return formClasspathString( classpathElements );
    }

    static Set fullClasspathFor( ClassLoader classLoader )
    {
        Set result = new LinkedHashSet<>();

        result.addAll( javaClasspath() );

        ClassLoader loader = classLoader;
        while ( loader != null )
        {
            if ( loader instanceof URLClassLoader )
            {
                for ( URL url : ((URLClassLoader) loader).getURLs() )
                {
                    result.add( canonicalPath( url ) );
                }
            }
            loader = loader.getParent();
        }

        return result;
    }

    private static String canonicalPath( URL url )
    {
        return canonicalPath( url.getPath() );
    }

    private static String canonicalPath( String path )
    {
        try
        {
            File file = new File( path );
            return file.getCanonicalPath();
        }
        catch ( IOException e )
        {
            throw new RuntimeException( "Failed to get canonical path for: '" + path + "'", e );
        }
    }

    private static String formClasspathString( Set classPathElements )
    {
        StringBuilder classpath = new StringBuilder();

        Iterator classPathElementsIterator = classPathElements.iterator();
        while ( classPathElementsIterator.hasNext() )
        {
            classpath.append( classPathElementsIterator.next() );
            if ( classPathElementsIterator.hasNext() )
            {
                classpath.append( File.pathSeparator );
            }
        }

        return classpath.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy