List of usage examples for org.apache.commons.vfs FileObject exists
public boolean exists() throws FileSystemException;
From source file:org.openbi.kettle.plugins.refreshtableauextract.RefreshTableauExtract.java
public Result execute(Result previousResult, int nr) throws KettleException { Result result = previousResult; result.setResult(validate());//from w w w. jav a 2 s .c o m if (!result.getResult()) { return result; } String[] commands; String tableauCommand = getRealValue(getTableauClient()).trim(); if (tableauCommand.toLowerCase().endsWith(".exe")) { tableauCommand = tableauCommand.substring(0, tableauCommand.length() - 4); } tableauCommand = "\"" + tableauCommand + "\""; if (getRefreshType() == 0 || getRefreshType() == 1) { tableauCommand += " refreshextract"; } else if (getRefreshType() == 2) { tableauCommand += " addfiletoextract"; } else { logError(BaseMessages.getString(PKG, "RefreshTableauExtract.Error.InvalidRefreshType")); result.setResult(false); return result; } tableauCommand += " --server " + protocolList[getProtocol()] + "://" + getRealValue(getServer()); if (getRealValue(getServerPort()) != null && !getRealValue(getServerPort()).isEmpty()) { tableauCommand += ":" + getRealValue(getServerPort()); } tableauCommand += " --username " + getRealValue(getServerUser()); tableauCommand += " --password " + getRealValue(getServerPassword()); tableauCommand += " --datasource \"" + getRealValue(getDataSource()) + "\""; if (getRealValue(getSiteName()) != null && !getRealValue(getSiteName()).isEmpty()) { tableauCommand += " --site \"" + getRealValue(getSiteName()) + "\""; } if (getRealValue(getProject()) != null && !getRealValue(getProject()).isEmpty()) { tableauCommand += " --project \"" + getRealValue(getProject()) + "\""; } if (getRealValue(getProxyUser()) != null && !getRealValue(getProxyUser()).isEmpty()) { tableauCommand += " --proxy-username " + getRealValue(getProxyUser()); } if (getRealValue(getProxyPassword()) != null && !getRealValue(getProxyPassword()).isEmpty()) { tableauCommand += " --proxy-password " + getRealValue(getProxyPassword()); } if (getRefreshType() == 0) { commands = new String[1]; tableauCommand += " --original-file \"" + getRealValue(getExtractFile()) + "\""; commands[0] = new String(tableauCommand); } else if (getRefreshType() == 1) { commands = new String[1]; if (getFullRefresh()) { tableauCommand += " --force-full-refresh"; } if (getRealValue(getSourceUser()) != null && !getRealValue(getSourceUser()).isEmpty()) { tableauCommand += " --source-username " + getRealValue(getSourceUser()); } if (getRealValue(getSourcePassword()) != null & !getRealValue(getSourcePassword()).isEmpty()) { tableauCommand += " --source-password " + getRealValue(getSourcePassword()); } commands[0] = new String(tableauCommand); } else { String[] fileStrings = null; if (processResultFiles) { if (result != null && previousResult.getResultFiles().size() > 0) { int size = previousResult.getResultFiles().size(); if (log.isBasic()) { logBasic(BaseMessages.getString(PKG, "RefreshTableauExtract.FilesFound", "" + size)); } try { List<ResultFile> resultFiles = previousResult.getResultFilesList(); List<String> files = new ArrayList<String>(); Iterator<ResultFile> it = resultFiles.iterator(); while (it.hasNext()) { ResultFile f = it.next(); FileObject o = f.getFile(); if (o.getType().equals(FileType.FILE)) { if (o.exists()) { files.add(o.getName().toString().startsWith("file:///") ? o.getName().toString().substring(8) : o.getName().toString()); } else { logBasic(BaseMessages.getString(PKG, "RefreshTableauExtract.FileNotExist", "" + o.getName())); } } else { logBasic(BaseMessages.getString(PKG, "RefreshTableauExtract.ResultNotFile", "" + o.getName())); } } if (files.size() > 0) { Iterator<String> ite = files.iterator(); fileStrings = new String[files.size()]; int i = 0; while (ite.hasNext()) { fileStrings[i] = ite.next(); i++; } } else { logBasic(BaseMessages.getString(PKG, "RefreshTableauExtract.NoFilesOnResult")); result.setResult(true); return result; } } catch (Exception ex) { logError(ex.toString()); result.setResult(false); return result; } } else { logBasic(BaseMessages.getString(PKG, "RefreshTableauExtract.NoFilesOnResult")); result.setResult(true); return result; } } else { // Get source and destination files, also wildcard String[] vFilePaths = filePaths; String[] vWildcards = wildcards; boolean[] includeSubfolders = new boolean[vFilePaths.length]; String[] fileRequired = new String[vFilePaths.length]; for (int i = 0; i < includeSubfolders.length; i++) { includeSubfolders[i] = false; } FileInputList files = FileInputList.createFileList(this, vFilePaths, vWildcards, fileRequired, includeSubfolders); fileStrings = new String[files.getFileStrings().length]; fileStrings = files.getFileStrings(); } commands = new String[fileStrings.length]; for (int i = 0; i < fileStrings.length; i++) { commands[i] = new String(tableauCommand + " --file \"" + fileStrings[i] + "\""); } } FileObject fileObject = null; String realScript = ""; FileObject tempFile = null; for (int i = 0; i < commands.length; i++) { // realScript+="echo Running: "+commands[i]+"\n"; realScript += commands[i] + "\n"; if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "RefreshTableauExtract.Commands", commands[i])); } } try { // What's the exact command? String[] command; if (log.isBasic()) { logBasic(BaseMessages.getString(PKG, "RefreshTableuaExtract.RunningOn", Const.getOS())); } if (Const.getOS().equals("Windows 95")) { //base = new String[] { "command.com", "/C" }; tempFile = KettleVFS.createTempFile("kettle", "shell.bat", null, this); fileObject = createTemporaryShellFile(tempFile, realScript); command = new String[] { "command.com", "/C", "\"" + Const.optionallyQuoteStringByOS(KettleVFS.getFilename(fileObject)) + "\"" }; } else if (Const.getOS().startsWith("Windows")) { //base = new String[] { "cmd.exe", "/C" }; tempFile = KettleVFS.createTempFile("kettle", "shell.bat", null, this); fileObject = createTemporaryShellFile(tempFile, realScript); command = new String[] { "cmd.exe", "/C", "\"" + Const.optionallyQuoteStringByOS(KettleVFS.getFilename(fileObject)) + "\"" }; } else { tempFile = KettleVFS.createTempFile("kettle", "shell", null, this); fileObject = createTemporaryShellFile(tempFile, realScript); command = new String[] { Const.optionallyQuoteStringByOS(KettleVFS.getFilename(fileObject)) }; } ProcessBuilder pb = new ProcessBuilder(command); Map<String, String> env = pb.environment(); String[] variables = listVariables(); for (int i = 0; i < variables.length; i++) { env.put(variables[i], getVariable(variables[i])); } if (getWorkingDirectory() != null && !Const.isEmpty(Const.rtrim(getRealValue(getWorkingDirectory())))) { String vfsFilename = environmentSubstitute(getRealValue(getWorkingDirectory())); File file = new File(KettleVFS.getFilename(KettleVFS.getFileObject(vfsFilename, this))); pb.directory(file); } if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "RefreshTableauExtract.CommandStarted")); } Process proc = pb.start(); // any error message? StreamLogger errorLogger = new StreamLogger(log, proc.getErrorStream(), "(stderr)"); // any output? StreamLogger outputLogger = new StreamLogger(log, proc.getInputStream(), "(stdout)"); // kick them off new Thread(errorLogger).start(); new Thread(outputLogger).start(); proc.waitFor(); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "RefreshTableauExtract.CommandFinished")); } // What's the exit status? result.setExitStatus(proc.exitValue()); if (result.getExitStatus() != 0) { logError(BaseMessages.getString(PKG, "RefreshTableauExtract.ExitStatus", "" + result.getExitStatus())); result.setResult(false); } // close the streams // otherwise you get "Too many open files, java.io.IOException" after a lot of iterations proc.getErrorStream().close(); proc.getOutputStream().close(); } catch (Exception ex) { logError(ex.toString()); result.setResult(false); } finally { // If we created a temporary file, remove it... // if (tempFile != null) { try { tempFile.delete(); } catch (Exception e) { BaseMessages.getString(PKG, "RefreshTableauExtract.UnexpectedError", tempFile.toString(), e.toString()); } } } return result; }
From source file:org.openbi.kettle.plugins.tableauDataExtract.TDEOutput.java
private void createParentFolder(String filename) throws Exception { // Check for parent folder FileObject parentfolder = null; try {// w w w.java 2 s.com // Get parent folder parentfolder = KettleVFS.getFileObject(filename).getParent(); if (parentfolder.exists()) { if (isDetailed()) logDetailed( BaseMessages.getString(PKG, "TDEOutput.Log.ParentFolderExist", parentfolder.getName())); } else { if (isDetailed()) logDetailed(BaseMessages.getString(PKG, "TDEOutput.Log.ParentFolderNotExist", parentfolder.getName())); if (meta.isCreateParentFolder()) { parentfolder.createFolder(); if (isDetailed()) logDetailed(BaseMessages.getString(PKG, "TDEOutput.Log.ParentFolderCreated", parentfolder.getName())); } else { throw new KettleException(BaseMessages.getString(PKG, "TDEOutput.Log.ParentFolderNotExistCreateIt", parentfolder.getName(), filename)); } } } finally { if (parentfolder != null) { try { parentfolder.close(); } catch (Exception ex) { } ; } } }
From source file:org.openbi.kettle.plugins.tableauDataExtract.TDEOutput.java
private void checkFileExists(String filename) throws Exception { FileObject file = null;/*from w w w .ja v a 2s . co m*/ FileObject parentfolder = null; if (!meta.isCreateParentFolder()) { parentfolder = KettleVFS.getFileObject(filename).getParent(); if (!parentfolder.exists()) { throw new KettleException(BaseMessages.getString(PKG, "TDEOutput.Error.ParentFolderDoesNotExist")); } } file = KettleVFS.getFileObject(filename); if (!meta.isFileAppended() && file.exists()) { file.delete(); if (isDetailed()) logDetailed(BaseMessages.getString(PKG, "TDEOutput.Log.FileDeleted", filename)); } else { if (isDetailed()) logDetailed(BaseMessages.getString(PKG, "TDEOutput.Log.FileDoesNotExist")); } }
From source file:org.pentaho.di.core.database.Database.java
/** * Execute an SQL statement inside a file on the database connection (has to be open) * * @param sql//from w ww. j av a 2s . co m * The file that contains SQL to execute * @sendSinglestatement send one statement * @return a Result object indicating the number of lines read, deleted, inserted, updated, ... * @throws KettleDatabaseException * in case anything goes wrong. */ public Result execStatementsFromFile(String filename, boolean sendSinglestatement) throws KettleException { FileObject sqlFile = null; InputStream is = null; InputStreamReader bis = null; try { if (Const.isEmpty(filename)) { throw new KettleException("Filename is missing!"); } sqlFile = KettleVFS.getFileObject(filename); if (!sqlFile.exists()) { throw new KettleException("We can not find file [" + filename + "]!"); } is = KettleVFS.getInputStream(sqlFile); bis = new InputStreamReader(new BufferedInputStream(is, 500)); StringBuffer lineStringBuffer = new StringBuffer(256); lineStringBuffer.setLength(0); BufferedReader buff = new BufferedReader(bis); String sLine = null; String sql = Const.CR; while ((sLine = buff.readLine()) != null) { if (Const.isEmpty(sLine)) { sql = sql + Const.CR; } else { sql = sql + Const.CR + sLine; } } if (sendSinglestatement) { return execStatement(sql); } else { return execStatements(sql); } } catch (Exception e) { throw new KettleException(e); } finally { try { if (sqlFile != null) { sqlFile.close(); } if (is != null) { is.close(); } if (bis != null) { bis.close(); } } catch (Exception e) { // Ignore } } }
From source file:org.pentaho.di.core.fileinput.FileInputList.java
public static FileInputList createFileList(VariableSpace space, String[] fileName, String[] fileMask, String[] excludeFileMask, String[] fileRequired, boolean[] includeSubdirs, FileTypeFilter[] fileTypeFilters) { FileInputList fileInputList = new FileInputList(); // Replace possible environment variables... final String[] realfile = space.environmentSubstitute(fileName); final String[] realmask = space.environmentSubstitute(fileMask); final String[] realExcludeMask = space.environmentSubstitute(excludeFileMask); for (int i = 0; i < realfile.length; i++) { final String onefile = realfile[i]; final String onemask = realmask[i]; final String excludeonemask = realExcludeMask[i]; final boolean onerequired = YES.equalsIgnoreCase(fileRequired[i]); final boolean subdirs = includeSubdirs[i]; final FileTypeFilter filter = ((fileTypeFilters == null || fileTypeFilters[i] == null) ? FileTypeFilter.ONLY_FILES : fileTypeFilters[i]);// w ww . j a va 2 s . c o m if (Const.isEmpty(onefile)) { continue; } // // If a wildcard is set we search for files // if (!Const.isEmpty(onemask) || !Const.isEmpty(excludeonemask)) { try { FileObject directoryFileObject = KettleVFS.getFileObject(onefile, space); boolean processFolder = true; if (onerequired) { if (!directoryFileObject.exists()) { // if we don't find folder..no need to continue fileInputList.addNonExistantFile(directoryFileObject); processFolder = false; } else { if (!directoryFileObject.isReadable()) { fileInputList.addNonAccessibleFile(directoryFileObject); processFolder = false; } } } // Find all file names that match the wildcard in this directory // if (processFolder) { if (directoryFileObject != null && directoryFileObject.getType() == FileType.FOLDER) // it's a directory { FileObject[] fileObjects = directoryFileObject.findFiles(new AllFileSelector() { @Override public boolean traverseDescendents(FileSelectInfo info) { return info.getDepth() == 0 || subdirs; } @Override public boolean includeFile(FileSelectInfo info) { // Never return the parent directory of a file list. if (info.getDepth() == 0) { return false; } FileObject fileObject = info.getFile(); try { if (fileObject != null && filter.isFileTypeAllowed(fileObject.getType())) { String name = info.getFile().getName().getBaseName(); boolean matches = true; if (!Const.isEmpty(onemask)) { matches = Pattern.matches(onemask, name); } boolean excludematches = false; if (!Const.isEmpty(excludeonemask)) { excludematches = Pattern.matches(excludeonemask, name); } return (matches && !excludematches); } return false; } catch (IOException ex) { // Upon error don't process the file. return false; } } }); if (fileObjects != null) { for (int j = 0; j < fileObjects.length; j++) { if (fileObjects[j].exists()) { fileInputList.addFile(fileObjects[j]); } } } if (Const.isEmpty(fileObjects)) { if (onerequired) { fileInputList.addNonAccessibleFile(directoryFileObject); } } // Sort the list: quicksort, only for regular files fileInputList.sortFiles(); } else { FileObject[] children = directoryFileObject.getChildren(); for (int j = 0; j < children.length; j++) { // See if the wildcard (regexp) matches... String name = children[j].getName().getBaseName(); boolean matches = true; if (!Const.isEmpty(onemask)) { matches = Pattern.matches(onemask, name); } boolean excludematches = false; if (!Const.isEmpty(excludeonemask)) { excludematches = Pattern.matches(excludeonemask, name); } if (matches && !excludematches) { fileInputList.addFile(children[j]); } } // We don't sort here, keep the order of the files in the archive. } } } catch (Exception e) { if (onerequired) { fileInputList.addNonAccessibleFile(new NonAccessibleFileObject(onefile)); } log.logError(Const.getStackTracker(e)); } } else { // A normal file... try { FileObject fileObject = KettleVFS.getFileObject(onefile, space); if (fileObject.exists()) { if (fileObject.isReadable()) { fileInputList.addFile(fileObject); } else { if (onerequired) { fileInputList.addNonAccessibleFile(fileObject); } } } else { if (onerequired) { fileInputList.addNonExistantFile(fileObject); } } } catch (Exception e) { if (onerequired) { fileInputList.addNonAccessibleFile(new NonAccessibleFileObject(onefile)); } log.logError(Const.getStackTracker(e)); } } } return fileInputList; }
From source file:org.pentaho.di.core.fileinput.FileInputList.java
public static FileInputList createFolderList(VariableSpace space, String[] folderName, String[] folderRequired) { FileInputList fileInputList = new FileInputList(); // Replace possible environment variables... final String[] realfolder = space.environmentSubstitute(folderName); for (int i = 0; i < realfolder.length; i++) { final String onefile = realfolder[i]; final boolean onerequired = YES.equalsIgnoreCase(folderRequired[i]); final boolean subdirs = true; final FileTypeFilter filter = FileTypeFilter.ONLY_FOLDERS; if (Const.isEmpty(onefile)) { continue; }//from w ww . jav a2s . co m FileObject directoryFileObject = null; try { // Find all folder names in this directory // directoryFileObject = KettleVFS.getFileObject(onefile, space); if (directoryFileObject != null && directoryFileObject.getType() == FileType.FOLDER) // it's a directory { FileObject[] fileObjects = directoryFileObject.findFiles(new AllFileSelector() { @Override public boolean traverseDescendents(FileSelectInfo info) { return info.getDepth() == 0 || subdirs; } @Override public boolean includeFile(FileSelectInfo info) { // Never return the parent directory of a file list. if (info.getDepth() == 0) { return false; } FileObject fileObject = info.getFile(); try { if (fileObject != null && filter.isFileTypeAllowed(fileObject.getType())) { return true; } return false; } catch (IOException ex) { // Upon error don't process the file. return false; } } }); if (fileObjects != null) { for (int j = 0; j < fileObjects.length; j++) { if (fileObjects[j].exists()) { fileInputList.addFile(fileObjects[j]); } } } if (Const.isEmpty(fileObjects)) { if (onerequired) { fileInputList.addNonAccessibleFile(directoryFileObject); } } // Sort the list: quicksort, only for regular files fileInputList.sortFiles(); } else { if (onerequired && !directoryFileObject.exists()) { fileInputList.addNonExistantFile(directoryFileObject); } } } catch (Exception e) { log.logError(Const.getStackTracker(e)); } finally { try { if (directoryFileObject != null) { directoryFileObject.close(); } directoryFileObject = null; } catch (Exception e) { // Ignore } } } return fileInputList; }
From source file:org.pentaho.di.core.playlist.FilePlayListReplay.java
private void initializeCurrent(FileObject file, String filePart) throws KettleException { try {//from w w w .j a v a 2s. c o m FileObject lineFile = AbstractFileErrorHandler.getReplayFilename(lineNumberDirectory, file.getName().getBaseName(), replayDate, lineNumberExtension, filePart); if (lineFile.exists()) { currentLineNumberFile = new FilePlayListReplayLineNumberFile(lineFile, encoding, file, filePart); } else { currentLineNumberFile = new FilePlayListReplayFile(file, filePart); } FileObject errorFile = AbstractFileErrorHandler.getReplayFilename(errorDirectory, file.getName().getURI(), replayDate, errorExtension, AbstractFileErrorHandler.NO_PARTS); if (errorFile.exists()) { currentErrorFile = new FilePlayListReplayErrorFile(errorFile, file); } else { currentErrorFile = new FilePlayListReplayFile(file, AbstractFileErrorHandler.NO_PARTS); } } catch (IOException e) { throw new KettleException(e); } }
From source file:org.pentaho.di.core.vfs.KettleVFS.java
public static boolean fileExists(String vfsFilename, VariableSpace space) throws KettleFileException { FileObject fileObject = null; try {/*from www . ja v a 2 s .co m*/ fileObject = getFileObject(vfsFilename, space); return fileObject.exists(); } catch (IOException e) { throw new KettleFileException(e); } finally { if (fileObject != null) { try { fileObject.close(); } catch (Exception e) { /* Ignore */ } } } }
From source file:org.pentaho.di.core.vfs.KettleVFS.java
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, "KettleVFS.Exception.ParentDirectoryDoesNotExist", getFilename(parent))); }//from www . j a va2 s . c o m } 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; } } }
From source file:org.pentaho.di.core.vfs.KettleVFS.java
public static FileObject createTempFile(String prefix, String suffix, String directory, VariableSpace space) throws KettleFileException { try {//ww w.ja v a2 s . c om FileObject fileObject; do { // Build temporary file name using UUID to ensure uniqueness. Old mechanism would fail using Sort Rows (for // example) // when there multiple nodes with multiple JVMs on each node. In this case, the temp file names would end up // being // duplicated which would cause the sort to fail. String filename = new StringBuffer(50).append(directory).append('/').append(prefix).append('_') .append(UUIDUtil.getUUIDAsString()).append(suffix).toString(); fileObject = getFileObject(filename, space); } while (fileObject.exists()); return fileObject; } catch (IOException e) { throw new KettleFileException(e); } }