Example usage for java.io File pathSeparatorChar

List of usage examples for java.io File pathSeparatorChar

Introduction

In this page you can find the example usage for java.io File pathSeparatorChar.

Prototype

char pathSeparatorChar

To view the source code for java.io File pathSeparatorChar.

Click Source Link

Document

The system-dependent path-separator character.

Usage

From source file:com.cerema.cloud2.ui.activity.FileDisplayActivity.java

private void requestSimpleUpload(Intent data, int resultCode) {
    String filePath = null;/*from ww w. j a  v a  2  s .  c  o m*/
    String mimeType = null;

    Uri selectedImageUri = data.getData();

    try {
        mimeType = getContentResolver().getType(selectedImageUri);

        String fileManagerString = selectedImageUri.getPath();
        String selectedImagePath = UriUtils.getLocalPath(selectedImageUri, this);

        if (selectedImagePath != null)
            filePath = selectedImagePath;
        else
            filePath = fileManagerString;

    } catch (Exception e) {
        Log_OC.e(TAG, "Unexpected exception when trying to read the result of " + "Intent.ACTION_GET_CONTENT",
                e);

    } finally {
        if (filePath == null) {
            Log_OC.e(TAG, "Couldn't resolve path to file");
            Toast t = Toast.makeText(this, getString(R.string.filedisplay_unexpected_bad_get_content),
                    Toast.LENGTH_LONG);
            t.show();
            return;
        }
    }

    Intent i = new Intent(this, FileUploader.class);
    i.putExtra(FileUploader.KEY_ACCOUNT, getAccount());
    OCFile currentDir = getCurrentDir();
    String remotePath = (currentDir != null) ? currentDir.getRemotePath() : OCFile.ROOT_PATH;

    if (filePath.startsWith(UriUtils.URI_CONTENT_SCHEME)) {
        Cursor cursor = getContentResolver().query(Uri.parse(filePath), null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                Log_OC.v(TAG, "Display Name: " + displayName);

                displayName.replace(File.separatorChar, '_');
                displayName.replace(File.pathSeparatorChar, '_');
                remotePath += displayName + DisplayUtils.getComposedFileExtension(filePath);

            }
            // and what happens in case of error?; wrong target name for the upload
        } finally {
            cursor.close();
        }

    } else {
        remotePath += new File(filePath).getName();
    }

    i.putExtra(FileUploader.KEY_LOCAL_FILE, filePath);
    i.putExtra(FileUploader.KEY_REMOTE_FILE, remotePath);
    i.putExtra(FileUploader.KEY_MIME_TYPE, mimeType);
    i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
    if (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)
        i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
    startService(i);
}

From source file:fr.novia.zaproxyplugin.ZAProxy.java

/**
 * Return the ZAProxy program name with separator prefix (\zap.bat or /zap.sh) depending of the build node and the OS.
 * /*w  ww  . ja  v  a2 s .  c  o m*/
 * @param build
 * @return the ZAProxy program name with separator prefix (\zap.bat or /zap.sh)
 * @throws IOException
 * @throws InterruptedException
 */
private String getZAPProgramNameWithSeparator(AbstractBuild<?, ?> build)
        throws IOException, InterruptedException {
    Node node = build.getBuiltOn();
    String zapProgramName = "";

    // Append zap program following Master/Slave and Windows/Unix
    if ("".equals(node.getNodeName())) { // Master
        if (File.pathSeparatorChar == ':') { // UNIX
            zapProgramName = "/" + ZAP_PROG_NAME_SH;
        } else { // Windows (pathSeparatorChar == ';')
            zapProgramName = "\\" + ZAP_PROG_NAME_BAT;
        }
    } else { // Slave
        if ("Unix".equals(((SlaveComputer) node.toComputer()).getOSDescription())) {
            zapProgramName = "/" + ZAP_PROG_NAME_SH;
        } else {
            zapProgramName = "\\" + ZAP_PROG_NAME_BAT;
        }
    }
    return zapProgramName;
}

