List of usage examples for org.apache.commons.vfs2 FileObject createFolder
void createFolder() throws FileSystemException;
From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignSource.java
public void createDesign(String designName) throws WGNotSupportedException, WGADesignCreationException { try {/* ww w. ja v a 2 s. c om*/ // Create the folder FileObject designDir = _dir.resolveFile(designName); if (designDir.exists()) { throw new WGADesignCreationException("A directory of name '" + designName + "' already exists"); } designDir.createFolder(); // Nuffin else to do .... initial deploy will do the rest } catch (Exception e) { throw new WGADesignCreationException("Exception creating file system design '" + designName + "'", e); } }
From source file:com.stratuscom.harvester.deployer.StarterServiceDeployer.java
void createWorkDirectoryFor(ApplicationEnvironment env) throws IOException { FileObject managerDir = fileUtility.getWorkingDirectory(env.getApplicationManagerName()); FileObject workingDir = managerDir.resolveFile(env.getServiceName()); if (!workingDir.exists()) { workingDir.createFolder(); }//from w ww.java 2s .co m File workingDirFile = new File(workingDir.getName().getPath()); env.setWorkingDirectory(workingDirFile); }
From source file:hadoopInstaller.installation.HostInstallation.java
private FileObject sftpConnect() throws InstallationError { FileObject remoteDirectory; log.debug("HostInstallation.SFTP.Connect.Start", //$NON-NLS-1$ host.getHostname(), host.getUsername()); try {//from www . j ava 2 s . c o m String uri = new URI("sftp", host.getUsername(), //$NON-NLS-1$ host.getHostname(), host.getPort(), host.getInstallationDirectory(), null, null).toString(); remoteDirectory = VFS.getManager().resolveFile(uri, installer.getSftpOptions()); } catch (URISyntaxException | FileSystemException e) { throw new InstallationError(e, "HostInstallation.SFTP.Connect.End", //$NON-NLS-1$ host.getHostname()); } try { if (!remoteDirectory.exists()) { remoteDirectory.createFolder(); } } catch (FileSystemException e) { throw new InstallationError(e, "HostInstallation.CouldNotCreate", //$NON-NLS-1$ remoteDirectory.getName().getURI(), host.getUsername()); } log.debug("HostInstallation.SFTP.Connect.Success", //$NON-NLS-1$ host.getHostname(), host.getUsername()); return remoteDirectory; }
From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignProvider.java
private static boolean performChange(ChangedDocument changedDocument, FileSystemDesignProvider originalDesignProvider, OverlayStatus status, String targetEncoding, FileObject baseFolder, Logger log, DesignFileValidator validator) throws FileSystemException, NoSuchAlgorithmException, UnsupportedEncodingException, IOException, WGDesignSyncException { boolean conflictFileCreated = false; log.info("Updating overlay resource " + changedDocument.getDocumentKey()); // Find files which represent the document in source and target FileObject sourceDocFile = originalDesignProvider.getBaseFolder() .resolveFile(changedDocument.getSourceFilePath()); FileObject targetDocFile = baseFolder.resolveFile(changedDocument.getTargetFilePath()); // Collect files to copy and delete Map<FileObject, FileObject> filesToCopy = new HashMap<FileObject, FileObject>(); List<FileObject> filesToDelete = new ArrayList<FileObject>(); // Collect for file containers: Must traverse container content if (changedDocument.getDocumentKey().getDocType() == WGDocument.TYPE_FILECONTAINER) { if (changedDocument.getChangeType() == ChangeType.NEW) { targetDocFile.createFolder(); }// w w w . ja v a 2 s. com // Copy all files in container from the source to the target for (FileObject sourceFile : sourceDocFile.getChildren()) { if (sourceFile.getType().equals(FileType.FILE)) { if (!isValidDesignFile(sourceFile, validator)) { continue; } filesToCopy.put(sourceFile, targetDocFile.resolveFile(sourceFile.getName().getBaseName())); } } // Delete all files in target that were deployed with previous base version but are deleted in current base version for (FileObject targetFile : targetDocFile.getChildren()) { if (targetFile.getType().equals(FileType.FILE)) { if (!isValidDesignFile(targetFile, validator)) { continue; } FileObject sourceFile = sourceDocFile.resolveFile(targetFile.getName().getBaseName()); if (sourceFile.exists()) { continue; } // Only delete those that were deployed with previous base version and have unaltered content String resourcePath = baseFolder.getName().getRelativeName(targetFile.getName()); ResourceData resourceData = status.getOverlayData().getOverlayResources().get(resourcePath); if (resourceData != null) { String hash = MD5HashingInputStream.getStreamHash(targetFile.getContent().getInputStream()); if (resourceData.getMd5Hash().equals(hash)) { filesToDelete.add(targetFile); } } } } } // Collect for anything else else { filesToCopy.put(sourceDocFile, targetDocFile); } // Copy files for (Map.Entry<FileObject, FileObject> files : filesToCopy.entrySet()) { FileObject sourceFile = files.getKey(); FileObject targetFile = files.getValue(); String resourcePath = baseFolder.getName().getRelativeName(targetFile.getName()); if (changedDocument.getChangeType() == ChangeType.CONFLICT) { // Do a test if the current file is conflicting. If so we write to a conflict file instead InputStream in = new BufferedInputStream(sourceFile.getContent().getInputStream()); String currentHash = MD5HashingInputStream.getStreamHash(in); ResourceData deployedHash = status.getOverlayData().getOverlayResources().get(resourcePath); boolean skipConflict = false; // Conflict on file container: A single file might just be missing in the target. We can safely copy that to the target without treating as conflict (#00002440) if (deployedHash == null && changedDocument.getDocumentKey().getDocType() == WGDocument.TYPE_FILECONTAINER && !targetFile.exists()) { skipConflict = true; } if (!skipConflict && (deployedHash == null || !deployedHash.getMd5Hash().equals(currentHash))) { targetFile = createConflictFile(targetFile); conflictFileCreated = true; log.warn("Modified overlay resource " + resourcePath + " is updated in base design. We write the updated base version to conflict file for manual resolution: " + baseFolder.getName().getRelativeName(targetFile.getName())); } } // Write file InputStream in = new BufferedInputStream(sourceFile.getContent().getInputStream()); MD5HashingOutputStream out = new MD5HashingOutputStream( new BufferedOutputStream(targetFile.getContent().getOutputStream(false))); // Update resource data resourceInToOut(in, originalDesignProvider.getFileEncoding(), out, targetEncoding); OverlayData.ResourceData resourceData = new OverlayData.ResourceData(); resourceData.setMd5Hash(out.getHash()); status.getOverlayData().setOverlayResource(resourcePath, resourceData); } // Delete files for (FileObject fileToDelete : filesToDelete) { String resourcePath = baseFolder.getName().getRelativeName(fileToDelete.getName()); fileToDelete.delete(); status.getOverlayData().removeOverlayResource(resourcePath); } return conflictFileCreated; }
From source file:de.innovationgate.wgpublisher.services.WGACoreServicesImpl.java
public void mkFSDesignDir(RemoteSession session, String path) throws WGAServiceException { if (!isAdminServiceEnabled()) { throw new WGAServiceException("Administrative services are disabled"); }/*from ww w . j ava 2 s . com*/ if (!isAdminSession(session)) { throw new WGAServiceException("You need an administrative login to access this service."); } WGADesignSource source = _core.getDesignManager().getDesignSources() .get(WGAConfiguration.UID_DESIGNSOURCE_FILESYSTEM); if (source instanceof FileSystemDesignSource) { FileSystemDesignSource fsSource = (FileSystemDesignSource) source; try { fsSource.getDir().refresh(); FileObject resource = fsSource.getDir().resolveFile(path); String basePath = fsSource.getDir().getURL().getPath(); String resourcePath = resource.getURL().getPath(); if (!resourcePath.startsWith(basePath)) { throw new WGAServiceException( new IllegalArgumentException("Illegal design resource path '" + path + "'.")); } resource.createFolder(); } catch (FileSystemException e) { throw new WGAServiceException("Creating of FSDesignDir '" + path + "' failed.", e); } } }
From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignManager.java
protected FileObject initialDeployFileContainer(WGFileContainer con) throws IOException, InstantiationException, IllegalAccessException, WGAPIException, WGDesignSyncException { // Create container folder FileObject containerFolder = getFilesFolder().resolveFile(con.getName()); _log.info("Creating file container folder " + getRelativePath(containerFolder)); try {//from w ww . j a va 2s . c o m containerFolder.createFolder(); } catch (FileSystemException e) { throw new WGInitialDeployException( "Could not create file container folder '" + containerFolder.getName().getPathDecoded() + "'", e); } // Create metadata file FCMetadata metaData = new FCMetadata(con); FileObject metadataFile = containerFolder .resolveFile(AbstractDesignFile.FILECONTAINER_METADATA_FILENAME + DesignDirectory.SUFFIX_METADATA); _log.info("Creating file container metadata file " + getRelativePath(metadataFile)); Writer writer = createWriter(metadataFile); writer.write(_xstream.toXML(metaData.getInfo())); writer.close(); // Create contained files Iterator fileNames = con.getFileNames().iterator(); String fileName; FileObject file; while (fileNames.hasNext()) { fileName = (String) fileNames.next(); InputStream in = con.getFileData(fileName); file = containerFolder.resolveFile(fileName); _log.info("Creating file container file " + getRelativePath(file)); try { file.createFile(); } catch (FileSystemException e) { throw new WGInitialDeployException( "Could not create container file '" + getRelativePath(file) + "'", e); } OutputStream out = file.getContent().getOutputStream(); WGUtils.inToOut(in, out, 2048); in.close(); out.close(); } return containerFolder; }
From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignProvider.java
public WGDocumentCore createDesignDocument(int type, String name, String mediaKey) throws WGAuthorisationException, WGCreationException { try {//www .j a v a2 s. c o m switch (type) { case WGDocument.TYPE_TML: { FileObject newTml = getTmlFolder() .resolveFile(mediaKey.toLowerCase() + "/" + name.replace(":", "/") + ".tml"); if (newTml.exists()) { throw new WGCreationException( "Document already exists: " + (new WGDocumentKey(type, name, mediaKey)).toString()); } newTml.createFile(); break; } case WGDocument.TYPE_CSSJS: { String suffix = DesignDirectory.getScriptInformation(mediaKey).getSuffix(); FileObject newScript = getScriptTypeFolder(mediaKey) .resolveFile(mediaKey.toLowerCase() + "/" + name.replace(":", "/") + suffix); if (newScript.exists()) { throw new WGCreationException( "Document already exists: " + (new WGDocumentKey(type, name, mediaKey)).toString()); } newScript.createFile(); break; } case WGDocument.TYPE_FILECONTAINER: { FileObject newFC = getFilesFolder().resolveFile(name.replace(":", "/")); if (newFC.exists()) { throw new WGCreationException( "Document already exists: " + (new WGDocumentKey(type, name, mediaKey)).toString()); } newFC.createFolder(); break; } } clearCache(); return getDesignObject(type, name, mediaKey); } catch (Exception e) { throw new WGCreationException( "Exception creating design document " + (new WGDocumentKey(type, name, mediaKey)).toString(), e); } }
From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignManager.java
protected FileObject initialDeployTMLModule(WGTMLModule mod) throws IOException, InstantiationException, IllegalAccessException, WGAPIException, WGDesignSyncException { // Find/create media key folder FileObject mediaKeyFolder = getTmlFolder().resolveFile(mod.getMediaKey()); if (!mediaKeyFolder.exists()) { _log.info("Creating media key folder " + getRelativePath(mediaKeyFolder)); try {//from w w w .j a va 2 s. co m mediaKeyFolder.createFolder(); } catch (FileSystemException e) { throw new WGInitialDeployException( "Could not create media key folder '" + mediaKeyFolder.getName().getPathDecoded() + "'"); } } // Eventually create intermediate directories List<String> path = WGUtils.deserializeCollection(mod.getName(), ":", true); String localName = (String) path.get(path.size() - 1); FileObject currentDir = mediaKeyFolder; for (int i = 0; i < path.size() - 1; i++) { currentDir = currentDir.resolveFile((String) path.get(i)); if (!currentDir.exists()) { _log.info("Creating tml category directory " + getRelativePath(currentDir)); try { currentDir.createFolder(); } catch (FileSystemException e) { throw new WGInitialDeployException( "Could not create tml category folder '" + getRelativePath(currentDir) + "'", e); } } else if (!currentDir.getType().equals(FileType.FOLDER)) { throw new WGInitialDeployException( "Cannot deploy " + mod.getDocumentKey() + " to sync folder because the directory name '" + path.get(i) + "' is already used by another file"); } } // Create code file FileObject tmlCodeFile = currentDir.resolveFile(localName + DesignDirectory.SUFFIX_TML); _log.info("Creating tml module file " + getRelativePath(tmlCodeFile)); try { tmlCodeFile.createFile(); } catch (FileSystemException e) { throw new WGInitialDeployException( "Could not create tml code file '" + getRelativePath(tmlCodeFile) + "'"); } Writer writer = createWriter(tmlCodeFile); writer.write(mod.getCode()); writer.close(); // Create metadata file TMLMetadata metaData = new TMLMetadata(mod); FileObject metadataDir = currentDir.resolveFile(DesignDirectory.NAME_METADATADIR); if (!metadataDir.exists()) { _log.info("Creating tml metadata directory " + getRelativePath(metadataDir)); try { metadataDir.createFolder(); } catch (FileSystemException e) { throw new WGInitialDeployException( "Could not create metadata folder '" + metadataDir.getName().getPathDecoded() + "'"); } } FileObject metadataFile = metadataDir.resolveFile(localName + DesignDirectory.SUFFIX_METADATA); _log.info("Creating tml metadata file " + getRelativePath(metadataFile)); writer = createWriter(metadataFile); writer.write(_xstream.toXML(metaData.getInfo())); writer.close(); return tmlCodeFile; }
From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignManager.java
protected FileObject initialDeployScriptModule(WGCSSJSModule script) throws IOException, InstantiationException, IllegalAccessException, WGAPIException, WGDesignSyncException { if (script.isMetadataModule()) { return null; }/*from w w w . j ava 2s . co m*/ ScriptInformation info = DesignDirectory.getScriptInformation(script.getCodeType()); if (info == null) { _log.warn("Cannot deploy unknown script code type: " + script.getCodeType()); return null; } // Get script type folder FileObject scriptTypeFolder = getScriptTypeFolder(info.getFolder()); // Eventually create intermediate directories List<String> path = WGUtils.deserializeCollection(script.getName(), ":", true); String localName = (String) path.get(path.size() - 1); FileObject currentDir = scriptTypeFolder; for (int i = 0; i < path.size() - 1; i++) { currentDir = currentDir.resolveFile((String) path.get(i)); if (!currentDir.exists()) { _log.info("Creating script category directory" + getRelativePath(currentDir)); try { currentDir.createFolder(); } catch (FileSystemException e) { throw new WGInitialDeployException( "Could not create script category folder '" + getRelativePath(currentDir) + "'", e); } } else if (!currentDir.getType().equals(FileType.FOLDER)) { throw new WGInitialDeployException( "Cannot deploy " + script.getDocumentKey() + " to sync folder because the directory name '" + path.get(i) + "' is already used by another file"); } } // Create code file FileObject codeFile = currentDir.resolveFile(localName + info.getSuffix()); _log.info("Creating script module file " + getRelativePath(codeFile)); try { codeFile.createFile(); } catch (FileSystemException e) { throw new WGInitialDeployException( "Could not create script code file '" + codeFile.getName().getPathDecoded() + "'"); } Writer writer = createWriter(codeFile); writer.write(script.getCode()); writer.close(); // Create metadata file ScriptMetadata metaData = new ScriptMetadata(script); FileObject metadataDir = currentDir.resolveFile(DesignDirectory.NAME_METADATADIR); if (!metadataDir.exists()) { _log.info("Creating script metadata directory " + getRelativePath(metadataDir)); try { metadataDir.createFolder(); } catch (FileSystemException e) { throw new WGInitialDeployException( "Could not create metadata folder '" + getRelativePath(metadataDir) + "'"); } } FileObject metadataFile = metadataDir.resolveFile(localName + DesignDirectory.SUFFIX_METADATA); _log.info("Creating script metadata file " + getRelativePath(metadataFile)); writer = createWriter(metadataFile); writer.write(_xstream.toXML(metaData.getInfo())); writer.close(); return codeFile; }
From source file:de.innovationgate.utils.WGUtils.java
/** * Returns a VFS file object for a folder. If the folder does not exist it is created. * @param parent The parent folder of the retrieved folder * @param name The name of the folder to retrieve * @return The folder/* w ww .j av a2 s. c o m*/ * @throws IOException */ public static FileObject getOrCreateFolder(FileObject parent, String name) throws IOException { if (!parent.getType().equals(FileType.FOLDER)) { throw new IllegalArgumentException("Parent file is no folder: " + parent.getName().getPathDecoded()); } FileObject folder = parent.resolveFile(name); if (!folder.exists()) { if (!folder.getFileSystem().hasCapability(Capability.CREATE)) { throw new IOException("File system of file " + folder.getURL().toString() + " is read only"); } folder.createFolder(); } if (!folder.getType().equals(FileType.FOLDER)) { throw new IllegalArgumentException("There is already a file of this name: " + name); } return folder; }