Java Code Examples for java.net.URLClassLoader#findResource()

The following examples show how to use java.net.URLClassLoader#findResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JarHandlerPkgPrefix.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Path jarPath = Paths.get(System.getProperty("user.dir", "."), "test.jar");
    try {
        createJar(jarPath);

        URL j = new URL("jar:file:" + jarPath.toString() + "!/");
        URLClassLoader ucl = new URLClassLoader(new URL[]{j});
        ucl.findResource("resource.txt");

        URL r = new URL("jar:file:" + jarPath.toString() + "!/resource.txt");
        if (!Handler.URLS.contains(r))
            throw new AssertionError("jar: URL handler not invoked");
    }
    finally {
        Files.delete(jarPath);
    }
}
 
Example 2
Source File: Misc.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean urlExists(URL url) {
        try {
            URLConnection connection = url.openConnection();
            if ( connection instanceof JarURLConnection ) {
                JarURLConnection jarURLConnection = (JarURLConnection)connection;
                URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {
                                                                             jarURLConnection.getJarFileURL()
});
                try { return urlClassLoader.findResource(jarURLConnection.getEntryName())!=null; }
                finally {
                    if ( urlClassLoader instanceof Closeable ) {
                        ((Closeable)urlClassLoader).close();
                    }
                }
            }
            InputStream is = null;
            try {
                is = url.openStream();
            }
            finally {
                if ( is!=null ) {
                    is.close();
                }
            }
            return is!=null;
        }
        catch (IOException ioe) { return false; }
    }
 
Example 3
Source File: Misc.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean urlExists(URL url) {
        try {
            URLConnection connection = url.openConnection();
            if ( connection instanceof JarURLConnection ) {
                JarURLConnection jarURLConnection = (JarURLConnection)connection;
                URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {
jarURLConnection.getJarFileURL()});
                try {
                    return urlClassLoader.findResource(jarURLConnection.getEntryName())!=null;
                }
                finally {
                    if ( urlClassLoader instanceof Closeable ) {
                        ((Closeable)urlClassLoader).close();
                    }
                }
            }
            InputStream is = null;
            try {
                is = url.openStream();
                }
            finally {
                if ( is!=null ) {
                    is.close();
                }
            }
            return is!=null;
        }
        catch (IOException ioe) {
            return false;
        }
    }
 
Example 4
Source File: Misc.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean urlExists(URL url) {
        try {
            URLConnection connection = url.openConnection();
            if ( connection instanceof JarURLConnection ) {
                JarURLConnection jarURLConnection = (JarURLConnection)connection;
                URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{
jarURLConnection.getJarFileURL()
                                                                   });
                try {
                    return urlClassLoader.findResource(jarURLConnection.getEntryName())!=null;
                }
                finally {
                    if ( urlClassLoader instanceof Closeable ) {
                        ((Closeable)urlClassLoader).close();
                    }
                }
            }
            InputStream is = null;
            try {
                is = url.openStream();
            }
            finally {
                if ( is!=null ) {
                    is.close();
                }
            }
            return is!=null;
        }
        catch (IOException ioe) {
            return false;
        }
    }
 
Example 5
Source File: FileUtil.java    From desktop with GNU General Public License v3.0 5 votes vote down vote up
public static String getPropertyFromManifest(String manifestPath, String property){
    try {
        URLClassLoader cl = (URLClassLoader) FileUtil.class.getClassLoader();
        
        URL url = cl.findResource(manifestPath);
        Manifest manifest = new Manifest(url.openStream());
        Attributes attr = manifest.getMainAttributes();
        
        return attr.getValue(property);            
    } catch (IOException ex) {
        logger.debug("Exception: ", ex);
        return null;
    }
}
 
Example 6
Source File: Misc.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean urlExists(URL url) {
        try {
            URLConnection connection = url.openConnection();
            if ( connection instanceof JarURLConnection ) {
                JarURLConnection jarURLConnection = (JarURLConnection)connection;
                URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{
jarURLConnection.getJarFileURL()
                                                                   });
                try {
                    return urlClassLoader.findResource(jarURLConnection.getEntryName())!=null;
                }
                finally {
                    if ( urlClassLoader instanceof Closeable ) {
                        ((Closeable)urlClassLoader).close();
                    }
                }
            }
            InputStream is = null;
            try {
                is = url.openStream();
            }
            finally {
                if ( is!=null ) {
                    is.close();
                }
            }
            return is!=null;
        }
        catch (IOException ioe) {
            return false;
        }
    }
 
