org.pentaho.di.core.vfs.KettleVFS Java Examples

The following examples show how to use org.pentaho.di.core.vfs.KettleVFS. 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: JobEntryFTPSGetIT.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void downloadFile_WhenDestinationIsSetViaVariable() throws Exception {
  final String myVar = "my-var";
  final String expectedDownloadedFilePath = ramDir + "/" + FtpsServer.SAMPLE_FILE;

  JobEntryFTPSGet job = createCommonJob();
  job.setVariable( myVar, ramDir );
  job.setTargetDirectory( String.format( "${%s}", myVar ) );

  FileObject downloaded = KettleVFS.getFileObject( expectedDownloadedFilePath );
  assertFalse( downloaded.exists() );
  try {
    job.execute( new Result(), 1 );
    downloaded = KettleVFS.getFileObject( expectedDownloadedFilePath );
    assertTrue( downloaded.exists() );
  } finally {
    downloaded.delete();
  }
}
 
Example #2
Source File: VFSFileProvider.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * @param inputStream
 * @param destDir
 * @param path
 * @param overwrite
 * @return
 * @throws FileException
 */
@Override public VFSFile writeFile( InputStream inputStream, VFSFile destDir,
                                    String path, boolean overwrite )
  throws FileException {
  FileObject fileObject = null;
  try {
    fileObject = KettleVFS
      .getFileObject( path, new Variables(), VFSHelper.getOpts( destDir.getPath(), destDir.getConnection() ) );
  } catch ( KettleException ke ) {
    throw new FileException();
  }
  if ( fileObject != null ) {
    try ( OutputStream outputStream = fileObject.getContent().getOutputStream(); ) {
      IOUtils.copy( inputStream, outputStream );
      outputStream.flush();
      return VFSFile.create( destDir.getPath(), fileObject, destDir.getConnection(), destDir.getDomain() );
    } catch ( IOException e ) {
      return null;
    }
  }
  return null;
}
 
Example #3
Source File: ValueMetaPluginType.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Override
protected void registerXmlPlugins() throws KettlePluginException {
  for ( PluginFolderInterface folder : pluginFolders ) {

    if ( folder.isPluginXmlFolder() ) {
      List<FileObject> pluginXmlFiles = findPluginXmlFiles( folder.getFolder() );
      for ( FileObject file : pluginXmlFiles ) {

        try {
          Document document = XMLHandler.loadXMLFile( file );
          Node pluginNode = XMLHandler.getSubNode( document, "plugin" );
          if ( pluginNode != null ) {
            registerPluginFromXmlResource( pluginNode, KettleVFS.getFilename( file.getParent() ), this
              .getClass(), false, file.getParent().getURL() );
          }
        } catch ( Exception e ) {
          // We want to report this plugin.xml error, perhaps an XML typo or something like that...
          //
          log.logError( "Error found while reading step plugin.xml file: " + file.getName().toString(), e );
        }
      }
    }
  }
}
 
Example #4
Source File: VFSFileProvider.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * @param files
 * @return
 */
public List<VFSFile> delete( List<VFSFile> files ) {
  List<VFSFile> deletedFiles = new ArrayList<>();
  for ( VFSFile file : files ) {
    try {
      FileObject fileObject = KettleVFS
        .getFileObject( file.getPath(), new Variables(), VFSHelper.getOpts( file.getPath(), file.getConnection() ) );
      if ( fileObject.delete() ) {
        deletedFiles.add( file );
      }
    } catch ( KettleFileException | FileSystemException kfe ) {
      // Ignore don't add
    }
  }
  return deletedFiles;
}
 
Example #5
Source File: VFSFileProvider.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * @param file
 * @param toPath
 * @param overwrite
 * @return
 * @throws FileException
 */
