List of usage examples for java.net URL getFile
public String getFile()
From source file:com.feedzai.fos.server.remote.impl.RemoteInterfacesTest.java
/** * Scans all classes accessible from the context class loader which belong to the given package and subpackages. * * @param packageName The base package//from w w w . j a va2 s .c o m * @return The classes * @throws ClassNotFoundException * @throws java.io.IOException */ private static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); List<File> dirs = new ArrayList<>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); } ArrayList<Class> classes = new ArrayList<>(); for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes.toArray(new Class[classes.size()]); }
From source file:de.oth.keycloak.initKeycloakServer.IT_TestAuth.java
@BeforeClass public static void setUpClass() throws IOException { keycloak = initKeycloak(SERVER, REALM, USER, PWD, CLIENTSTR); String initFileStr = "integration_test_config.json"; File initFile = new File(initFileStr); if (!initFile.isFile()) { URL url = TestRun.class.getClassLoader().getResource(initFileStr); if (url != null) { initFile = new File(url.getFile()); if (!initFile.isFile()) { fail("test config is no file: " + initFileStr); }//w w w . ja v a 2 s .co m } else { fail("can't load test config: " + initFileStr); } } RealmResource rRes = KeycloakAccess.getRealm(keycloak, GM_REALM, false); if (rRes != null) { rRes.remove(); } rRes = KeycloakAccess.getRealm(keycloak, APP_REALM, false); if (rRes != null) { rRes.remove(); } ObjectMapper mapper = new ObjectMapper(); RealmsConfig realmsConfig = mapper.readValue(initFile, RealmsConfig.class); if (realmsConfig != null) { List<RealmConfig> realmList = realmsConfig.getRealms(); if (realmList == null || realmList.isEmpty()) { fail("realmList is empty: " + initFileStr); } for (RealmConfig realmConf : realmList) { InitKeycloakServer.addRealm(keycloak, realmConf); } } else { fail("no realm config found in: " + initFileStr); } }
From source file:com.googlecode.xmlzen.utils.FileUtils.java
/** * Gets a {@link File} from a String that represents file path which is * relative to current Class Path/* w ww .ja v a2s . c o m*/ * * @param path Path to File * @param classLoader {@link ClassLoader} that should be able to see the * file * @return {@link File} object or null if nothing is found */ public static File getClassPathFile(final String path, final ClassLoader classLoader) { final URL url = classLoader.getResource(path); if (url == null) { return null; } return new File(url.getFile()); }
From source file:com.surenpi.autotest.suite.SuiteRunnerLauncher.java
/** * @param centerPanel//from w w w.j a va 2s. c om * @param urlList */ private static void createItemsPanel(JPanel centerPanel, List<URL> urlList) { JPanel itemsPanel = new JPanel(); centerPanel.add(itemsPanel, BorderLayout.CENTER); if (CollectionUtils.isEmpty(urlList)) { return; } for (URL url : urlList) { String text = url.getFile(); JCheckBox box = new JCheckBox(new File(text).getName()); box.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JCheckBox source = (JCheckBox) e.getSource(); if (source.isSelected()) { runnerList.add(source.getText()); } else { runnerList.remove(source.getText()); } } }); itemsPanel.add(box); } }
From source file:info.dolezel.fatrat.plugins.helpers.NativeHelpers.java
public static Class[] findAnnotatedClasses(String packageName, Class annotation) throws IOException, ClassNotFoundException { String path = packageName.replace('.', '/'); Enumeration<URL> resources = loader.getResources(path); Set<File> dirs = new HashSet<File>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); }/*from ww w.ja v a 2s . c o m*/ for (URL url : loader.getURLs()) { dirs.add(new File(url.getFile())); } ArrayList<Class> classes = new ArrayList<Class>(); for (File directory : dirs) { classes.addAll(findClasses(directory, packageName, annotation)); } return classes.toArray(new Class[classes.size()]); }
From source file:com.floragunn.searchguard.test.helper.file.FileHelper.java
public static File getAbsoluteFilePathFromClassPath(final String fileNameFromClasspath) { File file = null;// w w w. ja v a2s. c o m final URL fileUrl = AbstractSGUnitTest.class.getClassLoader().getResource(fileNameFromClasspath); if (fileUrl != null) { try { file = new File(URLDecoder.decode(fileUrl.getFile(), "UTF-8")); } catch (final UnsupportedEncodingException e) { return null; } if (file.exists() && file.canRead()) { return file; } else { log.error("Cannot read from {}, maybe the file does not exists? ", file.getAbsolutePath()); } } else { log.error("Failed to load " + fileNameFromClasspath); } return null; }
From source file:info.magnolia.cms.util.ClasspathResourcesUtil.java
/** * Return a collection containing the resource names which passed the filter. * * @param filter//from ww w .ja va 2 s .c o m * @return * @throws IOException */ public static String[] findResources(Filter filter) { Collection resources = new ArrayList(); ClassLoader cl = ClasspathResourcesUtil.class.getClassLoader(); // if the classloader is an URLClassloader we have a better method for discovering resources // whis will also fetch files from jars outside WEB-INF/lib, useful during development if (cl instanceof URLClassLoader) { // tomcat classloader is org.apache.catalina.loader.WebappClassLoader URL[] urls = ((URLClassLoader) cl).getURLs(); for (int j = 0; j < urls.length; j++) { URL url = urls[j]; File tofile = new File(url.getFile()); collectFiles(resources, tofile, filter); } } else { // no way, we have to assume a standard war structure and look in the WEB-INF/lib and WEB-INF/classes dirs // read the jars in the lib dir File dir = new File(Path.getAbsoluteFileSystemPath("WEB-INF/lib")); //$NON-NLS-1$ if (dir.exists()) { File[] files = dir.listFiles(new FilenameFilter() { public boolean accept(File file, String name) { return name.endsWith(".jar"); } }); for (int i = 0; i < files.length; i++) { collectFiles(resources, files[i], filter); } } // read files in WEB-INF/classes collectFiles(resources, new File(Path.getAbsoluteFileSystemPath("WEB-INF/classes")), filter); } return (String[]) resources.toArray(new String[resources.size()]); }
From source file:by.stub.cli.CommandLineIntepreter.java
/** * Returns stubby4j JAR path, relative to current execution directory under local filesystem based * on the given class executed within the JAR. * * @param theclass Class executed within the JAR. The class discloses JAR's current location * @return relative stubby4j JAR path/*from w w w.j av a 2 s . co m*/ */ public static String getCurrentJarLocation(final Class theclass) { final URL location = theclass.getProtectionDomain().getCodeSource().getLocation(); final String jarAbsolutePath = new File(location.getFile()).getAbsolutePath(); final String jar = jarAbsolutePath.replaceAll(System.getProperty("user.dir") + "/", ""); if (StringUtils.toLower(jar).endsWith(".jar")) { return jar; } return "stubby4j-x.x.x-SNAPSHOT.jar"; }
From source file:info.dolezel.fatrat.plugins.helpers.NativeHelpers.java
public static Class[] findAnnotatedClasses(String path, String packageName, String annotation) throws IOException, ClassNotFoundException { URL url = new URL("jar", "", "file:" + path + "!/"); Set<Class> classes = findClasses(new File(url.getFile()), packageName, Class.forName(annotation)); return classes.toArray(new Class[classes.size()]); }
From source file:Main.java
/** * Convert from a <code>URL</code> to a <code>File</code>. * <p/>/*from w w w.j a va 2 s . c om*/ * From version 1.1 this method will decode the URL. * Syntax such as <code>file:///my%20docs/file.txt</code> will be * correctly decoded to <code>/my docs/file.txt</code>. Starting with version * 1.5, this method uses UTF-8 to decode percent-encoded octets to characters. * Additionally, malformed percent-encoded octets are handled leniently by * passing them through literally. * * @param url the file URL to convert, {@code null} returns {@code null} * @return the equivalent <code>File</code> object, or {@code null} * if the URL's protocol is not <code>file</code> */ public static File toFile(URL url) { if (url == null || !"file".equalsIgnoreCase(url.getProtocol())) { return null; } else { String filename = url.getFile().replace('/', File.separatorChar); filename = decodeUrl(filename); return new File(filename); } }