Search in sources :

Example 36 with URLClassLoader

use of java.net.URLClassLoader in project jna by java-native-access.

the class Native method extractFromResourcePath.

/** Attempt to extract a native library from the resource path using the
     * given class loader.
     * @param name Base name of native library to extract.  May also be an
     * absolute resource path (i.e. starts with "/"), in which case the
     * no transformations of the library name are performed.  If only the base
     * name is given, the resource path is attempted both with and without
     * {@link Platform#RESOURCE_PREFIX}, after mapping the library name via
     * {@link NativeLibrary#mapSharedLibraryName(String)}.
     * @param loader Class loader to use to load resources
     * @return File indicating extracted resource on disk
     * @throws IOException if resource not found
     */
public static File extractFromResourcePath(String name, ClassLoader loader) throws IOException {
    final boolean DEBUG = DEBUG_LOAD || (DEBUG_JNA_LOAD && name.indexOf("jnidispatch") != -1);
    if (loader == null) {
        loader = Thread.currentThread().getContextClassLoader();
        // Context class loader is not guaranteed to be set
        if (loader == null) {
            loader = Native.class.getClassLoader();
        }
    }
    if (DEBUG) {
        System.out.println("Looking in classpath from " + loader + " for " + name);
    }
    String libname = name.startsWith("/") ? name : NativeLibrary.mapSharedLibraryName(name);
    String resourcePath = name.startsWith("/") ? name : Platform.RESOURCE_PREFIX + "/" + libname;
    if (resourcePath.startsWith("/")) {
        resourcePath = resourcePath.substring(1);
    }
    URL url = loader.getResource(resourcePath);
    if (url == null && resourcePath.startsWith(Platform.RESOURCE_PREFIX)) {
        // If not found with the standard resource prefix, try without it
        url = loader.getResource(libname);
    }
    if (url == null) {
        String path = System.getProperty("java.class.path");
        if (loader instanceof URLClassLoader) {
            path = Arrays.asList(((URLClassLoader) loader).getURLs()).toString();
        }
        throw new IOException("Native library (" + resourcePath + ") not found in resource path (" + path + ")");
    }
    if (DEBUG) {
        System.out.println("Found library resource at " + url);
    }
    File lib = null;
    if (url.getProtocol().toLowerCase().equals("file")) {
        try {
            lib = new File(new URI(url.toString()));
        } catch (URISyntaxException e) {
            lib = new File(url.getPath());
        }
        if (DEBUG) {
            System.out.println("Looking in " + lib.getAbsolutePath());
        }
        if (!lib.exists()) {
            throw new IOException("File URL " + url + " could not be properly decoded");
        }
    } else if (!Boolean.getBoolean("jna.nounpack")) {
        InputStream is = loader.getResourceAsStream(resourcePath);
        if (is == null) {
            throw new IOException("Can't obtain InputStream for " + resourcePath);
        }
        FileOutputStream fos = null;
        try {
            // Suffix is required on windows, or library fails to load
            // Let Java pick the suffix, except on windows, to avoid
            // problems with Web Start.
            File dir = getTempDir();
            lib = File.createTempFile(JNA_TMPLIB_PREFIX, Platform.isWindows() ? ".dll" : null, dir);
            if (!Boolean.getBoolean("jnidispatch.preserve")) {
                lib.deleteOnExit();
            }
            fos = new FileOutputStream(lib);
            int count;
            byte[] buf = new byte[1024];
            while ((count = is.read(buf, 0, buf.length)) > 0) {
                fos.write(buf, 0, count);
            }
        } catch (IOException e) {
            throw new IOException("Failed to create temporary file for " + name + " library: " + e.getMessage());
        } finally {
            try {
                is.close();
            } catch (IOException e) {
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                }
            }
        }
    }
    return lib;
}
Also used : InputStream(java.io.InputStream) URLClassLoader(java.net.URLClassLoader) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URI(java.net.URI) URL(java.net.URL)

Example 37 with URLClassLoader

use of java.net.URLClassLoader in project hadoop by apache.

the class Submitter method run.