@Override
public VFSFile copy( VFSFile file, String toPath, boolean overwrite ) throws FileException {
  try {
    FileObject fileObject = KettleVFS
      .getFileObject( file.getPath(), new Variables(), VFSHelper.getOpts( file.getPath(), file.getConnection() ) );
    FileObject copyObject =
      KettleVFS.getFileObject( toPath, new Variables(), VFSHelper.getOpts( file.getPath(), file.getConnection() ) );
    copyObject.copyFrom( fileObject, Selectors.SELECT_SELF );
    if ( file instanceof VFSDirectory ) {
      return VFSDirectory
        .create( copyObject.getParent().getPublicURIString(), fileObject, file.getConnection(), file.getDomain() );
    } else {
      return VFSFile
        .create( copyObject.getParent().getPublicURIString(), fileObject, file.getConnection(), file.getDomain() );
    }
  } catch ( KettleFileException | FileSystemException e ) {
    throw new FileException();
  }
}
 
Example #6
Source File: JobEntryPGPEncryptFiles.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private void addFileToResultFilenames( String fileaddentry, Result result, Job parentJob ) {
  try {
    ResultFile resultFile =
      new ResultFile( ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject( fileaddentry ), parentJob
        .getJobname(), toString() );
    result.getResultFiles().put( resultFile.getFile().toString(), resultFile );

    if ( isDebug() ) {
      logDebug( " ------ " );
      logDebug( BaseMessages.getString( PKG, "JobPGPEncryptFiles.Log.FileAddedToResultFilesName", fileaddentry ) );
    }

  } catch ( Exception e ) {
    logError( BaseMessages.getString( PKG, "JobPGPEncryptFiles.Error.AddingToFilenameResult" ), fileaddentry
      + "" + e.getMessage() );
  }
}
 
Example #7
Source File: TextFileInputMeta.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Since the exported transformation 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 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...
    // In case the name of the file comes from previous steps, forget about this!
    //
    if ( !acceptingFilenames ) {

      // Replace the filename ONLY (folder or filename)
      //
      for ( int i = 0; i < fileName.length; i++ ) {
        FileObject fileObject = KettleVFS.getFileObject( space.environmentSubstitute( fileName[i] ), space );
        fileName[i] = resourceNamingInterface.nameResource( fileObject, space, Utils.isEmpty( fileMask[i] ) );
      }
    }
    return null;
  } catch ( Exception e ) {
    throw new KettleException( e );
  }
}
 
Example #8
Source File: PartitionerPluginType.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
protected void registerXmlPlugins() throws KettlePluginException {
  for ( PluginFolderInterface folder : pluginFolders ) {

    if ( folder.isPluginXmlFolder() ) {
      List<FileObject> pluginXmlFiles = findPluginXmlFiles( folder.getFolder() );
      for ( FileObject file : pluginXmlFiles ) {

        try {
          Document document = XMLHandler.loadXMLFile( file );
          Node pluginNode = XMLHandler.getSubNode( document, "partitioner-plugin" );
          if ( pluginNode != null ) {
            registerPluginFromXmlResource( pluginNode, KettleVFS.getFilename( file.getParent() ), this
              .getClass(), false, file.getParent().getURL() );
          }
        } catch ( Exception e ) {
          // We want to report this plugin.xml error, perhaps an XML typo or something like that...
          //
          log.logError(
            "Error found while reading partitioning plugin.xml file: " + file.getName().toString(), e );
        }
      }
    }
  }
}
 
Example #9
Source File: XMLOutputMeta.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Since the exported transformation that runs this will reside in a ZIP file, we can't reference files relatively. So
 * what this does is turn the name of the base path into an absolute path.
 * 
 * @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
 */
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...
    //
    if ( !Utils.isEmpty( fileName ) ) {
      FileObject fileObject = KettleVFS.getFileObject( space.environmentSubstitute( fileName ), space );
      fileName = resourceNamingInterface.nameResource( fileObject, space, true );
    }

    return null;
  } catch ( Exception e ) {
    throw new KettleException( e );
  }
}
 
Example #10
Source File: JobMeta.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Load the job from the XML file specified.
 *
 * @param fname The filename to load as a job
 * @param rep   The repository to bind againt, null if there is no repository available.
 * @throws KettleXMLException
 */
