Java Code Examples for org.apache.commons.vfs2.FileObject#exists()

The following examples show how to use org.apache.commons.vfs2.FileObject#exists() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PluginPropertiesUtil.java    From pentaho-hadoop-shims with Apache License 2.0 6 votes vote down vote up
/**
 * Loads a properties file from the plugin directory for the plugin interface provided
 *
 * @param plugin
 * @return
 * @throws KettleFileException
 * @throws IOException
 */
protected Properties loadProperties( PluginInterface plugin, String relativeName ) throws KettleFileException,
  IOException {
  if ( plugin == null ) {
    throw new NullPointerException();
  }
  FileObject propFile =
    KettleVFS.getFileObject( plugin.getPluginDirectory().getPath() + Const.FILE_SEPARATOR + relativeName );
  if ( !propFile.exists() ) {
    throw new FileNotFoundException( propFile.toString() );
  }
  try {
    return new PropertiesConfigurationProperties( propFile );
  } catch ( ConfigurationException e ) {
    throw new IOException( e );
  }
}
 
Example 2
Source File: ProviderReadTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Tests can perform operations on a folder while reading from a different files.
 */
public void testConcurrentReadFolder() throws Exception {
    final FileObject file = resolveFile1Txt();
    assertTrue(file.exists());
    final FileObject folder = getReadFolderDir1();
    assertTrue(folder.exists());

    // Start reading from the file
    final InputStream instr = file.getContent().getInputStream();
    try {
        // Do some operations
        folder.exists();
        folder.getType();
        folder.getChildren();
    } finally {
        instr.close();
    }
}
 
Example 3
Source File: CubeOutputMeta.java    From hop with Apache License 2.0 6 votes vote down vote up
/**
 * @param variables                   the variable space to use
 * @param definitions
 * @param iResourceNaming
 * @param metadataProvider               the metadataProvider in which non-hop metadata could reside.
 * @return the filename of the exported resource
 */
public String exportResources( IVariables variables, Map<String, ResourceDefinition> definitions,
                               IResourceNaming iResourceNaming, IHopMetadataProvider metadataProvider ) throws HopException {
  try {
    // The object that we're modifying here is a copy of the original!
    // So let's change the filename from relative to absolute by grabbing the file object...
    //
    // From : ${Internal.Pipeline.Filename.Directory}/../foo/bar.data
    // To : /home/matt/test/files/foo/bar.data
    //
    FileObject fileObject = HopVfs.getFileObject( variables.environmentSubstitute( filename ) );

    // If the file doesn't exist, forget about this effort too!
    //
    if ( fileObject.exists() ) {
      // Convert to an absolute path...
      //
      filename = iResourceNaming.nameResource( fileObject, variables, true );

      return filename;
    }
    return null;
  } catch ( Exception e ) {
    throw new HopException( e );
  }
}
 
Example 4
Source File: ResourceAgent.java    From spoofax with Apache License 2.0 6 votes vote down vote up
@Override public String[] readdir(String fn) {
    try {
        final FileObject resource = resourceService.resolve(workingDir, fn);
        if(!resource.exists() || resource.getType() == FileType.FILE) {
            return new String[0];
        }
        final FileName name = resource.getName();
        final FileObject[] children = resource.getChildren();
        final String[] strings = new String[children.length];
        for(int i = 0; i < children.length; ++i) {
            final FileName absName = children[i].getName();
            strings[i] = name.getRelativeName(absName);
        }
        return strings;
    } catch(FileSystemException e) {
        throw new RuntimeException("Could not list contents of directory " + fn, e);
    }
}
 
Example 5
Source File: ResourceAgent.java    From spoofax with Apache License 2.0 6 votes vote down vote up
@Override public int openRandomAccessFile(String fn, String mode) throws IOException {
    boolean appendMode = mode.indexOf('a') >= 0;
    boolean writeMode = appendMode || mode.indexOf('w') >= 0;
    boolean clearFile = false;

    final FileObject resource = resourceService.resolve(workingDir, fn);

    if(writeMode) {
        if(!resource.exists()) {
            resource.createFile();
        } else if(!appendMode) {
            clearFile = true;
        }
    }

    if(clearFile) {
        resource.delete();
        resource.createFile();
    }

    openFiles.put(fileCounter, new ResourceHandle(resource));

    return fileCounter++;
}
 
