List of usage examples for org.apache.commons.vfs FileSystemManager resolveFile
public FileObject resolveFile(String name) throws FileSystemException;
From source file:org.cfeclipse.cfml.views.explorer.vfs.view.VFSView.java
/** * Gets filesystem root entries// w ww .j a va2s . co m * @param fsManager * @return an array of Files corresponding to the root directories on the platform, * may be empty but not null */ FileObject[] getRoots(FileSystemManager fsManager) throws FileSystemException { /* * On JDK 1.22 only... */ // return File.listRoots(); FileObject[] roots = null; // FileObject[] newRequest = null; /* * On JDK 1.1.7 and beyond... * -- PORTABILITY ISSUES HERE -- */ if (System.getProperty("os.name").indexOf("Windows") != -1) { Vector /* of FileObject */ list = new Vector(); list.add(fsManager.resolveFile(DRIVE_A)); for (char i = 'c'; i <= 'z'; ++i) { //FileObject drive = new FileObject(i + ":" + FileName.SEPARATOR); FileObject drive = fsManager.resolveFile(i + ":" + FileName.SEPARATOR); if (VFSUtil.isDirectory(drive) && drive.exists()) { list.add(drive); if (initial && i == 'c') { setCurrentDirectory(drive); setCurrentConnectionId(drive.getName().toString()); initial = false; } } } roots = (FileObject[]) list.toArray(new FileObject[list.size()]); VFSUtil.sortFiles(roots); //return roots; } else { FileObject root = fsManager.resolveFile(FileName.SEPARATOR); if (initial) { setCurrentDirectory(root); setCurrentConnectionId(root.getName().toString()); initial = false; } roots = new FileObject[] { root }; } return roots; //newRequest; }
From source file:org.codehaus.mojo.unix.core.OperationTest.java
public static FileObject getBaseFileObject() { try {/*from ww w.j a va 2s .co m*/ if (baseFileObject == null) { FileSystemManager fsManager = VFS.getManager(); baseFileObject = fsManager.resolveFile(PlexusTestCase.getBasedir()); } return baseFileObject; } catch (FileSystemException e) { throw new RuntimeException(e); } }
From source file:org.codehaus.mojo.unix.core.OperationTest.java
public void testExtractWithPattern() throws Exception { String archivePath = PlexusTestCase.getTestPath("src/test/resources/operation/extract.jar"); FileSystemManager fsManager = VFS.getManager(); FileObject archiveObject = fsManager.resolveFile(archivePath); assertEquals(FileType.FILE, archiveObject.getType()); FileObject archive = fsManager.createFileSystem(archiveObject); FileObject fooLicense = archive.getChild("foo-license.txt"); UnixFsObject.RegularFile fooLicenseUnixFile = fromFileObject(relativePath("licenses/foo-license.txt"), fooLicense, fileAttributes); FileObject barLicense = archive.getChild("mydir").getChild("bar-license.txt"); UnixFsObject.RegularFile barLicenseUnixFile = fromFileObject(relativePath("licenses/bar-license.txt"), barLicense, fileAttributes); MockControl control = MockControl.createControl(FileCollector.class); FileCollector fileCollector = (FileCollector) control.getMock(); control.expectAndReturn(fileCollector.addFile(barLicense, barLicenseUnixFile), fileCollector); control.expectAndReturn(fileCollector.addFile(fooLicense, fooLicenseUnixFile), fileCollector); control.replay();/* w w w .j a v a2 s. co m*/ new CopyDirectoryOperation(archive, relativePath("licenses"), asList("**/*license.txt"), null, some(p(".*/(.*license.*)", "$1")), fileAttributes, directoryAttributes).perform(fileCollector); control.verify(); }
From source file:org.codehaus.mojo.unix.maven.MojoHelper.java
public static Execution create(Map platforms, String platformType, Map formats, String formatType, SnapshotTransformation snapshotTransformation, MavenProjectWrapper project, boolean debug, boolean attachedMode, F<UnixPackage, UnixPackage> validateMojoSettingsAndApplyFormatSpecificSettingsToPackage, PackagingMojoParameters mojoParameters, final Log log) throws MojoFailureException, MojoExecutionException { MavenCommonLoggingLogFactory.setMavenLogger(log); PackagingFormat format = (PackagingFormat) formats.get(formatType); if (format == null) { throw new MojoFailureException("INTERNAL ERROR: could not find format: '" + formatType + "'."); }//from ww w .ja va2 s. c om UnixPlatform platform = (UnixPlatform) platforms.get(platformType); if (platform == null) { throw new MojoFailureException("INTERNAL ERROR: could not find platform: '" + platformType + "'."); } // TODO: This is using a private Maven API that might change. Perhaps use some reflection magic here. String timestamp = snapshotTransformation.getDeploymentTimestamp(); FileObject buildDirectory; try { FileSystemManager fileSystemManager = VFS.getManager(); buildDirectory = fileSystemManager.resolveFile(project.buildDirectory.getAbsolutePath()); } catch (FileSystemException e) { throw new MojoExecutionException("Error while initializing Commons VFS", e); } PackageVersion version = PackageVersion.packageVersion(project.version, timestamp, project.artifact.isSnapshot(), mojoParameters.revision); List<P3<UnixPackage, Package, List<AssemblyOperation>>> packages = nil(); for (Package pakke : validatePackages(mojoParameters.packages, attachedMode)) { try { String name = "unix/root-" + formatType + pakke.classifier.map(dashString).orSome(""); FileObject packageRoot = buildDirectory.resolveFile(name); packageRoot.createFolder(); PackageParameters parameters = calculatePackageParameters(project, version, platform, mojoParameters, pakke); UnixPackage unixPackage = format.start().parameters(parameters).setVersion(version). // TODO: This should go away workingDirectory(packageRoot).debug(debug).basedir(project.basedir); // ----------------------------------------------------------------------- // Let the implementation add its metadata // ----------------------------------------------------------------------- unixPackage = validateMojoSettingsAndApplyFormatSpecificSettingsToPackage.f(unixPackage); // TODO: here the logic should be different if many packages are to be created. // Example: name should be taken from mojoParameters if there is only a single package, if not // it should come from the Pakke object. This should also be validated, at least for // name List<AssemblyOperation> assemblyOperations = createAssemblyOperations(project, parameters, unixPackage, project.basedir, buildDirectory, mojoParameters.assembly, pakke.assembly); // ----------------------------------------------------------------------- // Dump the execution // ----------------------------------------------------------------------- if (debug) { log.info("======================================================================="); log.info("Package parameters: " + parameters.id); log.info("Default file attributes: "); log.info(" File : " + parameters.defaultFileAttributes); log.info(" Directory : " + parameters.defaultDirectoryAttributes); log.info("Assembly operations: "); for (AssemblyOperation operation : assemblyOperations) { operation.streamTo(new AbstractLineStreamWriter() { protected void onLine(String line) { log.info(line); } }); } } packages = packages.cons(p(unixPackage, pakke, assemblyOperations)); } catch (UnknownArtifactException e) { Map map = new TreeMap<String, Artifact>(e.artifactMap); // TODO: Do not log here, throw a CouldNotFindArtifactException with the map as an argument log.warn("Could not find artifact:" + e.artifact); log.warn("Available artifacts:"); for (Object o : map.keySet()) { log.warn(o.toString()); } throw new MojoFailureException( "Unable to find artifact: '" + e.artifact + "'. See log for available artifacts."); } catch (MissingSettingException e) { String msg = "Missing required setting '" + e.getSetting() + "'"; if (!pakke.classifier.isNone()) { msg += ", for '" + pakke.classifier.some() + "'"; } msg += ", format '" + formatType + "'."; throw new MojoFailureException(msg); } catch (IOException e) { throw new MojoExecutionException( "Error creating package '" + pakke.classifier + "', format '" + formatType + "'.", e); } } return new Execution(packages, project, formatType, attachedMode); }
From source file:org.codehaus.mojo.unix.maven.plugin.ExtractArtifact.java
public AssemblyOperation createOperation(FileObject basedir, FileAttributes defaultFileAttributes, FileAttributes defaultDirectoryAttributes) throws MojoFailureException, FileSystemException, UnknownArtifactException { File artifactFile = validateArtifact(artifact); FileSystemManager fsManager = VFS.getManager(); FileObject archiveObject = fsManager.resolveFile(artifactFile.getAbsolutePath()); FileObject archive = fsManager.createFileSystem(archiveObject); return createOperationInternal(archive, defaultFileAttributes, defaultDirectoryAttributes); }
From source file:org.codehaus.mojo.unix.maven.rpm.RpmUnixPackageTest.java
public void testBasic() throws Exception { if (!Rpmbuild.available()) { return;//from w w w .j a v a 2 s . c o m } String archivePath = getTestPath("src/test/resources/operation/extract.jar"); FileSystemManager fsManager = VFS.getManager(); FileObject pomXml = fsManager.resolveFile(getTestPath("pom.xml")); FileObject archiveObject = fsManager.resolveFile(archivePath); FileObject archive = fsManager.createFileSystem(archiveObject); FileObject fooLicense = archive.getChild("foo-license.txt"); FileObject barLicense = archive.getChild("mydir").getChild("bar-license.txt"); RpmPackagingFormat packagingFormat = (RpmPackagingFormat) lookup(PackagingFormat.ROLE, "rpm"); FileObject rpmTest = VFS.getManager().resolveFile(getTestPath("target/rpm-test")); FileObject packageRoot = rpmTest.resolveFile("root"); File packageFile = getTestFile("target/rpm-test/file.rpm"); PackageVersion version = packageVersion("1.0-1", "123", false, Option.<String>none()); PackageParameters parameters = packageParameters("mygroup", "myartifact", version, "id", "default-name", Option.<String>none(), EMPTY, EMPTY).contact("Kurt Cobain").architecture("all").name("Yo!") .license("BSD"); UnixPackage unixPackage = RpmPackagingFormat .cast(packagingFormat.start().parameters(parameters).workingDirectory(packageRoot)).group("Fun"); LocalDateTime now = new LocalDateTime(); Option<FileAttributes> none = Option.none(); unixPackage.addFile(pomXml, regularFile(relativePath("/pom.xml"), now, 0, none)) .addFile(fooLicense, regularFile(relativePath("/foo-license.txt"), now, 0, none)) .addFile(barLicense, regularFile(relativePath("/bar-license.txt"), now, 0, none)); unixPackage.debug(true).packageToFile(packageFile, ScriptUtil.Strategy.SINGLE); assertTrue(packageFile.canRead()); }
From source file:org.codehaus.mojo.unix.sysvpkg.prototype.PrototypeFileTest.java
public void testBasic() throws Exception { FileSystemManager fsManager = VFS.getManager(); FileObject root = fsManager.resolveFile(getTestPath("target/prototype-test/assembly")); root.createFolder();/* ww w . jav a2s . c o m*/ PrototypeFile prototypeFile = new PrototypeFile(defaultEntry); FileObject bashProfileObject = fsManager.resolveFile(getTestPath("src/test/non-existing/bash_profile")); FileObject extractJarObject = fsManager.resolveFile(getTestPath("src/test/non-existing/extract.jar")); UnixFsObject.RegularFile extractJar = regularFile(extractJarPath, dateTime, 0, some(fileAttributes)); UnixFsObject.RegularFile bashProfile = regularFile(bashProfilePath, dateTime, 0, some(fileAttributes)); UnixFsObject.RegularFile smfManifestXml = regularFile(smfManifestXmlPath, dateTime, 0, some(fileAttributes.addTag("class:smf"))); prototypeFile.addFile(bashProfileObject, bashProfile); prototypeFile.addFile(extractJarObject, extractJar); prototypeFile.addFile(extractJarObject, smfManifestXml); prototypeFile.addDirectory(directory(BASE, dateTime, dirAttributes)); prototypeFile.addDirectory(directory(specialPath, dateTime, dirAttributes)); prototypeFile.apply(filter(extractJarPath, fileAttributes.user("funnyuser"))); prototypeFile.apply(filter(specialPath, dirAttributes.group("funnygroup"))); LineFile stream = new LineFile(); prototypeFile.streamTo(stream); assertEquals( new LineFile() .add("f none /extract.jar=" + extractJarObject.getName().getPath() + " 0644 funnyuser nogroup") .add("d none /opt ? default default").add("d none /opt/jetty ? default default") .add("f none /opt/jetty/.bash_profile=" + bashProfileObject.getName().getPath() + " 0644 nouser nogroup") .add("d none /smf ? default default") .add("f smf /smf/manifest.xml=" + extractJarObject.getName().getPath() + " 0644 nouser nogroup") .add("d none /special 0755 nouser funnygroup").toString(), stream.toString()); }
From source file:org.codehaus.mojo.unix.util.vfs.IncludeExcludeTest.java
public void testBasic() throws Exception { String myProjectPath = new TestUtil(this).getTestPath("src/test/resources/my-project"); FileSystemManager fsManager = VFS.getManager(); FileObject myProject = fsManager.resolveFile(myProjectPath); assertEquals(FileType.FOLDER, myProject.getType()); List<FileObject> selection = new ArrayList<FileObject>(); myProject.findFiles(IncludeExcludeFileSelector.build(myProject.getName()) .addInclude(new PathExpression("/src/main/unix/files/**")).addInclude(new PathExpression("*.java")) .addExclude(new PathExpression("**/huge-file")).filesOnly(). // noDefaultExcludes(). create(), true, selection);//from w w w . j a v a2 s . c o m System.out.println("Included:"); for (FileObject fileObject : selection) { System.out.println(myProject.getName().getRelativeName(fileObject.getName())); } assertEquals(2, selection.size()); assertTrue(selection.contains(myProject.resolveFile("src/main/unix/files/opt/comp/myapp/etc/myapp.conf"))); assertTrue(selection.contains(myProject.resolveFile("Included.java"))); }
From source file:org.eclim.plugin.core.command.archive.ArchiveReadCommand.java
/** * {@inheritDoc}/*from ww w . j a v a 2 s . co m*/ */ public String execute(CommandLine commandLine) throws Exception { InputStream in = null; OutputStream out = null; FileSystemManager fsManager = null; try { String file = commandLine.getValue(Options.FILE_OPTION); fsManager = VFS.getManager(); FileObject fileObject = fsManager.resolveFile(file); FileObject tempFile = fsManager .resolveFile(SystemUtils.JAVA_IO_TMPDIR + "/eclim/" + fileObject.getName().getPath()); // the vfs file cache isn't very intelligent, so clear it. fsManager.getFilesCache().clear(fileObject.getFileSystem()); fsManager.getFilesCache().clear(tempFile.getFileSystem()); // NOTE: FileObject.getName().getPath() does not include the drive // information. String path = tempFile.getName().getURI().substring(URI_PREFIX.length()); // account for windows uri which has an extra '/' in front of the drive // letter (file:///C:/blah/blah/blah). if (WIN_PATH.matcher(path).matches()) { path = path.substring(1); } //if(!tempFile.exists()){ tempFile.createFile(); in = fileObject.getContent().getInputStream(); out = tempFile.getContent().getOutputStream(); IOUtils.copy(in, out); new File(path).deleteOnExit(); //} return path; } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } }
From source file:org.eclim.plugin.jdt.command.search.SearchCommand.java
/** * Creates a Position from the supplied SearchMatch. * * @param project The project searching from. * @param match The SearchMatch.// w w w. j av a 2 s . c om * @return The Position. */ protected Position createPosition(IProject project, SearchMatch match) throws Exception { IJavaElement element = (IJavaElement) match.getElement(); IJavaElement parent = JavaUtils.getPrimaryElement(element); String file = null; String elementName = JavaUtils.getFullyQualifiedName(parent); if (parent.getElementType() == IJavaElement.CLASS_FILE) { IResource resource = parent.getResource(); // occurs with a referenced project as a lib with no source and class // files that are not archived in that project if (resource != null && resource.getType() == IResource.FILE && !isJarArchive(resource.getLocation())) { file = resource.getLocation().toOSString(); } else { IPath path = null; IPackageFragmentRoot root = (IPackageFragmentRoot) parent.getParent().getParent(); resource = root.getResource(); if (resource != null) { if (resource.getType() == IResource.PROJECT) { path = ProjectUtils.getIPath((IProject) resource); } else { path = resource.getLocation(); } } else { path = root.getPath(); } String classFile = elementName.replace('.', File.separatorChar); if (isJarArchive(path)) { file = "jar:file://" + path.toOSString() + '!' + classFile + ".class"; } else { file = path.toOSString() + '/' + classFile + ".class"; } // android injects its jdk classes, so filter those out if the project // doesn't have the android nature. if (ANDROID_JDK_URL.matcher(file).matches() && project != null && !project.hasNature(ANDROID_NATURE)) { return null; } // if a source path attachment exists, use it. IPath srcPath = root.getSourceAttachmentPath(); if (srcPath != null) { String rootPath; IProject elementProject = root.getJavaProject().getProject(); // determine if src path is project relative or file system absolute. if (srcPath.isAbsolute() && elementProject.getName().equals(srcPath.segment(0))) { rootPath = ProjectUtils.getFilePath(elementProject, srcPath.toString()); } else { rootPath = srcPath.toOSString(); } String srcFile = FileUtils.toUrl(rootPath + File.separator + classFile + ".java"); // see if source file exists at source path. FileSystemManager fsManager = VFS.getManager(); FileObject fileObject = fsManager.resolveFile(srcFile); if (fileObject.exists()) { file = srcFile; // jdk sources on osx are under a "src/" dir in the jar } else if (Os.isFamily(Os.FAMILY_MAC)) { srcFile = FileUtils .toUrl(rootPath + File.separator + "src" + File.separator + classFile + ".java"); fileObject = fsManager.resolveFile(srcFile); if (fileObject.exists()) { file = srcFile; } } } } } else { IPath location = match.getResource().getLocation(); file = location != null ? location.toOSString() : null; } elementName = JavaUtils.getFullyQualifiedName(element); return Position.fromOffset(file.replace('\\', '/'), elementName, match.getOffset(), match.getLength()); }