public JobMeta( VariableSpace parentSpace, String fname, Repository rep, IMetaStore metaStore,
    OverwritePrompter prompter ) throws KettleXMLException {
  this.initializeVariablesFrom( parentSpace );
  this.metaStore = metaStore;
  try {
    // OK, try to load using the VFS stuff...
    Document doc = XMLHandler.loadXMLFile( KettleVFS.getFileObject( fname, this ) );
    if ( doc != null ) {
      // The jobnode
      Node jobnode = XMLHandler.getSubNode( doc, XML_TAG );

      loadXML( jobnode, fname, rep, metaStore, false, prompter );
    } else {
      throw new KettleXMLException(
          BaseMessages.getString( PKG, "JobMeta.Exception.ErrorReadingFromXMLFile" ) + fname );
    }
  } catch ( Exception e ) {
    throw new KettleXMLException(
        BaseMessages.getString( PKG, "JobMeta.Exception.UnableToLoadJobFromXMLFile" ) + fname + "]", e );
  }
}
 
Example #11
Source File: FileLoggingEventListener.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Log only lines belonging to the specified log channel ID or one of it's children (grandchildren) to the specified
 * file.
 *
 * @param logChannelId
 * @param filename
 * @param append
 * @throws KettleException
 */
public FileLoggingEventListener( String logChannelId, String filename, boolean append ) throws KettleException {
  this.logChannelId = logChannelId;
  this.filename = filename;
  this.layout = new KettleLogLayout( true );
  this.exception = null;

  file = KettleVFS.getFileObject( filename );
  outputStream = null;
  try {
    outputStream = KettleVFS.getOutputStream( file, append );
  } catch ( Exception e ) {
    throw new KettleException(
      "Unable to create a logging event listener to write to file '" + filename + "'", e );
  }
}
 
Example #12
Source File: LoadFileInputMeta.java    From pentaho-kettle with Apache License 2.0 6 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
 */
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...
    //
    if ( !fileinfield ) {
      for ( int i = 0; i < fileName.length; i++ ) {
        FileObject fileObject = KettleVFS.getFileObject( space.environmentSubstitute( fileName[i] ), space );
        fileName[i] = resourceNamingInterface.nameResource( fileObject, space, Utils.isEmpty( fileMask[i] ) );
      }
    }
    return null;
  } catch ( Exception e ) {
    throw new KettleException( e );
  }
}
 
Example #13
Source File: FixedInputMeta.java    From pentaho-kettle with Apache License 2.0 6 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
 */
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.txt
    // To : /home/matt/test/files/foo/bar.txt
    //
    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: SwtSvgImageUtil.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Internal image loading from Kettle's VFS.
 */
private static SwtUniversalImage loadFromSimpleVFS( Display display, String location ) {
  try {
    InputStream s = KettleVFS.getInputStream( location );
    if ( s == null ) {
      return null;
    }
    try {
      return loadImage( display, s, location );
    } finally {
      IOUtils.closeQuietly( s );
    }
  } catch ( KettleFileException e ) {
    // do nothing. try to load next
  }
  return null;
}
 
Example #15
Source File: JobEntrySSH2PUT.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private List<FileObject> getFiles( String localfolder ) throws KettleFileException {
  try {
    List<FileObject> myFileList = new ArrayList<FileObject>();

    // Get all the files in the local directory...

    FileObject localFiles = KettleVFS.getFileObject( localfolder, this );
    FileObject[] children = localFiles.getChildren();
    if ( children != null ) {
      for ( int i = 0; i < children.length; i++ ) {
        // Get filename of file or directory
        if ( children[i].getType().equals( FileType.FILE ) ) {
          myFileList.add( children[i] );

        }
      } // end for
    }

    return myFileList;
  } catch ( IOException e ) {
    throw new KettleFileException( e );
  }

}
 