@Override
public int run(String[] args) throws Exception {
    CommandLineParser cli = new CommandLineParser();
    if (args.length == 0) {
        cli.printUsage();
        return 1;
    }
    cli.addOption("input", false, "input path to the maps", "path");
    cli.addOption("output", false, "output path from the reduces", "path");
    cli.addOption("jar", false, "job jar file", "path");
    cli.addOption("inputformat", false, "java classname of InputFormat", "class");
    //cli.addArgument("javareader", false, "is the RecordReader in Java");
    cli.addOption("map", false, "java classname of Mapper", "class");
    cli.addOption("partitioner", false, "java classname of Partitioner", "class");
    cli.addOption("reduce", false, "java classname of Reducer", "class");
    cli.addOption("writer", false, "java classname of OutputFormat", "class");
    cli.addOption("program", false, "URI to application executable", "class");
    cli.addOption("reduces", false, "number of reduces", "num");
    cli.addOption("jobconf", false, "\"n1=v1,n2=v2,..\" (Deprecated) Optional. Add or override a JobConf property.", "key=val");
    cli.addOption("lazyOutput", false, "Optional. Create output lazily", "boolean");
    Parser parser = cli.createParser();
    try {
        GenericOptionsParser genericParser = new GenericOptionsParser(getConf(), args);
        CommandLine results = parser.parse(cli.options, genericParser.getRemainingArgs());
        JobConf job = new JobConf(getConf());
        if (results.hasOption("input")) {
            FileInputFormat.setInputPaths(job, results.getOptionValue("input"));
        }
        if (results.hasOption("output")) {
            FileOutputFormat.setOutputPath(job, new Path(results.getOptionValue("output")));
        }
        if (results.hasOption("jar")) {
            job.setJar(results.getOptionValue("jar"));
        }
        if (results.hasOption("inputformat")) {
            setIsJavaRecordReader(job, true);
            job.setInputFormat(getClass(results, "inputformat", job, InputFormat.class));
        }
        if (results.hasOption("javareader")) {
            setIsJavaRecordReader(job, true);
        }
        if (results.hasOption("map")) {
            setIsJavaMapper(job, true);
            job.setMapperClass(getClass(results, "map", job, Mapper.class));
        }
        if (results.hasOption("partitioner")) {
            job.setPartitionerClass(getClass(results, "partitioner", job, Partitioner.class));
        }
        if (results.hasOption("reduce")) {
            setIsJavaReducer(job, true);
            job.setReducerClass(getClass(results, "reduce", job, Reducer.class));
        }
        if (results.hasOption("reduces")) {
            job.setNumReduceTasks(Integer.parseInt(results.getOptionValue("reduces")));
        }
        if (results.hasOption("writer")) {
            setIsJavaRecordWriter(job, true);
            job.setOutputFormat(getClass(results, "writer", job, OutputFormat.class));
        }
        if (results.hasOption("lazyOutput")) {
            if (Boolean.parseBoolean(results.getOptionValue("lazyOutput"))) {
                LazyOutputFormat.setOutputFormatClass(job, job.getOutputFormat().getClass());
            }
        }
        if (results.hasOption("program")) {
            setExecutable(job, results.getOptionValue("program"));
        }
        if (results.hasOption("jobconf")) {
            LOG.warn("-jobconf option is deprecated, please use -D instead.");
            String options = results.getOptionValue("jobconf");
            StringTokenizer tokenizer = new StringTokenizer(options, ",");
            while (tokenizer.hasMoreTokens()) {
                String keyVal = tokenizer.nextToken().trim();
                String[] keyValSplit = keyVal.split("=");
                job.set(keyValSplit[0], keyValSplit[1]);
            }
        }
        // if they gave us a jar file, include it into the class path
        String jarFile = job.getJar();
        if (jarFile != null) {
            final URL[] urls = new URL[] { FileSystem.getLocal(job).pathToFile(new Path(jarFile)).toURL() };
            //FindBugs complains that creating a URLClassLoader should be
            //in a doPrivileged() block. 
            ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {

                public ClassLoader run() {
                    return new URLClassLoader(urls);
                }
            });
            job.setClassLoader(loader);
        }
        runJob(job);
        return 0;
    } catch (ParseException pe) {
        LOG.info("Error : " + pe);
        cli.printUsage();
        return 1;
    }
}
Also used : Path(org.apache.hadoop.fs.Path) NullOutputFormat(org.apache.hadoop.mapred.lib.NullOutputFormat) OutputFormat(org.apache.hadoop.mapred.OutputFormat) LazyOutputFormat(org.apache.hadoop.mapred.lib.LazyOutputFormat) FileOutputFormat(org.apache.hadoop.mapred.FileOutputFormat) URL(java.net.URL) GenericOptionsParser(org.apache.hadoop.util.GenericOptionsParser) BasicParser(org.apache.commons.cli.BasicParser) Parser(org.apache.commons.cli.Parser) Mapper(org.apache.hadoop.mapred.Mapper) CommandLine(org.apache.commons.cli.CommandLine) StringTokenizer(java.util.StringTokenizer) InputFormat(org.apache.hadoop.mapred.InputFormat) FileInputFormat(org.apache.hadoop.mapred.FileInputFormat) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) ParseException(org.apache.commons.cli.ParseException) Reducer(org.apache.hadoop.mapred.Reducer) JobConf(org.apache.hadoop.mapred.JobConf) HashPartitioner(org.apache.hadoop.mapred.lib.HashPartitioner) Partitioner(org.apache.hadoop.mapred.Partitioner) GenericOptionsParser(org.apache.hadoop.util.GenericOptionsParser)

Example 38 with URLClassLoader

use of java.net.URLClassLoader in project hadoop by apache.

the class TestClassWithNoPackage method testGoodClassOrNull.

