Search in sources :

Example 76 with URL

use of java.net.URL in project groovy by apache.

the class SourceExtensionHandler method getRegisteredExtensions.

public static Set<String> getRegisteredExtensions(ClassLoader loader) {
    Set<String> extensions = new LinkedHashSet<String>();
    extensions.add("groovy");
    try {
        Enumeration<URL> globalServices = loader.getResources("META-INF/services/org.codehaus.groovy.source.Extensions");
        while (globalServices.hasMoreElements()) {
            BufferedReader svcIn = null;
            URL service = globalServices.nextElement();
            try {
                svcIn = new BufferedReader(new InputStreamReader(service.openStream()));
                String extension = svcIn.readLine();
                while (extension != null) {
                    extension = extension.trim();
                    if (!extension.startsWith("#") && extension.length() > 0) {
                        extensions.add(extension);
                    }
                    extension = svcIn.readLine();
                }
            } catch (IOException ex) {
                throw new GroovyRuntimeException("IO Exception attempting to load registered source extension " + service.toExternalForm() + ". Exception: " + ex.toString());
            } finally {
                if (svcIn != null)
                    svcIn.close();
            }
        }
    } catch (IOException ex) {
        throw new GroovyRuntimeException("IO Exception getting registered source extensions. Exception: " + ex.toString());
    }
    return extensions;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) URL(java.net.URL)

Example 77 with URL

use of java.net.URL in project groovy by apache.

the class Groovyc method buildClassLoaderFor.

