Package com.xebialabs.overthere

Examples of com.xebialabs.overthere.OverthereFile


    options.set(OPERATING_SYSTEM, UNIX);
    options.set(CONNECTION_TYPE, SFTP);
    OverthereConnection connection = Overthere.getConnection("ssh", options);
    try {
      connection.execute(CmdLine.build("cp", "/etc/motd", "/tmp/motd1"));
      OverthereFile motd1 = connection.getFile("/tmp/motd1");
      OverthereFile motd2 = connection.getFile("/tmp/motd2");
      motd2.delete();

      System.err.println("Exists #1: " + motd1.exists());
      System.err.println("Exists #2: " + motd2.exists());

      motd1.renameTo(motd2);
      System.err.println("Exists #1: " + motd1.exists());
      System.err.println("Exists #2: " + motd2.exists());

      motd2.copyTo(motd1);
      System.err.println("Exists #1: " + motd1.exists());
      System.err.println("Exists #2: " + motd2.exists());

      motd1.delete();
      motd2.delete();
      System.err.println("Exists #1: " + motd1.exists());
      System.err.println("Exists #2: " + motd2.exists());
    } finally {
      connection.close();
    }
  }
View Full Code Here


    options.set(PASSWORD, "secret");
    options.set(OPERATING_SYSTEM, UNIX);
    options.set(CONNECTION_TYPE, SFTP);
    OverthereConnection connection = Overthere.getConnection("ssh", options);
    try {
      OverthereFile motd = connection.getFile("/etc/motd");
      BufferedReader r = new BufferedReader(new InputStreamReader(motd.getInputStream()));
      try {
        String line;
        while((line = r.readLine()) != null) {
          System.err.println(line);
        }
View Full Code Here

    public final synchronized OverthereFile getTempFile(String name) {
        if (name == null || name.trim().isEmpty()) {
            name = "tmp";
        }

        OverthereFile temporaryDirectory = getFile(temporaryDirectoryPath);
        RuntimeException originalExc = null;
        for (int i = 0; i <= temporaryFileCreationRetries; i++) {
            String holderName = temporaryFileHolderDirectoryNamePrefix;
            if (temporaryFileHolderDirectoryNameSuffix > 0) {
                holderName += "." + temporaryFileHolderDirectoryNameSuffix;
            }
            OverthereFile holder = getFileForTempFile(temporaryDirectory, holderName);
            if (!holder.exists()) {
                logger.trace("Creating holder directory {} for temporary file with name {}", holder, name);
                try {
                    originalExc = null;
                    holder.mkdir();
                    temporaryFileHolderDirectories.add(holder);
                    OverthereFile tempFile = holder.getFile(name);
                    logger.debug("Generated temporary file name {}", tempFile);
                    return tempFile;
                } catch(RuntimeException exc) {
                    originalExc = exc;
                    logger.debug(format("Failed to create holder directory %s - Trying with the next suffix", holder), exc);
View Full Code Here

    @Override
    public InputStream getInputStream() throws RuntimeIOException {
        if (isTempFile) {
            return super.getInputStream();
        } else {
            OverthereFile tempFile = connection.getTempFile(getName());
            copyToTempFile(tempFile);
            return tempFile.getInputStream();
        }
    }
View Full Code Here

        if (isTempFile) {
            super.copyFrom(source);
            overrideUmask(this);
        } else {
            logger.debug("Copying file or directory {} to {}", source, this);
            OverthereFile tempFile = getConnection().getTempFile(getName());
            try {
                connection.getSshClient().newSCPFileTransfer().newSCPUploadClient().copy(new OverthereFileLocalSourceFile(source), tempFile.getPath());
            } catch (IOException e) {
                throw new RuntimeIOException("Cannot copy " + source + " to " + this, e);
            }
            overrideUmask(tempFile);
            copyFromTempFile(tempFile);
View Full Code Here

        OverthereFileCopier.checkDirectoryExists(srcDir, SOURCE);
    }

    @Override
    protected void handleDirectoryStart(OverthereFile scrDir, int depth) throws IOException {
        OverthereFile dstDir = getCurrentDestinationDir();
        if (depth != ROOT) {
            dstDir = createSubdirectoryAndMakeCurrent(dstDir, scrDir.getName());
        }

        if (dstDir.exists()) {
            OverthereFileCopier.checkReallyIsADirectory(dstDir, DESTINATION);
            logger.debug("About to copy files into existing directory {}", dstDir);
        } else {
            logger.debug("Creating destination directory {}", dstDir);
            dstDir.mkdir();
        }
    }
View Full Code Here

            dstDir.mkdir();
        }
    }

    private OverthereFile createSubdirectoryAndMakeCurrent(OverthereFile parentDir, String subdirName) {
        OverthereFile subdir = parentDir.getFile(subdirName);
        dstDirStack.push(subdir);
        return subdir;
    }
View Full Code Here

        return dstDirStack.peek();
    }

    @Override
    protected void handleFile(OverthereFile srcFile, int depth) throws IOException {
        OverthereFile dstFile = getCurrentDestinationDir().getFile(srcFile.getName());
        OverthereFileCopier.copyFile(srcFile, dstFile);
    }
View Full Code Here

    public void shouldCreateWriteReadAndRemoveTemporaryFile() throws IOException {
        final String prefix = "prefix";
        final String suffix = "suffix";
        final byte[] contents = ("Contents of the temporary file created at " + System.currentTimeMillis() + "ms since the epoch").getBytes();

        OverthereFile tempFile = connection.getTempFile(prefix, suffix);
        assertThat("Expected a non-null return value from HostConnection.getTempFile()", tempFile, notNullValue());
        assertThat("Expected name of temporary file to start with the prefix", tempFile.getName(), startsWith(prefix));
        assertThat("Expected name of temporary file to end with the suffix", tempFile.getName(), endsWith(suffix));
        assertThat("Expected temporary file to not exist yet", tempFile.exists(), equalTo(false));

        OutputStream out = tempFile.getOutputStream();
        try {
            out.write(contents);
        } finally {
            closeQuietly(out);
        }

        assertThat("Expected temporary file to exist after writing to it", tempFile.exists(), equalTo(true));
        assertThat("Expected temporary file to not be a directory", tempFile.isDirectory(), equalTo(false));
        assertThat("Expected temporary file to have the size of the contents written to it", tempFile.length(), equalTo((long) contents.length));
        assertThat("Expected temporary file to be readable", tempFile.canRead(), equalTo(true));
        assertThat("Expected temporary file to be writeable", tempFile.canWrite(), equalTo(true));

        // Windows systems don't support the concept of checking for executability
        if (connection.getHostOperatingSystem() == OperatingSystemFamily.UNIX) {
            assertFalse("Expected temporary file to not be executable", tempFile.canExecute());
        }

        DataInputStream in = new DataInputStream(tempFile.getInputStream());
        try {
            final byte[] contentsRead = new byte[contents.length];
            in.readFully(contentsRead);
            assertThat("Expected input stream to be exhausted after reading the full contents", in.available(), equalTo(0));
            assertThat("Expected contents in temporary file to be identical to data written into it", contentsRead, equalTo(contents));
        } finally {
            closeQuietly(in);
        }

        tempFile.delete();
        assertThat("Expected temporary file to no longer exist", tempFile.exists(), equalTo(false));
    }
View Full Code Here

    @Test
    public void shouldCreatePopulateListAndRemoveTemporaryDirectory() {
        final String prefix = "prefix";
        final String suffix = "suffix";

        OverthereFile tempDir = connection.getTempFile(prefix, suffix);
        assertThat("Expected a non-null return value from HostConnection.getTempFile()", tempDir, notNullValue());
        assertThat("Expected name of temporary file to start with the prefix", tempDir.getName(), startsWith(prefix));
        assertThat("Expected name of temporary file to end with the suffix", tempDir.getName(), endsWith(suffix));
        assertThat("Expected temporary file to not exist yet", tempDir.exists(), equalTo(false));

        tempDir.mkdir();
        assertThat("Expected temporary directory to exist after creating it", tempDir.exists(), equalTo(true));
        assertThat("Expected temporary directory to be a directory", tempDir.isDirectory(), equalTo(true));

        OverthereFile anotherTempDir = connection.getTempFile(prefix, suffix);
        assertThat("Expected temporary directories created with identical prefix and suffix to still be different", tempDir.getPath(),
                not(equalTo(anotherTempDir.getPath())));

        OverthereFile nested1 = tempDir.getFile("nested1");
        OverthereFile nested2 = nested1.getFile("nested2");
        OverthereFile nested3 = nested2.getFile("nested3");
        assertThat("Expected deeply nested directory to not exist", nested3.exists(), equalTo(false));
        try {
            nested3.mkdir();
            fail("Expected not to be able to create a deeply nested directory in one go");
        } catch (RuntimeIOException expected1) {
        }
        assertThat("Expected deeply nested directory to still not exist", nested3.exists(), equalTo(false));
        nested3.mkdirs();
        assertThat("Expected deeply nested directory to exist after invoking mkdirs on it", nested3.exists(), equalTo(true));

        final byte[] contents = ("Contents of the temporary file created at " + System.currentTimeMillis() + "ms since the epoch").getBytes();
        OverthereFile regularFile = tempDir.getFile("somefile.txt");
        OverthereUtils.write(contents, regularFile);

        List<OverthereFile> dirContents = tempDir.listFiles();
        assertThat("Expected directory to contain two entries", dirContents.size(), equalTo(2));
        assertThat("Expected directory to contain parent of deeply nested directory", dirContents.contains(nested1), equalTo(true));
        assertThat("Expected directory to contain regular file that was just created", dirContents.contains(regularFile), equalTo(true));

        try {
            nested1.delete();
        } catch (RuntimeIOException expected2) {
        }
        nested1.deleteRecursively();
        assertThat("Expected parent of deeply nested directory to have been removed recursively", nested1.exists(), equalTo(false));

        regularFile.delete();
        tempDir.delete();
        assertThat("Expected temporary directory to not exist after removing it when it was empty", tempDir.exists(), equalTo(false));
    }
View Full Code Here

TOP

Related Classes of com.xebialabs.overthere.OverthereFile

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.