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;
}
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;
}
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();
}
}
}
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;
}
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);
}
Aggregations