Example usage for org.apache.commons.lang SystemUtils IS_OS_WINDOWS

List of usage examples for org.apache.commons.lang SystemUtils IS_OS_WINDOWS

Introduction

In this page you can find the example usage for org.apache.commons.lang SystemUtils IS_OS_WINDOWS.

Prototype

boolean IS_OS_WINDOWS

To view the source code for org.apache.commons.lang SystemUtils IS_OS_WINDOWS.

Click Source Link

Document

Is true if this is Windows.

The field will return false if OS_NAME is null.

Usage

From source file:org.jodconverter.office.LocalOfficeUtilsTest.java

/** Tests the LocalOfficeUtils.toUrl function on Windows OS. */
@Test/*w ww  .  j  a v a  2  s. c om*/
public void windowsToUrl() {

    assumeTrue(SystemUtils.IS_OS_WINDOWS);

    String tempDir = System.getProperty("java.io.tmpdir");
    final File tempDirFile = new File(tempDir);
    tempDir = FilenameUtils.normalizeNoEndSeparator(tempDir, true);

    assertThat(toUrl(new File(tempDirFile, "document.odt"))).isEqualTo("file:///" + tempDir + "/document.odt");
    assertThat(toUrl(new File(tempDirFile, "document with spaces.odt")))
            .isEqualTo("file:///" + tempDir + "/document%20with%20spaces.odt");
}

From source file:org.jodconverter.process.ProcessManagerTest.java

/**
 * Tests the WindowsProcessManager class.
 *
 * @throws Exception if an error occurs.
 *///w w  w  .  j  a  v a  2 s  .  c  o m
@Test
public void windowsProcessManager() throws Exception {
    assumeTrue(SystemUtils.IS_OS_WINDOWS);

    final ProcessManager processManager = WindowsProcessManager.getDefault();
    final Process process = Runtime.getRuntime().exec("ping 127.0.0.1 -n 5");
    final ProcessQuery query = new ProcessQuery("ping", "127.0.0.1 -n 5");

    final long pid = processManager.findPid(query);
    assertThat(pid).isNotEqualTo(ProcessManager.PID_NOT_FOUND);
    // Won't work on Windows, skip this assertion
    //Number javaPid = (Number) FieldUtils.readDeclaredField(process, "pid", true);
    //assertThat(pid).isEqualTo(javaPid.longValue());

    processManager.kill(process, pid);
    assertThat(processManager.findPid(query)).isEqualTo(ProcessManager.PID_NOT_FOUND);
}

From source file:org.jwebsocket.util.Tools.java

/**
 * Return TRUE if the file is located inside of the given base path, FALSE otherwise
 *
 * @param aFile// w w w .j  a  v  a2  s. com
 * @param aBasePath
 * @return
 */
public static boolean isParentPath(File aFile, String aBasePath) {
    try {
        String lCanonicalPath = FilenameUtils.separatorsToSystem(aFile.getCanonicalPath()) + File.separator;
        String lBasePath = FilenameUtils.separatorsToSystem(aBasePath);
        if (SystemUtils.IS_OS_WINDOWS) {
            if (lCanonicalPath.toLowerCase().startsWith(lBasePath.toLowerCase())) {
                return true;
            }
        } else {
            if (!lCanonicalPath.startsWith(lBasePath)) {
                return false;
            }
        }
    } catch (IOException lEx) {
        return false;
    }
    return true;
}

From source file:org.kitodo.filemanagement.FileManagement.java