From source file:org.apache.nifi.registry.bootstrap.RunNiFiRegistry.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public void start() throws IOException, InterruptedException {
    final Integer port = getCurrentPort(cmdLogger);
    if (port != null) {
        cmdLogger.info("Apache NiFi Registry is already running, listening to Bootstrap on port " + port);
        return;/*from w ww .j a va 2 s  .co  m*/
    }

    final File prevLockFile = getLockFile(cmdLogger);
    if (prevLockFile.exists() && !prevLockFile.delete()) {
        cmdLogger.warn("Failed to delete previous lock file {}; this file should be cleaned up manually",
                prevLockFile);
    }

    final ProcessBuilder builder = new ProcessBuilder();

    if (!bootstrapConfigFile.exists()) {
        throw new FileNotFoundException(bootstrapConfigFile.getAbsolutePath());
    }

    final Properties properties = new Properties();
    try (final FileInputStream fis = new FileInputStream(bootstrapConfigFile)) {
        properties.load(fis);
    }

    final Map<String, String> props = new HashMap<>();
    props.putAll((Map) properties);

    final String specifiedWorkingDir = props.get("working.dir");
    if (specifiedWorkingDir != null) {
        builder.directory(new File(specifiedWorkingDir));
    }

    final File bootstrapConfigAbsoluteFile = bootstrapConfigFile.getAbsoluteFile();
    final File binDir = bootstrapConfigAbsoluteFile.getParentFile();
    final File workingDir = binDir.getParentFile();

    if (specifiedWorkingDir == null) {
        builder.directory(workingDir);
    }

    final String nifiRegistryLogDir = replaceNull(
            System.getProperty("org.apache.nifi.registry.bootstrap.config.log.dir"), DEFAULT_LOG_DIR).trim();

    final String libFilename = replaceNull(props.get("lib.dir"), "./lib").trim();
    File libDir = getFile(libFilename, workingDir);
    File libSharedDir = getFile(libFilename + "/shared", workingDir);

    final String confFilename = replaceNull(props.get("conf.dir"), "./conf").trim();
    File confDir = getFile(confFilename, workingDir);

    String nifiRegistryPropsFilename = props.get("props.file");
    if (nifiRegistryPropsFilename == null) {
        if (confDir.exists()) {
            nifiRegistryPropsFilename = new File(confDir, "nifi-registry.properties").getAbsolutePath();
        } else {
            nifiRegistryPropsFilename = DEFAULT_CONFIG_FILE;
        }
    }

    nifiRegistryPropsFilename = nifiRegistryPropsFilename.trim();

    final List<String> javaAdditionalArgs = new ArrayList<>();
    for (final Map.Entry<String, String> entry : props.entrySet()) {
        final String key = entry.getKey();
        final String value = entry.getValue();

        if (key.startsWith("java.arg")) {
            javaAdditionalArgs.add(value);
        }
    }

    final File[] libSharedFiles = libSharedDir.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(final File dir, final String filename) {
            return filename.toLowerCase().endsWith(".jar");
        }
    });

    if (libSharedFiles == null || libSharedFiles.length == 0) {
        throw new RuntimeException("Could not find lib shared directory at " + libSharedDir.getAbsolutePath());
    }

    final File[] libFiles = libDir.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(final File dir, final String filename) {
            return filename.toLowerCase().endsWith(".jar");
        }
    });

    if (libFiles == null || libFiles.length == 0) {
        throw new RuntimeException("Could not find lib directory at " + libDir.getAbsolutePath());
    }

    final File[] confFiles = confDir.listFiles();
    if (confFiles == null || confFiles.length == 0) {
        throw new RuntimeException("Could not find conf directory at " + confDir.getAbsolutePath());
    }

    final List<String> cpFiles = new ArrayList<>(confFiles.length + libFiles.length + libSharedFiles.length);
    cpFiles.add(confDir.getAbsolutePath());
    for (final File file : libSharedFiles) {
        cpFiles.add(file.getAbsolutePath());
    }
    for (final File file : libFiles) {
        cpFiles.add(file.getAbsolutePath());
    }

    final StringBuilder classPathBuilder = new StringBuilder();
    for (int i = 0; i < cpFiles.size(); i++) {
        final String filename = cpFiles.get(i);
        classPathBuilder.append(filename);
        if (i < cpFiles.size() - 1) {
            classPathBuilder.append(File.pathSeparatorChar);
        }
    }

    final String classPath = classPathBuilder.toString();
    String javaCmd = props.get("java");
    if (javaCmd == null) {
        javaCmd = DEFAULT_JAVA_CMD;
    }
    if (javaCmd.equals(DEFAULT_JAVA_CMD)) {
        String javaHome = System.getenv("JAVA_HOME");
        if (javaHome != null) {
            String fileExtension = isWindows() ? ".exe" : "";
            File javaFile = new File(
                    javaHome + File.separatorChar + "bin" + File.separatorChar + "java" + fileExtension);
            if (javaFile.exists() && javaFile.canExecute()) {
                javaCmd = javaFile.getAbsolutePath();
            }
        }
    }

    final NiFiRegistryListener listener = new NiFiRegistryListener();
    final int listenPort = listener.start(this);

    final List<String> cmd = new ArrayList<>();

    cmd.add(javaCmd);
    cmd.add("-classpath");
    cmd.add(classPath);
    cmd.addAll(javaAdditionalArgs);
    cmd.add("-Dnifi.registry.properties.file.path=" + nifiRegistryPropsFilename);
    cmd.add("-Dnifi.registry.bootstrap.config.file.path=" + bootstrapConfigFile.getAbsolutePath());
    cmd.add("-Dnifi.registry.bootstrap.listen.port=" + listenPort);
    cmd.add("-Dapp=NiFiRegistry");
    cmd.add("-Dorg.apache.nifi.registry.bootstrap.config.log.dir=" + nifiRegistryLogDir);
    cmd.add("org.apache.nifi.registry.NiFiRegistry");

    builder.command(cmd);

    final StringBuilder cmdBuilder = new StringBuilder();
    for (final String s : cmd) {
        cmdBuilder.append(s).append(" ");
    }

    cmdLogger.info("Starting Apache NiFi Registry...");
    cmdLogger.info("Working Directory: {}", workingDir.getAbsolutePath());
    cmdLogger.info("Command: {}", cmdBuilder.toString());

    String gracefulShutdown = props.get(GRACEFUL_SHUTDOWN_PROP);
    if (gracefulShutdown == null) {
        gracefulShutdown = DEFAULT_GRACEFUL_SHUTDOWN_VALUE;
    }

    final int gracefulShutdownSeconds;
    try {
        gracefulShutdownSeconds = Integer.parseInt(gracefulShutdown);
    } catch (final NumberFormatException nfe) {
        throw new NumberFormatException("The '" + GRACEFUL_SHUTDOWN_PROP
                + "' property in Bootstrap Config File " + bootstrapConfigAbsoluteFile.getAbsolutePath()
                + " has an invalid value. Must be a non-negative integer");
    }

    if (gracefulShutdownSeconds < 0) {
        throw new NumberFormatException("The '" + GRACEFUL_SHUTDOWN_PROP
                + "' property in Bootstrap Config File " + bootstrapConfigAbsoluteFile.getAbsolutePath()
                + " has an invalid value. Must be a non-negative integer");
    }

    Process process = builder.start();
    handleLogging(process);
    Long pid = OSUtils.getProcessId(process, cmdLogger);
    if (pid == null) {
        cmdLogger.warn("Launched Apache NiFi Registry but could not determined the Process ID");
    } else {
        nifiRegistryPid = pid;
        final Properties pidProperties = new Properties();
        pidProperties.setProperty(PID_KEY, String.valueOf(nifiRegistryPid));
        savePidProperties(pidProperties, cmdLogger);
        cmdLogger.info("Launched Apache NiFi Registry with Process ID " + pid);
    }

    shutdownHook = new ShutdownHook(process, this, secretKey, gracefulShutdownSeconds, loggingExecutor);
    final Runtime runtime = Runtime.getRuntime();
    runtime.addShutdownHook(shutdownHook);

    final String hostname = getHostname();
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
    String now = sdf.format(System.currentTimeMillis());
    String user = System.getProperty("user.name");
    if (user == null || user.trim().isEmpty()) {
        user = "Unknown User";
    }

    while (true) {
        final boolean alive = isAlive(process);

        if (alive) {
            try {
                Thread.sleep(1000L);
            } catch (final InterruptedException ie) {
            }
        } else {
            try {
                runtime.removeShutdownHook(shutdownHook);
            } catch (final IllegalStateException ise) {
                // happens when already shutting down
            }

            now = sdf.format(System.currentTimeMillis());
            if (autoRestartNiFiRegistry) {
                final File statusFile = getStatusFile(defaultLogger);
                if (!statusFile.exists()) {
                    defaultLogger.info("Status File no longer exists. Will not restart NiFi Registry ");
                    return;
                }

                final File lockFile = getLockFile(defaultLogger);
                if (lockFile.exists()) {
                    defaultLogger.info("A shutdown was initiated. Will not restart NiFi Registry ");
                    return;
                }

                final boolean previouslyStarted = getNifiRegistryStarted();
                if (!previouslyStarted) {
                    defaultLogger.info("NiFi Registry never started. Will not restart NiFi Registry ");
                    return;
                } else {
                    setNiFiRegistryStarted(false);
                }

                defaultLogger.warn("Apache NiFi Registry appears to have died. Restarting...");
                process = builder.start();
                handleLogging(process);

                pid = OSUtils.getProcessId(process, defaultLogger);
                if (pid == null) {
                    cmdLogger.warn("Launched Apache NiFi Registry but could not obtain the Process ID");
                } else {
                    nifiRegistryPid = pid;
                    final Properties pidProperties = new Properties();
                    pidProperties.setProperty(PID_KEY, String.valueOf(nifiRegistryPid));
                    savePidProperties(pidProperties, defaultLogger);
                    cmdLogger.info("Launched Apache NiFi Registry with Process ID " + pid);
                }

                shutdownHook = new ShutdownHook(process, this, secretKey, gracefulShutdownSeconds,
                        loggingExecutor);
                runtime.addShutdownHook(shutdownHook);

                final boolean started = waitForStart();

                if (started) {
                    defaultLogger.info("Successfully started Apache NiFi Registry {}",
                            (pid == null ? "" : " with PID " + pid));
                } else {
                    defaultLogger.error("Apache NiFi Registry does not appear to have started");
                }
            } else {
                return;
            }
        }
    }
}

