Java Code Examples for org.pentaho.di.core.vfs.KettleVFS#getOutputStream()

The following examples show how to use org.pentaho.di.core.vfs.KettleVFS#getOutputStream() . 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: 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 2
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 3
Source File: CubeOutput.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private void prepareFile() throws KettleFileException {
  try {
    String filename = environmentSubstitute( meta.getFilename() );
    if ( meta.isAddToResultFiles() ) {
      // Add this to the result file names...
      ResultFile resultFile =
        new ResultFile(
          ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject( filename, getTransMeta() ), getTransMeta()
            .getName(), getStepname() );
      resultFile.setComment( "This file was created with a cube file output step" );
      addResultFile( resultFile );
    }

    data.fos = KettleVFS.getOutputStream( filename, getTransMeta(), false );
    data.zip = new GZIPOutputStream( data.fos );
    data.dos = new DataOutputStream( data.zip );
  } catch ( Exception e ) {
    throw new KettleFileException( e );
  }
}
 
Example 4
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 5
Source File: LogChannelFileWriter.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new log channel file writer
 *
 * @param logChannelId
 *          The log channel (+children) to write to the log file
 * @param logFile
 *          The logging file to write to
 * @param appending
 *          set to true if you want to append to an existing file
 * @param pollingInterval
 *          The polling interval in milliseconds.
 *
 * @throws KettleException
 *           in case the specified log file can't be created.
 */
public LogChannelFileWriter( String logChannelId, FileObject logFile, boolean appending, int pollingInterval ) throws KettleException {
  this.logChannelId = logChannelId;
  this.logFile = logFile;
  this.appending = appending;
  this.pollingInterval = pollingInterval;

  active = new AtomicBoolean( false );
  finished = new AtomicBoolean( false );

  try {
    logFileOutputStream = KettleVFS.getOutputStream( logFile, appending );
  } catch ( IOException e ) {
    throw new KettleException( "There was an error while trying to open file '" + logFile + "' for writing", e );
  }

  this.buffer = new LogChannelFileWriterBuffer( this.logChannelId );
  LoggingRegistry.getInstance().registerLogChannelFileWriterBuffer( this.buffer );
}
 
Example 6
Source File: StarModelerPerspective.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public boolean exportFile(EngineMetaInterface meta, String filename) {

    try {
      String xml = meta.getXML();
      OutputStream outputStream = KettleVFS.getOutputStream(filename, false);
      outputStream.write(xml.getBytes(Const.XML_ENCODING));
      outputStream.close();

      meta.setFilename(filename);
      return true;
    } catch(Exception e) {
      new ErrorDialog(Spoon.getInstance().getShell(), "Error", "Error export star domain to XML", e);
      return false;
    }
  }
 
Example 7
Source File: ExcelWriterStep.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Copies a VFS File
 *
 * @param in  the source file object
 * @param out the destination file object
 * @throws KettleException
 */
public static void copyFile( FileObject in, FileObject out ) throws KettleException {
  try ( BufferedInputStream fis = new BufferedInputStream( KettleVFS.getInputStream( in ) );
        BufferedOutputStream fos = new BufferedOutputStream( KettleVFS.getOutputStream( out, false ) ) ) {
    byte[] buf = new byte[ 1024 * 1024 ]; // copy in chunks of 1 MB
    int i = 0;
    while ( ( i = fis.read( buf ) ) != -1 ) {
      fos.write( buf, 0, i );
    }
  } catch ( Exception e ) {
    throw new KettleException( e );
  }
}
 
Example 8
Source File: ExcelWriterStep.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void closeOutputFile() throws KettleException {
  try ( BufferedOutputStreamWithCloseDetection out =  new BufferedOutputStreamWithCloseDetection( KettleVFS.getOutputStream( data.file, false ) ) ) {
    // may have to write a footer here
    if ( meta.isFooterEnabled() ) {
      writeHeader();
    }
    // handle auto size for columns
    if ( meta.isAutoSizeColums() ) {

      // track all columns for autosizing if using streaming worksheet
      if (  data.sheet instanceof SXSSFSheet ) {
        ( (SXSSFSheet) data.sheet ).trackAllColumnsForAutoSizing();
      }

      if ( meta.getOutputFields() == null || meta.getOutputFields().length == 0 ) {
        for ( int i = 0; i < data.inputRowMeta.size(); i++ ) {
          data.sheet.autoSizeColumn( i + data.startingCol );
        }
      } else {
        for ( int i = 0; i < meta.getOutputFields().length; i++ ) {
          data.sheet.autoSizeColumn( i + data.startingCol );
        }
      }
    }
    // force recalculation of formulas if requested
    if ( meta.isForceFormulaRecalculation() ) {
      recalculateAllWorkbookFormulas();
    }

    data.wb.write( out );
  } catch ( IOException e ) {
    throw new KettleException( e );
  }
}
 