@Test
public void testGoodClassOrNull() throws Exception {
    String NAME = "ClassWithNoPackage";
    ClassLoader cl = TestClassWithNoPackage.class.getClassLoader();
    String JAR = JarFinder.getJar(cl.loadClass(NAME));
    // Add testjob jar file to classpath.
    Configuration conf = new Configuration();
    conf.setClassLoader(new URLClassLoader(new URL[] { new URL("file", null, JAR) }, null));
    // Get class with no package name.
    String defaultPackage = this.getClass().getPackage().getName();
    Class c = StreamUtil.goodClassOrNull(conf, NAME, defaultPackage);
    assertNotNull("Class " + NAME + " not found!", c);
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) Test(org.junit.Test)

Example 39 with URLClassLoader

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

the class JavacJavaCompiler method makeParameters.

private String[] makeParameters(List<String> files, GroovyClassLoader parentClassLoader) {
    Map options = config.getJointCompilationOptions();
    LinkedList<String> paras = new LinkedList<String>();
    File target = config.getTargetDirectory();
    if (target == null)
        target = new File(".");
    // defaults
    paras.add("-d");
    paras.add(target.getAbsolutePath());
    paras.add("-sourcepath");
    paras.add(((File) options.get("stubDir")).getAbsolutePath());
    // add flags
    String[] flags = (String[]) options.get("flags");
    if (flags != null) {
        for (String flag : flags) {
            paras.add('-' + flag);
        }
    }
    boolean hadClasspath = false;
    // add namedValues
    String[] namedValues = (String[]) options.get("namedValues");
    if (namedValues != null) {
        for (int i = 0; i < namedValues.length; i += 2) {
            String name = namedValues[i];
            if (name.equals("classpath"))
                hadClasspath = true;
            paras.add('-' + name);
            paras.add(namedValues[i + 1]);
        }
    }
    // append classpath if not already defined
    if (!hadClasspath) {
        // add all classpaths that compilation unit sees
        List<String> paths = new ArrayList<String>(config.getClasspath());
        ClassLoader cl = parentClassLoader;
        while (cl != null) {
            if (cl instanceof URLClassLoader) {
                for (URL u : ((URLClassLoader) cl).getURLs()) {
                    try {
                        paths.add(new File(u.toURI()).getPath());
                    } catch (URISyntaxException e) {
                    // ignore it
                    }
                }
            }
            cl = cl.getParent();
        }
        try {
            CodeSource codeSource = AccessController.doPrivileged(new PrivilegedAction<CodeSource>() {

                @Override
                public CodeSource run() {
                    return GroovyObject.class.getProtectionDomain().getCodeSource();
                }
            });
            if (codeSource != null) {
                paths.add(new File(codeSource.getLocation().toURI()).getPath());
            }
        } catch (URISyntaxException e) {
        // ignore it
        }
        StringBuilder resultPath = new StringBuilder(DefaultGroovyMethods.join((Iterable) paths, File.pathSeparator));
        paras.add("-classpath");
        paras.add(resultPath.toString());
    }
    // files to compile
    paras.addAll(files);
    return paras.toArray(new String[paras.size()]);
}
Also used : URISyntaxException(java.net.URISyntaxException) CodeSource(java.security.CodeSource) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) File(java.io.File)

Example 40 with URLClassLoader

use of java.net.URLClassLoader in project hadoop by apache.

the class MetricsConfig method getPluginLoader.

ClassLoader getPluginLoader() {
    if (pluginLoader != null)
        return pluginLoader;
    final ClassLoader defaultLoader = getClass().getClassLoader();
    Object purls = super.getProperty(PLUGIN_URLS_KEY);
    if (purls == null)
        return defaultLoader;
    Iterable<String> jars = SPLITTER.split((String) purls);
    int len = Iterables.size(jars);
    if (len > 0) {
        final URL[] urls = new URL[len];
        try {
            int i = 0;
            for (String jar : jars) {
                LOG.debug(jar);
                urls[i++] = new URL(jar);
            }
        } catch (Exception e) {
            throw new MetricsConfigException(e);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("using plugin jars: " + Iterables.toString(jars));
        }
        pluginLoader = doPrivileged(new PrivilegedAction<ClassLoader>() {

            @Override
            public ClassLoader run() {
                return new URLClassLoader(urls, defaultLoader);
            }
        });
        return pluginLoader;
    }
    if (parent instanceof MetricsConfig) {
        return ((MetricsConfig) parent).getPluginLoader();
    }
    return defaultLoader;
}
Also used : PrivilegedAction(java.security.PrivilegedAction) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException)

Aggregations

URLClassLoader (java.net.URLClassLoader)1351 URL (java.net.URL)872 File (java.io.File)514 Test (org.junit.Test)317 IOException (java.io.IOException)256 ArrayList (java.util.ArrayList)202 MalformedURLException (java.net.MalformedURLException)186 Method (java.lang.reflect.Method)177 InvocationTargetException (java.lang.reflect.InvocationTargetException)68 JarFile (java.util.jar.JarFile)54 InputStream (java.io.InputStream)50 HashSet (java.util.HashSet)49 HashMap (java.util.HashMap)44 URISyntaxException (java.net.URISyntaxException)41 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)35 Path (java.nio.file.Path)33 QuickTest (com.hazelcast.test.annotation.QuickTest)32 Test (org.junit.jupiter.api.Test)28 URI (java.net.URI)27 List (java.util.List)27