List of usage examples for org.apache.commons.vfs2 FileSystemManager resolveFile
FileObject resolveFile(URL url) throws FileSystemException;
From source file:ShowProperties.java
public static void main(String[] args) throws FileSystemException { if (args.length == 0) { System.err.println("Please pass the name of a file as parameter."); System.err.println("e.g. java org.apache.commons.vfs2.example.ShowProperties LICENSE.txt"); return;/*from w w w . ja v a2 s .c om*/ } for (int i = 0; i < args.length; i++) { try { FileSystemManager mgr = VFS.getManager(); System.out.println(); System.out.println("Parsing: " + args[i]); FileObject file = mgr.resolveFile(args[i]); System.out.println("URL: " + file.getURL()); System.out.println("getName(): " + file.getName()); System.out.println("BaseName: " + file.getName().getBaseName()); System.out.println("Extension: " + file.getName().getExtension()); System.out.println("Path: " + file.getName().getPath()); System.out.println("Scheme: " + file.getName().getScheme()); System.out.println("URI: " + file.getName().getURI()); System.out.println("Root URI: " + file.getName().getRootURI()); System.out.println("Parent: " + file.getName().getParent()); System.out.println("Type: " + file.getType()); System.out.println("Exists: " + file.exists()); System.out.println("Readable: " + file.isReadable()); System.out.println("Writeable: " + file.isWriteable()); System.out.println("Root path: " + file.getFileSystem().getRoot().getName().getPath()); if (file.exists()) { if (file.getType().equals(FileType.FILE)) { System.out.println("Size: " + file.getContent().getSize() + " bytes"); } else if (file.getType().equals(FileType.FOLDER) && file.isReadable()) { FileObject[] children = file.getChildren(); System.out.println("Directory with " + children.length + " files"); for (int iterChildren = 0; iterChildren < children.length; iterChildren++) { System.out.println("#" + iterChildren + ": " + children[iterChildren].getName()); if (iterChildren > 5) { break; } } } System.out.println("Last modified: " + DateFormat.getInstance().format(new Date(file.getContent().getLastModifiedTime()))); } else { System.out.println("The file does not exist"); } file.close(); } catch (FileSystemException ex) { ex.printStackTrace(); } } }
From source file:maspack.fileutil.ZipUtility.java
public static void unzip(URIx src, final File dest) throws IOException { dest.mkdirs();//from ww w . j a v a2s. com final FileSystemManager fileSystemManager = VFS.getManager(); final FileObject zipFileObject = fileSystemManager.resolveFile(src.toString()); try { final FileObject fileSystem = fileSystemManager.createFileSystem(zipFileObject); try { fileSystemManager.toFileObject(dest).copyFrom(fileSystem, new AllFileSelector()); } finally { fileSystem.close(); } } finally { zipFileObject.close(); } }
From source file:fulcrum.xml.ParserTest.java
private static InputStream getInputStream(String location) { InputStream is = null;/*from w w w. j a v a 2 s . c om*/ try { FileSystemManager fsManager = VFS.getManager(); FileObject fileObj = fsManager.resolveFile(location); if (fileObj != null && fileObj.exists() && fileObj.isReadable()) { FileContent content = fileObj.getContent(); is = content.getInputStream(); } } catch (Exception e) { e.printStackTrace(); } return is; }
From source file:fi.mystes.synapse.mediator.vfs.VFSTestHelper.java
public static void assertFilesExists(String directory, int expectedCount) throws FileSystemException { FileSystemManager fsManager = VFS.getManager(); FileObject file = fsManager.resolveFile(directory); FileObject[] children = file.getChildren(); assertEquals("File count doesn't match", expectedCount, children.length); }
From source file:fi.mystes.synapse.mediator.vfs.VFSTestHelper.java
public static TestFile createFile(String path, String content) throws IOException { FileSystemManager fsManager = VFS.getManager(); FileObject file = fsManager.resolveFile(path); file.getContent().getOutputStream().write(content.getBytes()); file.getContent().close();//from w ww .j av a 2 s . c o m return new TestFileImpl(path, content, file.getContent().getSize()); }
From source file:gridool.sqlet.catalog.PartitioningConf.java
private static void loadSettingsFromCsv(String uri, List<Partition> list) throws SqletException { final InputStream is; try {//from w w w. j av a2 s . c o m FileSystemManager fsManager = VFS.getManager(); FileObject fileObj = fsManager.resolveFile(uri); FileContent fileContent = fileObj.getContent(); is = fileContent.getInputStream(); } catch (FileSystemException e) { throw new SqletException(SqletErrorType.configFailed, "failed to load a file: " + uri, e); } InputStreamReader reader = new InputStreamReader(new FastBufferedInputStream(is)); HeaderAwareCsvReader csvReader = new HeaderAwareCsvReader(reader, ',', '"'); final Map<String, Integer> headerMap; try { headerMap = csvReader.parseHeader(); } catch (IOException e) { throw new SqletException(SqletErrorType.configFailed, "failed to parse a header: " + uri, e); } final int[] fieldIndexes = toFieldIndexes(headerMap); final Map<GridNode, Partition> masterSlave = new HashMap<GridNode, Partition>(128); while (csvReader.next()) { String nodeStr = csvReader.get(fieldIndexes[0]); String masterStr = csvReader.get(fieldIndexes[1]); String dbUrl = csvReader.get(fieldIndexes[2]); String user = csvReader.get(fieldIndexes[3]); String password = csvReader.get(fieldIndexes[4]); String mapOutput = csvReader.get(fieldIndexes[5]); Preconditions.checkNotNull(nodeStr, dbUrl); GridNode node = GridUtils.getNode(nodeStr); Partition p = new Partition(node, dbUrl, user, password, mapOutput); if (masterStr == null || masterStr.length() == 0) { masterSlave.put(node, p); list.add(p); } else { GridNode master = GridUtils.getNode(masterStr); Partition masterPartition = masterSlave.get(master); if (masterPartition == null) { LOG.error("Master partition is not found for slave: " + p); } else { masterPartition.addSlave(p); } } } }
From source file:com.yenlo.synapse.transport.vfs.VFSUtils.java
/** * Release a file item lock acquired either by the VFS listener or a sender * * @param fsManager which is used to resolve the processed file * @param fo representing the processed file */// w w w. j a va 2 s.co m public static void releaseLock(FileSystemManager fsManager, FileObject fo) { try { String fullPath = fo.getName().getURI(); int pos = fullPath.indexOf("?"); if (pos > -1) { fullPath = fullPath.substring(0, pos); } FileObject lockObject = fsManager.resolveFile(fullPath + ".lock"); if (lockObject.exists()) { lockObject.delete(); } } catch (FileSystemException e) { log.error("Couldn't release the lock for the file : " + fo.getName() + " after processing"); } }
From source file:net.simon04.guavavfs.VirtualFiles.java
/** * Returns true if the files contains the same bytes. * * @throws IOException if an I/O error occurs *///w w w . j a v a2 s . co m public static boolean equal(String file1, String file2) throws IOException { checkNotNull(file1); checkNotNull(file2); final FileSystemManager vfs = VFS.getManager(); if (Objects.equals(file1, file2) || vfs.resolveFile(file1).equals(vfs.resolveFile(file2))) { return true; } /* * Some operating systems may return zero as the length for files * denoting system-dependent entities such as devices or pipes, in * which case we must fall back on comparing the bytes directly. */ long len1 = length(file1); long len2 = length(file2); if (len1 != 0 && len2 != 0 && len1 != len2) { return false; } return asByteSource(file1).contentEquals(asByteSource(file2)); }
From source file:com.yenlo.synapse.transport.vfs.VFSUtils.java
/** * Acquires a file item lock before processing the item, guaranteing that the file is not * processed while it is being uploaded and/or the item is not processed by two listeners * * @param fsManager used to resolve the processing file * @param fo representing the processing file item * @return boolean true if the lock has been acquired or false if not *///from w ww . jav a 2 s.c om public synchronized static boolean acquireLock(FileSystemManager fsManager, FileObject fo) { // generate a random lock value to ensure that there are no two parties // processing the same file Random random = new Random(); byte[] lockValue = String.valueOf(random.nextLong()).getBytes(); try { // check whether there is an existing lock for this item, if so it is assumed // to be processed by an another listener (downloading) or a sender (uploading) // lock file is derived by attaching the ".lock" second extension to the file name String fullPath = fo.getName().getURI(); int pos = fullPath.indexOf("?"); if (pos != -1) { fullPath = fullPath.substring(0, pos); } FileObject lockObject = fsManager.resolveFile(fullPath + ".lock"); if (lockObject.exists()) { log.debug("There seems to be an external lock, aborting the processing of the file " + fo.getName() + ". This could possibly be due to some other party already " + "processing this file or the file is still being uploaded"); } else { // write a lock file before starting of the processing, to ensure that the // item is not processed by any other parties lockObject.createFile(); OutputStream stream = lockObject.getContent().getOutputStream(); try { stream.write(lockValue); stream.flush(); stream.close(); } catch (IOException e) { lockObject.delete(); log.error("Couldn't create the lock file before processing the file " + fullPath, e); return false; } finally { lockObject.close(); } // check whether the lock is in place and is it me who holds the lock. This is // required because it is possible to write the lock file simultaneously by // two processing parties. It checks whether the lock file content is the same // as the written random lock value. // NOTE: this may not be optimal but is sub optimal FileObject verifyingLockObject = fsManager.resolveFile(fullPath + ".lock"); if (verifyingLockObject.exists() && verifyLock(lockValue, verifyingLockObject)) { return true; } } } catch (FileSystemException fse) { log.error("Cannot get the lock for the file : " + maskURLPassword(fo.getName().getURI()) + " before processing"); } return false; }
From source file:cz.lbenda.rcp.config.TildaFileProvider.java
@Override public FileObject findFile(FileObject fileObject, String uri, FileSystemOptions fileSystemOptions) throws FileSystemException { String userHome = System.getProperty("user.home"); if (uri.startsWith("~")) { uri = uri.replace("~://", "file://" + userHome + "/"); uri = uri.replace("~", "file://" + userHome); } else {//from w w w . j a v a2 s . c om uri = "file://" + userHome + "/" + uri.replace("home://", ""); } FileSystemManager fsManager = VFS.getManager(); return fsManager.resolveFile(uri); }