Example 7
Source File: Misc.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean urlExists(URL url) {
        try {
            URLConnection connection = url.openConnection();
            if ( connection instanceof JarURLConnection ) {
                JarURLConnection jarURLConnection = (JarURLConnection)connection;
                URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {
jarURLConnection.getJarFileURL()});
                try {
                    return urlClassLoader.findResource(jarURLConnection.getEntryName())!=null;
                }
                finally {
                    if ( urlClassLoader instanceof Closeable ) {
                        ((Closeable)urlClassLoader).close();
                    }
                }
            }
            InputStream is = null;
            try {
                is = url.openStream();
                }
            finally {
                if ( is!=null ) {
                    is.close();
                }
            }
            return is!=null;
        }
        catch (IOException ioe) {
            return false;
        }
    }
 
Example 8
Source File: XotsDominoExecutor.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
/**
 * Helper for WrappedCallable/WrappedRunnable
 * 
 * @param ctx
 * @param mcl
 * @param wrapper
 * @throws ServletException
 */

static void initModule(final NotesContext ctx, final ClassLoader mcl, final Object wrappedObject) throws ServletException {
	// next initialize the context with a FakeHttpRequest. This is neccessary so that internal things
	// like session-creation and so on work properly

	// RPr:
	// You may ask what I do here: ReLoading the module again from the MCL triggers signature checking.

	if (mcl instanceof ModuleClassLoader) {
		URLClassLoader dcl = (URLClassLoader) ((ModuleClassLoader) mcl).getDynamicClassLoader();
		String className = wrappedObject.getClass().getName();
		String str = className.replace('.', '/') + ".class";
		URL url = dcl.findResource(str);
		if (url != null && url.getProtocol().startsWith("xspnsf")) {
			// Set up the "TopLevelXPageSigner == Signer of the runnable
			// As soon as we are in a xspnsf, we do not have a SessionFactory!

			ctx.setSignerSessionRights("WEB-INF/classes/" + str);

			// RPr: There is a bug: you can decide if you want to use "sessionAsSigner" or "sessionAsSignerFullAccess"
			// But you cannot use both simultaneously!
			Session signerSession = ctx.getSessionAsSigner(true);
			if (signerSession != null) {
				Factory.setSessionFactory(new XPageSignerSessionFactory(false), SessionType.SIGNER);
				Factory.setSessionFactory(new XPageSignerSessionFactory(true), SessionType.SIGNER_FULL_ACCESS);
			} else {
				// do not setup signer sessions if it is not properly signed!
				Factory.setSessionFactory(new InvalidSessionFactory(), SessionType.SIGNER);
				Factory.setSessionFactory(new InvalidSessionFactory(), SessionType.SIGNER_FULL_ACCESS);
			}
		} else {
			// The code is not part from an NSF, so it resides on the server
			Factory.setSessionFactory(new XPageNamedSessionFactory(Factory.getLocalServerName(), false), SessionType.SIGNER);
			Factory.setSessionFactory(new XPageNamedSessionFactory(Factory.getLocalServerName(), true), SessionType.SIGNER_FULL_ACCESS);
		}
	}
	Factory.setClassLoader(mcl);
}
 
Example 9
Source File: Misc.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean urlExists(URL url) {
        try {
            URLConnection connection = url.openConnection();
            if ( connection instanceof JarURLConnection ) {
                JarURLConnection jarURLConnection = (JarURLConnection)connection;
                URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{
jarURLConnection.getJarFileURL()
                                                                   });
                try {
                    return urlClassLoader.findResource(jarURLConnection.getEntryName())!=null;
                }
                finally {
                    if ( urlClassLoader instanceof Closeable ) {
                        ((Closeable)urlClassLoader).close();
                    }
                }
            }
            InputStream is = null;
            try {
                is = url.openStream();
            }
            finally {
                if ( is!=null ) {
                    is.close();
                }
            }
            return is!=null;
        }
        catch (IOException ioe) {
            return false;
        }
    }
 
