List of usage examples for java.io File toURI
public URI toURI()
From source file:com.offbynull.coroutines.instrumenter.testhelpers.TestUtils.java
/** * Writes entries to a JAR and loads it up. * @param entries class nodes to put in to jar * @return class loader with files in newly created JAR available * @throws IOException if any IO error occurs * @throws NullPointerException if any argument is {@code null} or contains {@code null} * @throws IllegalArgumentException if {@code classNodes} is empty *//*from w ww . j a v a 2s . c o m*/ public static URLClassLoader createJarAndLoad(JarEntry... entries) throws IOException { Validate.notNull(entries); Validate.noNullElements(entries); File jarFile = createJar(entries); return URLClassLoader.newInstance(new URL[] { jarFile.toURI().toURL() }, TestUtils.class.getClassLoader()); }
From source file:it.geosolutions.geobatch.action.scripting.ScriptingAction.java
/** * /*from w ww . j ava2 s.c o m*/ * @param moduleFile * @throws IOException */ private static void addFile(File moduleFile) throws IOException { URL moduleURL = moduleFile.toURI().toURL(); final Class[] parameters = new Class[] { URL.class }; URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class sysclass = URLClassLoader.class; try { Method method = sysclass.getDeclaredMethod("addURL", parameters); method.setAccessible(true); method.invoke(sysloader, new Object[] { moduleURL }); } catch (Throwable t) { LOGGER.error("Error, could not add URL to system classloader", t); throw new IOException("Error, could not add URL to system classloader"); } }
From source file:ArchiveUtil.java
public static boolean extract(File archive, File targetDir) throws IOException { return extract(archive.toURI().toURL(), targetDir, true); }
From source file:com.marklogic.contentpump.ContentPump.java
/** * Set class loader for current thread and for Confifguration based on * Hadoop home.//from ww w . ja v a2 s . c o m * * @param hdConfDir Hadoop home directory * @param conf Hadoop configuration * @throws MalformedURLException */ private static void setClassLoader(File hdConfDir, Configuration conf) throws Exception { ClassLoader parent = conf.getClassLoader(); URL url = hdConfDir.toURI().toURL(); URL[] urls = new URL[1]; urls[0] = url; ClassLoader classLoader = new URLClassLoader(urls, parent); Thread.currentThread().setContextClassLoader(classLoader); conf.setClassLoader(classLoader); }
From source file:com.kelveden.rastajax.cli.Runner.java
private static List<URL> getClasspathURLs(File workingDirectory) throws CliExecutionException { final File classesFolder = new File(workingDirectory, "WEB-INF/classes"); final File libFolder = new File(workingDirectory, "WEB-INF/lib"); final List<URL> urls = new ArrayList<URL>(); try {//w w w. j a v a 2 s . co m urls.add(classesFolder.toURI().toURL()); } catch (final MalformedURLException e) { throw new CliExecutionException("Could not get the URL for '" + classesFolder.getAbsolutePath() + "'.", e); } for (File file : FileUtils.listFiles(libFolder, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE)) { try { urls.add(file.toURI().toURL()); } catch (final MalformedURLException e) { throw new CliExecutionException("Could not get the URL for '" + file.getAbsolutePath() + "'.", e); } } return urls; }
From source file:com.dx.ss.plugins.ptree.utils.ClassloaderUtility.java
public static ClassLoader getCustomClassloader(String classpathEntry) { File file = null; try {//from w w w .j av a2s . c o m if (StringUtils.isNotBlank(classpathEntry)) { file = new File(classpathEntry); if (file.exists()) { ClassLoader parent = Thread.currentThread().getContextClassLoader(); URLClassLoader ucl = new URLClassLoader(new URL[] { file.toURI().toURL() }, parent); return ucl; } } } catch (MalformedURLException e) { e.printStackTrace(); } return null; }
From source file:eu.eubrazilcc.lvl.core.conf.ConfigurationFinder.java
/** * Finds available configuration files, searching the different sources in the following order: * <ol>/* ww w .j a va 2s.co m*/ * <li>Application environment;</li> * <li>JNDI context;</li> * <li>Default location in the file-system locally available to the application; and finally</li> * <li>If none of the above works, configuration files are returned from the resources available * in the application class path.</li> * </ol> * @return configuration files available to the application */ public final static ImmutableList<URL> findConfigurationFiles() { ImmutableList<File> configFiles = null; // try to read environment variable and retrieve configuration files from the file system try { final String location = getenv(ENV_HOME_VAR) + "/etc"; if (isNotBlank(location) && testConfigurationFiles(location, CONFIGURATION_FILENAMES)) { configFiles = convertPathListToFiles(location, CONFIGURATION_FILENAMES); LOGGER.trace("Environment variable '" + ENV_HOME_VAR + "' found. Configuration files: " + filesToString(configFiles)); } else { LOGGER.trace("Environment variable '" + ENV_HOME_VAR + "' was not found"); } } catch (Exception ignore) { } // try to read from JNDI context if (configFiles == null) { try { final Context initCtx = new InitialContext(); final Context envCtx = (Context) initCtx.lookup("java:comp/env"); final String location = (String) envCtx.lookup(ENV_HOME_VAR) + "/etc"; if (isNotBlank(location) && testConfigurationFiles(location, CONFIGURATION_FILENAMES)) { configFiles = convertPathListToFiles(location, CONFIGURATION_FILENAMES); LOGGER.trace("JNDI context variable '" + DEFAULT_LOCATION + "' found. Configuration files: " + filesToString(configFiles)); } else { LOGGER.trace("JNDI context variable '" + DEFAULT_LOCATION + "' was not found"); } } catch (Exception e) { LOGGER.trace( "Failed to search for configuration files in JNDI context variable: '" + ENV_HOME_VAR + "'", e); } } // try to retrieve configuration files from the default location if (configFiles == null) { try { final String location = DEFAULT_LOCATION + "/etc"; if (isNotBlank(location) && testConfigurationFiles(location, CONFIGURATION_FILENAMES)) { configFiles = convertPathListToFiles(location, CONFIGURATION_FILENAMES); LOGGER.trace("Default location '" + DEFAULT_LOCATION + "' found. Configuration files: " + filesToString(configFiles)); } else { LOGGER.trace("Default location '" + DEFAULT_LOCATION + "' was not found"); } } catch (Exception e) { LOGGER.warn("Failed to search for configuration files in default location: " + DEFAULT_LOCATION, e); } } // return default configuration files if (configFiles == null) { return getDefaultConfiguration(); } else { return from(configFiles).transform(new Function<File, URL>() { @Override public URL apply(final File file) { URL url = null; if (file != null) { try { url = file.toURI().toURL(); } catch (MalformedURLException e) { LOGGER.warn("Ignoring file: " + file.getName(), e); } } return url; } }).filter(notNull()).toList(); } }
From source file:info.servertools.core.util.FileUtils.java
public static void zipDirectory(File directory, File zipfile, @Nullable Collection<String> fileBlacklist, @Nullable Collection<String> folderBlacklist) throws IOException { URI baseDir = directory.toURI(); Deque<File> queue = new LinkedList<>(); queue.push(directory);/*from ww w. j av a 2s. c o m*/ OutputStream out = new FileOutputStream(zipfile); Closeable res = out; try { ZipOutputStream zout = new ZipOutputStream(out); res = zout; while (!queue.isEmpty()) { directory = queue.removeFirst(); File[] dirFiles = directory.listFiles(); if (dirFiles != null && dirFiles.length != 0) { for (File child : dirFiles) { if (child != null) { String name = baseDir.relativize(child.toURI()).getPath(); if (child.isDirectory() && (folderBlacklist == null || !folderBlacklist.contains(child.getName()))) { queue.push(child); name = name.endsWith("/") ? name : name + "/"; zout.putNextEntry(new ZipEntry(name)); } else { if (fileBlacklist != null && !fileBlacklist.contains(child.getName())) { zout.putNextEntry(new ZipEntry(name)); copy(child, zout); zout.closeEntry(); } } } } } } } finally { res.close(); } }
From source file:com.wavemaker.tools.ws.XJCCompiler.java
@SuppressWarnings("deprecation") public static S2JJAXBModel createSchemaModel(Map<String, Element> schemas, List<com.wavemaker.tools.io.File> bindingFiles, String packageName, Set<String> auxiliaryClasses, WebServiceType type) throws GenerationException { if (schemas == null || schemas.isEmpty()) { return null; }//from w w w . j a v a 2s.com SchemaCompiler sc = XJC.createSchemaCompiler(); if (type == WebServiceType.SOAP) { // mimic what JAXWS's WsimportTool would do for SEI class name and // JAXB class name collision. ClassNameAllocator allocator = new ClassNameAllocatorImpl( new SimpleClassNameCollector(auxiliaryClasses)); sc.setClassNameAllocator(allocator); } JAXBCompilerErrorListener listener = new JAXBCompilerErrorListener(); sc.setErrorListener(listener); try { Field ncc = sc.getClass().getDeclaredField("NO_CORRECTNESS_CHECK"); ncc.setAccessible(true); ncc.set(sc, true); } catch (Exception e) { throw new GenerationException(e); } if (packageName != null) { sc.setDefaultPackageName(packageName); } for (Entry<String, Element> entry : schemas.entrySet()) { Element schema = entry.getValue(); // need to remove xsd:import or you will get element/type already // defined error during sc.bind() Element updatedSchema = removeImportElement(schema); sc.parseSchema(entry.getKey(), updatedSchema); } if (bindingFiles != null) { for (com.wavemaker.tools.io.File file : bindingFiles) { try { InputSource inputSource = new InputSource(file.getContent().asInputStream()); // cftempfix - if binding files are NOT ALWAYS local files (eg. mongo DB file), we may need to // correctly implement // logic for none-local file case. if (file instanceof LocalFile) { File f = ((LocalFile) file).getLocalFile(); inputSource.setSystemId(f.toURI().toString()); } else { inputSource.setSystemId(ResourceURL.get(file).toURI().toString()); } sc.parseSchema(inputSource); } catch (MalformedURLException e) { throw new GenerationException(e); } catch (URISyntaxException e) { throw new GenerationException(e); } } } Options options = sc.getOptions(); options.target = SpecVersion.V2_1; // suppress generation of package level annotations options.packageLevelAnnotations = false; // generate setter methods for Collection based properties options.activePlugins.add(new com.sun.tools.xjc.addon.collection_setter_injector.PluginImpl()); // replace isXXX with getXXX for Boolean type properties options.activePlugins.add(new com.wavemaker.tools.ws.jaxb.boolean_getter.PluginImpl()); S2JJAXBModel model = sc.bind(); if (listener.hasError) { throw listener.getException(); } return model; }
From source file:Main.java
private static URI makeURIFromFilespec(final String filespec, final String relativePrefix) { // make sure the file is absolute & canonical file url File file = new File(decode(filespec)); // if we have a prefix and the file is not abs then prepend if (relativePrefix != null && !file.isAbsolute()) { file = new File(relativePrefix, filespec); }/*from w w w . ja va 2s. co m*/ return file.toURI(); }