Example 6
Source File: SQLFileOutputMeta.java    From hop with Apache License 2.0 6 votes vote down vote up
/**
 * Since the exported pipeline that runs this will reside in a ZIP file, we can't reference files relatively. So
 * what this does is turn the name of files into absolute paths OR it simply includes the resource in the ZIP file.
 * For now, we'll simply turn it into an absolute path and pray that the file is on a shared drive or something like
 * that.
 *
 * @param variables                   the variable space to use
 * @param definitions
 * @param iResourceNaming
 * @param metadataProvider               the metadataProvider in which non-hop metadata could reside.
 * @return the filename of the exported resource
 */
public String exportResources( IVariables variables, Map<String, ResourceDefinition> definitions,
                               IResourceNaming iResourceNaming, IHopMetadataProvider metadataProvider ) throws HopException {
  try {
    // The object that we're modifying here is a copy of the original!
    // So let's change the filename from relative to absolute by grabbing the file object...
    //
    // From : ${Internal.Pipeline.Filename.Directory}/../foo/bar.data
    // To : /home/matt/test/files/foo/bar.data
    //
    FileObject fileObject = HopVFS.getFileObject( variables.environmentSubstitute( fileName ), variables );

    // If the file doesn't exist, forget about this effort too!
    //
    if ( fileObject.exists() ) {
      // Convert to an absolute path...
      //
      fileName = iResourceNaming.nameResource( fileObject, variables, true );

      return fileName;
    }
    return null;
  } catch ( Exception e ) {
    throw new HopException( e );
  }
}
 
Example 7
Source File: ParseXmlInZipTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
private DocumentBuilder newDocumentBuilder(final FileObject containerFile, final FileObject sourceFile,
        final String pathToXsdInZip) throws IOException {
    final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    final boolean validate = pathToXsdInZip != null;
    documentBuilderFactory.setValidating(validate);
    documentBuilderFactory.setNamespaceAware(true);
    if (validate) {
        documentBuilderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                "http://www.w3.org/2001/XMLSchema");
        @SuppressWarnings("resource")
        final FileObject schema = containerFile.resolveFile(pathToXsdInZip);
        if (schema.exists()) {
            documentBuilderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
                    schema.getContent().getInputStream());
        } else {
            schema.close();
            throw new FileNotFoundException(schema.toString());
        }
    }
    DocumentBuilder documentBuilder = null;
    try {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
        documentBuilder.setEntityResolver(new TestEntityResolver(containerFile, sourceFile));
    } catch (final ParserConfigurationException e) {
        throw new IOException("Cannot read Java Connector configuration: " + e, e);
    }
    documentBuilder.setErrorHandler(new TestErrorHandler(containerFile + " - " + sourceFile));
    return documentBuilder;
}
 
Example 8
Source File: RestDataspaceImpl.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
public void writeFile(InputStream inputStream, FileObject outputFile, String encoding)
        throws FileSystemException, IOException {
    try {
        if (outputFile.exists()) {
            outputFile.delete(SELECT_SELF);
        }
        if (Strings.isNullOrEmpty(encoding)) {
            outputFile.createFile();
            logger.debug("Writing single file " + outputFile);
            FileSystem.copy(inputStream, outputFile);
        } else if ("gzip".equals(encoding)) {
            logger.debug("Expanding gzip archive into " + outputFile);
            VFSZipper.GZIP.unzip(inputStream, outputFile);
        } else if ("zip".equals(encoding)) {
            logger.debug("Expanding zip archive into " + outputFile);
            VFSZipper.ZIP.unzip(inputStream, outputFile);
        } else {
            logger.debug("Writing single file " + outputFile);
            outputFile.createFile();
            FileSystem.copy(inputStream, outputFile);
        }
    } catch (Throwable error) {
        if (outputFile != null) {
            try {
                if (outputFile.exists()) {
                    outputFile.delete(SELECT_SELF);
                }
            } catch (FileSystemException e1) {
                logger.error("Error occurred while deleting partially created file.", e1);
            }
        }
        Throwables.propagateIfInstanceOf(error, FileSystemException.class);
        Throwables.propagateIfInstanceOf(error, IOException.class);
        Throwables.propagate(error);
    }
}
 