Example 9
Source File: KettleFileRepository.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectId insertLogEntry( String description ) throws KettleException {
  String logfile = calcDirectoryName( null ) + LOG_FILE;
  try {
    OutputStream outputStream = KettleVFS.getOutputStream( logfile, true );
    outputStream.write( description.getBytes() );
    outputStream.write( Const.CR.getBytes() );
    outputStream.close();

    return new StringObjectId( logfile );
  } catch ( IOException e ) {
    throw new KettleException( "Unable to write log entry to file [" + logfile + "]" );
  }
}
 
Example 10
Source File: SharedObjects.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private boolean copyFile( String src, String dest ) throws KettleFileException, IOException {
  FileObject srcFile = getFileObjectFromKettleVFS( src );
  FileObject destFile = getFileObjectFromKettleVFS( dest );
  try ( InputStream in = KettleVFS.getInputStream( srcFile );
      OutputStream out = KettleVFS.getOutputStream( destFile, false ) ) {
    IOUtils.copy( in, out );
  }
  return true;
}
 
Example 11
Source File: TextFileOutput.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
protected OutputStream getOutputStream( String vfsFilename, VariableSpace space, boolean append ) throws KettleFileException {
  return KettleVFS.getOutputStream( vfsFilename, space, append );
}
 
Example 12
Source File: SQLFileOutput.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public boolean openNewFile() {
  boolean retval = false;
  data.writer = null;

  try {

    String filename = buildFilename();
    if ( meta.AddToResult() ) {
      // Add this to the result file names...
      ResultFile resultFile =
        new ResultFile(
          ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject( filename, getTransMeta() ), getTransMeta()
            .getName(), getStepname() );
      resultFile.setComment( "This file was created with a text file output step" );
      addResultFile( resultFile );
    }
    OutputStream outputStream;

    if ( log.isDetailed() ) {
      logDetailed( "Opening output stream in nocompress mode" );
    }
    OutputStream fos = KettleVFS.getOutputStream( filename, getTransMeta(), meta.isFileAppended() );
    outputStream = fos;

    if ( log.isDetailed() ) {
      logDetailed( "Opening output stream in default encoding" );
    }
    data.writer = new OutputStreamWriter( new BufferedOutputStream( outputStream, 5000 ) );

    if ( !Utils.isEmpty( meta.getEncoding() ) ) {
      if ( log.isBasic() ) {
        logDetailed( "Opening output stream in encoding: " + meta.getEncoding() );
      }
      data.writer =
        new OutputStreamWriter( new BufferedOutputStream( outputStream, 5000 ), environmentSubstitute( meta
          .getEncoding() ) );
    } else {
      if ( log.isBasic() ) {
        logDetailed( "Opening output stream in default encoding" );
      }
      data.writer = new OutputStreamWriter( new BufferedOutputStream( outputStream, 5000 ) );
    }

    if ( log.isDetailed() ) {
      logDetailed( "Opened new file with name [" + filename + "]" );
    }

    data.splitnr++;

    retval = true;

  } catch ( Exception e ) {
    logError( "Error opening new file : " + e.toString() );
  }

  return retval;
}
 
