List of usage examples for org.apache.commons.vfs FileObject getChildren
public FileObject[] getChildren() throws FileSystemException;
From source file:org.josso.tooling.gshell.install.installer.JBossInstaller.java
public void installApplication(JOSSOArtifact artifact, boolean replace) throws InstallException { try {/*ww w. j av a2 s .c o m*/ // If the war is already expanded, copy it with a new name. FileObject srcFile = getFileSystemManager().resolveFile(artifact.getLocation()); // Is this the josso gateaway ? String name = artifact.getBaseName(); boolean isFolder = srcFile.getType().equals(FileType.FOLDER); if (artifact.getType().equals("war") && name.startsWith("josso-gateway-web")) { // INSTALL GWY String newName = "josso.war"; // Do we have to explode the war ? if (getTargetPlatform().isJOSSOWarExploded() && !isFolder) { installJar(srcFile, this.targetDeployDir, newName, true, replace); if (getTargetPlatform().getVersion().startsWith("6")) { // Under JBoss 6 remove commons logging JAR files which conflict with slf4j replacement FileObject webInfLibFolder = targetDeployDir.resolveFile(newName + "/WEB-INF/lib"); boolean exists = webInfLibFolder.exists(); if (exists) { FileObject[] sharedLibs = webInfLibFolder.getChildren(); for (int i = 0; i < sharedLibs.length; i++) { FileObject jarFile = sharedLibs[i]; if (!jarFile.getType().getName().equals(FileType.FILE.getName())) { // ignore folders continue; } if (jarFile.getName().getBaseName().startsWith("commons-logging") && jarFile.getName().getBaseName().endsWith(".jar")) { jarFile.delete(); } } } } } else { installFile(srcFile, this.targetDeployDir, replace); } return; } if ((getTargetPlatform().getVersion().startsWith("5") || getTargetPlatform().getVersion().startsWith("6")) && artifact.getType().equals("ear") && artifact.getBaseName().startsWith("josso-partner-jboss5")) { installFile(srcFile, this.targetDeployDir, replace); return; } else if (!(getTargetPlatform().getVersion().startsWith("5") || getTargetPlatform().getVersion().startsWith("6")) && artifact.getType().equals("ear") && artifact.getBaseName().startsWith("josso-partner-jboss-app")) { installFile(srcFile, this.targetDeployDir, replace); return; } log.debug("Skipping partner application : " + srcFile.getName().getFriendlyURI()); } catch (IOException e) { throw new InstallException(e.getMessage(), e); } }
From source file:org.josso.tooling.gshell.install.installer.VFSInstaller.java
public boolean removeOldJar(String fullJarName, FileObject destDir, boolean backup) { try {/* ww w.j a v a2 s . com*/ if (fullJarName.endsWith(".jar") && fullJarName.lastIndexOf("-") != -1) { String jarNameWithoutVersion = fullJarName.substring(0, fullJarName.lastIndexOf("-")); FileObject[] files = destDir.getChildren(); for (int i = 0; i < files.length; i++) { FileObject file = files[i]; if (!file.getType().getName().equals(FileType.FILE.getName())) { // ignore folders continue; } String fileName = file.getName().getBaseName(); if (fileName.startsWith(jarNameWithoutVersion) && fileName.endsWith(".jar") && fileName .substring(jarNameWithoutVersion.length(), jarNameWithoutVersion.length() + 2) .matches("-[0-9]|\\.j")) { if (backup) { // backup files in the same folder they're installed in backupFile(file, file.getParent()); } file.delete(); break; } } } } catch (Exception e) { getPrinter().printErrStatus("RemoveOldJar", e.getMessage()); return false; } return true; }
From source file:org.josso.tooling.gshell.install.installer.WasceInstaller.java
@Override public void performAdditionalTasks(FileObject libsDir) throws InstallException { try {/* w w w. j a v a2s. co m*/ // undeploy wasce tomcat6 module if exists FileObject tomcatModule = targetDir.resolveFile("repository/org/apache/geronimo/configs/tomcat6"); if (tomcatModule.exists()) { getPrinter().printMsg("Undeploying tomcat6 module"); int status = undeploy("tomcat6"); if (status == 0) { getPrinter().printOkStatus("Undeploy tomcat6 module", "Successful"); } else { getPrinter().printErrStatus("Undeploy tomcat6 module", "Error"); throw new InstallException("Error undeploying tomcat6 module!!!"); } } // undeploy old josso wasce agent if exists FileObject jossoWasceAgentModule = targetDir.resolveFile("repository/org/josso/josso-wasce-agent"); if (jossoWasceAgentModule.exists()) { getPrinter().printMsg("Undeploying old josso wasce agent"); int status = undeploy("josso-wasce-agent"); if (status == 0) { getPrinter().printOkStatus("Undeploy josso wasce agent", "Successful"); } else { getPrinter().printErrStatus("Undeploy josso wasce agent", "Error"); throw new InstallException("Error undeploying josso wasce agent!!!"); } } // install jars to wasce repository try { getPrinter().printMsg("Installing new jars to WASCE repository"); FileObject wasceRepo = targetDir.resolveFile("repository"); FileObject wasceRepoFolder = libsDir.resolveFile("repository"); wasceRepo.copyFrom(wasceRepoFolder, Selectors.SELECT_ALL); getPrinter().printOkStatus("Install new jars", "Successful"); } catch (FileSystemException e) { getPrinter().printErrStatus("Install new jars", "Error"); throw new InstallException("Error copying jars to wasce repository!!!"); } // deploy josso wasce agent getPrinter().printMsg("Deploying josso wasce agent"); FileObject jossoWasceCarFile = null; FileObject[] agentBins = libsDir.getChildren(); for (int i = 0; i < agentBins.length; i++) { FileObject agentBin = agentBins[i]; if (agentBin.getName().getBaseName().startsWith("josso-wasce")) { jossoWasceCarFile = agentBin; break; } } if (jossoWasceCarFile == null) { throw new InstallException("Josso wasce agent car file doesn't exist!!!"); } int status = installPlugin(jossoWasceCarFile); if (status == 0) { getPrinter().printOkStatus("Install josso wasce agent", "Successful"); } else { getPrinter().printErrStatus("Install josso wasce agent", "Error"); throw new InstallException("Error installing josso wasce agent!!!"); } // start stopped services getPrinter().printMsg("Starting tomcat related services"); status = startTomcatRelatedServices(); if (status == 0) { getPrinter().printOkStatus("Start tomcat related services", "Successful"); } else { getPrinter().printErrStatus("Start tomcat related services", "Error"); throw new InstallException("Error starting tomcat related services!!!"); } } catch (IOException e) { throw new InstallException(e.getMessage(), e); } }
From source file:org.josso.tooling.gshell.install.installer.WeblogicInstaller.java
@Override public void installComponentFromSrc(JOSSOArtifact artifact, boolean replace) throws InstallException { try {/* w w w. ja va2s .c o m*/ if (!artifact.getBaseName().contains(this.wlVersionStr)) return; // Prepare paths FileObject homeDir = getFileSystemManager().resolveFile(System.getProperty("josso-gsh.home")); FileObject srcDir = homeDir .resolveFile("dist/agents/src/josso-weblogic" + wlVersionStr + "-agent-mbeans-src"); FileObject jossoLibDir = homeDir.resolveFile("dist/agents/bin"); FileObject thrdPartyLibDir = jossoLibDir.resolveFile("3rdparty"); FileObject descriptorFile = srcDir.resolveFile( "org/josso/wls" + wlVersionStr + "/agent/mbeans/JOSSOAuthenticatorProviderImpl.xml"); FileObject mbeanFile = this.targetJOSSOMBeansDir .resolveFile("josso-weblogic" + wlVersionStr + "-agent-mbeans.jar"); FileObject javaDir = getFileSystemManager().resolveFile(System.getProperty("java.home") + "/../"); FileObject javaToolsFile = javaDir.resolveFile("lib/tools.jar"); FileObject javaFile = javaDir.resolveFile("bin/java"); getPrinter().printMsg("Using JAVA JDK at " + getLocalFilePath(javaDir)); if (!javaDir.exists()) { getPrinter().printActionErrStatus("Generate", "WL MBeans Descriptors", "JAVA JDK is required : " + getLocalFilePath(javaDir)); throw new InstallException("JAVA JDK is required for WL : " + getLocalFilePath(javaDir)); } if (!javaToolsFile.exists()) { getPrinter().printActionErrStatus("Generate", "WL MBeans Descriptors", "JAVA JDK is required : " + getLocalFilePath(javaToolsFile)); throw new InstallException("JAVA JDK is required for WL : " + getLocalFilePath(javaToolsFile)); } if (!javaToolsFile.exists()) { getPrinter().printActionErrStatus("Generate", "WL MBeans Descriptors", "JAVA JDK is required : " + getLocalFilePath(javaToolsFile)); throw new InstallException("JAVA JDK is required for WL : " + getLocalFilePath(javaToolsFile)); } // Java CMD and Class path : String javaCmd = getLocalFilePath(javaFile); String classpath = ""; String pathSeparator = ""; // JOSSO Jars for (FileObject child : jossoLibDir.getChildren()) { if (!child.getName().getBaseName().endsWith(".jar")) continue; classpath += pathSeparator + getLocalFilePath(child); pathSeparator = System.getProperty("path.separator"); } // JOSSO 3rd party Jars for (FileObject child : thrdPartyLibDir.getChildren()) { if (!child.getName().getBaseName().endsWith(".jar")) continue; classpath += pathSeparator + getLocalFilePath(child); pathSeparator = System.getProperty("path.separator"); } for (FileObject child : this.targetDir.resolveFile("server/lib").getChildren()) { if (!child.getName().getBaseName().endsWith(".jar")) continue; classpath += pathSeparator + getLocalFilePath(child); pathSeparator = System.getProperty("path.separator"); } classpath += pathSeparator + getLocalFilePath(javaToolsFile); pathSeparator = System.getProperty("path.separator"); for (FileObject child : javaDir.resolveFile("jre/lib").getChildren()) { classpath += pathSeparator + getLocalFilePath(child); pathSeparator = System.getProperty("path.separator"); } // ---------------------------------------------------------------- // 1. Create the MBean Descriptor Files // ---------------------------------------------------------------- { /* <argument>-Dfiles=${basedir}/target/generated-sources</argument> <argument>-DMDF=${project.build.directory}/generated-sources/org/josso/wls92/agent/mbeans/JOSSOAuthenticatorProviderImpl.xml</argument> <argument>-DtargetNameSpace=urn:org:josso:wls92:agent:mbeans</argument> <argument>-DpreserveStubs=false</argument> <argument>-DcreateStubs=true</argument> <argument>-classpath</argument> <classpath/> <argument>weblogic.management.commo.WebLogicMBeanMaker</argument> */ ProcessBuilder generateMBeanDescriptorProcessBuilder = new ProcessBuilder(javaCmd, "-Dfiles=" + getLocalFilePath(srcDir), "-DMDF=" + getLocalFilePath(descriptorFile), "-DtargetNameSpace=urn:org:josso:wls" + wlVersionStr + ":agent:mbeans", "-DpreserveStubs=false", "-DcreateStubs=true", "-classpath", classpath, "weblogic.management.commo.WebLogicMBeanMaker"); log.info("Executing: " + generateMBeanDescriptorProcessBuilder.command()); Process generateMBeanDescriptorProcess = generateMBeanDescriptorProcessBuilder.start(); PumpStreamHandler generateMBeanHandler = new PumpStreamHandler(getPrinter().getIo().inputStream, getPrinter().getIo().outputStream, getPrinter().getIo().errorStream); generateMBeanHandler.attach(generateMBeanDescriptorProcess); generateMBeanHandler.start(); log.debug("Waiting for process to exit..."); int statusDescr = generateMBeanDescriptorProcess.waitFor(); log.info("Process exited w/status: " + statusDescr); generateMBeanHandler.stop(); getPrinter().printActionOkStatus("Generate", "WL MBeans Descriptors", ""); } // ---------------------------------------------------------------- // 2. Create the MBean JAR File // ---------------------------------------------------------------- { /* <argument>-Dfiles=${project.build.directory}/generated-sources</argument> <argument>-DMJF=${project.build.directory}/josso-weblogic92-agent-mbeans-${pom.version}.jar</argument> <argument>-DpreserveStubs=false</argument> <argument>-DcreateStubs=true</argument> <argument>-classpath</argument> <classpath/> <argument>weblogic.management.commo.WebLogicMBeanMaker</argument> */ ProcessBuilder generateMBeanJarProcessBuilder = new ProcessBuilder(javaCmd, "-Dfiles=" + getLocalFilePath(srcDir), "-DMJF=" + getLocalFilePath(mbeanFile), "-DpreserveStubs=false", "-DcreateStubs=true", "-classpath", classpath, "weblogic.management.commo.WebLogicMBeanMaker"); log.info("Executing: " + generateMBeanJarProcessBuilder.command()); Process generateMBeanJarProcess = generateMBeanJarProcessBuilder.start(); PumpStreamHandler generateMBeanJarHandler = new PumpStreamHandler(getPrinter().getIo().inputStream, getPrinter().getIo().outputStream, getPrinter().getIo().errorStream); generateMBeanJarHandler.attach(generateMBeanJarProcess); generateMBeanJarHandler.start(); log.debug("Waiting for process to exit..."); int statusJar = generateMBeanJarProcess.waitFor(); log.info("Process exited w/status: " + statusJar); generateMBeanJarHandler.stop(); getPrinter().printActionOkStatus("Generate", "WL MBeans JAR", getLocalFilePath(mbeanFile)); } } catch (Exception e) { getPrinter().printActionErrStatus("Generate", "WL MBeans", e.getMessage()); throw new InstallException("Cannot generate WL MBeans Descriptors : " + e.getMessage(), e); } // 2. Create the MBean JAR File // 3. Install the file in the target platform // We need to create WL Mbeans using MBean Maker! }
From source file:org.org.eclipse.core.utils.platform.tools.ArchivesToolBox.java
private static void decompressFileObjectTo(FileObject fileObject, File targetFolder, IWriteHinter writeHinter) throws IOException { for (FileObject child : fileObject.getChildren()) { String childName = child.getName().getBaseName(); FileType type = child.getType(); if (type.equals(FileType.FOLDER)) { String folderName = writeHinter.alterFolderName(targetFolder, childName); File folder = new File(targetFolder, folderName); if (!folder.exists()) { folder.mkdirs();//from w ww . j a v a 2 s .co m } decompressFileObjectTo(child, folder, writeHinter); } if (type.equals(FileType.FILE)) { String fileName = writeHinter.alterFileName(targetFolder, childName); File newFile = new File(targetFolder, fileName); newFile.createNewFile(); WriteMode writeMode = writeHinter.getFileWriteMode(newFile); if (writeMode != WriteMode.SKIP) { FileOutputStream fileOutputStream = new FileOutputStream(newFile); InputStream inputStream = child.getContent().getInputStream(); try { IOToolBox.inToOut(inputStream, fileOutputStream); } finally { inputStream.close(); fileOutputStream.close(); } } } } }
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]);//from w w w . j a v a 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.job.entries.sftpput.JobEntrySFTPPUT.java
public Result execute(Result previousResult, int nr) throws KettleException { Result result = previousResult; List<RowMetaAndData> rows = result.getRows(); result.setResult(false);//from w w w .java 2s . com if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.StartJobEntry")); } ArrayList<FileObject> myFileList = new ArrayList<FileObject>(); if (copyprevious) { if (rows.size() == 0) { if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.ArgsFromPreviousNothing")); } result.setResult(true); return result; } try { RowMetaAndData resultRow = null; // 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)) { FileObject file = KettleVFS.getFileObject(file_previous, this); if (!file.exists()) { logError(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FilefromPreviousNotFound", file_previous)); } else { myFileList.add(file); if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FilenameFromResult", file_previous)); } } } } } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobSFTPPUT.Error.ArgFromPrevious")); result.setNrErrors(1); // free resource myFileList = null; return result; } } if (copypreviousfiles) { List<ResultFile> resultFiles = result.getResultFilesList(); if (resultFiles == null || resultFiles.size() == 0) { if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.ArgsFromPreviousNothingFiles")); } result.setResult(true); return result; } try { for (Iterator<ResultFile> it = resultFiles.iterator(); it.hasNext() && !parentJob.isStopped();) { ResultFile resultFile = it.next(); FileObject file = resultFile.getFile(); if (file != null) { if (!file.exists()) { logError(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FilefromPreviousNotFound", file.toString())); } else { myFileList.add(file); if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FilenameFromResult", file.toString())); } } } } } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobSFTPPUT.Error.ArgFromPrevious")); result.setNrErrors(1); // free resource myFileList = null; return result; } } SFTPClient sftpclient = null; // String substitution.. String realServerName = environmentSubstitute(serverName); String realServerPort = environmentSubstitute(serverPort); String realUsername = environmentSubstitute(userName); String realPassword = Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(password)); String realSftpDirString = environmentSubstitute(sftpDirectory); String realWildcard = environmentSubstitute(wildcard); String realLocalDirectory = environmentSubstitute(localDirectory); String realKeyFilename = null; String realPassPhrase = null; // Destination folder (Move to) String realDestinationFolder = environmentSubstitute(getDestinationFolder()); try { // Let's perform some checks before starting if (getAfterFTPS() == AFTER_FTPSPUT_MOVE) { if (Const.isEmpty(realDestinationFolder)) { logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.DestinatFolderMissing")); result.setNrErrors(1); return result; } else { FileObject folder = null; try { folder = KettleVFS.getFileObject(realDestinationFolder, this); // Let's check if folder exists... if (!folder.exists()) { // Do we need to create it? if (createDestinationFolder) { folder.createFolder(); } else { logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.DestinatFolderNotExist", realDestinationFolder)); result.setNrErrors(1); return result; } } realDestinationFolder = KettleVFS.getFilename(folder); } catch (Exception e) { throw new KettleException(e); } finally { if (folder != null) { try { folder.close(); } catch (Exception e) { /* Ignore */ } } } } } if (isUseKeyFile()) { // We must have here a private keyfilename realKeyFilename = environmentSubstitute(getKeyFilename()); if (Const.isEmpty(realKeyFilename)) { // Error..Missing keyfile logError(BaseMessages.getString(PKG, "JobSFTP.Error.KeyFileMissing")); result.setNrErrors(1); return result; } if (!KettleVFS.fileExists(realKeyFilename)) { // Error.. can not reach keyfile logError(BaseMessages.getString(PKG, "JobSFTP.Error.KeyFileNotFound")); result.setNrErrors(1); return result; } realPassPhrase = environmentSubstitute(getKeyPassPhrase()); } // Create sftp client to host ... sftpclient = new SFTPClient(InetAddress.getByName(realServerName), Const.toInt(realServerPort, 22), realUsername, realKeyFilename, realPassPhrase); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.OpenedConnection", realServerName, "" + realServerPort, realUsername)); } // Set compression sftpclient.setCompression(getCompression()); // Set proxy? String realProxyHost = environmentSubstitute(getProxyHost()); if (!Const.isEmpty(realProxyHost)) { // Set proxy sftpclient.setProxy(realProxyHost, environmentSubstitute(getProxyPort()), environmentSubstitute(getProxyUsername()), environmentSubstitute(getProxyPassword()), getProxyType()); } // login to ftp host ... sftpclient.login(realPassword); // Don't show the password in the logs, it's not good for security audits // logDetailed("logged in using password "+realPassword); // Logging this seems a bad idea! Oh well. // move to spool dir ... if (!Const.isEmpty(realSftpDirString)) { boolean existfolder = sftpclient.folderExists(realSftpDirString); if (!existfolder) { if (!isCreateRemoteFolder()) { throw new KettleException(BaseMessages.getString(PKG, "JobSFTPPUT.Error.CanNotFindRemoteFolder", realSftpDirString)); } if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Error.CanNotFindRemoteFolder", realSftpDirString)); } // Let's create folder sftpclient.createFolder(realSftpDirString); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.RemoteFolderCreated", realSftpDirString)); } } sftpclient.chdir(realSftpDirString); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.ChangedDirectory", realSftpDirString)); } } // end if if (!copyprevious && !copypreviousfiles) { // Get all the files in the local directory... myFileList = new ArrayList<FileObject>(); FileObject localFiles = KettleVFS.getFileObject(realLocalDirectory, 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].getAbsolutePath()); myFileList.add(children[i]); } } // end for } } if (myFileList == null || myFileList.size() == 0) { if (isSuccessWhenNoFile()) { // Just warn user if (isBasic()) { logBasic(BaseMessages.getString(PKG, "JobSFTPPUT.Error.NoFileToSend")); } } else { // Fail logError(BaseMessages.getString(PKG, "JobSFTPPUT.Error.NoFileToSend")); result.setNrErrors(1); return result; } } if (log.isDetailed()) { logDetailed( BaseMessages.getString(PKG, "JobSFTPPUT.Log.RowsFromPreviousResult", myFileList.size())); } Pattern pattern = null; if (!copyprevious && !copypreviousfiles) { if (!Const.isEmpty(realWildcard)) { pattern = Pattern.compile(realWildcard); } } // Get the files in the list and execute sftp.put() for each file Iterator<FileObject> it = myFileList.iterator(); while (it.hasNext() && !parentJob.isStopped()) { FileObject myFile = it.next(); try { String localFilename = myFile.toString(); String destinationFilename = myFile.getName().getBaseName(); boolean getIt = true; // First see if the file matches the regular expression! if (pattern != null) { Matcher matcher = pattern.matcher(destinationFilename); getIt = matcher.matches(); } if (getIt) { if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobSFTPPUT.Log.PuttingFile", localFilename, realSftpDirString)); } sftpclient.put(myFile, destinationFilename); if (log.isDetailed()) { logDetailed( BaseMessages.getString(PKG, "JobSFTPPUT.Log.TransferedFile", localFilename)); } // We successfully uploaded the file // what's next ... switch (getAfterFTPS()) { case AFTER_FTPSPUT_DELETE: myFile.delete(); if (log.isDetailed()) { logDetailed( BaseMessages.getString(PKG, "JobSFTPPUT.Log.DeletedFile", localFilename)); } break; case AFTER_FTPSPUT_MOVE: FileObject destination = null; try { destination = KettleVFS.getFileObject(realDestinationFolder + Const.FILE_SEPARATOR + myFile.getName().getBaseName(), this); myFile.moveTo(destination); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FileMoved", myFile, destination)); } } finally { if (destination != null) { destination.close(); } } break; default: if (addFilenameResut) { // Add to the result files... ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, myFile, parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FilenameAddedToResultFilenames", localFilename)); } } break; } } } finally { if (myFile != null) { myFile.close(); } } } // end for result.setResult(true); // JKU: no idea if this is needed...! // result.setNrFilesRetrieved(filesRetrieved); } catch (Exception e) { result.setNrErrors(1); logError(BaseMessages.getString(PKG, "JobSFTPPUT.Exception", e.getMessage())); logError(Const.getStackTracker(e)); } finally { // close connection, if possible try { if (sftpclient != null) { sftpclient.disconnect(); } } catch (Exception e) { // just ignore this, makes no big difference } // end catch myFileList = null; } // end finally return result; }
From source file:org.pentaho.di.job.entries.ssh2put.JobEntrySSH2PUT.java
private List<FileObject> getFiles(String localfolder) throws KettleFileException { try {/*from w ww. ja v a2s . com*/ 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); } }
From source file:org.pentaho.di.job.entries.unzip.JobEntryUnZip.java
private boolean processOneFile(Result result, Job parentJob, FileObject fileObject, String realTargetdirectory, String realWildcard, String realWildcardExclude, FileObject movetodir, String realMovetodirectory, String realWildcardSource) { boolean retval = false; try {/*from w w w .ja va 2 s .c o m*/ if (fileObject.getType().equals(FileType.FILE)) { // We have to unzip one zip file if (!unzipFile(fileObject, realTargetdirectory, realWildcard, realWildcardExclude, result, parentJob, fileObject, movetodir, realMovetodirectory)) { updateErrors(); } else { updateSuccess(); } } else { // Folder..let's see wildcard FileObject[] children = fileObject.getChildren(); for (int i = 0; i < children.length && !parentJob.isStopped(); i++) { if (successConditionBroken) { if (!successConditionBrokenExit) { logError(BaseMessages.getString(PKG, "JobUnZip.Error.SuccessConditionbroken", "" + NrErrors)); successConditionBrokenExit = true; } return false; } // Get only file! if (!children[i].getType().equals(FileType.FOLDER)) { boolean unzip = true; String filename = children[i].getName().getPath(); Pattern patternSource = null; if (!Const.isEmpty(realWildcardSource)) { patternSource = Pattern.compile(realWildcardSource); } // First see if the file matches the regular expression! if (patternSource != null) { Matcher matcher = patternSource.matcher(filename); unzip = matcher.matches(); } if (unzip) { if (!unzipFile(children[i], realTargetdirectory, realWildcard, realWildcardExclude, result, parentJob, fileObject, movetodir, realMovetodirectory)) { updateErrors(); } else { updateSuccess(); } } } } } } catch (Exception e) { updateErrors(); logError(BaseMessages.getString(PKG, "JobUnZip.Error.Label", e.getMessage())); } finally { if (fileObject != null) { try { fileObject.close(); } catch (IOException ex) { /* Ignore */ } } } return retval; }
From source file:org.pentaho.di.job.entries.zipfile.JobEntryZipFile.java
public boolean processRowFile(Job parentJob, Result result, String realZipfilename, String realWildcard, String realWildcardExclude, String realSourceDirectoryOrFile, String realMovetodirectory, boolean createparentfolder) { boolean Fileexists = false; File tempFile = null;/*from w w w . j av a 2 s . c o m*/ File fileZip = null; boolean resultat = false; boolean renameOk = false; boolean orginExist = false; // Check if target file/folder exists! FileObject originFile = null; ZipInputStream zin = null; byte[] buffer = null; OutputStream dest = null; BufferedOutputStream buff = null; ZipOutputStream out = null; ZipEntry entry = null; String localSourceFilename = realSourceDirectoryOrFile; try { originFile = KettleVFS.getFileObject(realSourceDirectoryOrFile, this); localSourceFilename = KettleVFS.getFilename(originFile); orginExist = originFile.exists(); } catch (Exception e) { // Ignore errors } finally { if (originFile != null) { try { originFile.close(); } catch (IOException ex) { logError("Error closing file '" + originFile.toString() + "'", ex); } } } String localrealZipfilename = realZipfilename; if (realZipfilename != null && orginExist) { FileObject fileObject = null; try { fileObject = KettleVFS.getFileObject(localrealZipfilename, this); localrealZipfilename = KettleVFS.getFilename(fileObject); // Check if Zip File exists if (fileObject.exists()) { Fileexists = true; if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileExists1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileExists2.Label")); } } // Let's see if we need to create parent folder of destination zip filename if (createparentfolder) { createParentFolder(localrealZipfilename); } // Let's start the process now if (ifZipFileExists == 3 && Fileexists) { // the zip file exists and user want to Fail resultat = false; } else if (ifZipFileExists == 2 && Fileexists) { // the zip file exists and user want to do nothing if (addFileToResult) { // Add file to result files name ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileObject, parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); } resultat = true; } else if (afterZip == 2 && realMovetodirectory == null) { // After Zip, Move files..User must give a destination Folder resultat = false; logError( BaseMessages.getString(PKG, "JobZipFiles.AfterZip_No_DestinationFolder_Defined.Label")); } else { // After Zip, Move files..User must give a destination Folder // Let's see if we deal with file or folder FileObject[] fileList = null; FileObject sourceFileOrFolder = KettleVFS.getFileObject(localSourceFilename); boolean isSourceDirectory = sourceFileOrFolder.getType().equals(FileType.FOLDER); final Pattern pattern; final Pattern patternexclude; if (isSourceDirectory) { // Let's prepare the pattern matcher for performance reasons. // We only do this if the target is a folder ! // if (!Const.isEmpty(realWildcard)) { pattern = Pattern.compile(realWildcard); } else { pattern = null; } if (!Const.isEmpty(realWildcardExclude)) { patternexclude = Pattern.compile(realWildcardExclude); } else { patternexclude = null; } // Target is a directory // Get all the files in the directory... // if (includingSubFolders) { fileList = sourceFileOrFolder.findFiles(new FileSelector() { public boolean traverseDescendents(FileSelectInfo fileInfo) throws Exception { return true; } public boolean includeFile(FileSelectInfo fileInfo) throws Exception { boolean include; // Only include files in the sub-folders... // When we include sub-folders we match the whole filename, not just the base-name // if (fileInfo.getFile().getType().equals(FileType.FILE)) { include = true; if (pattern != null) { String name = fileInfo.getFile().getName().getPath(); include = pattern.matcher(name).matches(); } if (include && patternexclude != null) { String name = fileInfo.getFile().getName().getPath(); include = !pattern.matcher(name).matches(); } } else { include = false; } return include; } }); } else { fileList = sourceFileOrFolder.getChildren(); } } else { pattern = null; patternexclude = null; // Target is a file fileList = new FileObject[] { sourceFileOrFolder }; } if (fileList.length == 0) { resultat = false; logError(BaseMessages.getString(PKG, "JobZipFiles.Log.FolderIsEmpty", localSourceFilename)); } else if (!checkContainsFile(localSourceFilename, fileList, isSourceDirectory)) { resultat = false; logError(BaseMessages.getString(PKG, "JobZipFiles.Log.NoFilesInFolder", localSourceFilename)); } else { if (ifZipFileExists == 0 && Fileexists) { // the zip file exists and user want to create new one with unique name // Format Date // do we have already a .zip at the end? if (localrealZipfilename.toLowerCase().endsWith(".zip")) { // strip this off localrealZipfilename = localrealZipfilename.substring(0, localrealZipfilename.length() - 4); } localrealZipfilename += "_" + StringUtil.getFormattedDateTimeNow(true) + ".zip"; if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileNameChange1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileNameChange1.Label")); } } else if (ifZipFileExists == 1 && Fileexists) { // the zip file exists and user want to append // get a temp file fileZip = getFile(localrealZipfilename); tempFile = File.createTempFile(fileZip.getName(), null); // delete it, otherwise we cannot rename existing zip to it. tempFile.delete(); renameOk = fileZip.renameTo(tempFile); if (!renameOk) { logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp1.Label") + fileZip.getAbsolutePath() + BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp2.Label") + tempFile.getAbsolutePath() + BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp3.Label")); } if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileAppend1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileAppend2.Label")); } } if (log.isDetailed()) { logDetailed( BaseMessages.getString(PKG, "JobZipFiles.Files_Found1.Label") + fileList.length + BaseMessages.getString(PKG, "JobZipFiles.Files_Found2.Label") + localSourceFilename + BaseMessages.getString(PKG, "JobZipFiles.Files_Found3.Label")); } // Prepare Zip File buffer = new byte[18024]; dest = KettleVFS.getOutputStream(localrealZipfilename, false); buff = new BufferedOutputStream(dest); out = new ZipOutputStream(buff); HashSet<String> fileSet = new HashSet<String>(); if (renameOk) { // User want to append files to existing Zip file // The idea is to rename the existing zip file to a temporary file // and then adds all entries in the existing zip along with the new files, // excluding the zip entries that have the same name as one of the new files. zin = new ZipInputStream(new FileInputStream(tempFile)); entry = zin.getNextEntry(); while (entry != null) { String name = entry.getName(); if (!fileSet.contains(name)) { // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(name)); // Transfer bytes from the ZIP file to the output file int len; while ((len = zin.read(buffer)) > 0) { out.write(buffer, 0, len); } fileSet.add(name); } entry = zin.getNextEntry(); } // Close the streams zin.close(); } // Set the method out.setMethod(ZipOutputStream.DEFLATED); // Set the compression level if (compressionRate == 0) { out.setLevel(Deflater.NO_COMPRESSION); } else if (compressionRate == 1) { out.setLevel(Deflater.DEFAULT_COMPRESSION); } if (compressionRate == 2) { out.setLevel(Deflater.BEST_COMPRESSION); } if (compressionRate == 3) { out.setLevel(Deflater.BEST_SPEED); } // Specify Zipped files (After that we will move,delete them...) FileObject[] zippedFiles = new FileObject[fileList.length]; int fileNum = 0; // Get the files in the list... for (int i = 0; i < fileList.length && !parentJob.isStopped(); i++) { boolean getIt = true; boolean getItexclude = false; // First see if the file matches the regular expression! // ..only if target is a folder ! if (isSourceDirectory) { // If we include sub-folders, we match on the whole name, not just the basename // String filename; if (includingSubFolders) { filename = fileList[i].getName().getPath(); } else { filename = fileList[i].getName().getBaseName(); } if (pattern != null) { // Matches the base name of the file (backward compatible!) // Matcher matcher = pattern.matcher(filename); getIt = matcher.matches(); } if (patternexclude != null) { Matcher matcherexclude = patternexclude.matcher(filename); getItexclude = matcherexclude.matches(); } } // Get processing File String targetFilename = KettleVFS.getFilename(fileList[i]); if (sourceFileOrFolder.getType().equals(FileType.FILE)) { targetFilename = localSourceFilename; } FileObject file = KettleVFS.getFileObject(targetFilename); boolean isTargetDirectory = file.exists() && file.getType().equals(FileType.FOLDER); if (getIt && !getItexclude && !isTargetDirectory && !fileSet.contains(targetFilename)) { // We can add the file to the Zip Archive if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip1.Label") + fileList[i] + BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip2.Label") + localSourceFilename + BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip3.Label")); } // Associate a file input stream for the current file InputStream in = KettleVFS.getInputStream(file); // Add ZIP entry to output stream. // String relativeName; String fullName = fileList[i].getName().getPath(); String basePath = sourceFileOrFolder.getName().getPath(); if (isSourceDirectory) { if (fullName.startsWith(basePath)) { relativeName = fullName.substring(basePath.length() + 1); } else { relativeName = fullName; } } else if (isFromPrevious) { int depth = determineDepth(environmentSubstitute(storedSourcePathDepth)); relativeName = determineZipfilenameForDepth(fullName, depth); } else { relativeName = fileList[i].getName().getBaseName(); } out.putNextEntry(new ZipEntry(relativeName)); int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } out.flush(); out.closeEntry(); // Close the current file input stream in.close(); // Get Zipped File zippedFiles[fileNum] = fileList[i]; fileNum = fileNum + 1; } } // Close the ZipOutPutStream out.close(); buff.close(); dest.close(); if (log.isBasic()) { logBasic(BaseMessages.getString(PKG, "JobZipFiles.Log.TotalZippedFiles", "" + zippedFiles.length)); } // Delete Temp File if (tempFile != null) { tempFile.delete(); } // -----Get the list of Zipped Files and Move or Delete Them if (afterZip == 1 || afterZip == 2) { // iterate through the array of Zipped files for (int i = 0; i < zippedFiles.length; i++) { if (zippedFiles[i] != null) { // Delete, Move File FileObject fileObjectd = zippedFiles[i]; if (!isSourceDirectory) { fileObjectd = KettleVFS.getFileObject(localSourceFilename); } // Here we can move, delete files if (afterZip == 1) { // Delete File boolean deleted = fileObjectd.delete(); if (!deleted) { resultat = false; logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_Delete_File1.Label") + localSourceFilename + Const.FILE_SEPARATOR + zippedFiles[i] + BaseMessages .getString(PKG, "JobZipFiles.Cant_Delete_File2.Label")); } // File deleted if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.File_Deleted1.Label") + localSourceFilename + Const.FILE_SEPARATOR + zippedFiles[i] + BaseMessages.getString(PKG, "JobZipFiles.File_Deleted2.Label")); } } else if (afterZip == 2) { // Move File FileObject fileObjectm = null; try { fileObjectm = KettleVFS.getFileObject(realMovetodirectory + Const.FILE_SEPARATOR + fileObjectd.getName().getBaseName()); fileObjectd.moveTo(fileObjectm); } catch (IOException e) { logError( BaseMessages.getString(PKG, "JobZipFiles.Cant_Move_File1.Label") + zippedFiles[i] + BaseMessages.getString(PKG, "JobZipFiles.Cant_Move_File2.Label") + e.getMessage()); resultat = false; } finally { try { if (fileObjectm != null) { fileObjectm.close(); } } catch (Exception e) { if (fileObjectm != null) { logError("Error closing file '" + fileObjectm.toString() + "'", e); } } } // File moved if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.File_Moved1.Label") + zippedFiles[i] + BaseMessages.getString(PKG, "JobZipFiles.File_Moved2.Label")); } } } } } if (addFileToResult) { // Add file to result files name ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileObject, parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); } resultat = true; } } } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_CreateZipFile1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.Cant_CreateZipFile2.Label"), e); resultat = false; } finally { if (fileObject != null) { try { fileObject.close(); fileObject = null; } catch (IOException ex) { logError("Error closing file '" + fileObject.toString() + "'", ex); } } try { if (out != null) { out.close(); } if (buff != null) { buff.close(); } if (dest != null) { dest.close(); } if (zin != null) { zin.close(); } if (entry != null) { entry = null; } } catch (IOException ex) { logError("Error closing zip file entry for file '" + originFile.toString() + "'", ex); } } } else { resultat = true; if (localrealZipfilename == null) { logError(BaseMessages.getString(PKG, "JobZipFiles.No_ZipFile_Defined.Label")); } if (!orginExist) { logError(BaseMessages.getString(PKG, "JobZipFiles.No_FolderCible_Defined.Label", localSourceFilename)); } } // return a verifier return resultat; }