private URI performRename(URI mappedFileURI, URI mappedNewFileURI) throws IOException {
    File fileToRename = new File(mappedFileURI);
    File renamedFile = new File(mappedNewFileURI);

    final int sleepIntervalMilliseconds = 20;
    final int maxWaitMilliseconds = KitodoConfig
            .getIntParameter(ParameterFileManagement.FILE_MAX_WAIT_MILLISECONDS);

    boolean success;
    int millisWaited = 0;

    do {/*from   www .  jav a 2  s.  com*/
        if (SystemUtils.IS_OS_WINDOWS && millisWaited == sleepIntervalMilliseconds) {
            logger.warn("Renaming {} failed. This is Windows. Running the garbage collector may yield good"
                    + " results. Forcing immediate garbage collection now!", fileToRename.getName());
            System.gc();
        }
        success = fileToRename.renameTo(renamedFile);
        if (!success) {
            if (millisWaited == 0) {
                logger.info("Renaming {} failed. File may be locked. Retrying...", fileToRename.getName());
            }
            waitForThread(sleepIntervalMilliseconds);
            millisWaited += sleepIntervalMilliseconds;
        }
    } while (!success && millisWaited < maxWaitMilliseconds);

    if (!success) {
        logger.error("Rename {} failed. This is a permanent error. Giving up.", fileToRename.getName());
        throw new IOException(
                "Renaming of " + fileToRename.getName() + " into " + renamedFile.getName() + " failed.");
    }

    if (millisWaited > 0) {
        logger.info("Rename finally succeeded after {} milliseconds.", Integer.toString(millisWaited));
    }
    return fileMapper.unmapUriFromKitodoDataDirectoryUri(Paths.get(renamedFile.getPath()).toUri());
}

From source file:org.kitodo.filemanagement.FileManagementTest.java

@Test
public void shouldCreateSymLink() throws IOException {
    assumeTrue(!SystemUtils.IS_OS_WINDOWS && !SystemUtils.IS_OS_MAC);
    URI symLinkSource = URI.create("symLinkSource");
    URI symLinkTarget = URI.create("symLinkTarget");

    File script = new File(KitodoConfig.getParameter("script_createSymLink"));
    URI directory = fileManagement.create(URI.create(""), "symLinkSource", false);
    fileManagement.create(directory, "meta.xml", true);
    setFileExecutable(script);/*  w w  w .  j  av  a 2s  .  c  o m*/
    boolean result = fileManagement.createSymLink(symLinkSource, symLinkTarget, false, SystemUtils.USER_NAME);
    setFileNotExecutable(script);
    assertTrue("Create symbolic link has failed!", result);

    File scriptClean = new File(KitodoConfig.getParameter("script_deleteSymLink"));
    setFileExecutable(scriptClean);
    fileManagement.deleteSymLink(symLinkTarget);
    setFileNotExecutable(scriptClean);
    fileManagement.delete(symLinkSource);
    fileManagement.delete(symLinkTarget);
}

From source file:org.kitodo.filemanagement.FileManagementTest.java

@Test
public void shouldDeleteSymLink() throws IOException {
    assumeTrue(!SystemUtils.IS_OS_WINDOWS && !SystemUtils.IS_OS_MAC);

    URI symLinkSource = URI.create("symLinkSource");
    URI symLinkTarget = URI.create("symLinkTarget");

    File scriptPrepare = new File(KitodoConfig.getParameter("script_createSymLink"));
    URI directory = fileManagement.create(URI.create(""), "symLinkSource", false);
    fileManagement.create(directory, "meta.xml", true);
    setFileExecutable(scriptPrepare);//from  w  ww  .j a  v a2 s  . c o m
    fileManagement.createSymLink(symLinkSource, symLinkTarget, false, SystemUtils.USER_NAME);
    setFileNotExecutable(scriptPrepare);

    File script = new File(KitodoConfig.getParameter("script_deleteSymLink"));
    setFileExecutable(script);
    boolean result = fileManagement.deleteSymLink(symLinkTarget);
    setFileNotExecutable(script);
    assertTrue("Delete symbolic link has failed!", result);

    fileManagement.delete(symLinkSource);
    fileManagement.delete(symLinkTarget);
}

From source file:org.kitodo.filemanagement.locking.ImmutableReadFileManagement.java

