List of usage examples for java.lang ClassLoader getResource
public URL getResource(String name)
From source file:com.krawler.portal.tools.SourceFormatter.java
private static void _readExclusions() throws IOException { _exclusions = new Properties(); ClassLoader classLoader = SourceFormatter.class.getClassLoader(); String sourceFormatterExclusions = System.getProperty("source-formatter-exclusions", "com/liferay/portal/tools/dependencies/" + "source_formatter_exclusions.properties"); URL url = classLoader.getResource(sourceFormatterExclusions); if (url == null) { return;// ww w . ja v a 2s. c o m } InputStream is = url.openStream(); _exclusions.load(is); is.close(); }
From source file:com.neuronrobotics.bowlerstudio.MainController.java
/** * Returns the location of the Jar archive or .class file the specified * class has been loaded from. <b>Note:</b> this only works if the class is * loaded from a jar archive or a .class file on the locale file system. * * @param cls// w ww . ja va 2s . c o m * class to locate * @return the location of the Jar archive the specified class comes from */ public static File getClassLocation(Class<?> cls) { // VParamUtil.throwIfNull(cls); String className = cls.getName(); ClassLoader cl = cls.getClassLoader(); URL url = cl.getResource(className.replace(".", "/") + ".class"); String urlString = url.toString().replace("jar:", ""); if (!urlString.startsWith("file:")) { throw new IllegalArgumentException("The specified class\"" + cls.getName() + "\" has not been loaded from a location" + "on the local filesystem."); } urlString = urlString.replace("file:", ""); urlString = urlString.replace("%20", " "); int location = urlString.indexOf(".jar!"); if (location > 0) { urlString = urlString.substring(0, location) + ".jar"; } else { // System.err.println("No Jar File found: " + cls.getName()); } return new File(urlString); }
From source file:Which4J.java
/** * Search the specified classloader for the given classname. * // w w w .j a va2s . c om * @param classname the fully qualified name of the class to search for * @param loader the classloader to search * @return the source location of the resource, or null if it wasn't found */ public static String which(String classname, ClassLoader loader) { if (isArrayType(classname)) { classname = getElementType(classname); } if (isPrimitiveOrVoid(classname)) { return "'" + classname + "' primitive"; } String classnameAsResource = classname.replace('.', '/') + ".class"; if (loader == null) { // some VM's return null from getClassLoader to indicate that // the class was loaded by the bootstrap class loader loader = ClassLoader.getSystemClassLoader(); } URL it = loader.getResource(classnameAsResource); if (it != null) { return it.toString(); } else { return null; } }
From source file:org.jboss.spring.vfs.VFSResourcePatternResolvingHelper.java
public static Resource[] locateResources(String locationPattern, String rootDirPath, ClassLoader classLoader, PathMatcher pathMatcher, boolean oneMatchingRootOnly) throws IOException { String subPattern = locationPattern.substring(rootDirPath.length()); if (rootDirPath.startsWith("/")) { rootDirPath = rootDirPath.substring(1); }//w w w . ja v a 2 s. co m List<Resource> resources = new ArrayList<Resource>(); Enumeration<URL> urls = classLoader.getResources(rootDirPath); if (!oneMatchingRootOnly) { while (urls.hasMoreElements()) { resources.addAll(getVFSResources(urls.nextElement(), subPattern, pathMatcher)); } } else { resources.addAll(getVFSResources(classLoader.getResource(rootDirPath), subPattern, pathMatcher)); } return resources.toArray(new Resource[resources.size()]); }
From source file:net.sf.jasperreports.engine.util.JRLoader.java
/** * Returns the resource URL for a specified resource name. * // w ww. j a v a 2 s . com * @param resource the resource name * @return the URL of the resource having the specified name, or * <code>null</code> if none found * @see ClassLoader#getResource(String) */ public static URL getResource(String resource) { URL location = null; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader != null) { location = classLoader.getResource(resource); } if (location == null) { classLoader = JRLoader.class.getClassLoader(); if (classLoader != null) { location = classLoader.getResource(resource); } if (location == null) { location = JRPropertiesUtil.class.getResource("/" + resource); } } return location; }
From source file:com.piaoyou.util.FileUtil.java
/** * This function is used to get the classpath of a reference of one class * First, this method tries to get the path from system properties * named "mvncore.context.path" (can be configured in web.xml). If it cannot * find this parameter, then it will tries to load from the ClassLoader * @todo FIXME: load from ClassLoader is not correct on Resin/Linux *///from w ww. java 2s . co m public static String getServletClassesPath() { if (servletClassesPath == null) { String strPath = System.getProperty("mvncore.context.path"); if ((strPath != null) && (strPath.length() > 0)) { servletClassesPath = strPath; } else { ClassLoader classLoader = instance.getClass().getClassLoader(); URL url = classLoader.getResource("/"); if (url == null) { // not run on the Servlet environment servletClassesPath = "."; } else { servletClassesPath = url.getPath(); } } log.debug("servletClassesPath = " + servletClassesPath); if (servletClassesPath.endsWith(File.separator) == false) { servletClassesPath = servletClassesPath + File.separatorChar; //log.warn("servletClassesPath does not end with /: " + servletClassesPath); } } return servletClassesPath; }
From source file:com.piaoyou.util.FileUtil.java
public static String getServletClassesPath(String flag) { String servletClassesPath = ""; String strPath = System.getProperty("mvncore.context.path"); if ((strPath != null) && (strPath.length() > 0)) { servletClassesPath = strPath;/*from w w w . j a v a 2 s . c o m*/ } else { ClassLoader classLoader = instance.getClass().getClassLoader(); URL url = null; if (flag == null) url = classLoader.getResource("/"); else url = classLoader.getResource(flag); if (url == null) { // not run on the Servlet environment servletClassesPath = "."; } else { servletClassesPath = url.getPath(); } } log.debug("servletClassesPath = " + servletClassesPath); if (servletClassesPath.contains(":/")) { servletClassesPath = servletClassesPath.substring(1).replace("%20", " "); //log.warn("servletClassesPath does not end with /: " + servletClassesPath); } else if (servletClassesPath.endsWith(File.separator) == false) { servletClassesPath = servletClassesPath + File.separatorChar; } return servletClassesPath; }
From source file:org.jboss.as.test.integration.web.sso.SSOTestBase.java
public static EnterpriseArchive createSsoEar() { ClassLoader tccl = Thread.currentThread().getContextClassLoader(); String resourcesLocation = "org/jboss/as/test/integration/web/sso/resources/"; WebArchive war1 = createSsoWar("sso-form-auth1.war"); WebArchive war2 = createSsoWar("sso-form-auth2.war"); WebArchive war3 = createSsoWar("sso-with-no-auth.war"); // Remove jboss-web.xml so the war will not have an authenticator war3.delete(war3.get("WEB-INF/jboss-web.xml").getPath()); JavaArchive webEjbs = ShrinkWrap.create(JavaArchive.class, "jbosstest-web-ejbs.jar"); webEjbs.addAsManifestResource(tccl.getResource(resourcesLocation + "ejb-jar.xml"), "ejb-jar.xml"); webEjbs.addAsManifestResource(tccl.getResource(resourcesLocation + "jboss.xml"), "jboss.xml"); webEjbs.addPackage(StatelessSession.class.getPackage()); EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class, "web-sso.ear"); ear.setApplicationXML(tccl.getResource(resourcesLocation + "application.xml")); ear.addAsModule(war1);/*ww w .j a va 2 s . com*/ ear.addAsModule(war2); ear.addAsModule(war3); ear.addAsModule(webEjbs); return ear; }
From source file:com.intuit.tank.tools.debugger.ActionProducer.java
public static Icon getIcon(String string, IconSize size) { ImageIcon ret = null;/*from w ww.ja v a 2 s. c o m*/ String resourcePath = (size == IconSize.SMALL ? "gfx/16/" : "gfx/32/") + string; try { ClassLoader cl = Thread.currentThread().getContextClassLoader(); URL url = cl.getResource(resourcePath); // System.out.println("URL is: " + url); ret = new ImageIcon(ImageIO.read(url)); } catch (Exception e) { // System.out.println("URL is: " + url); LOG.error("Error loading image " + resourcePath + ": " + e); } return ret; }
From source file:com.all4tec.sa.maven.proguard.ProGuardMojo.java
private static File getProguardJar(ProGuardMojo mojo) throws MojoExecutionException { Artifact proguardArtifact = null;/*from w ww . j a v a 2 s .c om*/ int proguardArtifactDistance = -1; // This should be solved in Maven 2.1 for (Iterator i = mojo.pluginArtifacts.iterator(); i.hasNext();) { Artifact artifact = (Artifact) i.next(); mojo.getLog().debug("pluginArtifact: " + artifact.getFile()); if (artifact.getArtifactId().startsWith("proguard") && !artifact.getArtifactId().startsWith("proguard-maven-plugin")) { int distance = artifact.getDependencyTrail().size(); mojo.getLog().debug("proguard DependencyTrail: " + distance); if ((mojo.proguardVersion != null) && (mojo.proguardVersion.equals(artifact.getVersion()))) { proguardArtifact = artifact; break; } else if (proguardArtifactDistance == -1) { proguardArtifact = artifact; proguardArtifactDistance = distance; } else if (distance < proguardArtifactDistance) { proguardArtifact = artifact; proguardArtifactDistance = distance; } } } if (proguardArtifact != null) { mojo.getLog().debug("proguardArtifact: " + proguardArtifact.getFile()); return proguardArtifact.getFile().getAbsoluteFile(); } mojo.getLog().info("proguard jar not found in pluginArtifacts"); ClassLoader cl; cl = mojo.getClass().getClassLoader(); // cl = Thread.currentThread().getContextClassLoader(); String classResource = "/" + mojo.proguardMainClass.replace('.', '/') + ".class"; URL url = cl.getResource(classResource); if (url == null) { throw new MojoExecutionException( "Obfuscation failed ProGuard (" + mojo.proguardMainClass + ") not found in classpath"); } String proguardJar = url.toExternalForm(); if (proguardJar.startsWith("jar:file:")) { proguardJar = proguardJar.substring("jar:file:".length()); proguardJar = proguardJar.substring(0, proguardJar.indexOf('!')); } else { throw new MojoExecutionException("Unrecognized location (" + proguardJar + ") in classpath"); } return new File(proguardJar); }