From source file:org.codehaus.enunciate.modules.gwt.GWTDeploymentModule.java

/**
 * Invokes GWTCompile on the apps specified in the configuration file.
 *///www .j av  a 2s .com
protected void doGWTCompile() throws EnunciateException, IOException {
    if (this.gwtHome == null) {
        throw new EnunciateException(
                "To compile a GWT app you must specify the GWT home directory, either in configuration, by setting the GWT_HOME environment variable, or setting the 'gwt.home' system property.");
    }

    File gwtHomeDir = new File(this.gwtHome);
    if (!gwtHomeDir.exists()) {
        throw new EnunciateException("GWT home not found ('" + gwtHomeDir.getAbsolutePath() + "').");
    }

    File gwtUserJar = new File(gwtHomeDir, "gwt-user.jar");
    if (!gwtUserJar.exists()) {
        warn("Unable to find %s. You may be GWT compile errors.", gwtUserJar.getAbsolutePath());
    }

    //now we have to find gwt-dev.jar.
    //start by assuming linux...
    File gwtDevJar = new File(gwtHomeDir, "gwt-dev.jar");
    if (!gwtDevJar.exists()) {
        File linuxDevJar = new File(gwtHomeDir, "gwt-dev-linux.jar");
        gwtDevJar = linuxDevJar;

        if (!gwtDevJar.exists()) {
            //linux not found. try mac...
            File macDevJar = new File(gwtHomeDir, "gwt-dev-mac.jar");
            gwtDevJar = macDevJar;

            if (!gwtDevJar.exists()) {
                //okay, we'll try windows if we have to...
                File windowsDevJar = new File(gwtHomeDir, "gwt-dev-windows.jar");
                gwtDevJar = windowsDevJar;

                if (!gwtDevJar.exists()) {
                    throw new EnunciateException(
                            String.format("Unable to find GWT dev jar. Looked for %s, %s, and %s.",
                                    linuxDevJar.getAbsolutePath(), macDevJar.getAbsolutePath(),
                                    windowsDevJar.getAbsolutePath()));
                }
            }
        }
    }

    boolean windows = false;
    File javaBinDir = new File(System.getProperty("java.home"), "bin");
    File javaExecutable = new File(javaBinDir, "java");
    if (!javaExecutable.exists()) {
        //append the "exe" for windows users.
        javaExecutable = new File(javaBinDir, "java.exe");
        windows = true;
    }

    String javaCommand = javaExecutable.getAbsolutePath();
    if (!javaExecutable.exists()) {
        warn("No java executable found in %s.  We'll just hope the environment is set up to execute 'java'...",
                javaBinDir.getAbsolutePath());
        javaCommand = "java";
    }

    StringBuilder classpath = new StringBuilder(enunciate.getEnunciateRuntimeClasspath());
    //append the client-side gwt directory.
    classpath.append(File.pathSeparatorChar).append(getClientSideGenerateDir().getAbsolutePath());
    //append the gwt-user jar.
    classpath.append(File.pathSeparatorChar).append(gwtUserJar.getAbsolutePath());
    //append the gwt-dev jar.
    classpath.append(File.pathSeparatorChar).append(gwtDevJar.getAbsolutePath());

    //so here's the GWT compile command:
    //java [extra jvm args] -cp [classpath] [compilerClass] -gen [gwt-gen-dir] -style [style] -out [out] [moduleName]
    List<String> jvmargs = getGwtCompileJVMArgs();
    List<String> compilerArgs = getGwtCompilerArgs();
    List<String> gwtcCommand = new ArrayList<String>(jvmargs.size() + compilerArgs.size() + 11);
    int argIndex = 0;
    gwtcCommand.add(argIndex++, javaCommand);
    for (String arg : jvmargs) {
        gwtcCommand.add(argIndex++, arg);
    }
    gwtcCommand.add(argIndex++, "-cp");
    int classpathArgIndex = argIndex; //app-specific arg.
    gwtcCommand.add(argIndex++, null);
    int compileClassIndex = argIndex;
    gwtcCommand.add(argIndex++, getGwtCompilerClass());
    gwtcCommand.add(argIndex++, "-gen");
    gwtcCommand.add(argIndex++, getGwtGenDir().getAbsolutePath());
    gwtcCommand.add(argIndex++, "-style");
    int styleArgIndex = argIndex;
    gwtcCommand.add(argIndex++, null); //app-specific arg.
    gwtcCommand.add(argIndex++, gwtVersionGreaterThan(1, 5) ? "-war" : "-out");
    int outArgIndex = argIndex;
    gwtcCommand.add(argIndex++, null); //app-specific arg.
    for (String arg : compilerArgs) {
        gwtcCommand.add(argIndex++, arg);
    }
    int moduleNameIndex = argIndex;
    gwtcCommand.add(argIndex, null); //module-specific arg.

    for (GWTApp gwtApp : gwtApps) {
        String appName = gwtApp.getName();
        File appSource = enunciate.resolvePath(gwtApp.getSrcDir());
        String style = gwtApp.getJavascriptStyle().toString();
        File appDir = getAppGenerateDir(appName);

        gwtcCommand.set(classpathArgIndex,
                classpath.toString() + File.pathSeparatorChar + appSource.getAbsolutePath());
        gwtcCommand.set(styleArgIndex, style);
        gwtcCommand.set(outArgIndex, appDir.getAbsolutePath());

        boolean upToDate = enunciate.isUpToDate(getClientSideGenerateDir(), appDir)
                && enunciate.isUpToDate(appSource, appDir);
        if (!upToDate) {
            for (GWTAppModule appModule : gwtApp.getModules()) {
                String moduleName = appModule.getName();

                gwtcCommand.set(moduleNameIndex, moduleName);
                debug("Executing GWTCompile for module '%s'...", moduleName);
                if (enunciate.isDebug()) {
                    StringBuilder command = new StringBuilder();
                    for (String commandPiece : gwtcCommand) {
                        command.append(' ').append(commandPiece);
                    }
                    debug("Executing GWTCompile for module %s with the command: %s", moduleName, command);
                }
                ProcessBuilder processBuilder = new ProcessBuilder(gwtcCommand);
                processBuilder.directory(getGenerateDir());
                processBuilder.redirectErrorStream(true);
                Process process = processBuilder.start();
                BufferedReader procReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line = procReader.readLine();
                while (line != null) {
                    line = URLDecoder.decode(line, "utf-8").replaceAll("%", "%%").trim(); //GWT URL-encodes spaces and other weird Windows characters.
                    info(line);
                    line = procReader.readLine();
                }
                int procCode;
                try {
                    procCode = process.waitFor();
                } catch (InterruptedException e1) {
                    throw new EnunciateException("Unexpected inturruption of the GWT compile process.");
                }

                if (procCode != 0) {
                    throw new EnunciateException("GWT compile failed for module " + moduleName);
                }

                if (!gwtVersionGreaterThan(1, 5)) {
                    File moduleOutputDir = appDir;
                    String outputPath = appModule.getOutputPath();
                    if ((outputPath != null) && (!"".equals(outputPath.trim()))) {
                        moduleOutputDir = new File(appDir, outputPath);
                    }

                    File moduleGenDir = new File(appDir, moduleName);
                    if (!moduleOutputDir.equals(moduleGenDir)) {
                        moduleOutputDir.mkdirs();
                        enunciate.copyDir(moduleGenDir, moduleOutputDir);
                        deleteDir(moduleGenDir);
                    }
                }

                StringBuilder shellCommand = new StringBuilder();
                for (int i = 0; i < moduleNameIndex; i++) {
                    String commandArg = gwtcCommand.get(i);
                    if (i == compileClassIndex) {
                        commandArg = gwtVersionGreaterThan(1, 5) ? "com.google.gwt.dev.HostedMode"
                                : "com.google.gwt.dev.GWTShell";
                    } else if (commandArg.indexOf(' ') >= 0) {
                        commandArg = '"' + commandArg + '"';
                    }

                    shellCommand.append(commandArg).append(' ');
                }

                //add any extra args before the module name.
                shellCommand.append(windows ? "%*" : "$@").append(' ');

                String shellPage = getModuleId(moduleName) + ".html";
                if (appModule.getShellPage() != null) {
                    shellPage = appModule.getShellPage();
                }

                if (!gwtVersionGreaterThan(1, 5)) {
                    //when invoking the shell for GWT 1.4 or 1.5, it requires a URL to load.
                    //The URL is the [moduleName]/[shellPage.html]
                    shellCommand.append(moduleName).append('/').append(shellPage);
                } else {
                    //as of 1.6, you invoke it with -startupUrl [shellPage.html] [moduleName]
                    shellCommand.append("-startupUrl ").append(shellPage).append(' ').append(moduleName);
                }

                File scriptFile = getShellScriptFile(appName, moduleName);
                scriptFile.getParentFile().mkdirs();
                FileWriter writer = new FileWriter(scriptFile);
                writer.write(shellCommand.toString());
                writer.flush();
                writer.close();

                File shellFile = getShellScriptFile(appName, moduleName);
                if (shellFile.exists()) {
                    StringBuilder scriptArtifactId = new StringBuilder();
                    if ((appName != null) && (appName.trim().length() > 0)) {
                        scriptArtifactId.append(appName).append('.');
                    }
                    scriptArtifactId.append(moduleName).append(".shell");
                    getEnunciate()
                            .addArtifact(new FileArtifact(getName(), scriptArtifactId.toString(), shellFile));
                } else {
                    debug("No GWT shell script file exists at %s.  No artifact added.", shellFile);
                }
            }
        } else {
            info("Skipping GWT compile for app %s as everything appears up-to-date...", appName);
        }
    }
}