Example 10
Source File: Misc.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean urlExists(URL url) {
        try {
            URLConnection connection = url.openConnection();
            if ( connection instanceof JarURLConnection ) {
                JarURLConnection jarURLConnection = (JarURLConnection)connection;
                URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {
                                                                             jarURLConnection.getJarFileURL()
});
                try { return urlClassLoader.findResource(jarURLConnection.getEntryName())!=null; }
                finally {
                    if ( urlClassLoader instanceof Closeable ) {
                        ((Closeable)urlClassLoader).close();
                    }
                }
            }
            InputStream is = null;
            try {
                is = url.openStream();
            }
            finally {
                if ( is!=null ) {
                    is.close();
                }
            }
            return is!=null;
        }
        catch (IOException ioe) { return false; }
    }
 
Example 11
Source File: Misc.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean urlExists(URL url) {
    try {
        URLConnection connection = url.openConnection();
        if ( connection instanceof JarURLConnection ) {
            JarURLConnection jarURLConnection = (JarURLConnection)connection;
            URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {
                                                                         jarURLConnection.getJarFileURL()});
            try { return urlClassLoader.findResource(jarURLConnection.getEntryName())!=null;
            }
            finally {
                if ( urlClassLoader instanceof Closeable ) {
                    ((Closeable)urlClassLoader).close();
                }
            }
        }
        InputStream is = null;
        try {
            is = url.openStream();
        }
        finally {
            if ( is!=null ) {
                is.close();
            }
        }
        return is!=null;
    }
    catch (IOException ioe) { return false;
    }
}
 
Example 12
Source File: Misc.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean urlExists(URL url) {
        try {
            URLConnection connection = url.openConnection();
            if ( connection instanceof JarURLConnection ) {
                JarURLConnection jarURLConnection = (JarURLConnection)connection;
                URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {
                                                                             jarURLConnection.getJarFileURL()
});
                try { return urlClassLoader.findResource(jarURLConnection.getEntryName())!=null; }
                finally {
                    if ( urlClassLoader instanceof Closeable ) {
                        ((Closeable)urlClassLoader).close();
                    }
                }
            }
            InputStream is = null;
            try {
                is = url.openStream();
            }
            finally {
                if ( is!=null ) {
                    is.close();
                }
            }
            return is!=null;
        }
        catch (IOException ioe) { return false; }
    }
 
Example 13
Source File: Misc.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean urlExists(URL url) {
        try {
            URLConnection connection = url.openConnection();
            if ( connection instanceof JarURLConnection ) {
                JarURLConnection jarURLConnection = (JarURLConnection)connection;
                URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {
                                                                             jarURLConnection.getJarFileURL()
});
                try { return urlClassLoader.findResource(jarURLConnection.getEntryName())!=null; }
                finally {
                    if ( urlClassLoader instanceof Closeable ) {
                        ((Closeable)urlClassLoader).close();
                    }
                }
            }
            InputStream is = null;
            try {
                is = url.openStream();
            }
            finally {
                if ( is!=null ) {
                    is.close();
                }
            }
            return is!=null;
        }
        catch (IOException ioe) { return false; }
    }
 
Example 14
Source File: Misc.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean urlExists(URL url) {
    try {
        URLConnection connection = url.openConnection();
        if ( connection instanceof JarURLConnection ) {
            JarURLConnection jarURLConnection = (JarURLConnection)connection;
            URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {
                                                                         jarURLConnection.getJarFileURL()});
            try { return urlClassLoader.findResource(jarURLConnection.getEntryName())!=null;
            }
            finally {
                if ( urlClassLoader instanceof Closeable ) {
                    ((Closeable)urlClassLoader).close();
                }
            }
        }
        InputStream is = null;
        try {
            is = url.openStream();
        }
        finally {
            if ( is!=null ) {
                is.close();
            }
        }
        return is!=null;
    }
    catch (IOException ioe) { return false;
    }
}
 