Example #16
Source File: JobEntryTalendJobExec.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private URL[] prepareJarFiles( FileObject zipFile ) throws Exception {

    // zip:file:///tmp/foo.zip
    FileInputList fileList = FileInputList.createFileList( this, new String[] { "zip:" + zipFile.toString(), },
      new String[] { ".*\\.jar$", }, // Include mask: only jar files
      new String[] { ".*classpath\\.jar$", }, // Exclude mask: only jar files
      new String[] { "Y", }, // File required
      new boolean[] { true, } ); // Search sub-directories

    List<URL> files = new ArrayList<URL>();

    // Copy the jar files in the temp folder...
    //
    for ( FileObject file : fileList.getFiles() ) {
      FileObject jarfilecopy =
        KettleVFS.createTempFile(
          file.getName().getBaseName(), ".jar", environmentSubstitute( "${java.io.tmpdir}" ) );
      jarfilecopy.copyFrom( file, new AllFileSelector() );
      files.add( jarfilecopy.getURL() );
    }

    return files.toArray( new URL[files.size()] );
  }
 
Example #17
Source File: TextFileInputTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private static String createVirtualFile( String filename, String... rows ) throws Exception {
  String virtualFile = TestUtils.createRamFile( filename );

  StringBuilder content = new StringBuilder();
  if ( rows != null ) {
    for ( String row : rows ) {
      content.append( row );
    }
  }
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  bos.write( content.toString().getBytes() );

  try ( OutputStream os = KettleVFS.getFileObject( virtualFile ).getContent().getOutputStream() ) {
    IOUtils.copy( new ByteArrayInputStream( bos.toByteArray() ), os );
  }

  return virtualFile;
}
 
Example #18
Source File: SQLFileOutputMeta.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Since the exported transformation 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 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
 */
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 #19
Source File: JobEntryMSAccessBulkLoad.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private void addFileToResultFilenames( String fileaddentry, Result result, Job parentJob ) {
  try {
    ResultFile resultFile =
      new ResultFile( ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject( fileaddentry, this ), parentJob
        .getJobname(), toString() );
    result.getResultFiles().put( resultFile.getFile().toString(), resultFile );

    if ( log.isDebug() ) {
      logDebug( " ------ " );
      logDebug( BaseMessages.getString(
        PKG, "JobEntryMSAccessBulkLoad.Log.FileAddedToResultFilesName", fileaddentry ) );
    }
  } catch ( Exception e ) {
    log.logError(
      BaseMessages.getString( PKG, "JobEntryMSAccessBulkLoad.Error.AddingToFilenameResult" ), fileaddentry
        + "" + e.getMessage() );
  }
}
 
Example #20
Source File: LegacyShimLocator.java    From pentaho-hadoop-shims with Apache License 2.0 6 votes vote down vote up
public static String getLegacyDefaultShimDir( String shimFolder ) throws IOException {
  PluginInterface pluginInterface =
    PluginRegistry.getInstance().findPluginWithId( LifecyclePluginType.class, HADOOP_SPOON_PLUGIN );
  Properties legacyProperties;

  try {
    legacyProperties = loadProperties( pluginInterface, BIG_DATA_PLUGIN_PROPERTIES );
    String legacyShimsFolder = legacyProperties.getProperty( HADOOP_CONFIGURATIONS_PATH );
    FileObject shimDirectoryObject =
      KettleVFS.getFileObject( pluginInterface.getPluginDirectory().getPath() + Const.FILE_SEPARATOR
        + legacyShimsFolder + Const.FILE_SEPARATOR + shimFolder );
    return shimDirectoryObject.getURL().getPath();
  } catch ( KettleFileException | NullPointerException e ) {
    throw new IOException( e );
  }
}
 
Example #21
Source File: RegisterPackageServlet.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Copy contents of <code>inputStream</code> to <code>directory</code>. Expecting zip file.
 * @param inputStream zip file input stream.
 * @param directory local destination directory.
 * @return copied file path.
 * @throws KettleException
 */