From source file:org.apache.nifi.bootstrap.RunNiFi.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public void start() throws IOException, InterruptedException {
    final Integer port = getCurrentPort(cmdLogger);
    if (port != null) {
        cmdLogger.info("Apache NiFi is already running, listening to Bootstrap on port " + port);
        return;//  w  w  w  .j  a v  a  2s  .  c om
    }

    final File prevLockFile = getLockFile(cmdLogger);
    if (prevLockFile.exists() && !prevLockFile.delete()) {
        cmdLogger.warn("Failed to delete previous lock file {}; this file should be cleaned up manually",
                prevLockFile);
    }

    final ProcessBuilder builder = new ProcessBuilder();

    if (!bootstrapConfigFile.exists()) {
        throw new FileNotFoundException(bootstrapConfigFile.getAbsolutePath());
    }

    final Properties properties = new Properties();
    try (final FileInputStream fis = new FileInputStream(bootstrapConfigFile)) {
        properties.load(fis);
    }

    final Map<String, String> props = new HashMap<>();
    props.putAll((Map) properties);

    final String specifiedWorkingDir = props.get("working.dir");
    if (specifiedWorkingDir != null) {
        builder.directory(new File(specifiedWorkingDir));
    }

    final File bootstrapConfigAbsoluteFile = bootstrapConfigFile.getAbsoluteFile();
    final File binDir = bootstrapConfigAbsoluteFile.getParentFile();
    final File workingDir = binDir.getParentFile();

    if (specifiedWorkingDir == null) {
        builder.directory(workingDir);
    }

    final String nifiLogDir = replaceNull(System.getProperty("org.apache.nifi.bootstrap.config.log.dir"),
            DEFAULT_LOG_DIR).trim();

    final String libFilename = replaceNull(props.get("lib.dir"), "./lib").trim();
    File libDir = getFile(libFilename, workingDir);

    final String confFilename = replaceNull(props.get("conf.dir"), "./conf").trim();
    File confDir = getFile(confFilename, workingDir);

    String nifiPropsFilename = props.get("props.file");
    if (nifiPropsFilename == null) {
        if (confDir.exists()) {
            nifiPropsFilename = new File(confDir, "nifi.properties").getAbsolutePath();
        } else {
            nifiPropsFilename = DEFAULT_CONFIG_FILE;
        }
    }

    nifiPropsFilename = nifiPropsFilename.trim();

    final List<String> javaAdditionalArgs = new ArrayList<>();
    for (final Map.Entry<String, String> entry : props.entrySet()) {
        final String key = entry.getKey();
        final String value = entry.getValue();

        if (key.startsWith("java.arg")) {
            javaAdditionalArgs.add(value);
        }
    }

    final File[] libFiles = libDir.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(final File dir, final String filename) {
            return filename.toLowerCase().endsWith(".jar");
        }
    });

    if (libFiles == null || libFiles.length == 0) {
        throw new RuntimeException("Could not find lib directory at " + libDir.getAbsolutePath());
    }

    final File[] confFiles = confDir.listFiles();
    if (confFiles == null || confFiles.length == 0) {
        throw new RuntimeException("Could not find conf directory at " + confDir.getAbsolutePath());
    }

    final List<String> cpFiles = new ArrayList<>(confFiles.length + libFiles.length);
    cpFiles.add(confDir.getAbsolutePath());
    for (final File file : libFiles) {
        cpFiles.add(file.getAbsolutePath());
    }

    final StringBuilder classPathBuilder = new StringBuilder();
    for (int i = 0; i < cpFiles.size(); i++) {
        final String filename = cpFiles.get(i);
        classPathBuilder.append(filename);
        if (i < cpFiles.size() - 1) {
            classPathBuilder.append(File.pathSeparatorChar);
        }
    }

    final String classPath = classPathBuilder.toString();
    String javaCmd = props.get("java");
    if (javaCmd == null) {
        javaCmd = DEFAULT_JAVA_CMD;
    }
    if (javaCmd.equals(DEFAULT_JAVA_CMD)) {
        String javaHome = System.getenv("JAVA_HOME");
        if (javaHome != null) {
            String fileExtension = isWindows() ? ".exe" : "";
            File javaFile = new File(
                    javaHome + File.separatorChar + "bin" + File.separatorChar + "java" + fileExtension);
            if (javaFile.exists() && javaFile.canExecute()) {
                javaCmd = javaFile.getAbsolutePath();
            }
        }
    }

    final NiFiListener listener = new NiFiListener();
    final int listenPort = listener.start(this);

    final List<String> cmd = new ArrayList<>();

    cmd.add(javaCmd);
    cmd.add("-classpath");
    cmd.add(classPath);
    cmd.addAll(javaAdditionalArgs);
    cmd.add("-Dnifi.properties.file.path=" + nifiPropsFilename);
    cmd.add("-Dnifi.bootstrap.listen.port=" + listenPort);
    cmd.add("-Dapp=NiFi");
    cmd.add("-Dorg.apache.nifi.bootstrap.config.log.dir=" + nifiLogDir);
    cmd.add("org.apache.nifi.NiFi");
    if (props.containsKey(NIFI_BOOTSTRAP_SENSITIVE_KEY)
            && !StringUtils.isBlank(props.get(NIFI_BOOTSTRAP_SENSITIVE_KEY))) {
        cmd.add("-k " + props.get(NIFI_BOOTSTRAP_SENSITIVE_KEY));
    }

    builder.command(cmd);

    final StringBuilder cmdBuilder = new StringBuilder();
    for (final String s : cmd) {
        // Mask the key
        if (s.startsWith("-k ")) {
            cmdBuilder.append("-k ****");
        } else {
            cmdBuilder.append(s).append(" ");
        }
    }

    cmdLogger.info("Starting Apache NiFi...");
    cmdLogger.info("Working Directory: {}", workingDir.getAbsolutePath());
    cmdLogger.info("Command: {}", cmdBuilder.toString());

    String gracefulShutdown = props.get(GRACEFUL_SHUTDOWN_PROP);
    if (gracefulShutdown == null) {
        gracefulShutdown = DEFAULT_GRACEFUL_SHUTDOWN_VALUE;
    }

    final int gracefulShutdownSeconds;
    try {
        gracefulShutdownSeconds = Integer.parseInt(gracefulShutdown);
    } catch (final NumberFormatException nfe) {
        throw new NumberFormatException("The '" + GRACEFUL_SHUTDOWN_PROP
                + "' property in Bootstrap Config File " + bootstrapConfigAbsoluteFile.getAbsolutePath()
                + " has an invalid value. Must be a non-negative integer");
    }

    if (gracefulShutdownSeconds < 0) {
        throw new NumberFormatException("The '" + GRACEFUL_SHUTDOWN_PROP
                + "' property in Bootstrap Config File " + bootstrapConfigAbsoluteFile.getAbsolutePath()
                + " has an invalid value. Must be a non-negative integer");
    }

    Process process = builder.start();
    handleLogging(process);
    Long pid = getPid(process, cmdLogger);
    if (pid == null) {
        cmdLogger.info("Launched Apache NiFi but could not determined the Process ID");
    } else {
        nifiPid = pid;
        final Properties pidProperties = new Properties();
        pidProperties.setProperty(PID_KEY, String.valueOf(nifiPid));
        savePidProperties(pidProperties, cmdLogger);
        cmdLogger.info("Launched Apache NiFi with Process ID " + pid);
    }

    shutdownHook = new ShutdownHook(process, this, secretKey, gracefulShutdownSeconds, loggingExecutor);
    final Runtime runtime = Runtime.getRuntime();
    runtime.addShutdownHook(shutdownHook);

    final String hostname = getHostname();
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
    String now = sdf.format(System.currentTimeMillis());
    String user = System.getProperty("user.name");
    if (user == null || user.trim().isEmpty()) {
        user = "Unknown User";
    }
    serviceManager.notify(NotificationType.NIFI_STARTED, "NiFi Started on Host " + hostname,
            "Hello,\n\nApache NiFi has been started on host " + hostname + " at " + now + " by user " + user);

    while (true) {
        final boolean alive = isAlive(process);

        if (alive) {
            try {
                Thread.sleep(1000L);
            } catch (final InterruptedException ie) {
            }
        } else {
            try {
                runtime.removeShutdownHook(shutdownHook);
            } catch (final IllegalStateException ise) {
                // happens when already shutting down
            }

            now = sdf.format(System.currentTimeMillis());
            if (autoRestartNiFi) {
                final File statusFile = getStatusFile(defaultLogger);
                if (!statusFile.exists()) {
                    defaultLogger.info("Status File no longer exists. Will not restart NiFi");
                    return;
                }

                final File lockFile = getLockFile(defaultLogger);
                if (lockFile.exists()) {
                    defaultLogger.info("A shutdown was initiated. Will not restart NiFi");
                    return;
                }

                final boolean previouslyStarted = getNifiStarted();
                if (!previouslyStarted) {
                    defaultLogger.info("NiFi never started. Will not restart NiFi");
                    return;
                } else {
                    setNiFiStarted(false);
                }

                defaultLogger.warn("Apache NiFi appears to have died. Restarting...");
                process = builder.start();
                handleLogging(process);

                pid = getPid(process, defaultLogger);
                if (pid == null) {
                    cmdLogger.info("Launched Apache NiFi but could not obtain the Process ID");
                } else {
                    nifiPid = pid;
                    final Properties pidProperties = new Properties();
                    pidProperties.setProperty(PID_KEY, String.valueOf(nifiPid));
                    savePidProperties(pidProperties, defaultLogger);
                    cmdLogger.info("Launched Apache NiFi with Process ID " + pid);
                }

                shutdownHook = new ShutdownHook(process, this, secretKey, gracefulShutdownSeconds,
                        loggingExecutor);
                runtime.addShutdownHook(shutdownHook);

                final boolean started = waitForStart();

                if (started) {
                    defaultLogger.info("Successfully started Apache NiFi{}",
                            (pid == null ? "" : " with PID " + pid));
                    // We are expected to restart nifi, so send a notification that it died. If we are not restarting nifi,
                    // then this means that we are intentionally stopping the service.
                    serviceManager.notify(NotificationType.NIFI_DIED, "NiFi Died on Host " + hostname,
                            "Hello,\n\nIt appears that Apache NiFi has died on host " + hostname + " at " + now
                                    + "; automatically restarting NiFi");
                } else {
                    defaultLogger.error("Apache NiFi does not appear to have started");
                    // We are expected to restart nifi, so send a notification that it died. If we are not restarting nifi,
                    // then this means that we are intentionally stopping the service.
                    serviceManager.notify(NotificationType.NIFI_DIED, "NiFi Died on Host " + hostname,
                            "Hello,\n\nIt appears that Apache NiFi has died on host " + hostname + " at " + now
                                    + ". Attempted to restart NiFi but the services does not appear to have restarted!");
                }
            } else {
                return;
            }
        }
    }
}

