List of usage examples for java.net URL getFile
public String getFile()
From source file:no.dusken.aranea.service.StoreImageServiceImpl.java
public Image createImage(URL url) throws IOException { File file = new File(imageDirectory + "/tmp/" + url.getFile()); FileUtils.copyURLToFile(url, file);/* w ww. java 2 s . c om*/ return createImage(file); }
From source file:org.brutusin.rpc.RpcComponent.java
public URL getSourceCode() { try {/* w w w .j av a 2 s . co m*/ URL jarUrl = getClass().getProtectionDomain().getCodeSource().getLocation(); Enumeration<URL> resources = getClass().getClassLoader().getResources("META-INF/source-repo.txt"); while (resources.hasMoreElements()) { URL url = resources.nextElement(); if (url.getFile().contains(jarUrl.getFile())) { String baseUrl = Miscellaneous.toString(url.openStream(), "UTF-8"); Class clazz = getClass().getDeclaringClass(); if (clazz == null) { clazz = getClass(); } StringBuilder sb = new StringBuilder(baseUrl); if (!baseUrl.endsWith("/")) { sb.append("/"); } return new URL(sb.append(clazz.getName().replace('.', '/')).append(".java").toString()); } } } catch (IOException ex) { throw new RuntimeException(ex); } return null; }
From source file:br.eti.kinoshita.testlinkjavaapi.BaseTest.java
public void loadXMLRPCMockData(String xmlFile) { URL url = getClass().getResource("/br/eti/kinoshita/testlinkjavaapi/testdata/" + xmlFile); String filePath = url.getFile(); File file = new File(filePath); String mockXml;/*from w ww. j a v a 2s. co m*/ try { mockXml = FileUtils.readFileToString(file); } catch (IOException e) { throw new RuntimeException(e); } this.server.setMockResponseBody(mockXml); }
From source file:com.ariht.maven.plugins.config.ConfigGenerationMojoTest.java
/** * Given a relative path on the classpath, in this case src/tests/resources. *//* ww w. j ava2 s . c om*/ private String getAbsolutePath(final String subDirectoryName) throws IOException { final URL resource = getClass().getResource("/"); final String normalizedAbsolutePath = FilenameUtils.normalize(resource.getFile() + subDirectoryName); final File directoryFile = new File(normalizedAbsolutePath); if (!directoryFile.exists()) { FileUtils.forceMkdir(directoryFile); } return normalizedAbsolutePath; }
From source file:co.cask.cdap.security.server.ExternalLDAPAuthenticationServerSSLTest.java
@BeforeClass public static void beforeClass() throws Exception { URL certUrl = ExternalLDAPAuthenticationServerSSLTest.class.getClassLoader().getResource("cert.jks"); Assert.assertNotNull(certUrl);/*from ww w. j a va 2 s. co m*/ String authHandlerConfigBase = Constants.Security.AUTH_HANDLER_CONFIG_BASE; CConfiguration cConf = CConfiguration.create(); SConfiguration sConf = SConfiguration.create(); cConf.set(Constants.Security.AUTH_SERVER_BIND_ADDRESS, "127.0.0.1"); cConf.set(Constants.Security.SSL.EXTERNAL_ENABLED, "true"); cConf.set(Constants.Security.AuthenticationServer.SSL_PORT, "0"); cConf.set(authHandlerConfigBase.concat("useLdaps"), "true"); cConf.set(authHandlerConfigBase.concat("ldapsVerifyCertificate"), "false"); sConf.set(Constants.Security.AuthenticationServer.SSL_KEYSTORE_PATH, certUrl.getPath()); configuration = cConf; sConfiguration = sConf; String keystorePassword = sConf.get(Constants.Security.AuthenticationServer.SSL_KEYSTORE_PASSWORD); KeyStoreKeyManager keyManager = new KeyStoreKeyManager(certUrl.getFile(), keystorePassword.toCharArray()); SSLUtil sslUtil = new SSLUtil(keyManager, new TrustAllTrustManager()); ldapListenerConfig = InMemoryListenerConfig.createLDAPSConfig("LDAP", InetAddress.getByName("127.0.0.1"), ldapPort, sslUtil.createSSLServerSocketFactory(), sslUtil.createSSLSocketFactory()); testServer = new ExternalLDAPAuthenticationServerSSLTest(); testServer.setup(); }
From source file:com.alibaba.jstorm.blobstore.BlobStoreUtils.java
public static void downloadLocalStormCode(Map conf, String topologyId, String masterCodeDir) throws IOException, TException { // STORM_LOCAL_DIR/supervisor/tmp/(UUID) String tmpRoot = StormConfig.supervisorTmpDir(conf) + File.separator + UUID.randomUUID().toString(); // STORM-LOCAL-DIR/supervisor/stormdist/storm-id String stormRoot = StormConfig.supervisor_stormdist_root(conf, topologyId); BlobStore blobStore = null;//from w w w . j a v a2 s .c om try { blobStore = BlobStoreUtils.getNimbusBlobStore(conf, masterCodeDir, null); FileUtils.forceMkdir(new File(tmpRoot)); blobStore.readBlobTo(StormConfig.master_stormcode_key(topologyId), new FileOutputStream(StormConfig.stormcode_path(tmpRoot))); blobStore.readBlobTo(StormConfig.master_stormconf_key(topologyId), new FileOutputStream(StormConfig.stormconf_path(tmpRoot))); } finally { if (blobStore != null) blobStore.shutdown(); } File srcDir = new File(tmpRoot); File destDir = new File(stormRoot); try { FileUtils.moveDirectory(srcDir, destDir); } catch (FileExistsException e) { FileUtils.copyDirectory(srcDir, destDir); FileUtils.deleteQuietly(srcDir); } ClassLoader classloader = Thread.currentThread().getContextClassLoader(); String resourcesJar = resourcesJar(); URL url = classloader.getResource(StormConfig.RESOURCES_SUBDIR); String targetDir = stormRoot + '/' + StormConfig.RESOURCES_SUBDIR; if (resourcesJar != null) { LOG.info("Extracting resources from jar at " + resourcesJar + " to " + targetDir); JStormUtils.extractDirFromJar(resourcesJar, StormConfig.RESOURCES_SUBDIR, stormRoot); } else if (url != null) { LOG.info("Copying resources at " + url.toString() + " to " + targetDir); FileUtils.copyDirectory(new File(url.getFile()), (new File(targetDir))); } }
From source file:com.github.helenusdriver.commons.lang3.reflect.ReflectionUtils.java
/** * Finds all resources defined in a given package. * * @author paouelle/*from w w w . java2 s. c o m*/ * * @param pkg the package from which to find all defined resources * @param cl the classloader to find the resources with * @return the non-<code>null</code> collection of all resources defined in the * given package * @throws NullPointerException if <code>pkg</code> or <code>cl</code> is * <code>null</code> */ public static Collection<URL> findResources(String pkg, ClassLoader cl) { org.apache.commons.lang3.Validate.notNull(pkg, "invalid null pkg"); final String scannedPath = pkg.replace('.', File.separatorChar); final Enumeration<URL> resources; try { resources = cl.getResources(scannedPath); } catch (IOException e) { throw new IllegalArgumentException("Unable to get resources from path '" + scannedPath + "'. Are you sure the given '" + pkg + "' package exists?", e); } final List<URL> urls = new LinkedList<>(); while (resources.hasMoreElements()) { final URL url = resources.nextElement(); if ("jar".equals(url.getProtocol())) { ReflectionUtils.findResourcesFromJar(urls, url, scannedPath, cl); } else if ("file".equals(url.getProtocol())) { final File file = new File(url.getFile()); ReflectionUtils.findResourcesFromFile(urls, file, scannedPath, cl); } else { throw new IllegalArgumentException("package is provided by an unknown url: " + url); } } return urls; }
From source file:com.navercorp.pinpoint.bootstrap.java9.module.JarFileAnalyzerTest.java
@Test public void packageAnalyzer() throws IOException { URL url = CodeSourceUtils.getCodeLocation(Logger.class); JarFile jarFile = new JarFile(url.getFile()); logger.debug("jarFile:{}", jarFile.getName()); PackageAnalyzer packageAnalyzer = new JarFileAnalyzer(jarFile); PackageInfo packageInfo = packageAnalyzer.analyze(); Set<String> packageSet = packageInfo.getPackage(); logger.debug("package:{}", packageSet); Assert.assertEquals(packageSet, SLF4J_API_PACKAGE); }
From source file:com.picocontainer.script.StandaloneTestCase.java
private File getAbsoluteScriptPath() { String className = getClass().getName(); String relativeClassPath = "/" + className.replace('.', '/') + ".class"; URL classURL = Standalone.class.getResource(relativeClassPath); String absoluteClassPath = classURL.getFile(); File absoluteDirPath = new File(absoluteClassPath).getParentFile(); File absoluteScriptPath = new File(absoluteDirPath, "picocontainer.xml"); return absoluteScriptPath; }
From source file:com.github.helenusdriver.commons.lang3.reflect.ReflectionUtils.java
/** * Finds all classes defined in a given package. * * @author paouelle/*from ww w. j ava 2s . c o m*/ * * @param pkg the package from which to find all defined classes * @param cl the classloader to find the classes with * @return the non-<code>null</code> collection of all classes defined in the * given package * @throws NullPointerException if <code>pkg</code> or <code>cl</code> is * <code>null</code> */ public static Collection<Class<?>> findClasses(String pkg, ClassLoader cl) { org.apache.commons.lang3.Validate.notNull(pkg, "invalid null pkg"); final String scannedPath = pkg.replace('.', File.separatorChar); final Enumeration<URL> resources; try { resources = cl.getResources(scannedPath); } catch (IOException e) { throw new IllegalArgumentException("Unable to get resources from path '" + scannedPath + "'. Are you sure the given '" + pkg + "' package exists?", e); } final List<Class<?>> classes = new LinkedList<>(); while (resources.hasMoreElements()) { final URL url = resources.nextElement(); if ("jar".equals(url.getProtocol())) { ReflectionUtils.findClassesFromJar(classes, url, scannedPath, cl); } else if ("file".equals(url.getProtocol())) { final File file = new File(url.getFile()); ReflectionUtils.findClassesFromFile(classes, file, pkg, cl); } else { throw new IllegalArgumentException("package is provided by an unknown url: " + url); } } return classes; }