protected String copyRequestToDirectory( InputStream inputStream, String directory ) throws KettleException {
  String copiedFilePath;
  try {
    FileObject foDirectory = KettleVFS.getFileObject( directory );
    if ( !foDirectory.exists() ) {
      foDirectory.createFolder();
    }
    FileObject tempZipFile = KettleVFS.createTempFile( "export", ".zip", directory );
    OutputStream outputStream = KettleVFS.getOutputStream( tempZipFile, false );
    copyAndClose( inputStream, outputStream );
    copiedFilePath = tempZipFile.getName().getPath();
  } catch ( IOException ioe ) {
    throw new KettleException( BaseMessages.getString( PKG, "RegisterPackageServlet.Exception.CopyRequest",  directory ), ioe );
  }

  return copiedFilePath;
}
 
Example #22
Source File: DistributedCacheUtilImplTest.java    From pentaho-hadoop-shims with Apache License 2.0 6 votes vote down vote up
@Test
public void deleteDirectory() throws Exception {
  FileObject test = KettleVFS.getFileObject( "bin/test/deleteDirectoryTest" );
  test.createFolder();

  DistributedCacheUtilImpl ch = new DistributedCacheUtilImpl();
  ch.deleteDirectory( test );
  try {
    assertFalse( test.exists() );
  } finally {
    // Delete the directory with java.io.File if it wasn't removed
    File f = new File( "bin/test/deleteDirectoryTest" );
    if ( f.exists() && !f.delete() ) {
      throw new IOException( "unable to delete test directory: " + f.getAbsolutePath() );
    }
  }
}
 
Example #23
Source File: CsvInputDialog.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**'
 * Returns the {@link InputStream} corresponding to the csv file, or null if the file cannot be read.
 * @return the {@link InputStream} corresponding to the csv file, or null if the file cannot be read
 */
private InputStream getInputStream( final CsvInputMeta meta ) {
  InputStream inputStream = null;
  try {
    final String filename = transMeta.environmentSubstitute( meta.getFilename() );

    final FileObject fileObject = KettleVFS.getFileObject( filename );
    if ( !( fileObject instanceof LocalFile ) ) {
      // We can only use NIO on local files at the moment, so that's what we
      // limit ourselves to.
      //
      throw new KettleException( BaseMessages.getString( PKG, "CsvInput.Log.OnlyLocalFilesAreSupported" ) );
    }

    inputStream = KettleVFS.getInputStream( fileObject );
  } catch ( final Exception e ) {
    logError( BaseMessages.getString( PKG, "CsvInputDialog.ErrorGettingFileDesc.DialogMessage" ), e );
  }
  return inputStream;
}
 
Example #24
Source File: MailConnection.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Export message content to a filename.
 *
 * @param filename
 *          the target filename
 * @param foldername
 *          the parent folder of filename
 * @throws KettleException
 */

public void saveMessageContentToFile( String filename, String foldername ) throws KettleException {
  OutputStream os = null;
  try {
    os = KettleVFS.getOutputStream( foldername + ( foldername.endsWith( "/" ) ? "" : "/" ) + filename, false );
    getMessage().writeTo( os );
    updateSavedMessagesCounter();
  } catch ( Exception e ) {
    throw new KettleException( BaseMessages.getString( PKG, "MailConnection.Error.SavingMessageContent", ""
      + this.message.getMessageNumber(), filename, foldername ), e );
  } finally {
    if ( os != null ) {
      IOUtils.closeQuietly( os );
    }
  }
}
 