From source file:edu.uci.ics.asterix.aoya.AsterixYARNClient.java

/**
 * Submits the request to start the AsterixApplicationMaster to the YARN ResourceManager.
 * /*from w ww  .j  a va 2 s  .  c o  m*/
 * @param app
 *            The application attempt handle.
 * @param resources
 *            Resources to be distributed as part of the container launch
 * @param mode
 *            The mode of the ApplicationMaster
 * @return The application ID of the new Asterix instance.
 * @throws IOException
 * @throws YarnException
 */

public ApplicationId deployAM(YarnClientApplication app, List<DFSResourceCoordinate> resources, Mode mode)
        throws IOException, YarnException {

    // Set up the container launch context for the application master
    ContainerLaunchContext amContainer = Records.newRecord(ContainerLaunchContext.class);

    ApplicationSubmissionContext appContext = app.getApplicationSubmissionContext();

    // Set local resource info into app master container launch context
    Map<String, LocalResource> localResources = new HashMap<String, LocalResource>();
    for (DFSResourceCoordinate res : resources) {
        localResources.put(res.name, res.res);
    }
    amContainer.setLocalResources(localResources);
    // Set the env variables to be setup in the env where the application
    // master will be run
    LOG.info("Set the environment for the application master");
    Map<String, String> env = new HashMap<String, String>();

    // using the env info, the application master will create the correct
    // local resource for the
    // eventual containers that will be launched to execute the shell
    // scripts
    for (DFSResourceCoordinate res : resources) {
        if (res.envs == null) { //some entries may not have environment variables.
            continue;
        }
        for (Map.Entry<String, String> e : res.envs.entrySet()) {
            env.put(e.getValue(), e.getKey());
        }
    }
    //this is needed for when the RM address isn't known from the environment of the AM
    env.put(AConstants.RMADDRESS, conf.get("yarn.resourcemanager.address"));
    env.put(AConstants.RMSCHEDULERADDRESS, conf.get("yarn.resourcemanager.scheduler.address"));
    ///add miscellaneous environment variables.
    env.put(AConstants.INSTANCESTORE, CONF_DIR_REL + instanceFolder);
    env.put(AConstants.DFS_BASE, FileSystem.get(conf).getHomeDirectory().toUri().toString());
    env.put(AConstants.CC_JAVA_OPTS, ccJavaOpts);
    env.put(AConstants.NC_JAVA_OPTS, ncJavaOpts);

    // Add AppMaster.jar location to classpath
    // At some point we should not be required to add
    // the hadoop specific classpaths to the env.
    // It should be provided out of the box.
    // For now setting all required classpaths including
    // the classpath to "." for the application jar
    StringBuilder classPathEnv = new StringBuilder("").append("./*");
    for (String c : conf.getStrings(YarnConfiguration.YARN_APPLICATION_CLASSPATH,
            YarnConfiguration.DEFAULT_YARN_APPLICATION_CLASSPATH)) {
        classPathEnv.append(File.pathSeparatorChar);
        classPathEnv.append(c.trim());
    }
    classPathEnv.append(File.pathSeparatorChar).append("." + File.separator + "log4j.properties");

    // add the runtime classpath needed for tests to work
    if (conf.getBoolean(YarnConfiguration.IS_MINI_YARN_CLUSTER, false)) {
        LOG.info("In YARN MiniCluster");
        classPathEnv.append(System.getProperty("path.separator"));
        classPathEnv.append(System.getProperty("java.class.path"));
        env.put("HADOOP_CONF_DIR", System.getProperty("user.dir") + File.separator + "target" + File.separator);
    }
    LOG.info("AM Classpath:" + classPathEnv.toString());
    env.put("CLASSPATH", classPathEnv.toString());

    amContainer.setEnvironment(env);

    // Set the necessary command to execute the application master
    Vector<CharSequence> vargs = new Vector<CharSequence>(30);

    // Set java executable command
    LOG.info("Setting up app master command");
    vargs.add(JAVA_HOME + File.separator + "bin" + File.separator + "java");
    // Set class name
    vargs.add(appMasterMainClass);
    //Set params for Application Master
    if (debugFlag) {
        vargs.add("-debug");
    }
    if (mode == Mode.DESTROY) {
        vargs.add("-obliterate");
    } else if (mode == Mode.BACKUP) {
        vargs.add("-backup");
    } else if (mode == Mode.RESTORE) {
        vargs.add("-restore " + snapName);
    } else if (mode == Mode.INSTALL) {
        vargs.add("-initial ");
    }
    if (refresh) {
        vargs.add("-refresh");
    }
    //vargs.add("/bin/ls -alh asterix-server.zip/repo");
    vargs.add("1>" + ApplicationConstants.LOG_DIR_EXPANSION_VAR + File.separator + "AppMaster.stdout");
    vargs.add("2>" + ApplicationConstants.LOG_DIR_EXPANSION_VAR + File.separator + "AppMaster.stderr");
    // Get final commmand
    StringBuilder command = new StringBuilder();
    for (CharSequence str : vargs) {
        command.append(str).append(" ");
    }

    LOG.info("Completed setting up app master command " + command.toString());
    List<String> commands = new ArrayList<String>();
    commands.add(command.toString());
    amContainer.setCommands(commands);

    // Set up resource type requirements
    // For now, only memory is supported so we set memory requirements
    Resource capability = Records.newRecord(Resource.class);
    capability.setMemory(amMemory);
    appContext.setResource(capability);

    // Service data is a binary blob that can be passed to the application
    // Not needed in this scenario
    // amContainer.setServiceData(serviceData);

    // The following are not required for launching an application master
    // amContainer.setContainerId(containerId);

    appContext.setAMContainerSpec(amContainer);

    // Set the priority for the application master
    Priority pri = Records.newRecord(Priority.class);
    // TODO - what is the range for priority? how to decide?
    pri.setPriority(amPriority);
    appContext.setPriority(pri);

    // Set the queue to which this application is to be submitted in the RM
    appContext.setQueue(amQueue);

    // Submit the application to the applications manager
    // SubmitApplicationResponse submitResp =
    // applicationsManager.submitApplication(appRequest);
    // Ignore the response as either a valid response object is returned on
    // success
    // or an exception thrown to denote some form of a failure
    LOG.info("Submitting application to ASM");

    yarnClient.submitApplication(appContext);

    //now write the instance lock
    if (mode == Mode.INSTALL || mode == Mode.START) {
        FileSystem fs = FileSystem.get(conf);
        Path lock = new Path(fs.getHomeDirectory(), CONF_DIR_REL + instanceFolder + instanceLock);
        if (fs.exists(lock)) {
            throw new IllegalStateException("Somehow, this instance has been launched twice. ");
        }
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fs.create(lock, true)));
        try {
            out.write(app.getApplicationSubmissionContext().getApplicationId().toString());
            out.close();
        } finally {
            out.close();
        }
    }
    return app.getApplicationSubmissionContext().getApplicationId();

}