/**
 * Derives the name of the temporary file from the name of the original file
 * and creates an empty placeholder file with the derived file name in the
 * same directory. The name of the temporary file is formed depending on the
 * operating system so that the file can easily be recognized as a temporary
 * file. For example, {@code PPN012345678.xml} could become
 * {@code PPN012345678.xml-2448103947405693446~}, but
 * {@code PPN012345678_xml-2448103947405693446.tmp} on Windows. The sequence
 * of numbers is inserted by the Java runtime and chosen so that it does not
 * exist guaranteed./*from  w w  w . j a  v  a2  s. c  o m*/
 *
 * @param srcFile
 *            the original file
 * @return the derived file
 * @throws IOException
 *             if the file does not exist or if an error occurs in disk
 *             access, e.g. because the write permission for the directory
 *             is missing
 */
private File deriveTempFile(File srcFile) throws IOException {
    String prefix = srcFile.getName();
    if (SystemUtils.IS_OS_WINDOWS) {
        prefix = prefix.replace('.', '_');
    }
    prefix = prefix.concat("-");
    String suffix = SystemUtils.IS_OS_WINDOWS ? ".tmp" : "~";
    File directory = srcFile.getParentFile();
    return File.createTempFile(prefix, suffix, directory);
}

From source file:org.kitodo.filemanagement.locking.LockManagementTest.java

/**
 * Counts the number of found temporary files.
 *
 * @return the temporary files/*from ww w . j  a  v a  2  s.  co m*/
 */
private static String[] listTempFiles(URI uri) {
    File file = new File(uri.getPath());
    String prefix = file.getName();
    if (SystemUtils.IS_OS_WINDOWS) {
        prefix = prefix.replace('.', '_');
    }
    final String finalPrefix = prefix.concat("-");
    List<String> result = Arrays.asList(file.getParentFile().list((dir, name) -> name.startsWith(finalPrefix)));
    return result.toArray(new String[result.size()]);
}

From source file:org.kitodo.production.exporter.download.ExportMetsIT.java

@BeforeClass
public static void setUp() throws Exception {
    MockDatabase.startNode();/*from   w  w  w.  j a v a  2  s . co  m*/
    MockDatabase.insertProcessesFull();

    User user = ServiceManager.getUserService().getById(1);
    process = ServiceManager.getProcessService().getById(1);
    metadataDirectory = process.getId().toString();
    userDirectory = user.getLogin();
    exportUri = ConfigCore.getUriParameter(ParameterCore.DIR_USERS, userDirectory);

    fileService.createDirectory(URI.create(""), metadataDirectory);
    fileService.copyFile(URI.create("testmetaNewFormat.xml"), URI.create(metadataDirectory + "/meta.xml"));
    SecurityTestUtils.addUserDataToSecurityContext(user, 1);
    FileLoader.createConfigProjectsFile();

    if (!SystemUtils.IS_OS_WINDOWS) {
        ExecutionPermission.setExecutePermission(scriptCreateDirUserHome);
    }

    File userdataDirectory = new File(ConfigCore.getParameter(ParameterCore.DIR_USERS));
    if (!userdataDirectory.exists() && !userdataDirectory.mkdir()) {
        throw new IOException("Could not create users directory");
    }
}

From source file:org.kitodo.production.exporter.download.ExportMetsIT.java

@AfterClass
public static void tearDown() throws Exception {
    SecurityTestUtils.cleanSecurityContext();
    MockDatabase.stopNode();/*  w ww . j  a v  a 2  s.c o  m*/
    MockDatabase.cleanDatabase();
    fileService.delete(URI.create(metadataDirectory));
    fileService.delete(ConfigCore.getUriParameter(ParameterCore.DIR_USERS));
    FileLoader.deleteConfigProjectsFile();

    if (!SystemUtils.IS_OS_WINDOWS) {
        ExecutionPermission.setNoExecutePermission(scriptCreateDirUserHome);
    }
}