Example 13
Source File: KettleFileRepository.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public void save( RepositoryElementInterface repositoryElement, String versionComment,
  ProgressMonitorListener monitor, ObjectId parentId, boolean used ) throws KettleException {
  try {
    if ( !( repositoryElement instanceof XMLInterface )
      && !( repositoryElement instanceof SharedObjectInterface ) ) {
      throw new KettleException( "Class ["
        + repositoryElement.getClass().getName()
        + "] needs to implement the XML Interface in order to save it to disk" );
    }

    if ( !Utils.isEmpty( versionComment ) ) {
      insertLogEntry( "Save repository element : " + repositoryElement.toString() + " : " + versionComment );
    }

    ObjectId objectId = new StringObjectId( calcObjectId( repositoryElement ) );

    FileObject fileObject = getFileObject( repositoryElement );

    String xml = ( (XMLInterface) repositoryElement ).getXML();

    OutputStream os = KettleVFS.getOutputStream( fileObject, false );
    os.write( xml.getBytes( Const.XML_ENCODING ) );
    os.close();

    if ( repositoryElement instanceof ChangedFlagInterface ) {
      ( (ChangedFlagInterface) repositoryElement ).clearChanged();
    }

    // See if the element was already saved in the repository.
    // If the object ID is different, then we created an extra copy.
    // If so, we need to now remove the old file to prevent us from having multiple copies.
    //
    if ( repositoryElement.getObjectId() != null && !repositoryElement.getObjectId().equals( objectId ) ) {
      delObject( repositoryElement.getObjectId() );
    }

    repositoryElement.setObjectId( objectId );

    // Finally, see if there are external objects that need to be stored or updated in the MetaStore
    //
    if ( repositoryElement instanceof TransMeta ) {
      ( (TransMeta) repositoryElement ).saveMetaStoreObjects( this, metaStore );
    }
    if ( repositoryElement instanceof JobMeta ) {
      ( (JobMeta) repositoryElement ).saveMetaStoreObjects( this, metaStore );
    }

  } catch ( Exception e ) {
    throw new KettleException( "Unable to save repository element ["
      + repositoryElement + "] to XML file : " + calcFilename( repositoryElement ), e );
  }
}
 
Example 14
Source File: JsonOutput.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public boolean openNewFile() {
  if ( data.writer != null ) {
    return true;
  }
  boolean retval = false;

  try {

    if ( meta.isServletOutput() ) {
      data.writer = getTrans().getServletPrintWriter();
    } else {
      String filename = buildFilename();
      createParentFolder( filename );
      if ( meta.AddToResult() ) {
        // Add this to the result file names...
        ResultFile resultFile =
          new ResultFile(
            ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject( filename, getTransMeta() ),
            getTransMeta().getName(), getStepname() );
        resultFile.setComment( BaseMessages.getString( PKG, "JsonOutput.ResultFilenames.Comment" ) );
        addResultFile( resultFile );
      }

      OutputStream outputStream;
      OutputStream fos = KettleVFS.getOutputStream( filename, getTransMeta(), meta.isFileAppended() );
      outputStream = fos;

      if ( !Utils.isEmpty( meta.getEncoding() ) ) {
        data.writer =
          new OutputStreamWriter( new BufferedOutputStream( outputStream, 5000 ), environmentSubstitute( meta
            .getEncoding() ) );
      } else {
        data.writer = new OutputStreamWriter( new BufferedOutputStream( outputStream, 5000 ) );
      }

      if ( log.isDetailed() ) {
        logDetailed( BaseMessages.getString( PKG, "JsonOutput.FileOpened", filename ) );
      }

      data.splitnr++;
    }

    retval = true;

  } catch ( Exception e ) {
    logError( BaseMessages.getString( PKG, "JsonOutput.Error.OpeningFile", e.toString() ) );
  }

  return retval;
}
 
Example 15
Source File: Log4jFileAppender.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public Log4jFileAppender( FileObject file ) throws IOException {
  this.file = file;

  fileOutputStream = KettleVFS.getOutputStream( file, false );
}
 
Example 16
Source File: Log4jFileAppender.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public Log4jFileAppender( FileObject file, boolean append ) throws IOException {
  this.file = file;

  fileOutputStream = KettleVFS.getOutputStream( file, append );
}
 