Example 9
Source File: HopVfs.java    From hop with Apache License 2.0 5 votes vote down vote up
public static OutputStream getOutputStream( FileObject fileObject, boolean append ) throws IOException {
  FileObject parent = fileObject.getParent();
  if ( parent != null ) {
    if ( !parent.exists() ) {
      throw new IOException( BaseMessages.getString(
        PKG, "HopVFS.Exception.ParentDirectoryDoesNotExist", getFriendlyURI( parent ) ) );
    }
  }
  try {
    fileObject.createFile();
    FileContent content = fileObject.getContent();
    return content.getOutputStream( append );
  } catch ( FileSystemException e ) {
    // Perhaps if it's a local file, we can retry using the standard
    // File object. This is because on Windows there is a bug in VFS.
    //
    if ( fileObject instanceof LocalFile ) {
      try {
        String filename = getFilename( fileObject );
        return new FileOutputStream( new File( filename ), append );
      } catch ( Exception e2 ) {
        throw e; // throw the original exception: hide the retry.
      }
    } else {
      throw e;
    }
  }
}
 
Example 10
Source File: MustacheWriter.java    From spoofax with Apache License 2.0 5 votes vote down vote up
private void write(Mustache mustache, FileObject dst, boolean overwrite) throws FileSystemException {
    if(dst.exists() && !overwrite) {
        return;
    }
    dst.createFile();
    try(final PrintWriter writer = new PrintWriter(dst.getContent().getOutputStream())) {
        mustache.execute(writer, objects);
    }
    if(access != null) {
        access.write(dst);
    }
}
 
Example 11
Source File: PropertyOutput.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void createParentFolder() throws KettleException {
  if ( meta.isCreateParentFolder() ) {
    FileObject parentfolder = null;
    try {
      // Do we need to create parent folder ?

      // Check for parent folder
      // Get parent folder
      parentfolder = data.file.getParent();
      if ( !parentfolder.exists() ) {
        if ( log.isDetailed() ) {
          logDetailed( BaseMessages.getString( PKG, "PropertyOutput.Log.ParentFolderExists", parentfolder.getName().toString() ) );
        }
        parentfolder.createFolder();
        if ( log.isDetailed() ) {
          logDetailed( BaseMessages.getString( PKG, "PropertyOutput.Log.CanNotCreateParentFolder", parentfolder.getName().toString() ) );
        }
      }
    } catch ( Exception e ) {
      logError( BaseMessages.getString( PKG, "PropertyOutput.Log.CanNotCreateParentFolder", parentfolder.getName().toString() ) );
      throw new KettleException( BaseMessages.getString( PKG, "PropertyOutput.Log.CanNotCreateParentFolder", parentfolder.getName().toString() ) );
    } finally {
      if ( parentfolder != null ) {
        try {
          parentfolder.close();
        } catch ( Exception ex ) { /* Ignore */
        }
      }
    }
  }
}
 
Example 12
Source File: JobEntryZipFile.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private boolean checkContainsFile( String realSourceDirectoryOrFile, FileObject[] filelist, boolean isDirectory ) throws FileSystemException {
  boolean retval = false;
  for ( int i = 0; i < filelist.length; i++ ) {
    FileObject file = filelist[i];
    if ( ( file.exists() && file.getType().equals( FileType.FILE ) ) ) {
      retval = true;
    }
  }
  return retval;
}
 
Example 13
Source File: CubeInputMeta.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * @param space
 *          the variable space to use
 * @param definitions
 * @param resourceNamingInterface
 * @param repository
 *          The repository to optionally load other resources from (to be converted to XML)
 * @param metaStore
 *          the metaStore in which non-kettle metadata could reside.
 *
 * @return the filename of the exported resource
 */
@Override public String exportResources( VariableSpace space, Map<String, ResourceDefinition> definitions,
                                         ResourceNamingInterface resourceNamingInterface, Repository repository,
                                         IMetaStore metaStore ) throws KettleException {
  try {
    // The object that we're modifying here is a copy of the original!
    // So let's change the filename from relative to absolute by grabbing the file object...
    //
    // From : ${Internal.Transformation.Filename.Directory}/../foo/bar.data
    // To : /home/matt/test/files/foo/bar.data
    //
    FileObject fileObject = KettleVFS.getFileObject( space.environmentSubstitute( filename ), space );

    // If the file doesn't exist, forget about this effort too!
    //
    if ( fileObject.exists() ) {
      // Convert to an absolute path...
      //
      filename = resourceNamingInterface.nameResource( fileObject, space, true );

      return filename;
    }
    return null;
  } catch ( Exception e ) {
    throw new KettleException( e );
  }
}
 
