List of usage examples for org.apache.commons.vfs FileObject createFolder
public void createFolder() throws FileSystemException;
From source file:com.panet.imeta.job.entries.zipfile.JobEntryZipFile.java
private boolean createParentFolder(String filename) { // Check for parent folder FileObject parentfolder = null; boolean result = false; LogWriter log = LogWriter.getInstance(); try {/*from ww w . j ava2 s. c om*/ // Get parent folder parentfolder = KettleVFS.getFileObject(filename).getParent(); if (!parentfolder.exists()) { if (log.isDetailed()) log.logDetailed(toString(), "Folder" + parentfolder.getName() + " does not exist !"); parentfolder.createFolder(); if (log.isDetailed()) log.logDetailed(toString(), "Folder was created."); } else { if (log.isDetailed()) log.logDetailed(toString(), "Folder" + parentfolder.getName() + " exist !"); } result = true; } catch (Exception e) { log.logError(toString(), "Couldn't created folder " + parentfolder.getName()); } finally { if (parentfolder != null) { try { parentfolder.close(); } catch (Exception ex) { } ; } } return result; }
From source file:com.panet.imeta.job.entries.copyfiles.JobEntryCopyFiles.java
private boolean CreateDestinationFolder(FileObject filefolder) { LogWriter log = LogWriter.getInstance(); FileObject folder = null; try {// w ww. j a v a 2s . c o m if (destination_is_a_file) folder = filefolder.getParent(); else folder = filefolder; if (!folder.exists()) { if (create_destination_folder) { if (log.isDetailed()) log.logDetailed("Folder", "Folder " + folder.getName() + " does not exist !"); folder.createFolder(); if (log.isDetailed()) log.logDetailed("Folder", "Folder parent was created."); } else { log.logError("Folder", "Folder " + folder.getName() + " does not exist !"); return false; } } return true; } catch (Exception e) { log.logError(toString(), "Couldn't created parent folder " + folder.getName()); } finally { if (folder != null) { try { folder.close(); } catch (Exception ex) { } ; } } return false; }
From source file:com.panet.imeta.job.entries.ssh2get.JobEntrySSH2GET.java
private boolean CreateFolder(String filefolder) { FileObject folder = null; try {/*from w w w . jav a 2s .co m*/ folder = KettleVFS.getFileObject(filefolder); if (!folder.exists()) { if (createtargetfolder) { folder.createFolder(); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSSH2GET.Log.FolderCreated", folder.toString())); } else return false; } return true; } catch (Exception e) { log.logError(toString(), Messages.getString("JobSSH2GET.Log.CanNotCreateFolder", folder.toString())); } finally { if (folder != null) { try { folder.close(); } catch (Exception ex) { } ; } } return false; }
From source file:com.panet.imeta.job.entries.createfolder.JobEntryCreateFolder.java
public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) { LogWriter log = LogWriter.getInstance(); Result result = previousResult; result.setResult(false);/*from w ww . ja v a 2 s .c om*/ if (foldername != null) { String realFoldername = getRealFoldername(); FileObject FolderObject = null; try { FolderObject = KettleVFS.getFileObject(realFoldername); if (FolderObject.exists()) { boolean isFolder = false; //Check if it's a folder if (FolderObject.getType() == FileType.FOLDER) isFolder = true; if (isFailOfFolderExists()) { // Folder exists and fail flag is on. result.setResult(false); if (isFolder) log.logError(toString(), "Folder [" + realFoldername + "] exists, failing."); else log.logError(toString(), "File [" + realFoldername + "] exists, failing."); } else { // Folder already exists, no reason to try to create it result.setResult(true); if (log.isDetailed()) log.logDetailed(toString(), "Folder [" + realFoldername + "] already exists, not recreating."); } } else { // No Folder yet, create an empty Folder. FolderObject.createFolder(); if (log.isDetailed()) log.logDetailed(toString(), "Folder [" + realFoldername + "] created!"); result.setResult(true); } } catch (IOException e) { log.logError(toString(), "Could not create Folder [" + realFoldername + "], exception: " + e.getMessage()); result.setResult(false); result.setNrErrors(1); } finally { if (FolderObject != null) { try { FolderObject.close(); } catch (IOException ex) { } ; } } } else { log.logError(toString(), "No Foldername is defined."); } return result; }
From source file:com.panet.imeta.job.entries.unzip.JobEntryUnZip.java
private boolean unzipFile(LogWriter log, FileObject sourceFileObject, String realTargetdirectory, String realWildcard, String realWildcardExclude, Result result, Job parentJob, FileObject fileObject, FileObject movetodir, String realMovetodirectory) { boolean retval = false; try {//from w w w. j a v a 2 s .co m if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Log.ProcessingFile", sourceFileObject.toString())); // Do you create a root folder? // if (rootzip) { String shortSourceFilename = sourceFileObject.getName().getBaseName(); int lenstring = shortSourceFilename.length(); int lastindexOfDot = shortSourceFilename.lastIndexOf('.'); if (lastindexOfDot == -1) lastindexOfDot = lenstring; String foldername = realTargetdirectory + "/" + shortSourceFilename.substring(0, lastindexOfDot); FileObject rootfolder = KettleVFS.getFileObject(foldername); if (!rootfolder.exists()) { try { rootfolder.createFolder(); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Log.RootFolderCreated", foldername)); } catch (Exception e) { throw new Exception(Messages.getString("JobUnZip.Error.CanNotCreateRootFolder", foldername), e); } } } // Try to read the entries from the VFS object... // String zipFilename = "zip:" + sourceFileObject.getName().getFriendlyURI(); FileObject zipFile = KettleVFS.getFileObject(zipFilename); FileObject[] items = zipFile.findFiles(new AllFileSelector() { public boolean traverseDescendents(FileSelectInfo info) { return true; } public boolean includeFile(FileSelectInfo info) { // Never return the parent directory of a file list. if (info.getDepth() == 0) { return false; } FileObject fileObject = info.getFile(); return fileObject != null; } }); Pattern pattern = null; if (!Const.isEmpty(realWildcard)) { pattern = Pattern.compile(realWildcard); } Pattern patternexclude = null; if (!Const.isEmpty(realWildcardExclude)) { patternexclude = Pattern.compile(realWildcardExclude); } for (FileObject item : items) { if (successConditionBroken) { if (!successConditionBrokenExit) { log.logError(toString(), Messages.getString("JobUnZip.Error.SuccessConditionbroken", "" + NrErrors)); successConditionBrokenExit = true; } return false; } try { if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Log.ProcessingZipEntry", item.getName().getURI(), sourceFileObject.toString())); // get real destination filename // String newFileName = realTargetdirectory + Const.FILE_SEPARATOR + getTargetFilename(item.getName().getPath()); FileObject newFileObject = KettleVFS.getFileObject(newFileName); if (item.getType().equals(FileType.FOLDER)) { // Directory // if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.CreatingDirectory.Label", newFileName)); // Create Directory if necessary ... // if (!newFileObject.exists()) newFileObject.createFolder(); } else { // File // boolean getIt = true; boolean getItexclude = false; // First see if the file matches the regular expression! // if (pattern != null) { Matcher matcher = pattern.matcher(item.getName().getURI()); getIt = matcher.matches(); } if (patternexclude != null) { Matcher matcherexclude = patternexclude.matcher(item.getName().getURI()); getItexclude = matcherexclude.matches(); } boolean take = takeThisFile(log, item, newFileName); if (getIt && !getItexclude && take) { if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.ExtractingEntry.Label", item.getName().getURI(), newFileName)); if (iffileexist == IF_FILE_EXISTS_UNIQ) { // Create file with unique name int lenstring = newFileName.length(); int lastindexOfDot = newFileName.lastIndexOf('.'); if (lastindexOfDot == -1) lastindexOfDot = lenstring; newFileName = newFileName.substring(0, lastindexOfDot) + StringUtil.getFormattedDateTimeNow(true) + newFileName.substring(lastindexOfDot, lenstring); if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobUnZip.Log.CreatingUniqFile", newFileName)); } // See if the folder to the target file exists... // if (!newFileObject.getParent().exists()) { newFileObject.getParent().createFolder(); // creates // the // whole // path. } InputStream is = null; OutputStream os = null; try { is = KettleVFS.getInputStream(item); os = KettleVFS.getOutputStream(newFileObject, false); if (is != null) { byte[] buff = new byte[2048]; int len; while ((len = is.read(buff)) > 0) { os.write(buff, 0, len); } // Add filename to result filenames addFilenameToResultFilenames(result, parentJob, newFileName); } } finally { if (is != null) is.close(); if (os != null) os.close(); } } // end if take } } catch (Exception e) { updateErrors(); log.logError(toString(), Messages.getString("JobUnZip.Error.CanNotProcessZipEntry", item.getName().getURI(), sourceFileObject.toString()), e); } } // End while // Here gc() is explicitly called if e.g. createfile is used in the // same // job for the same file. The problem is that after creating the // file the // file object is not properly garbaged collected and thus the file // cannot // be deleted anymore. This is a known problem in the JVM. // System.gc(); // Unzip done... if (afterunzip == 1) { // delete zip file boolean deleted = fileObject.delete(); if (!deleted) { updateErrors(); log.logError(toString(), Messages.getString("JobUnZip.Cant_Delete_File.Label", sourceFileObject.toString())); } // File deleted if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobUnZip.File_Deleted.Label", sourceFileObject.toString())); } else if (afterunzip == 2) { FileObject destFile = null; // Move File try { String destinationFilename = movetodir + Const.FILE_SEPARATOR + fileObject.getName().getBaseName(); destFile = KettleVFS.getFileObject(destinationFilename); fileObject.moveTo(destFile); // File moved if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Log.FileMovedTo", sourceFileObject.toString(), realMovetodirectory)); } catch (Exception e) { updateErrors(); log.logError(toString(), Messages.getString("JobUnZip.Cant_Move_File.Label", sourceFileObject.toString(), realMovetodirectory, e.getMessage())); } finally { if (destFile != null) { try { destFile.close(); } catch (IOException ex) { } ; } } } retval = true; } catch (Exception e) { updateErrors(); log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.ErrorUnzip.Label", sourceFileObject.toString(), e.getMessage()), e); } return retval; }
From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java
public static void createFolder(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { try {/* w w w .ja v a 2s.c om*/ if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) { FileObject fileObject = null; try { fileObject = KettleVFS.getFileObject((String) ArgList[0]); if (!fileObject.exists()) { fileObject.createFolder(); } else { new RuntimeException("folder [" + (String) ArgList[0] + "] already exist!"); } } catch (IOException e) { throw new RuntimeException("The function call createFolder is not valid."); } finally { if (fileObject != null) try { fileObject.close(); } catch (Exception e) { } } } else { throw new RuntimeException("The function call createFolder is not valid."); } } catch (Exception e) { throw new RuntimeException(e.toString()); } }
From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static void createFolder(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { try {//from ww w. j a v a 2 s . c o m if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) { FileObject fileObject = null; try { fileObject = KettleVFS.getFileObject(Context.toString(ArgList[0])); if (!fileObject.exists()) { fileObject.createFolder(); } else { Context.reportRuntimeError("folder [" + Context.toString(ArgList[0]) + "] already exist!"); } } catch (IOException e) { throw Context.reportRuntimeError("The function call createFolder is not valid."); } finally { if (fileObject != null) try { fileObject.close(); } catch (Exception e) { } } } else { throw Context.reportRuntimeError("The function call createFolder is not valid."); } } catch (Exception e) { throw Context.reportRuntimeError(e.toString()); } }
From source file:com.panet.imeta.trans.steps.rssoutput.RssOutput.java
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { meta = (RssOutputMeta) smi;//from ww w. jav a 2 s . c o m data = (RssOutputData) sdi; Object[] r = getRow(); // this also waits for a previous step to be finished. if (r == null) // no more input to be expected... { if (!first) { if (!meta.isCustomRss()) { // No more input..so write and close the file. WriteToFile(data.channeltitlevalue, data.channellinkvalue, data.channeldescriptionvalue, data.channelpubdatevalue, data.channelcopyrightvalue, data.channelimagelinkvalue, data.channelimagedescriptionvalue, data.channelimagelinkvalue, data.channelimageurlvalue, data.channellanguagevalue, data.channelauthorvalue); } else { // Write to document OutputFormat format = org.dom4j.io.OutputFormat.createPrettyPrint(); // Set encoding ... if (Const.isEmpty(meta.getEncoding())) format.setEncoding("iso-8859-1"); else format.setEncoding(meta.getEncoding()); try { XMLWriter writer = new XMLWriter(new FileWriter(new File(data.filename)), format); writer.write(data.document); writer.close(); } catch (Exception e) { } finally { data.document = null; } } } setOutputDone(); return false; } if (first) { first = false; data.inputRowMeta = getInputRowMeta(); data.outputRowMeta = data.inputRowMeta.clone(); meta.getFields(data.outputRowMeta, getStepname(), null, null, this); // Let's check for filename... if (meta.isFilenameInField()) { if (Const.isEmpty(meta.getFileNameField())) { logError(Messages.getString("RssOutput.Log.FilenameFieldMissing")); setErrors(1); stopAll(); return false; } // get filename field index data.indexOfFieldfilename = data.inputRowMeta.indexOfValue(meta.getFileNameField()); if (data.indexOfFieldfilename < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getFileNameField())); throw new KettleException( Messages.getString("RssOutput.Log.ErrorFindingField", meta.getFileNameField())); } } else { data.filename = buildFilename(); } // Check if filename is empty.. if (Const.isEmpty(data.filename)) { logError(Messages.getString("RssOutput.Log.FilenameEmpty")); throw new KettleException(Messages.getString("RssOutput.Log.FilenameEmpty")); } // Do we need to create parent folder ? if (meta.isCreateParentFolder()) { // Check for parent folder FileObject parentfolder = null; try { // Get parent folder parentfolder = KettleVFS.getFileObject(data.filename).getParent(); if (!parentfolder.exists()) { if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("RssOutput.Log.ParentFolderExists", parentfolder.getName().toString())); parentfolder.createFolder(); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("RssOutput.Log.CanNotCreateParentFolder", parentfolder.getName().toString())); } } catch (Exception e) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.CanNotCreateParentFolder", parentfolder.getName().toString())); throw new KettleException(Messages.getString("RssOutput.Log.CanNotCreateParentFolder", parentfolder.getName().toString())); } finally { if (parentfolder != null) { try { parentfolder.close(); } catch (Exception ex) { } ; } } } if (!meta.isCustomRss()) { // Let's check for mandatory fields ... if (Const.isEmpty(meta.getChannelTitle())) { logError(Messages.getString("RssOutput.Log.ChannelTitleMissing")); setErrors(1); stopAll(); return false; } if (Const.isEmpty(meta.getChannelDescription())) { logError(Messages.getString("RssOutput.Log.ChannelDescription")); setErrors(1); stopAll(); return false; } if (Const.isEmpty(meta.getChannelLink())) { logError(Messages.getString("RssOutput.Log.ChannelLink")); setErrors(1); stopAll(); return false; } // Let's take the index of channel title field ... data.indexOfFieldchanneltitle = data.inputRowMeta.indexOfValue(meta.getChannelTitle()); if (data.indexOfFieldchanneltitle < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelTitle())); throw new KettleException( Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelTitle())); } data.channeltitlevalue = data.inputRowMeta.getString(r, data.indexOfFieldchanneltitle); // Let's take the index of channel description field ... data.indexOfFieldchanneldescription = data.inputRowMeta.indexOfValue(meta.getChannelDescription()); if (data.indexOfFieldchanneldescription < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelDescription())); throw new KettleException( Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelDescription())); } data.channeldescriptionvalue = data.inputRowMeta.getString(r, data.indexOfFieldchanneldescription); // Let's take the index of channel link field ... data.indexOfFieldchannellink = data.inputRowMeta.indexOfValue(meta.getChannelLink()); if (data.indexOfFieldchannellink < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelLink())); throw new KettleException( Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelLink())); } data.channellinkvalue = data.inputRowMeta.getString(r, data.indexOfFieldchannellink); if (!Const.isEmpty(meta.getItemTitle())) { // Let's take the index of item title field ... data.indexOfFielditemtitle = data.inputRowMeta.indexOfValue(meta.getItemTitle()); if (data.indexOfFielditemtitle < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemTitle())); throw new KettleException( Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemTitle())); } } if (!Const.isEmpty(meta.getItemDescription())) { // Let's take the index of item description field ... data.indexOfFielditemdescription = data.inputRowMeta.indexOfValue(meta.getItemDescription()); if (data.indexOfFielditemdescription < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemDescription())); throw new KettleException( Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemDescription())); } } if (meta.AddGeoRSS()) { if (Const.isEmpty(meta.getGeoPointLong())) throw new KettleException(Messages.getString("RssOutput.Log.GeoPointLatEmpty")); if (Const.isEmpty(meta.getGeoPointLong())) throw new KettleException(Messages.getString("RssOutput.Log.GeoPointLongEmpty")); // Let's take the index of item geopointX field ... data.indexOfFielditempointx = data.inputRowMeta.indexOfValue(meta.getGeoPointLat()); if (data.indexOfFielditempointx < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getGeoPointLat())); throw new KettleException( Messages.getString("RssOutput.Log.ErrorFindingField", meta.getGeoPointLat())); } // Let's take the index of item geopointY field ... data.indexOfFielditempointy = data.inputRowMeta.indexOfValue(meta.getGeoPointLong()); if (data.indexOfFielditempointy < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getGeoPointLong())); throw new KettleException( Messages.getString("RssOutput.Log.ErrorFindingField", meta.getGeoPointLong())); } } //It's time to check non empty fields ! // Channel PubDate field ... if (!Const.isEmpty(meta.getChannelPubDate())) { data.indexOfFieldchannelpubdate = data.inputRowMeta.indexOfValue(meta.getChannelPubDate()); if (data.indexOfFieldchannelpubdate < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelPubDate())); throw new KettleException( Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelPubDate())); } data.channelpubdatevalue = data.inputRowMeta.getDate(r, data.indexOfFieldchannelpubdate); } // Channel Language field ... if (!Const.isEmpty(meta.getChannelLanguage())) { data.indexOfFieldchannellanguage = data.inputRowMeta.indexOfValue(meta.getChannelLanguage()); if (data.indexOfFieldchannellanguage < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelLanguage())); throw new KettleException( Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelLanguage())); } data.channellanguagevalue = data.inputRowMeta.getString(r, data.indexOfFieldchannellanguage); } // Channel Copyright field ... if (!Const.isEmpty(meta.getChannelCopyright())) { data.indexOfFieldchannelcopyright = data.inputRowMeta.indexOfValue(meta.getChannelCopyright()); if (data.indexOfFieldchannelcopyright < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelCopyright())); throw new KettleException( Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelCopyright())); } data.channelcopyrightvalue = data.inputRowMeta.getString(r, data.indexOfFieldchannelcopyright); } // Channel Author field ... if (!Const.isEmpty(meta.getChannelAuthor())) { data.indexOfFieldchannelauthor = data.inputRowMeta.indexOfValue(meta.getChannelAuthor()); if (data.indexOfFieldchannelauthor < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelAuthor())); throw new KettleException( Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelAuthor())); } data.channelauthorvalue = data.inputRowMeta.getString(r, data.indexOfFieldchannelauthor); } // Channel Image field ... if (meta.AddImage()) { // Channel image title if (!Const.isEmpty(meta.getChannelImageTitle())) { data.indexOfFieldchannelimagetitle = data.inputRowMeta .indexOfValue(meta.getChannelImageTitle()); if (data.indexOfFieldchannelimagetitle < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelImageTitle())); throw new KettleException(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelImageTitle())); } data.channelimagetitlevalue = data.inputRowMeta.getString(r, data.indexOfFieldchannelimagetitle); } // Channel link title if (!Const.isEmpty(meta.getChannelImageLink())) { data.indexOfFieldchannelimagelink = data.inputRowMeta .indexOfValue(meta.getChannelImageLink()); if (data.indexOfFieldchannelimagelink < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelImageLink())); throw new KettleException(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelImageLink())); } data.channelimagelinkvalue = data.inputRowMeta.getString(r, data.indexOfFieldchannelimagelink); } // Channel url title if (!Const.isEmpty(meta.getChannelImageUrl())) { data.indexOfFieldchannelimageurl = data.inputRowMeta .indexOfValue(meta.getChannelImageUrl()); if (data.indexOfFieldchannelimageurl < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelImageUrl())); throw new KettleException(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelImageUrl())); } data.channelimageurlvalue = data.inputRowMeta.getString(r, data.indexOfFieldchannelimageurl); } // Channel description title if (!Const.isEmpty(meta.getChannelImageDescription())) { data.indexOfFieldchannelimagedescription = data.inputRowMeta .indexOfValue(meta.getChannelImageDescription()); if (data.indexOfFieldchannelimagedescription < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelImageDescription())); throw new KettleException(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelImageDescription())); } data.channelimagedescriptionvalue = data.inputRowMeta.getString(r, data.indexOfFieldchannelimagedescription); } } // Item link field ... if (!Const.isEmpty(meta.getItemLink())) { data.indexOfFielditemlink = data.inputRowMeta.indexOfValue(meta.getItemLink()); if (data.indexOfFielditemlink < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemLink())); throw new KettleException( Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemLink())); } } // Item pubdate field ... if (!Const.isEmpty(meta.getItemPubDate())) { data.indexOfFielditempubdate = data.inputRowMeta.indexOfValue(meta.getItemPubDate()); if (data.indexOfFielditempubdate < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemPubDate())); throw new KettleException( Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemPubDate())); } } // Item author field ... if (!Const.isEmpty(meta.getItemAuthor())) { data.indexOfFielditemauthor = data.inputRowMeta.indexOfValue(meta.getItemAuthor()); if (data.indexOfFielditemauthor < 0) { // The field is unreachable ! logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemAuthor())); throw new KettleException( Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemAuthor())); } } } else { // Custom RSS // Check Custom channel fields data.customchannels = new int[meta.getChannelCustomFields().length]; for (int i = 0; i < meta.getChannelCustomFields().length; i++) { data.customchannels[i] = data.inputRowMeta.indexOfValue(meta.getChannelCustomFields()[i]); if (data.customchannels[i] < 0) // couldn't find field! { throw new KettleStepException(Messages.getString("RssOutput.Exception.FieldRequired", //$NON-NLS-1$ meta.getChannelCustomFields()[i])); //$NON-NLS-2$ } } // Check Custom channel fields data.customitems = new int[meta.getItemCustomFields().length]; for (int i = 0; i < meta.getItemCustomFields().length; i++) { data.customitems[i] = data.inputRowMeta.indexOfValue(meta.getItemCustomFields()[i]); if (data.customitems[i] < 0) // couldn't find field! { throw new KettleStepException(Messages.getString("RssOutput.Exception.FieldRequired", //$NON-NLS-1$ meta.getItemCustomFields()[i])); //$NON-NLS-2$ } } // Prepare Output RSS Custom document data.document = DocumentHelper.createDocument(); data.rssElement = data.document.addElement("rss"); data.rssElement.addAttribute("version", "2.0"); // add namespaces here ... for (int i = 0; i < meta.getNameSpaces().length; i++) { data.rssElement.addAttribute(environmentSubstitute(meta.getNameSpacesTitle()[i]), environmentSubstitute(meta.getNameSpaces()[i])); } // Add channel data.channel = data.rssElement.addElement("channel"); // Set channel Only the first time ... for (int i = 0; i < data.customchannels.length; i++) { String channelname = environmentSubstitute(meta.getChannelCustomTags()[i]); String channelvalue = data.inputRowMeta.getString(r, data.customchannels[i]); if (log.isDetailed()) log.logDetailed(toString(), "outputting channel value <" + channelname + ">" + channelvalue + "<" + channelname + "/>"); // add Channel Element channeltag = data.channel.addElement(channelname); channeltag.setText(channelvalue); } } } // end test first time // Let's get value for each item... if (!meta.isCustomRss()) { String itemtitlevalue = null; String itemauthorvalue = null; String itemlinkvalue = null; Date itemdatevalue = null; String itemdescvalue = null; String itemgeopointx = null; String itemgeopointy = null; if (data.indexOfFielditemtitle > -1) itemtitlevalue = data.inputRowMeta.getString(r, data.indexOfFielditemtitle); if (data.indexOfFielditemauthor > -1) itemauthorvalue = data.inputRowMeta.getString(r, data.indexOfFielditemauthor); if (data.indexOfFielditemlink > -1) itemlinkvalue = data.inputRowMeta.getString(r, data.indexOfFielditemlink); if (data.indexOfFielditempubdate > -1) itemdatevalue = data.inputRowMeta.getDate(r, data.indexOfFielditempubdate); if (data.indexOfFielditemdescription > -1) itemdescvalue = data.inputRowMeta.getString(r, data.indexOfFielditemdescription); if (data.indexOfFielditempointx > -1) itemgeopointx = data.inputRowMeta.getString(r, data.indexOfFielditempointx); if (data.indexOfFielditempointy > -1) itemgeopointy = data.inputRowMeta.getString(r, data.indexOfFielditempointy); // Now add entry .. if (!createEntry(itemauthorvalue, itemtitlevalue, itemlinkvalue, itemdatevalue, itemdescvalue, itemgeopointx, itemgeopointy)) throw new KettleException("Error adding item to feed"); } else { // Set item tag at each row received if (meta.isDisplayItem()) { data.itemtag = data.channel.addElement("item"); } for (int i = 0; i < data.customitems.length; i++) { // get item value and name String itemname = environmentSubstitute(meta.getItemCustomTags()[i]); String itemvalue = data.inputRowMeta.getString(r, data.customitems[i]); if (log.isDetailed()) log.logDetailed(toString(), "outputting item value <" + itemname + ">" + itemvalue + "<" + itemname + "/>"); // add Item if (meta.isDisplayItem()) { Element itemtagsub = data.itemtag.addElement(itemname); itemtagsub.setText(itemvalue); } else { // display item at channel level data.channel.addElement(itemname); data.channel.setText(itemvalue); } } } try { putRow(data.outputRowMeta, r); // in case we want it to go further... incrementLinesOutput(); if (checkFeedback(getLinesOutput())) { if (log.isDebug()) logDebug(Messages.getString("RssOutput.Log.Linenr", "" + getLinesOutput())); } } catch (KettleStepException e) { logError(Messages.getString("RssOutputMeta.Log.ErrorInStep") + e.getMessage()); //$NON-NLS-1$ setErrors(1); stopAll(); setOutputDone(); // signal end to receiver(s) return false; } return true; }
From source file:com.panet.imeta.job.entries.sftp.JobEntrySFTP.java
public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) { LogWriter log = LogWriter.getInstance(); Result result = previousResult; List<RowMetaAndData> rows = result.getRows(); RowMetaAndData resultRow = null;/*from w w w . jav a 2 s . c om*/ result.setResult(false); long filesRetrieved = 0; if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTP.Log.StartJobEntry")); HashSet<String> list_previous_filenames = new HashSet<String>(); if (copyprevious) { if (rows.size() == 0) { if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTP.ArgsFromPreviousNothing")); result.setResult(true); return result; } try { // Copy the input row to the (command line) arguments for (int iteration = 0; iteration < rows.size(); iteration++) { resultRow = rows.get(iteration); // Get file names String file_previous = resultRow.getString(0, null); if (!Const.isEmpty(file_previous)) { list_previous_filenames.add(file_previous); if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobSFTP.Log.FilenameFromResult", file_previous)); } } } catch (Exception e) { log.logError(toString(), Messages.getString("JobSFTP.Error.ArgFromPrevious")); result.setNrErrors(1); return result; } } SFTPClient sftpclient = null; // String substitution.. String realServerName = environmentSubstitute(serverName); String realServerPort = environmentSubstitute(serverPort); String realUsername = environmentSubstitute(userName); String realPassword = environmentSubstitute(password); String realSftpDirString = environmentSubstitute(sftpDirectory); String realWildcard = environmentSubstitute(wildcard); String realTargetDirectory = environmentSubstitute(targetDirectory); FileObject TargetFolder = null; try { // Let's perform some checks before starting if (!Const.isEmpty(realTargetDirectory)) { TargetFolder = KettleVFS.getFileObject(realTargetDirectory); boolean TargetFolderExists = TargetFolder.exists(); if (TargetFolderExists) { if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTP.Log.TargetFolderExists", realTargetDirectory)); } else { log.logError(toString(), Messages.getString("JobSFTP.Error.TargetFolderNotExists", realTargetDirectory)); if (!createtargetfolder) { // Error..Target folder can not be found ! result.setNrErrors(1); return result; } else { // create target folder TargetFolder.createFolder(); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTP.Log.TargetFolderCreated", realTargetDirectory)); } } } if (TargetFolder != null) { TargetFolder.close(); TargetFolder = null; } // Create sftp client to host ... sftpclient = new SFTPClient(InetAddress.getByName(realServerName), Const.toInt(realServerPort, 22), realUsername); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTP.Log.OpenedConnection", realServerName, realServerPort, realUsername)); // login to ftp host ... sftpclient.login(realPassword); // Passwords should not appear in log files. //log.logDetailed(toString(), "logged in using password "+realPassword); // Logging this seems a bad idea! Oh well. // move to spool dir ... if (!Const.isEmpty(realSftpDirString)) { try { sftpclient.chdir(realSftpDirString); } catch (Exception e) { log.logError(toString(), Messages.getString("JobSFTP.Error.CanNotFindRemoteFolder", realSftpDirString)); throw new Exception(e); } if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTP.Log.ChangedDirectory", realSftpDirString)); } Pattern pattern = null; // Get all the files in the current directory... String[] filelist = sftpclient.dir(); if (filelist == null) { // Nothing was found !!! exit result.setResult(true); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTP.Log.Found", "" + 0)); return result; } if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTP.Log.Found", "" + filelist.length)); if (!copyprevious) { if (!Const.isEmpty(realWildcard)) pattern = Pattern.compile(realWildcard); } // Get the files in the list... for (int i = 0; i < filelist.length && !parentJob.isStopped(); i++) { boolean getIt = true; // First see if the file matches the regular expression! if (!copyprevious && pattern != null) { Matcher matcher = pattern.matcher(filelist[i]); getIt = matcher.matches(); } if (getIt || list_previous_filenames.contains(filelist[i])) { if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobSFTP.Log.GettingFiles", filelist[i], realTargetDirectory)); String targetFilename = realTargetDirectory + Const.FILE_SEPARATOR + filelist[i]; sftpclient.get(targetFilename, filelist[i]); filesRetrieved++; if (isaddresult) { // Add to the result files... ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(targetFilename), parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTP.Log.FilenameAddedToResultFilenames", filelist[i])); } if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTP.Log.TransferedFile", filelist[i])); // Delete the file if this is needed! if (remove) { sftpclient.delete(filelist[i]); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTP.Log.DeletedFile", filelist[i])); } } } result.setResult(true); result.setNrFilesRetrieved(filesRetrieved); } catch (Exception e) { result.setNrErrors(1); log.logError(toString(), Messages.getString("JobSFTP.Error.GettingFiles", e.getMessage())); log.logError(toString(), Const.getStackTracker(e)); } finally { // close connection, if possible try { if (sftpclient != null) sftpclient.disconnect(); } catch (Exception e) { // just ignore this, makes no big difference } try { if (TargetFolder != null) { TargetFolder.close(); TargetFolder = null; } if (list_previous_filenames != null) list_previous_filenames = null; } catch (Exception e) { } } return result; }
From source file:com.panet.imeta.job.entries.unzip.JobEntryUnZip.java
public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) { LogWriter log = LogWriter.getInstance(); Result result = previousResult; result.setResult(false);//from ww w . j a va2 s . co m result.setEntryNr(1); List<RowMetaAndData> rows = result.getRows(); RowMetaAndData resultRow = null; String realFilenameSource = environmentSubstitute(zipFilename); String realWildcardSource = environmentSubstitute(wildcardSource); String realWildcard = environmentSubstitute(wildcard); String realWildcardExclude = environmentSubstitute(wildcardexclude); String realTargetdirectory = environmentSubstitute(targetdirectory); String realMovetodirectory = environmentSubstitute(movetodirectory); limitFiles = Const.toInt(environmentSubstitute(getLimit()), 10); NrErrors = 0; NrSuccess = 0; successConditionBroken = false; successConditionBrokenExit = false; if (isfromprevious) { if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Log.ArgFromPrevious.Found", (rows != null ? rows.size() : 0) + "")); if (rows.size() == 0) return result; } else { if (Const.isEmpty(zipFilename)) { // Zip file/folder is missing log.logError(toString(), Messages.getString("JobUnZip.No_ZipFile_Defined.Label")); return result; } } FileObject fileObject = null; FileObject targetdir = null; FileObject movetodir = null; try { // Let's make some checks here, before running job entry ... boolean exitjobentry = false; // Target folder targetdir = KettleVFS.getFileObject(realTargetdirectory); if (!targetdir.exists()) { if (createfolder) { targetdir.createFolder(); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Log.TargetFolderCreated", realTargetdirectory)); } else { log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.TargetFolderNotFound.Label")); exitjobentry = true; } } else { if (!(targetdir.getType() == FileType.FOLDER)) { log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.TargetFolderNotFolder.Label", realTargetdirectory)); exitjobentry = true; } else { if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.TargetFolderExists.Label", realTargetdirectory)); } } // If user want to move zip files after process // movetodirectory must be provided if (afterunzip == 2) { if (Const.isEmpty(movetodirectory)) { log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.MoveToDirectoryEmpty.Label")); exitjobentry = true; } else { movetodir = KettleVFS.getFileObject(realMovetodirectory); if (!(movetodir.exists()) || !(movetodir.getType() == FileType.FOLDER)) { if (createMoveToDirectory) { movetodir.createFolder(); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Log.MoveToFolderCreated", realMovetodirectory)); } else { log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.MoveToDirectoryNotExists.Label")); exitjobentry = true; } } } } // We found errors...now exit if (exitjobentry) return result; if (isfromprevious) { if (rows != null) // Copy the input row to the (command line) // arguments { for (int iteration = 0; iteration < rows.size() && !parentJob.isStopped(); iteration++) { if (successConditionBroken) { if (!successConditionBrokenExit) { log.logError(toString(), Messages.getString("JobUnZip.Error.SuccessConditionbroken", "" + NrErrors)); successConditionBrokenExit = true; } result.setEntryNr(NrErrors); return result; } resultRow = rows.get(iteration); // Get sourcefile/folder and wildcard realFilenameSource = resultRow.getString(0, null); realWildcardSource = resultRow.getString(1, null); fileObject = KettleVFS.getFileObject(realFilenameSource); if (fileObject.exists()) { processOneFile(log, result, parentJob, fileObject, realTargetdirectory, realWildcard, realWildcardExclude, movetodir, realMovetodirectory, realWildcardSource); } else { updateErrors(); log.logError(toString(), Messages.getString("JobUnZip.Error.CanNotFindFile", realFilenameSource)); } } } } else { fileObject = KettleVFS.getFileObject(realFilenameSource); if (!fileObject.exists()) { log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.ZipFile.NotExists.Label", realFilenameSource)); return result; } if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Zip_FileExists.Label", realFilenameSource)); if (Const.isEmpty(targetdirectory)) { log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.TargetFolderNotFound.Label")); return result; } processOneFile(log, result, parentJob, fileObject, realTargetdirectory, realWildcard, realWildcardExclude, movetodir, realMovetodirectory, realWildcardSource); } } catch (Exception e) { log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.ErrorUnzip.Label", realFilenameSource, e.getMessage())); updateErrors(); } finally { if (fileObject != null) { try { fileObject.close(); } catch (IOException ex) { } ; } if (targetdir != null) { try { targetdir.close(); } catch (IOException ex) { } ; } if (movetodir != null) { try { movetodir.close(); } catch (IOException ex) { } ; } } result.setNrErrors(NrErrors); result.setNrLinesWritten(NrSuccess); if (getSuccessStatus()) result.setResult(true); displayResults(log); return result; }