protected GroovyClassLoader buildClassLoaderFor() {
    // GROOVY-5044
    if (!fork && !getIncludeantruntime()) {
        throw new IllegalArgumentException("The includeAntRuntime=false option is not compatible with fork=false");
    }
    ClassLoader parent = getIncludeantruntime() ? getClass().getClassLoader() : new AntClassLoader(new RootLoader(EMPTY_URL_ARRAY, null), getProject(), getClasspath());
    if (parent instanceof AntClassLoader) {
        AntClassLoader antLoader = (AntClassLoader) parent;
        String[] pathElm = antLoader.getClasspath().split(File.pathSeparator);
        List<String> classpath = configuration.getClasspath();
        /*
             * Iterate over the classpath provided to groovyc, and add any missing path
             * entries to the AntClassLoader.  This is a workaround, since for some reason
             * 'directory' classpath entries were not added to the AntClassLoader' classpath.
             */
        for (String cpEntry : classpath) {
            boolean found = false;
            for (String path : pathElm) {
                if (cpEntry.equals(path)) {
                    found = true;
                    break;
                }
            }
            /*
                 * fix for GROOVY-2284
                 * seems like AntClassLoader doesn't check if the file
                 * may not exist in the classpath yet
                 */
            if (!found && new File(cpEntry).exists()) {
                try {
                    antLoader.addPathElement(cpEntry);
                } catch (BuildException e) {
                    log.warn("The classpath entry " + cpEntry + " is not a valid Java resource");
                }
            }
        }
    }
    GroovyClassLoader loader = new GroovyClassLoader(parent, configuration);
    if (!forceLookupUnnamedFiles) {
        // in normal case we don't need to do script lookups
        loader.setResourceLoader(new GroovyResourceLoader() {

            public URL loadGroovySource(String filename) throws MalformedURLException {
                return null;
            }
        });
    }
    return loader;
}
Also used : MalformedURLException(java.net.MalformedURLException) AntClassLoader(org.apache.tools.ant.AntClassLoader) URL(java.net.URL) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyResourceLoader(groovy.lang.GroovyResourceLoader) RootLoader(org.codehaus.groovy.tools.RootLoader) AntClassLoader(org.apache.tools.ant.AntClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 78 with URL

use of java.net.URL in project flink by apache.

the class ClassLoaderUtilsTest method testWithURLClassLoader.

@Test
public void testWithURLClassLoader() {
    File validJar = null;
    File invalidJar = null;
    try {
        // file with jar contents
        validJar = File.createTempFile("flink-url-test", ".tmp");
        JarFileCreator jarFileCreator = new JarFileCreator(validJar);
        jarFileCreator.addClass(ClassLoaderUtilsTest.class);
        jarFileCreator.createJarFile();
        // validate that the JAR is correct and the test setup is not broken
        JarFile jarFile = null;
        try {
            jarFile = new JarFile(validJar.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
            fail("test setup broken: cannot create a valid jar file");
        } finally {
            if (jarFile != null)
                jarFile.close();
        }
        // file with some random contents
        invalidJar = File.createTempFile("flink-url-test", ".tmp");
        try (FileOutputStream invalidout = new FileOutputStream(invalidJar)) {
            invalidout.write(new byte[] { -1, 1, -2, 3, -3, 4 });
        }
        // non existing file
        File nonExisting = File.createTempFile("flink-url-test", ".tmp");
        assertTrue("Cannot create and delete temp file", nonExisting.delete());
        // create a URL classloader with
        // - a HTTP URL
        // - a file URL for an existing jar file
        // - a file URL for an existing file that is not a jar file
        // - a file URL for a non-existing file
        URL[] urls = { new URL("http", "localhost", 26712, "/some/file/path"), new URL("file", null, validJar.getAbsolutePath()), new URL("file", null, invalidJar.getAbsolutePath()), new URL("file", null, nonExisting.getAbsolutePath()) };
        URLClassLoader loader = new URLClassLoader(urls, getClass().getClassLoader());
        String info = ClassLoaderUtil.getUserCodeClassLoaderInfo(loader);
        assertTrue(info.indexOf("/some/file/path") > 0);
        assertTrue(info.indexOf(validJar.getAbsolutePath() + "' (valid") > 0);
        assertTrue(info.indexOf(invalidJar.getAbsolutePath() + "' (invalid JAR") > 0);
        assertTrue(info.indexOf(nonExisting.getAbsolutePath() + "' (missing") > 0);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (validJar != null) {
            //noinspection ResultOfMethodCallIgnored
            validJar.delete();
        }
        if (invalidJar != null) {
            //noinspection ResultOfMethodCallIgnored
            invalidJar.delete();
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) URLClassLoader(java.net.URLClassLoader) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 79 with URL

use of java.net.URL in project flink by apache.

the class ScalaShellRemoteEnvironment method getExecutor.

@Override
protected PlanExecutor getExecutor() throws Exception {
    // be a new jar file available for the user code classes
    if (this.executor != null) {
        this.executor.stop();
    }
    // write generated classes to disk so that they can be shipped to the cluster
    URL jarUrl = flinkILoop.writeFilesToDisk().getAbsoluteFile().toURI().toURL();
    List<URL> allJarFiles = new ArrayList<>(jarFiles);
    allJarFiles.add(jarUrl);
    this.executor = PlanExecutor.createRemoteExecutor(host, port, clientConfiguration, allJarFiles, globalClasspaths);
    executor.setPrintStatusDuringExecution(getConfig().isSysoutLoggingEnabled());
    return executor;
}
Also used : ArrayList(java.util.ArrayList) URL(java.net.URL)

Example 80 with URL

use of java.net.URL in project flink by apache.

the class ScalaShellRemoteStreamEnvironment method executeRemotely.

/**
	 * Executes the remote job.
	 *
	 * @param streamGraph
	 *            Stream Graph to execute
	 * @param jarFiles
	 * 			  List of jar file URLs to ship to the cluster
	 * @return The result of the job execution, containing elapsed time and accumulators.
	 */
@Override
protected JobExecutionResult executeRemotely(StreamGraph streamGraph, List<URL> jarFiles) throws ProgramInvocationException {
    URL jarUrl;
    try {
        jarUrl = flinkILoop.writeFilesToDisk().getAbsoluteFile().toURI().toURL();
    } catch (MalformedURLException e) {
        throw new ProgramInvocationException("Could not write the user code classes to disk.", e);
    }
    List<URL> allJarFiles = new ArrayList<>(jarFiles.size() + 1);
    allJarFiles.addAll(jarFiles);
    allJarFiles.add(jarUrl);
    return super.executeRemotely(streamGraph, allJarFiles);
}
Also used : MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) ProgramInvocationException(org.apache.flink.client.program.ProgramInvocationException) URL(java.net.URL)

Aggregations

URL (java.net.URL)8112 IOException (java.io.IOException)2006 Test (org.junit.Test)1653 File (java.io.File)1638 MalformedURLException (java.net.MalformedURLException)1165 HttpURLConnection (java.net.HttpURLConnection)1030 InputStream (java.io.InputStream)1028 ArrayList (java.util.ArrayList)633 URLConnection (java.net.URLConnection)515 InputStreamReader (java.io.InputStreamReader)473 URLClassLoader (java.net.URLClassLoader)451 BufferedReader (java.io.BufferedReader)390 HashMap (java.util.HashMap)361 URISyntaxException (java.net.URISyntaxException)286 URI (java.net.URI)269 Map (java.util.Map)259 FileInputStream (java.io.FileInputStream)227 List (java.util.List)205 FileOutputStream (java.io.FileOutputStream)194 OutputStream (java.io.OutputStream)194