Example 14
Source File: OldVFSNotebookRepo.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
protected FileObject getRootDir() throws IOException {
  FileObject rootDir = fsManager.resolveFile(getPath("/"));

  if (!rootDir.exists()) {
    throw new IOException("Root path does not exists");
  }

  if (!isDirectory(rootDir)) {
    throw new IOException("Root path is not a directory");
  }

  return rootDir;
}
 
Example 15
Source File: RestDataspaceImpl.java    From scheduling with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Response retrieve(@HeaderParam("sessionid") String sessionId,
        @HeaderParam("Accept-Encoding") String headerAcceptEncoding, @PathParam("dataspace") String dataspace,
        @PathParam("path-name") String pathname, @QueryParam("comp") String component,
        @QueryParam("includes") List<String> includes, @QueryParam("excludes") List<String> excludes,
        @QueryParam("encoding") String encoding) throws NotConnectedRestException, PermissionRestException {
    if (encoding == null) {
        encoding = headerAcceptEncoding;
    }
    logger.debug(String.format("Retrieving file %s in %s with encoding %s",
                               pathname,
                               dataspace.toUpperCase(),
                               encoding));
    Session session = checkSessionValidity(sessionId);
    try {
        checkPathParams(dataspace, pathname);
        FileObject fo = resolveFile(session, dataspace, pathname);

        if (!fo.exists()) {
            return notFoundRes();
        }
        if (!Strings.isNullOrEmpty(component)) {
            return componentResponse(component, fo, includes, excludes);
        }
        if (fo.getType() == FileType.FILE) {
            if (VFSZipper.isZipFile(fo)) {
                logger.debug(String.format("Retrieving file %s in %s", pathname, dataspace.toUpperCase()));
                return fileComponentResponse(fo);
            } else if (Strings.isNullOrEmpty(encoding) || encoding.contains("*") || encoding.contains("gzip")) {
                logger.debug(String.format("Retrieving file %s as gzip in %s", pathname, dataspace.toUpperCase()));
                return gzipComponentResponse(pathname, fo);
            } else if (encoding.contains("zip")) {
                logger.debug(String.format("Retrieving file %s as zip in %s", pathname, dataspace.toUpperCase()));
                return zipComponentResponse(fo, null, null);
            } else {
                logger.debug(String.format("Retrieving file %s in %s", pathname, dataspace.toUpperCase()));
                return fileComponentResponse(fo);
            }
        } else {
            // folder
            if (Strings.isNullOrEmpty(encoding) || encoding.contains("*") || encoding.contains("zip")) {
                logger.debug(String.format("Retrieving folder %s as zip in %s", pathname, dataspace.toUpperCase()));
                return zipComponentResponse(fo, includes, excludes);
            } else {
                return badRequestRes("Folder retrieval only supported with zip encoding.");
            }
        }
    } catch (Throwable error) {
        logger.error(String.format("Cannot retrieve %s in %s.", pathname, dataspace.toUpperCase()), error);
        throw rethrow(error);
    }
}
 
Example 16
Source File: ExcelOutput.java    From hop with Apache License 2.0 4 votes vote down vote up
private boolean createParentFolder( FileObject file ) {
  boolean retval = true;
  // Check for parent folder
  FileObject parentfolder = null;
  try {
    // Get parent folder
    parentfolder = file.getParent();
    if ( parentfolder.exists() ) {
      if ( isDetailed() ) {
        logDetailed( BaseMessages.getString( PKG, "ExcelOutput.Log.ParentFolderExist", parentfolder
          .getName().toString() ) );
      }
    } else {
      if ( isDetailed() ) {
        logDetailed( BaseMessages.getString( PKG, "ExcelOutput.Log.ParentFolderNotExist", parentfolder
          .getName().toString() ) );
      }
      if ( meta.isCreateParentFolder() ) {
        parentfolder.createFolder();
        if ( isDetailed() ) {
          logDetailed( BaseMessages.getString( PKG, "ExcelOutput.Log.ParentFolderCreated", parentfolder
            .getName().toString() ) );
        }
      } else {
        retval = false;
        logError( BaseMessages.getString( PKG, "ExcelOutput.Error.CanNotFoundParentFolder", parentfolder
          .getName().toString(), file.getName().toString() ) );
      }
    }
  } catch ( Exception e ) {
    retval = false;
    logError( BaseMessages.getString( PKG, "ExcelOutput.Log.CouldNotCreateParentFolder", parentfolder
      .getName().toString() ) );
  } finally {
    if ( parentfolder != null ) {
      try {
        parentfolder.close();
      } catch ( Exception ex ) {
        // Ignore
      }
    }
  }
  return retval;
}
 