Example 17
Source File: XMLOutput.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public boolean openNewFile() {
  boolean retval = false;
  data.writer = null;

  try {
    if ( meta.isServletOutput() ) {
      data.writer = XML_OUT_FACTORY.createXMLStreamWriter( getTrans().getServletPrintWriter() );
      if ( meta.getEncoding() != null && meta.getEncoding().length() > 0 ) {
        data.writer.writeStartDocument( meta.getEncoding(), "1.0" );
      } else {
        data.writer.writeStartDocument( Const.XML_ENCODING, "1.0" );
      }
      data.writer.writeCharacters( EOL );
    } else {

      FileObject file = KettleVFS.getFileObject( buildFilename( true ), getTransMeta() );

      if ( meta.isAddToResultFiles() ) {
        // Add this to the result file names...
        ResultFile resultFile =
            new ResultFile( ResultFile.FILE_TYPE_GENERAL, file, getTransMeta().getName(), getStepname() );
        resultFile.setComment( "This file was created with a xml output step" );
        addResultFile( resultFile );
      }

      if ( meta.isZipped() ) {
        OutputStream fos = KettleVFS.getOutputStream( file, false );
        data.zip = new ZipOutputStream( fos );
        File entry = new File( buildFilename( false ) );
        ZipEntry zipentry = new ZipEntry( entry.getName() );
        zipentry.setComment( "Compressed by Kettle" );
        data.zip.putNextEntry( zipentry );
        outputStream = data.zip;
      } else {
        outputStream = KettleVFS.getOutputStream( file, false );
      }
      if ( meta.getEncoding() != null && meta.getEncoding().length() > 0 ) {
        logBasic( "Opening output stream in encoding: " + meta.getEncoding() );
        data.writer = XML_OUT_FACTORY.createXMLStreamWriter( outputStream, meta.getEncoding() );
        data.writer.writeStartDocument( meta.getEncoding(), "1.0" );
      } else {
        logBasic( "Opening output stream in default encoding : " + Const.XML_ENCODING );
        data.writer = XML_OUT_FACTORY.createXMLStreamWriter( outputStream );
        data.writer.writeStartDocument( Const.XML_ENCODING, "1.0" );
      }
      data.writer.writeCharacters( EOL );
    }

    // OK, write the header & the parent element:
    data.writer.writeStartElement( meta.getMainElement() );
    // Add the name space if defined
    if ( ( meta.getNameSpace() != null ) && ( !"".equals( meta.getNameSpace() ) ) ) {
      data.writer.writeDefaultNamespace( meta.getNameSpace() );
    }
    data.writer.writeCharacters( EOL );

    retval = true;
  } catch ( Exception e ) {
    logError( "Error opening new file : " + e.toString() );
  }
  // System.out.println("end of newFile(), splitnr="+splitnr);

  data.splitnr++;

  return retval;
}
 
Example 18
Source File: SharedObjects.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
protected OutputStream initOutputStreamUsingKettleVFS( FileObject fileObject ) throws IOException {
  return KettleVFS.getOutputStream( fileObject, false );
}
 
Example 19
Source File: DistributedCacheUtilImpl.java    From pentaho-hadoop-shims with Apache License 2.0 4 votes vote down vote up
/**
 * Extract a zip archive to a directory.
 *
 * @param archive Zip archive to extract
 * @param dest    Destination directory. This must not exist!
 * @return Directory the zip was extracted into
 * @throws IllegalArgumentException when the archive file does not exist or the destination directory already exists
 * @throws IOException
 * @throws KettleFileException
 */
public FileObject extract( FileObject archive, FileObject dest ) throws IOException, KettleFileException {
  if ( !archive.exists() ) {
    throw new IllegalArgumentException( "archive does not exist: " + archive.getURL().getPath() );
  }

  if ( dest.exists() ) {
    throw new IllegalArgumentException( "destination already exists" );
  }
  dest.createFolder();

  try {
    byte[] buffer = new byte[ DEFAULT_BUFFER_SIZE ];
    int len = 0;
    ZipInputStream zis = new ZipInputStream( archive.getContent().getInputStream() );
    try {
      ZipEntry ze;
      while ( ( ze = zis.getNextEntry() ) != null ) {
        FileObject entry = KettleVFS.getFileObject( dest + Const.FILE_SEPARATOR + ze.getName() );
        FileObject parent = entry.getParent();
        if ( parent != null ) {
          parent.createFolder();
        }
        if ( ze.isDirectory() ) {
          entry.createFolder();
          continue;
        }

        OutputStream os = KettleVFS.getOutputStream( entry, false );
        try {
          while ( ( len = zis.read( buffer ) ) > 0 ) {
            os.write( buffer, 0, len );
          }
        } finally {
          if ( os != null ) {
            os.close();
          }
        }
      }
    } finally {
      if ( zis != null ) {
        zis.close();
      }
    }
  } catch ( Exception ex ) {
    // Try to clean up the temp directory and all files
    if ( !deleteDirectory( dest ) ) {
      throw new KettleFileException( "Could not clean up temp dir after error extracting", ex );
    }
    throw new KettleFileException( "error extracting archive", ex );
  }

  return dest;
}