Example #25
Source File: MailConnection.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static String findValidTarget( String folderName, final String fileName ) throws KettleException {
  if ( fileName == null || folderName == null ) {
    throw new IllegalArgumentException( "Cannot have null arguments to findValidTarget" );
  }
  String fileNameRoot = FilenameUtils.getBaseName( fileName ), ext = "." + FilenameUtils.getExtension( fileName );
  if ( ( ext.length() == 1 ) ) { // only a "."
    ext = "";
  }
  String rtn = "", base = FilenameUtils.concat( folderName, fileNameRoot );
  int baseSz = base.length();
  StringBuilder build = new StringBuilder( baseSz ).append( base );
  int i = -1;
  do {
    i++;
    build.setLength( baseSz ); // bring string back to size
    build.append( i > 0 ? Integer.toString( i ) : "" ).append( ext );
    rtn = build.toString();
  } while ( KettleVFS.fileExists( rtn ) );

  return rtn;
}
 
Example #26
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 #27
Source File: XLSWorkbook.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public XLSWorkbook( String filename, String encoding ) throws KettleException {
  this.filename = filename;
  this.encoding = encoding;

  WorkbookSettings ws = new WorkbookSettings();
  if ( !Utils.isEmpty( encoding ) ) {
    ws.setEncoding( encoding );
  }
  try {
    workbook = Workbook.getWorkbook( KettleVFS.getInputStream( filename ), ws );
  } catch ( Exception e ) {
    throw new KettleException( e );
  }
}
 
Example #28
Source File: SharedObjectsTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyBackupVfs() throws Exception {
  final String dirName = "ram:/SharedObjectsTest";

  FileObject baseDir = KettleVFS.getFileObject( dirName );
  try {
    baseDir.createFolder();
    final String fileName = dirName + "/shared.xml";
    SharedObjects sharedObjects = new SharedObjects( fileName );

    SharedObjectInterface shared1 = new TestSharedObject( "shared1", "<shared1>shared1</shared1>" );
    sharedObjects.storeObject( shared1 );
    sharedObjects.saveToFile();
    final String backupFileName = fileName + ".backup";
    FileObject backup = KettleVFS.getFileObject( backupFileName );
    Assert.assertFalse( backup.exists() );

    String contents = KettleVFS.getTextFileContent( fileName, "utf8" );
    Assert.assertTrue( contents.contains( shared1.getXML() ) );

    SharedObjectInterface shared2 = new TestSharedObject( "shared2", "<shared2>shared2</shared2>" );
    sharedObjects.storeObject( shared2 );
    sharedObjects.saveToFile();
    Assert.assertTrue( backup.exists() );
    String contentsBackup = KettleVFS.getTextFileContent( backupFileName, "utf8" );
    Assert.assertEquals( contents, contentsBackup );

  } finally {
    if ( baseDir.exists() ) {
      baseDir.deleteAll();
    }
  }

}
 
Example #29
Source File: PropertyOutputMeta.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Since the exported transformation 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 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
    //
    // In case the name of the file comes from previous steps, forget about this!
    if ( !fileNameInField ) {
      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 #30
Source File: JobEntryZipFile.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Get the requested part of the filename
 *
 * @param filename
 *          the filename (full) (/path/to/a/file.txt)
 * @param depth
 *          the depth to get. 0 means: the complete filename, 1: the name only (file.txt), 2: one folder (a/file.txt)
 *          3: two folders (to/a/file.txt) and so on.
 * @return the requested part of the file name up to a certain depth
 * @throws KettleFileException
 */
private String determineZipfilenameForDepth( String filename, int depth ) throws KettleException {
  try {
    if ( Utils.isEmpty( filename ) ) {
      return null;
    }
    if ( depth == 0 ) {
      return filename;
    }
    FileObject fileObject = KettleVFS.getFileObject( filename, this );
    FileObject folder = fileObject.getParent();
    String baseName = fileObject.getName().getBaseName();
    if ( depth == 1 ) {
      return baseName;
    }
    StringBuilder path = new StringBuilder( baseName );
    int d = 1;
    while ( d < depth && folder != null ) {
      path.insert( 0, '/' );
      path.insert( 0, folder.getName().getBaseName() );
      folder = folder.getParent();
      d++;
    }
    return path.toString();
  } catch ( Exception e ) {
    throw new KettleException( "Unable to get zip filename '" + filename + "' to depth " + depth, e );
  }
}