Example 17
Source File: SFTPPut.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
protected void finishTheJob( FileObject file, String sourceData, FileObject destinationFolder ) throws KettleException {
  try {
    switch ( meta.getAfterFTPS() ) {
      case JobEntrySFTPPUT.AFTER_FTPSPUT_DELETE:
        // Delete source file
        if ( !file.exists() ) {
          file.delete();
          if ( isDetailed() ) {
            logDetailed( BaseMessages.getString( PKG, "SFTPPut.Log.DeletedFile", sourceData ) );
          }
        }
        break;
      case JobEntrySFTPPUT.AFTER_FTPSPUT_MOVE:
        // Move source file
        FileObject destination = null;
        try {
          destination =
            KettleVFS.getFileObject( destinationFolder.getName().getBaseName()
              + Const.FILE_SEPARATOR + file.getName().getBaseName(), this );
          file.moveTo( destination );
          if ( isDetailed() ) {
            logDetailed( BaseMessages.getString( PKG, "SFTPPut.Log.FileMoved", file, destination ) );
          }
        } finally {
          if ( destination != null ) {
            destination.close();
          }
        }
        break;
      default:
        if ( meta.isAddFilenameResut() ) {
          // Add this to the result file names...
          ResultFile resultFile =
            new ResultFile( ResultFile.FILE_TYPE_GENERAL, file, getTransMeta().getName(), getStepname() );
          resultFile.setComment( BaseMessages.getString( PKG, "SFTPPut.Log.FilenameAddedToResultFilenames" ) );
          addResultFile( resultFile );
          if ( isDetailed() ) {
            logDetailed( BaseMessages.getString( PKG, "SFTPPut.Log.FilenameAddedToResultFilenames", sourceData ) );
          }
        }
        break;
    }
  } catch ( Exception e ) {
    throw new KettleException( e );
  }
}
 
Example 18
Source File: ValueDataUtil.java    From hop with Apache License 2.0 4 votes vote down vote up
private static void throwsErrorOnFileNotFound( FileObject file ) throws HopFileNotFoundException, FileSystemException {
  if ( file == null || !file.exists() ) {
    throw new HopFileNotFoundException( "File not found", file.getName().getPath() );
  }
}
 
Example 19
Source File: ValueDataUtil.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private static void throwsErrorOnFileNotFound( FileObject file ) throws KettleFileNotFoundException, FileSystemException {
  if ( file == null || !file.exists() ) {
    throw new KettleFileNotFoundException( "File not found", file.getName().getPath() );
  }
}
 
Example 20
Source File: JobEntryDeleteResultFilenames.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public Result execute( Result previousResult, int nr ) {
  Result result = previousResult;
  result.setResult( false );

  if ( previousResult != null ) {
    try {
      int size = previousResult.getResultFiles().size();
      if ( log.isBasic() ) {
        logBasic( BaseMessages.getString( PKG, "JobEntryDeleteResultFilenames.log.FilesFound", "" + size ) );
      }
      if ( !specifywildcard ) {
        // Delete all files
        previousResult.getResultFiles().clear();
        if ( log.isDetailed() ) {
          logDetailed( BaseMessages.getString( PKG, "JobEntryDeleteResultFilenames.log.DeletedFiles", "" + size ) );
        }
      } else {

        List<ResultFile> resultFiles = result.getResultFilesList();
        if ( resultFiles != null && resultFiles.size() > 0 ) {
          for ( Iterator<ResultFile> it = resultFiles.iterator(); it.hasNext() && !parentJob.isStopped(); ) {
            ResultFile resultFile = it.next();
            FileObject file = resultFile.getFile();
            if ( file != null && file.exists() ) {
              if ( CheckFileWildcard( file.getName().getBaseName(), environmentSubstitute( wildcard ), true )
                && !CheckFileWildcard(
                  file.getName().getBaseName(), environmentSubstitute( wildcardexclude ), false ) ) {
                // Remove file from result files list
                result.getResultFiles().remove( resultFile.getFile().toString() );

                if ( log.isDetailed() ) {
                  logDetailed( BaseMessages.getString(
                    PKG, "JobEntryDeleteResultFilenames.log.DeletedFile", file.toString() ) );
                }
              }

            }
          }
        }
      }
      result.setResult( true );
    } catch ( Exception e ) {
      logError( BaseMessages.getString( PKG, "JobEntryDeleteResultFilenames.Error", e.toString() ) );
    }
  }
  return result;
}