List of usage examples for org.apache.commons.vfs2 FileObject getURL
URL getURL() throws FileSystemException;
From source file:fr.cls.atoll.motu.library.misc.vfs.VFSManager.java
/** * Copy file to local file.//from w ww . j av a2 s.com * * @param uriSrc the uri src * @param fileDest the file dest * * @throws MotuException the motu exception */ public void copyFileToLocalFile(String uriSrc, String fileDest) throws MotuException { if (LOG.isDebugEnabled()) { LOG.debug("copyFileToLocalFile(String, String) - entering"); } // URI uri = new URI(uriSrc); // // String[] userInfo = uri.getUserInfo().split(":"); // String user = ""; // String pwd = ""; // // if (userInfo.length >= 2) { // pwd = userInfo[1]; // } // // if (userInfo.length >= 1) { // user = userInfo[0]; // } // // copyFile(user, pwd, uri.getScheme(), uri. FileObject foSrc = null; FileObject foDest = null; try { File newFile = VFSManager.createLocalFile(fileDest); foSrc = resolveFile(uriSrc); if (foSrc == null) { throw new MotuException(String.format("Unable to resolve source uri '%s' ", uriSrc)); } foDest = standardFileSystemManager.toFileObject(newFile); if (foDest == null) { throw new MotuException( String.format("Unable to resolve dest uri '%s' ", newFile.getAbsolutePath())); } this.copyFrom(foSrc, foDest, Selectors.SELECT_ALL); } catch (MotuException e) { LOG.error("copyFileToLocalFile(String, String)", e); throw e; } catch (Exception e) { LOG.error("copyFileToLocalFile(String, String)", e); try { throw new MotuException(String.format("Unable to copy file '%s' to '%s'", foSrc.getURL().toString(), foDest.getURL().toString()), e); } catch (FileSystemException e1) { LOG.error("copyFileToLocalFile(String, String)", e1); throw new MotuException(String.format("Unable to copy files", e1)); } } if (LOG.isDebugEnabled()) { LOG.debug("copyFileToLocalFile(String, String) - exiting"); } }
From source file:maspack.fileutil.FileCacher.java
public boolean copy(URIx from, URIx to, FileTransferMonitor monitor) throws FileSystemException { FileObject fromFile = null; FileObject toFile = null;/*from ww w .j a v a2s .c o m*/ // clear authenticators setAuthenticator(fsOpts, null); setIdentityFactory(fsOpts, null); // loop through authenticators until we either succeed or cancel boolean cancel = false; while (toFile == null && cancel == false) { toFile = resolveRemote(to); } cancel = false; while (fromFile == null && cancel == false) { fromFile = resolveRemote(from); } if (fromFile == null || !fromFile.exists()) { throw new FileSystemException("Cannot find source file <" + from.toString() + ">", new FileNotFoundException("<" + from.toString() + ">")); } if (toFile == null) { throw new FileSystemException("Cannot find destination <" + to.toString() + ">", new FileNotFoundException("<" + to.toString() + ">")); } // monitor the file transfer progress if (monitor != null) { monitor.monitor(fromFile, toFile, -1, fromFile.getName().getBaseName()); monitor.start(); monitor.fireStartEvent(toFile); } // transfer content try { if (fromFile.isFile()) { toFile.copyFrom(fromFile, Selectors.SELECT_SELF); } else if (fromFile.isFolder()) { // final FileObject fileSystem = manager.createFileSystem(remoteFile); toFile.copyFrom(fromFile, new AllFileSelector()); // fileSystem.close(); } if (monitor != null) { monitor.fireCompleteEvent(toFile); } } catch (Exception e) { throw new FileTransferException( "Failed to complete transfer of " + fromFile.getURL() + " to " + toFile.getURL(), e); } finally { // close files if we need to fromFile.close(); toFile.close(); if (monitor != null) { monitor.release(toFile); monitor.stop(); } } return true; }
From source file:de.innovationgate.utils.WGUtils.java
/** * Returns a VFS file object for a folder. If the folder does not exist it is created. * @param parent The parent folder of the retrieved folder * @param name The name of the folder to retrieve * @return The folder//from w w w .j a va2s . c o m * @throws IOException */ public static FileObject getOrCreateFolder(FileObject parent, String name) throws IOException { if (!parent.getType().equals(FileType.FOLDER)) { throw new IllegalArgumentException("Parent file is no folder: " + parent.getName().getPathDecoded()); } FileObject folder = parent.resolveFile(name); if (!folder.exists()) { if (!folder.getFileSystem().hasCapability(Capability.CREATE)) { throw new IOException("File system of file " + folder.getURL().toString() + " is read only"); } folder.createFolder(); } if (!folder.getType().equals(FileType.FOLDER)) { throw new IllegalArgumentException("There is already a file of this name: " + name); } return folder; }
From source file:com.app.server.EARDeployer.java
public void obtainUrls(FileObject rootEar, FileObject ear, CopyOnWriteArrayList<URL> fileObjects, CopyOnWriteArrayList<FileObject> warObjects, CopyOnWriteArrayList<FileObject> jarObjects, CopyOnWriteArrayList<FileObject> sarObjects, CopyOnWriteArrayList<FileObject> rarObjects, CopyOnWriteArrayList<FileObject> ezbObjects, StandardFileSystemManager fsManager) throws IOException { FileObject[] childrenJars = ear.getChildren(); for (int childcount = 0; childcount < childrenJars.length; childcount++) { if (childrenJars[childcount].getType() == FileType.FOLDER) { obtainUrls(rootEar, childrenJars[childcount], fileObjects, warObjects, jarObjects, sarObjects, rarObjects, ezbObjects, fsManager); }/*from w ww . j a va2s. c o m*/ // log.info(childrenJars[childcount]); // log.info(childrenJars[childcount].getName().getBaseName()); // log.info(ear.getURL()); if (childrenJars[childcount].getType() == FileType.FILE && (childrenJars[childcount].getName().getBaseName().endsWith(".jar") || childrenJars[childcount].getName().getBaseName().endsWith(".war") || childrenJars[childcount].getName().getBaseName().toLowerCase().endsWith(".rar") || childrenJars[childcount].getName().getBaseName().toLowerCase().endsWith(".sar"))) { // log.info(childrenJars[childcount].getURL()); if ((childrenJars[childcount].getName().getBaseName().toLowerCase().endsWith(".war") || childrenJars[childcount].getName().getBaseName().toLowerCase().endsWith(".jar") || childrenJars[childcount].getName().getBaseName().toLowerCase().endsWith(".rar") || childrenJars[childcount].getName().getBaseName().toLowerCase().endsWith(".sar") || childrenJars[childcount].getName().getBaseName().toLowerCase().endsWith(".ezb")) && !childrenJars[childcount].getURL().toString().trim() .startsWith(rootEar.getURL().toString() + "lib/")) { //File file=new File(serverConfig.getDeploydirectory()+"/"+childrenJars[childcount].getName().getBaseName()); /*if(!file.exists()||(file.exists()&&file.lastModified()!=childrenJars[childcount].getContent().getLastModifiedTime())){ //InputStream fistr=childrenJars[childcount].getContent().getInputStream(); byte[] filyByt=new byte[4096]; FileOutputStream warFile=new FileOutputStream(serverConfig.getDeploydirectory()+"/"+childrenJars[childcount].getName().getBaseName()+".part"); int len=0; while((len=fistr.read(filyByt))!=-1){ warFile.write(filyByt,0,len); } warFile.close(); fistr.close(); new File(serverConfig.getDeploydirectory()+"/"+childrenJars[childcount].getName().getBaseName()+".part").renameTo(file); warObjects.add(childrenJars[childcount]); }*/ if (childrenJars[childcount].getName().getBaseName().toLowerCase().endsWith(".war")) { warObjects.add(childrenJars[childcount]); } else if (childrenJars[childcount].getName().getBaseName().toLowerCase().endsWith(".jar")) { jarObjects.add(childrenJars[childcount]); } else if (childrenJars[childcount].getName().getBaseName().toLowerCase().endsWith(".sar")) { sarObjects.add(childrenJars[childcount]); } else if (childrenJars[childcount].getName().getBaseName().toLowerCase().endsWith(".rar")) { rarObjects.add(childrenJars[childcount]); } else if (childrenJars[childcount].getName().getBaseName().toLowerCase().endsWith(".ezb")) { ezbObjects.add(childrenJars[childcount]); } } else { log.info("ear libs/" + childrenJars[childcount]); fileObjects.add(new URL(childrenJars[childcount].getURL().toString() .replace(rootEar.getURL().toString(), "rsrc:"))); } // log.info(childrenJars[childcount].getURL().toString()+" "+rootEar.getURL()); } else { childrenJars[childcount].close(); } } }
From source file:com.web.server.EARDeployer.java
public void obtainUrls(FileObject rootEar, FileObject ear, CopyOnWriteArrayList<FileObject> fileObjects, ConcurrentHashMap jarclassListMap, CopyOnWriteArrayList<FileObject> warObjects, StandardFileSystemManager fsManager) throws IOException { FileObject[] childrenJars = ear.getChildren(); for (int childcount = 0; childcount < childrenJars.length; childcount++) { if (childrenJars[childcount].getType() == FileType.FOLDER) { obtainUrls(rootEar, childrenJars[childcount], fileObjects, jarclassListMap, warObjects, fsManager); }/*from www .ja va 2s . c o m*/ // System.out.println(childrenJars[childcount]); // System.out.println(childrenJars[childcount].getName().getBaseName()); // System.out.println(ear.getURL()); if (childrenJars[childcount].getType() == FileType.FILE && (childrenJars[childcount].getName().getBaseName().endsWith(".jar") || childrenJars[childcount].getName().getBaseName().endsWith(".war"))) { // System.out.println(childrenJars[childcount].getURL()); if (childrenJars[childcount].getName().getBaseName().endsWith(".war")) { File file = new File(scanDirectory + "/" + childrenJars[childcount].getName().getBaseName()); if (!file.exists() || (file.exists() && file.lastModified() != childrenJars[childcount] .getContent().getLastModifiedTime())) { InputStream fistr = childrenJars[childcount].getContent().getInputStream(); byte[] filyByt = new byte[4096]; FileOutputStream warFile = new FileOutputStream( scanDirectory + "/" + childrenJars[childcount].getName().getBaseName()); int len = 0; while ((len = fistr.read(filyByt)) != -1) { warFile.write(filyByt, 0, len); } warFile.close(); fistr.close(); warObjects.add(childrenJars[childcount]); } } // System.out.println(childrenJars[childcount].getURL().toString()+" "+rootEar.getURL()); else if (!childrenJars[childcount].getURL().toString().trim() .startsWith(rootEar.getURL().toString() + "lib/")) { CopyOnWriteArrayList<String> classList = new CopyOnWriteArrayList<String>(); getClassList(childrenJars[childcount], classList, fsManager); jarclassListMap.put(childrenJars[childcount], classList); } else { System.out.println("ear libs/" + childrenJars[childcount]); fileObjects.add(childrenJars[childcount]); } } else { childrenJars[childcount].close(); } } }
From source file:com.yenlo.synapse.transport.vfs.VFSTransportListener.java
/** * Search for files that match the given regex pattern and create a list * Then process each of these files and update the status of the scan on * the poll table/* ww w .ja v a 2 s.co m*/ * @param entry the poll table entry for the scan * @param fileURI the file or directory to be scanned */ private void scanFileOrDirectory(final PollTableEntry entry, String fileURI) { FileObject fileObject = null; //TODO : Trying to make the correct URL out of the malformed one. if (fileURI.contains("vfs:")) { fileURI = fileURI.substring(fileURI.indexOf("vfs:") + 4); } if (log.isDebugEnabled()) { log.debug("Scanning directory or file : " + VFSUtils.maskURLPassword(fileURI)); } boolean wasError = true; int retryCount = 0; int maxRetryCount = entry.getMaxRetryCount(); long reconnectionTimeout = entry.getReconnectTimeout(); while (wasError) { try { retryCount++; fileObject = fsManager.resolveFile(fileURI); if (fileObject == null) { log.error("fileObject is null"); throw new FileSystemException("fileObject is null"); } wasError = false; } catch (FileSystemException e) { if (retryCount >= maxRetryCount) { processFailure( "Repeatedly failed to resolve the file URI: " + VFSUtils.maskURLPassword(fileURI), e, entry); return; } else { log.warn("Failed to resolve the file URI: " + VFSUtils.maskURLPassword(fileURI) + ", in attempt " + retryCount + ", " + e.getMessage() + " Retrying in " + reconnectionTimeout + " milliseconds."); } } if (wasError) { try { Thread.sleep(reconnectionTimeout); } catch (InterruptedException e2) { log.error("Thread was interrupted while waiting to reconnect.", e2); } } } try { if (fileObject.exists() && fileObject.isReadable()) { entry.setLastPollState(PollTableEntry.NONE); FileObject[] children = null; try { children = fileObject.getChildren(); } catch (FileNotFolderException ignored) { } catch (FileSystemException ex) { log.error(ex.getMessage(), ex); } // if this is a file that would translate to a single message if (children == null || children.length == 0) { boolean isFailedRecord = false; if (entry.getMoveAfterMoveFailure() != null) { isFailedRecord = isFailedRecord(fileObject, entry); } if (fileObject.getType() == FileType.FILE && !isFailedRecord) { if (!entry.isFileLockingEnabled() || (entry.isFileLockingEnabled() && VFSUtils.acquireLock(fsManager, fileObject))) { try { processFile(entry, fileObject); entry.setLastPollState(PollTableEntry.SUCCSESSFUL); metrics.incrementMessagesReceived(); } catch (AxisFault e) { logException("Error processing File URI : " + fileObject.getName(), e); entry.setLastPollState(PollTableEntry.FAILED); metrics.incrementFaultsReceiving(); } try { moveOrDeleteAfterProcessing(entry, fileObject); } catch (AxisFault axisFault) { logException("File object '" + fileObject.getURL().toString() + "' " + "cloud not be moved", axisFault); entry.setLastPollState(PollTableEntry.FAILED); String timeStamp = VFSUtils.getSystemTime(entry.getFailedRecordTimestampFormat()); addFailedRecord(entry, fileObject, timeStamp); } if (entry.isFileLockingEnabled()) { VFSUtils.releaseLock(fsManager, fileObject); if (log.isDebugEnabled()) { log.debug("Removed the lock file '" + fileObject.toString() + ".lock' of the file '" + fileObject.toString()); } } } else if (log.isDebugEnabled()) { log.debug("Couldn't get the lock for processing the file : " + fileObject.getName()); } else if (isFailedRecord) { if (entry.isFileLockingEnabled()) { VFSUtils.releaseLock(fsManager, fileObject); } // schedule a cleanup task if the file is there if (fsManager.resolveFile(fileObject.getURL().toString()) != null && removeTaskState == STATE_STOPPED && entry.getMoveAfterMoveFailure() != null) { workerPool.execute(new FileRemoveTask(entry, fileObject)); } if (log.isDebugEnabled()) { log.debug("File '" + fileObject.getURL() + "' has been marked as a failed" + " record, it will not process"); } } } } else { int failCount = 0; int successCount = 0; int processCount = 0; Integer iFileProcessingInterval = entry.getFileProcessingInterval(); Integer iFileProcessingCount = entry.getFileProcessingCount(); if (log.isDebugEnabled()) { log.debug("File name pattern : " + entry.getFileNamePattern()); } for (FileObject child : children) { //skipping *.lock file if (child.getName().getBaseName().endsWith(".lock")) { continue; } boolean isFailedRecord = false; if (entry.getMoveAfterMoveFailure() != null) { isFailedRecord = isFailedRecord(child, entry); } if (entry.getFileNamePattern() != null && child.getName().getBaseName().matches(entry.getFileNamePattern())) { //child's file name matches the file name pattern //now we try to get the lock and process if (log.isDebugEnabled()) { log.debug("Matching file : " + child.getName().getBaseName()); } if ((!entry.isFileLockingEnabled() || (entry.isFileLockingEnabled() && VFSUtils.acquireLock(fsManager, child))) && !isFailedRecord) { //process the file try { if (log.isDebugEnabled()) { log.debug("Processing file :" + child); } processCount++; processFile(entry, child); successCount++; // tell moveOrDeleteAfterProcessing() file was success entry.setLastPollState(PollTableEntry.SUCCSESSFUL); metrics.incrementMessagesReceived(); } catch (Exception e) { logException("Error processing File URI : " + child.getName(), e); failCount++; // tell moveOrDeleteAfterProcessing() file failed entry.setLastPollState(PollTableEntry.FAILED); metrics.incrementFaultsReceiving(); } //skipping un-locking file if failed to do delete/move after process boolean skipUnlock = false; try { moveOrDeleteAfterProcessing(entry, child); } catch (AxisFault axisFault) { logException( "File object '" + child.getURL().toString() + "'cloud not be moved, will remain in \"locked\" state", axisFault); skipUnlock = true; failCount++; entry.setLastPollState(PollTableEntry.FAILED); String timeStamp = VFSUtils .getSystemTime(entry.getFailedRecordTimestampFormat()); addFailedRecord(entry, child, timeStamp); } // if there is a failure or not we'll try to release the lock if (entry.isFileLockingEnabled() && !skipUnlock) { VFSUtils.releaseLock(fsManager, child); } } } else if (entry.getFileNamePattern() != null && !child.getName().getBaseName().matches(entry.getFileNamePattern())) { //child's file name does not match the file name pattern if (log.isDebugEnabled()) { log.debug("Non-Matching file : " + child.getName().getBaseName()); } } else if (isFailedRecord) { //it is a failed record if (entry.isFileLockingEnabled()) { VFSUtils.releaseLock(fsManager, child); VFSUtils.releaseLock(fsManager, fileObject); } if (fsManager.resolveFile(child.getURL().toString()) != null && removeTaskState == STATE_STOPPED && entry.getMoveAfterMoveFailure() != null) { workerPool.execute(new FileRemoveTask(entry, child)); } if (log.isDebugEnabled()) { log.debug("File '" + fileObject.getURL() + "' has been marked as a failed record, it will not " + "process"); } } if (iFileProcessingInterval != null && iFileProcessingInterval > 0) { try { if (log.isDebugEnabled()) { log.debug("Put the VFS processor to sleep for : " + iFileProcessingInterval); } Thread.sleep(iFileProcessingInterval); } catch (InterruptedException ie) { log.error("Unable to set the interval between file processors." + ie); } } else if (iFileProcessingCount != null && iFileProcessingCount <= processCount) { break; } } if (failCount == 0 && successCount > 0) { entry.setLastPollState(PollTableEntry.SUCCSESSFUL); } else if (successCount == 0 && failCount > 0) { entry.setLastPollState(PollTableEntry.FAILED); } else { entry.setLastPollState(PollTableEntry.WITH_ERRORS); } } // processing of this poll table entry is complete long now = System.currentTimeMillis(); entry.setLastPollTime(now); entry.setNextPollTime(now + entry.getPollInterval()); } else if (log.isDebugEnabled()) { log.debug("Unable to access or read file or directory : " + VFSUtils.maskURLPassword(fileURI) + "." + " Reason: " + (fileObject.exists() ? (fileObject.isReadable() ? "Unknown reason" : "The file can not be read!") : "The file does not exists!")); } onPollCompletion(entry); } catch (FileSystemException e) { processFailure("Error checking for existence and readability : " + VFSUtils.maskURLPassword(fileURI), e, entry); } }
From source file:org.apache.accumulo.minicluster.impl.MiniAccumuloClusterImpl.java
private String getClasspath() throws IOException { try {/*from w w w. j ava 2s .c o m*/ ArrayList<ClassLoader> classloaders = new ArrayList<ClassLoader>(); ClassLoader cl = this.getClass().getClassLoader(); while (cl != null) { classloaders.add(cl); cl = cl.getParent(); } Collections.reverse(classloaders); StringBuilder classpathBuilder = new StringBuilder(); classpathBuilder.append(config.getConfDir().getAbsolutePath()); if (config.getHadoopConfDir() != null) classpathBuilder.append(File.pathSeparator).append(config.getHadoopConfDir().getAbsolutePath()); if (config.getClasspathItems() == null) { // assume 0 is the system classloader and skip it for (int i = 1; i < classloaders.size(); i++) { ClassLoader classLoader = classloaders.get(i); if (classLoader instanceof URLClassLoader) { for (URL u : ((URLClassLoader) classLoader).getURLs()) { append(classpathBuilder, u); } } else if (classLoader instanceof VFSClassLoader) { VFSClassLoader vcl = (VFSClassLoader) classLoader; for (FileObject f : vcl.getFileObjects()) { append(classpathBuilder, f.getURL()); } } else { throw new IllegalArgumentException( "Unknown classloader type : " + classLoader.getClass().getName()); } } } else { for (String s : config.getClasspathItems()) classpathBuilder.append(File.pathSeparator).append(s); } return classpathBuilder.toString(); } catch (URISyntaxException e) { throw new IOException(e); } }
From source file:org.apache.accumulo.minicluster.MiniAccumuloCluster.java
private String getClasspath() throws IOException { try {//www .j av a 2 s.c o m ArrayList<ClassLoader> classloaders = new ArrayList<ClassLoader>(); ClassLoader cl = this.getClass().getClassLoader(); while (cl != null) { classloaders.add(cl); cl = cl.getParent(); } Collections.reverse(classloaders); StringBuilder classpathBuilder = new StringBuilder(); classpathBuilder.append(config.getConfDir().getAbsolutePath()); if (config.getClasspathItems() == null) { // assume 0 is the system classloader and skip it for (int i = 1; i < classloaders.size(); i++) { ClassLoader classLoader = classloaders.get(i); if (classLoader instanceof URLClassLoader) { URLClassLoader ucl = (URLClassLoader) classLoader; for (URL u : ucl.getURLs()) { append(classpathBuilder, u); } } else if (classLoader instanceof VFSClassLoader) { VFSClassLoader vcl = (VFSClassLoader) classLoader; for (FileObject f : vcl.getFileObjects()) { append(classpathBuilder, f.getURL()); } } else { throw new IllegalArgumentException( "Unknown classloader type : " + classLoader.getClass().getName()); } } } else { for (String s : config.getClasspathItems()) classpathBuilder.append(File.pathSeparator).append(s); } return classpathBuilder.toString(); } catch (URISyntaxException e) { throw new IOException(e); } }
From source file:org.apache.accumulo.start.classloader.vfs.AccumuloReloadingVFSClassLoader.java
@Override public String toString() { StringBuilder buf = new StringBuilder(); for (FileObject f : files) { try {/*from ww w .j a v a 2s. co m*/ buf.append("\t").append(f.getURL().toString()).append("\n"); } catch (FileSystemException e) { log.error("Error getting URL for file", e); } } return buf.toString(); }
From source file:org.apache.accumulo.start.classloader.vfs.AccumuloVFSClassLoader.java
public static void printClassPath(Printer out) { try {/* ww w . j av a2 s. c om*/ ClassLoader cl = getClassLoader(); ArrayList<ClassLoader> classloaders = new ArrayList<ClassLoader>(); while (cl != null) { classloaders.add(cl); cl = cl.getParent(); } Collections.reverse(classloaders); int level = 0; for (ClassLoader classLoader : classloaders) { if (level > 0) out.print(""); level++; String classLoaderDescription; switch (level) { case 1: classLoaderDescription = level + ": Java System Classloader (loads Java system resources)"; break; case 2: classLoaderDescription = level + ": Java Classloader (loads everything defined by java classpath)"; break; case 3: classLoaderDescription = level + ": Accumulo Classloader (loads everything defined by general.classpaths)"; break; case 4: classLoaderDescription = level + ": Accumulo Dynamic Classloader (loads everything defined by general.dynamic.classpaths)"; break; default: classLoaderDescription = level + ": Mystery Classloader (someone probably added a classloader and didn't update the switch statement in " + AccumuloVFSClassLoader.class.getName() + ")"; break; } if (classLoader instanceof URLClassLoader) { // If VFS class loader enabled, but no contexts defined. out.print("Level " + classLoaderDescription + " URL classpath items are:"); for (URL u : ((URLClassLoader) classLoader).getURLs()) { out.print("\t" + u.toExternalForm()); } } else if (classLoader instanceof VFSClassLoader) { out.print("Level " + classLoaderDescription + " VFS classpaths items are:"); VFSClassLoader vcl = (VFSClassLoader) classLoader; for (FileObject f : vcl.getFileObjects()) { out.print("\t" + f.getURL().toExternalForm()); } } else { out.print("Unknown classloader configuration " + classLoader.getClass()); } } } catch (Throwable t) { throw new RuntimeException(t); } }