Example 15
Source File: Misc.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean urlExists(URL url) {
        try {
            URLConnection connection = url.openConnection();
            if ( connection instanceof JarURLConnection ) {
                JarURLConnection jarURLConnection = (JarURLConnection)connection;
                URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {
                                                                             jarURLConnection.getJarFileURL()
});
                try { return urlClassLoader.findResource(jarURLConnection.getEntryName())!=null; }
                finally {
                    if ( urlClassLoader instanceof Closeable ) {
                        ((Closeable)urlClassLoader).close();
                    }
                }
            }
            InputStream is = null;
            try {
                is = url.openStream();
            }
            finally {
                if ( is!=null ) {
                    is.close();
                }
            }
            return is!=null;
        }
        catch (IOException ioe) { return false; }
    }
 
Example 16
Source File: Misc.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean urlExists(URL url) {
	try {
		URLConnection connection = url.openConnection();
		if (connection instanceof JarURLConnection) {
			JarURLConnection jarURLConnection = (JarURLConnection)connection;
			URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { jarURLConnection.getJarFileURL() });
			try {
				return urlClassLoader.findResource(jarURLConnection.getEntryName()) != null;
			}
			finally {
				if (urlClassLoader instanceof Closeable) {
					((Closeable)urlClassLoader).close();
				}
			}
		}

		InputStream is = null;
		try {
			is = url.openStream();
		}
		finally {
			if (is != null) {
				is.close();
			}
		}

		return is != null;
	}
	catch (IOException ioe) {
		return false;
	}
}
 
Example 17
Source File: Utils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static URL getResourceURL(String resource, Class relatedClass) {
    URL result = null;
    ClassLoader classLoader = relatedClass.getClassLoader();

    if(classLoader instanceof java.net.URLClassLoader) {
        URLClassLoader urlClassLoader = (java.net.URLClassLoader) classLoader;
        result = urlClassLoader.findResource(resource);
    } else {
        result = classLoader.getResource(resource);
    }

    return result;
}
 
Example 18
Source File: XMLinJarFSTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Set up given number of FileObjects */
protected FileObject[] setUpFileObjects(int foCount) throws Exception {
    tmp = createTempFolder();
    destFolder = LocalFSTest.createFiles(foCount, 0, tmp);
    File xmlbase = generateXMLFile(destFolder, new ResourceComposer(LocalFSTest.RES_NAME, LocalFSTest.RES_EXT, foCount, 0));
    File jar = Utilities.createJar(tmp, "jarxmlfs.jar");
    cloader = new URLClassLoader(new URL[] { jar.toURL() });
    URL res = cloader.findResource(PACKAGE + xmlbase.getName());
    xmlfs = new XMLFileSystem();
    xmlfs.setXmlUrl(res, false);

    FileObject pkg = xmlfs.findResource(PACKAGE);
    return pkg.getChildren();
}
 
Example 19
Source File: SpringBootUtil.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the spring boot configuration (supports `application.properties` and `application.yml`)
 * or an empty properties object if not found
 *
 * @param springActiveProfile currently active spring-boot profile
 * @param compileClassLoader compile class loader
 * @return properties object
 */
public static Properties getSpringBootApplicationProperties(String springActiveProfile, URLClassLoader compileClassLoader) {
    URL ymlResource = compileClassLoader.findResource("application.yml");
    URL propertiesResource = compileClassLoader.findResource("application.properties");

    Properties props = getPropertiesFromApplicationYamlResource(springActiveProfile, ymlResource);
    props.putAll(getPropertiesResource(propertiesResource));
    return props;
}
 
Example 20
Source File: ThorntailUtil.java    From jkube with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Returns the thorntail configuration (supports `project-defaults.yml`)
 * or an empty properties object if not found
 *
 * @param compileClassLoader URLClassLoader for resource access
 * @return thorntail configuration properties
 */
public static Properties getThorntailProperties(URLClassLoader compileClassLoader) {
    URL ymlResource = compileClassLoader.findResource("project-defaults.yml");
    return YamlUtil.getPropertiesFromYamlResource(ymlResource);
}