Java Code Examples for org.apache.brooklyn.util.os.Os#isMicrosoftWindows()

The following examples show how to use org.apache.brooklyn.util.os.Os#isMicrosoftWindows() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileUtil.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/**
 * This utility will be deleted when we move to Java 7
 * 
 * @return The file permission (in a form like "-rwxr--r--"), or null if the permissions could not be determined.
 */
@Beta
public static Maybe<String> getFilePermissions(File file) {
    if (!file.exists()) return Maybe.absent("File "+file+" does not exist");
    
    if (Os.isMicrosoftWindows()) {
        return Maybe.absent("Cannot determine permissions on windows");
    } else {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayOutputStream err = new ByteArrayOutputStream();
        int exitcode = exec(ImmutableList.of("ls", "-ld", file.getAbsolutePath()), out, err);
        if (exitcode != 0) {
            if (LOG.isDebugEnabled()) LOG.debug("Could not determine permissions of file "+file+"; exit code "+exitcode+"; stderr "+new String(err.toByteArray()));
            return Maybe.absent("Could not determine permission of file "+file+"; exit code "+exitcode);
        }
        String stdout = new String(out.toByteArray());
        return (stdout.trim().isEmpty() ? Maybe.<String>absent("empty output") : Maybe.of(stdout.split("\\s")[0]));
    }
}
 
Example 2
Source File: DisableOnWindowsListener.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Override
public void transform(
        final ITestAnnotation annotation,
        final Class testClass,
        final Constructor testConstructor,
        final Method testMethod
) {
    if (testMethod != null ) {
        final DisableOnWindows disableOnWindows = testMethod.getAnnotation(DisableOnWindows.class);
        if (disableOnWindows != null && Os.isMicrosoftWindows()) {
            annotation.setEnabled(false);
            LOG.info(String.format("Disabled: %s.%s - %s",
                    testMethod.getDeclaringClass().getName(),
                    testMethod.getName(),
                    disableOnWindows.reason()));
        }
    }
}
 
Example 3
Source File: MonitorUtilsTest.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
private String getSilentPrefix() {
    if (Os.isMicrosoftWindows()) {
        return "@";
    } else {
        return "";
    }
}
 
Example 4
Source File: MonitorUtilsTest.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
private String getExecPrefix() {
    if (Os.isMicrosoftWindows()) {
        return "";
    } else {
        return "#!/bin/sh\n";
    }
}
 
Example 5
Source File: FileUtil.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private static void logSetFilePermissionsFailure(final String permissions, File file, IOException ex) {
    if (loggedSetFilePermissionsWarning) {
        if (LOG.isTraceEnabled()) LOG.trace("Failed to set permissions to {} for file {}: {}",
                new Object[] {permissions, file.getAbsolutePath(), ex});
    } else {
        if (Os.isMicrosoftWindows()) {
            if (LOG.isDebugEnabled()) LOG.debug("Failed to set permissions to {} for file {}; expected behaviour on Windows; {}; subsequent failures (on any file) will be logged at trace",
                    new Object[] {permissions, file.getAbsolutePath(), ex});
        } else {
            LOG.warn("Failed to set permissions to {} for file {}: {}; subsequent failures (on any file) will be logged at trace",
                    new Object[] {permissions, file.getAbsolutePath(), ex});
        }
        loggedSetFilePermissionsWarning = true;
    }
}
 
Example 6
Source File: FileUtil.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public static void moveDir(File srcDir, File destDir) throws IOException, InterruptedException {
    if (!Os.isMicrosoftWindows()) {
        String cmd = "mv '"+srcDir.getAbsolutePath()+"' '"+destDir.getAbsolutePath()+"'";
        Process proc = Runtime.getRuntime().exec(cmd);
        proc.waitFor();
        if (proc.exitValue() == 0) return;
    }
    
    FileUtils.moveDirectory(srcDir, destDir);
}
 
Example 7
Source File: FileUtil.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public static void copyDir(File srcDir, File destDir) throws IOException, InterruptedException {
    if (!Os.isMicrosoftWindows()) {
        String cmd = "cp -R '"+srcDir.getAbsolutePath()+"' '"+destDir.getAbsolutePath()+"'";
        Process proc = Runtime.getRuntime().exec(cmd);
        proc.waitFor();
        if (proc.exitValue() == 0) return;
    }
    
    FileUtils.copyDirectory(srcDir, destDir);
}
 
Example 8
Source File: FilePermissions.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public void apply(File file) throws IOException {
    Path filePath = file.toPath();

    // the appropriate condition is actually Files.getFileStore(filePath).supportsFileAttributeView(PosixFileAttributeView.class)
    // but that downs performance to ~9000 calls per second

    boolean done = false;
    try {
        // ~59000 calls per sec
        if (!Os.isMicrosoftWindows()) {
            Files.setPosixFilePermissions(filePath, posixPermissions);
        }
        done = true;
    } catch (UnsupportedOperationException ex) {}
    
    if (!done) {
        // ~42000 calls per sec
        // TODO: what happens to group permissions ?
        boolean setRead = file.setReadable(posixPermissions.contains(OTHERS_READ), false) & file.setReadable(posixPermissions.contains(OWNER_READ), true);
        boolean setWrite = file.setWritable(posixPermissions.contains(OTHERS_WRITE), false) & file.setWritable(posixPermissions.contains(OWNER_WRITE), true);
        boolean setExec = file.setExecutable(posixPermissions.contains(OTHERS_EXECUTE), false) & file.setExecutable(posixPermissions.contains(OWNER_EXECUTE), true);

        if (!(setRead && setWrite && setExec)) {
            throw new IOException("setting file permissions failed: read=" + setRead + " write=" + setWrite + " exec=" + setExec);
        }
    }
}
 
Example 9
Source File: ProcessToolStaticsTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private List<String> getTestCommand() {
    if(Os.isMicrosoftWindows()) {
        return Arrays.asList("cmd", "/c", "echo", "hello", "world");
    } else {
        return Arrays.asList("echo", "hello", "world");
    }
}
 
Example 10
Source File: DisableOnWindowsTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
@DisableOnWindows(reason = "unit test")
public void isDisabledOnWindows() {
    if (Os.isMicrosoftWindows()) {
        fail("Test should have been disabled on windows");
    }
}