From source file:org.apache.nifi.minifi.bootstrap.RunMiNiFi.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public Tuple<ProcessBuilder, Process> startMiNiFi() throws IOException, InterruptedException {
    final Integer port = getCurrentPort(cmdLogger);
    if (port != null) {
        cmdLogger.info("Apache MiNiFi is already running, listening to Bootstrap on port " + port);
        return null;
    }//  w ww.  j  a  va2s .  c  o m

    final File prevLockFile = getLockFile(cmdLogger);
    if (prevLockFile.exists() && !prevLockFile.delete()) {
        cmdLogger.warn("Failed to delete previous lock file {}; this file should be cleaned up manually",
                prevLockFile);
    }

    final ProcessBuilder builder = new ProcessBuilder();

    final Map<String, String> props = readProperties();

    final String specifiedWorkingDir = props.get("working.dir");
    if (specifiedWorkingDir != null) {
        builder.directory(new File(specifiedWorkingDir));
    }

    final File bootstrapConfigAbsoluteFile = bootstrapConfigFile.getAbsoluteFile();
    final File binDir = bootstrapConfigAbsoluteFile.getParentFile();
    final File workingDir = binDir.getParentFile();

    if (specifiedWorkingDir == null) {
        builder.directory(workingDir);
    }

    final String minifiLogDir = replaceNull(
            System.getProperty("org.apache.nifi.minifi.bootstrap.config.log.dir"), DEFAULT_LOG_DIR).trim();

    final String libFilename = replaceNull(props.get("lib.dir"), "./lib").trim();
    File libDir = getFile(libFilename, workingDir);

    final String confFilename = replaceNull(props.get(CONF_DIR_KEY), "./conf").trim();
    File confDir = getFile(confFilename, workingDir);

    String minifiPropsFilename = props.get("props.file");
    if (minifiPropsFilename == null) {
        if (confDir.exists()) {
            minifiPropsFilename = new File(confDir, "nifi.properties").getAbsolutePath();
        } else {
            minifiPropsFilename = DEFAULT_CONFIG_FILE;
        }
    }

    minifiPropsFilename = minifiPropsFilename.trim();

    final List<String> javaAdditionalArgs = new ArrayList<>();
    for (final Entry<String, String> entry : props.entrySet()) {
        final String key = entry.getKey();
        final String value = entry.getValue();

        if (key.startsWith("java.arg")) {
            javaAdditionalArgs.add(value);
        }
    }

    final File[] libFiles = libDir.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(final File dir, final String filename) {
            return filename.toLowerCase().endsWith(".jar");
        }
    });

    if (libFiles == null || libFiles.length == 0) {
        throw new RuntimeException("Could not find lib directory at " + libDir.getAbsolutePath());
    }

    final File[] confFiles = confDir.listFiles();
    if (confFiles == null || confFiles.length == 0) {
        throw new RuntimeException("Could not find conf directory at " + confDir.getAbsolutePath());
    }

    final List<String> cpFiles = new ArrayList<>(confFiles.length + libFiles.length);
    cpFiles.add(confDir.getAbsolutePath());
    for (final File file : libFiles) {
        cpFiles.add(file.getAbsolutePath());
    }

    final StringBuilder classPathBuilder = new StringBuilder();
    for (int i = 0; i < cpFiles.size(); i++) {
        final String filename = cpFiles.get(i);
        classPathBuilder.append(filename);
        if (i < cpFiles.size() - 1) {
            classPathBuilder.append(File.pathSeparatorChar);
        }
    }

    final String classPath = classPathBuilder.toString();
    String javaCmd = props.get("java");
    if (javaCmd == null) {
        javaCmd = DEFAULT_JAVA_CMD;
    }
    if (javaCmd.equals(DEFAULT_JAVA_CMD)) {
        String javaHome = System.getenv("JAVA_HOME");
        if (javaHome != null) {
            String fileExtension = isWindows() ? ".exe" : "";
            File javaFile = new File(
                    javaHome + File.separatorChar + "bin" + File.separatorChar + "java" + fileExtension);
            if (javaFile.exists() && javaFile.canExecute()) {
                javaCmd = javaFile.getAbsolutePath();
            }
        }
    }

    final MiNiFiListener listener = new MiNiFiListener();
    final int listenPort = listener.start(this);

    final List<String> cmd = new ArrayList<>();

    cmd.add(javaCmd);
    cmd.add("-classpath");
    cmd.add(classPath);
    cmd.addAll(javaAdditionalArgs);
    cmd.add("-Dnifi.properties.file.path=" + minifiPropsFilename);
    cmd.add("-Dnifi.bootstrap.listen.port=" + listenPort);
    cmd.add("-Dapp=MiNiFi");
    cmd.add("-Dorg.apache.nifi.minifi.bootstrap.config.log.dir=" + minifiLogDir);
    cmd.add("org.apache.nifi.minifi.MiNiFi");

    builder.command(cmd);

    final StringBuilder cmdBuilder = new StringBuilder();
    for (final String s : cmd) {
        cmdBuilder.append(s).append(" ");
    }

    cmdLogger.info("Starting Apache MiNiFi...");
    cmdLogger.info("Working Directory: {}", workingDir.getAbsolutePath());
    cmdLogger.info("Command: {}", cmdBuilder.toString());

    Process process = builder.start();
    handleLogging(process);
    Long pid = getPid(process, cmdLogger);
    if (pid != null) {
        minifiPid = pid;
        final Properties minifiProps = new Properties();
        minifiProps.setProperty(PID_KEY, String.valueOf(minifiPid));
        saveProperties(minifiProps, cmdLogger);
    }

    gracefulShutdownSeconds = getGracefulShutdownSeconds(props, bootstrapConfigAbsoluteFile);
    shutdownHook = new ShutdownHook(process, this, secretKey, gracefulShutdownSeconds, loggingExecutor);
    final Runtime runtime = Runtime.getRuntime();
    runtime.addShutdownHook(shutdownHook);

    return new Tuple<ProcessBuilder, Process>(builder, process);
}

From source file:org.apache.flex.swf.io.SWFDump.java

public static String dirName(String path) {
    int end = path.lastIndexOf(File.pathSeparatorChar);

    if (File.pathSeparatorChar != '/') {
        // some of us are grouchy about unix paths not being
        // parsed since they are totally legit at the system
        // level of win32.
        int altend = path.lastIndexOf('/');
        if ((end == -1) || (altend < end))
            end = altend;//  w ww. j a v a  2s .c o m
    }

    if (end == -1)
        return "";
    else
        ++end;

    return path.substring(0, end);
}

From source file:com.openkm.cmis.CmisRepository.java

/**
 * Checks if the given name is valid for a file system.
 *//*from w  ww.  ja  v  a  2s  . c om*/
private static boolean isValidName(String name) {
    if ((name == null) || (name.length() == 0) || (name.indexOf(File.separatorChar) != -1)
            || (name.indexOf(File.pathSeparatorChar) != -1)) {
        return false;
    }

    return true;
}