Example usage for com.google.common.io FileWriteMode APPEND

List of usage examples for com.google.common.io FileWriteMode APPEND

Introduction

In this page you can find the example usage for com.google.common.io FileWriteMode APPEND.

Prototype

FileWriteMode APPEND

To view the source code for com.google.common.io FileWriteMode APPEND.

Click Source Link

Document

Specifies that writes to the opened file should append to the end of the file.

Usage

From source file:net.simon04.guavavfs.VirtualFiles.java

private static FileWriteMode[] modes(boolean append) {
    return append ? new FileWriteMode[] { FileWriteMode.APPEND } : new FileWriteMode[0];
}

From source file:org.dishevelled.compress.Sinks.java

/**
 * Create and return a new char sink with support for bgzf, gzip, or bzip2 compression for the specified file
 * or <code>stdout</code> if the file is null.
 *
 * @param file file, if any/*from w  w w. j  a  va2  s  . c  om*/
 * @param charset charset, must not be null
 * @param append true to append to the specified file
 * @return a new char sink with support for bgzf, gzip, or bzip2 compression for the specified file
 *    or <code>stdout</code> if the file is null
 * @throws IOException if an I/O error occurs
 */
public static CharSink charSink(@Nullable final File file, final Charset charset, final boolean append)
        throws IOException {
    checkNotNull(charset);
    if (file == null) {
        return outputStreamCharSink(System.out);
    } else if (isBgzfFile(file)) {
        return bgzfFileCharSink(file);
    } else if (isGzipFile(file)) {
        return gzipFileCharSink(file, append);
    } else if (isBzip2File(file)) {
        return bzip2FileCharSink(file, append);
    }
    return append ? Files.asCharSink(file, charset, FileWriteMode.APPEND) : Files.asCharSink(file, charset);
}

From source file:org.sosy_lab.cpachecker.appengine.server.resource.TaskExecutorServerResource.java

private void saveStackTrace(Throwable e) throws IOException {
    try (OutputStream out = errorPath.asByteSink(FileWriteMode.APPEND).openStream()) {
        PrintStream ps = new PrintStream(out);
        e.printStackTrace(ps);/*  w w  w .  jav  a 2s.  c  om*/
        ps.flush();
    }
}

From source file:eu.eubrazilcc.lvl.core.entrez.EntrezHelper.java

private void efetchFlatFiles(final List<String> ids, final int retstart, final int retmax, final File directory)
        throws Exception {
    // save the bulk of files to a temporary file
    final File tmpFile = createTempFile("gb-", ".tmp", directory);
    final String idsParam = Joiner.on(",").skipNulls().join(ids);
    LOGGER.trace("Fetching " + ids.size() + " files from GenBank, retstart=" + retstart + ", retmax=" + retmax
            + ", file=" + tmpFile.getPath());
    httpClient.request(EFETCH_BASE_URI).post()
            .bodyForm(efetchForm(NUCLEOTIDE_DB, idsParam, retstart, retmax, "text").build())
            .saveContent(tmpFile, true);
    // go over the file extracting the sequences
    final ListenableFuture<String[]> future = TASK_RUNNER.submit(new Callable<String[]>() {
        @Override/*from   w  w w  .  j  a  v  a  2 s  . c  o m*/
        public String[] call() throws Exception {
            final Set<String> files = newHashSet();
            final BufferedReader reader = newBufferedReader(tmpFile.toPath(), DEFAULT_CHARSET);
            int i = -1;
            File file = null;
            ByteSink sink = null;
            String line = null;
            while ((line = reader.readLine()) != null) {
                // start parsing a new fragment
                if (file == null && i < ids.size() - 1) {
                    file = new File(directory, ids.get(++i) + ".gb");
                    sink = asByteSink(file, FileWriteMode.APPEND);
                    LOGGER.info("Processing file: " + file.getCanonicalPath());
                }
                if (i < ids.size()) {
                    // write non-empty lines to the file
                    if (isNotBlank(line)) {
                        if (sink != null) {
                            sink.write((line + "\n").getBytes(DEFAULT_CHARSET));
                        } else {
                            LOGGER.warn("Ingoring line when all files were closed: " + line);
                        }
                    }
                    // process line
                    if (line.startsWith("VERSION    ")) {
                        checkState(line.contains(ids.get(i)), "Id not found in the VERSION section");
                    } else if (line.startsWith("//")) {
                        files.add(file.getCanonicalPath());
                        file = null;
                    }
                } else {
                    if (isNotBlank(line)) {
                        LOGGER.warn("Ingoring line after all sequences were processed: " + line);
                    }
                }
            }
            return files.toArray(new String[files.size()]);
        }
    });
    addCallback(future, new FutureCallback<String[]>() {
        @Override
        public void onSuccess(final String[] result) {
            LOGGER.info("One bulk sequence file was processed successfully: " + tmpFile.getName()
                    + ", number of created files: " + result.length);
            deleteQuietly(tmpFile);
        }

        @Override
        public void onFailure(final Throwable error) {
            LOGGER.error("Failed to process bulk sequence file " + tmpFile.getName(), error);
        }
    });
    // wait for files to be processed
    future.get();
}

From source file:org.codehaus.mojo.nbm.CreateWebstartAppMojo.java

/**
 *
 * @throws org.apache.maven.plugin.MojoExecutionException
 * @throws org.apache.maven.plugin.MojoFailureException
 *//*w ww .j av  a 2s.c o m*/
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if ("none".equalsIgnoreCase(includeLocales)) {
        includeLocales = "";
    }

    if (signingThreads < 1) {
        signingThreads = Runtime.getRuntime().availableProcessors();
    }

    if ((signingMaximumThreads > 0) && (signingThreads > signingMaximumThreads)) {
        signingThreads = signingMaximumThreads;
    }

    getLog().info("Using " + signingThreads + " signing threads.");

    if (!"nbm-application".equals(project.getPackaging())) {
        throw new MojoExecutionException(
                "This goal only makes sense on project with nbm-application packaging.");
    }

    final Project antProject = antProject();

    getLog().warn(
            "WARNING: Unsigned and self-signed WebStart applications are deprecated from JDK7u21 onwards. To ensure future correct functionality please use trusted certificate.");

    if (keystore != null && keystorealias != null && keystorepassword != null) {
        File ks = new File(keystore);
        if (!ks.exists()) {
            throw new MojoFailureException("Cannot find keystore file at " + ks.getAbsolutePath());
        } else {
            //proceed..
        }
    } else if (keystore != null || keystorepassword != null || keystorealias != null) {
        throw new MojoFailureException(
                "If you want to sign the jnlp application, you need to define all three keystore related parameters.");
    } else {
        File generatedKeystore = new File(outputDirectory, "generated.keystore");
        if (!generatedKeystore.exists()) {
            getLog().warn("Keystore related parameters not set, generating a default keystore.");
            GenerateKey genTask = (GenerateKey) antProject.createTask("genkey");
            genTask.setAlias("jnlp");
            genTask.setStorepass("netbeans");
            genTask.setDname("CN=" + System.getProperty("user.name"));
            genTask.setKeystore(generatedKeystore.getAbsolutePath());
            genTask.execute();
        }
        keystore = generatedKeystore.getAbsolutePath();
        keystorepassword = "netbeans";
        keystorealias = "jnlp";
    }

    Taskdef taskdef = (Taskdef) antProject.createTask("taskdef");
    taskdef.setClassname(MakeJnlp2.class.getName());
    taskdef.setName("makejnlp");
    taskdef.execute();

    taskdef = (Taskdef) antProject.createTask("taskdef");
    taskdef.setClassname(Jar.class.getName());
    taskdef.setName("jar");
    taskdef.execute();

    taskdef = (Taskdef) antProject.createTask("taskdef");
    taskdef.setClassname(VerifyJNLP.class.getName());
    taskdef.setName("verifyjnlp");
    taskdef.execute();
    // +p

    try {
        final File webstartBuildDir = new File(
                outputDirectory + File.separator + "webstart" + File.separator + brandingToken);

        if (webstartBuildDir.exists()) {
            FileUtils.deleteDirectory(webstartBuildDir);
        }

        webstartBuildDir.mkdirs();

        // P: copy webappResources --[

        MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution(webappResources,
                webstartBuildDir, project, encoding, Collections.EMPTY_LIST, Collections.EMPTY_LIST, session);
        mavenResourcesExecution.setEscapeWindowsPaths(true);
        mavenResourcesFiltering.filterResources(mavenResourcesExecution);

        // ]--

        final String localCodebase = codebase != null ? codebase : webstartBuildDir.toURI().toString();
        getLog().info("Generating webstartable binaries at " + webstartBuildDir.getAbsolutePath());

        final File nbmBuildDirFile = new File(outputDirectory, brandingToken);

        // +p (needs to be before make jnlp)

        //TODO is it really netbeans/
        if (masterJnlpFileName == null) {
            masterJnlpFileName = brandingToken;
        }

        Properties props = new Properties();
        props.setProperty("jnlp.codebase", localCodebase);
        props.setProperty("app.name", brandingToken);
        props.setProperty("app.title", project.getName());
        if (project.getOrganization() != null) {
            props.setProperty("app.vendor", project.getOrganization().getName());
        } else {
            props.setProperty("app.vendor", "Nobody");
        }
        String description = project.getDescription() != null ? project.getDescription()
                : "No Project Description";
        props.setProperty("app.description", description);
        props.setProperty("branding.token", brandingToken);
        props.setProperty("master.jnlp.file.name", masterJnlpFileName);
        props.setProperty("netbeans.jnlp.fixPolicy", "false");

        StringBuilder stBuilder = new StringBuilder();
        if (additionalArguments != null) {
            StringTokenizer st = new StringTokenizer(additionalArguments);
            while (st.hasMoreTokens()) {
                String arg = st.nextToken();
                if (arg.startsWith("-J")) {
                    if (stBuilder.length() > 0) {
                        stBuilder.append(' ');
                    }
                    stBuilder.append(arg.substring(2));
                }
            }
        }
        props.setProperty("netbeans.run.params", stBuilder.toString());

        final File masterJnlp = new File(webstartBuildDir, masterJnlpFileName + ".jnlp");

        filterCopy(masterJnlpFile, "master.jnlp", masterJnlp, props);

        if (generateJnlpTimestamp) //  \/\/\/\/  bad bad bad  \/\/\/\/
        {
            final File masterJnlpFileTmp = File.createTempFile(masterJnlpFileName + "_", "");

            Files.append(JnlpUtils.getCurrentJnlpTimestamp() + "\n", masterJnlpFileTmp,
                    Charset.forName("UTF-8"));

            ByteSink sink = Files.asByteSink(masterJnlpFileTmp, FileWriteMode.APPEND);

            sink.write(Files.toByteArray(masterJnlp));

            Files.copy(masterJnlpFileTmp, masterJnlp);
        }

        File startup = copyLauncher(outputDirectory, nbmBuildDirFile);

        String masterJnlpStr = FileUtils.fileRead(masterJnlp);

        // P: JNLP-INF/APPLICATION_TEMPLATE.JNLP support --[
        // this can be done better and will
        // ashamed
        if (generateJnlpApplicationTemplate) {
            File jnlpInfDir = new File(outputDirectory, "JNLP-INF");

            getLog().info("Generate JNLP application template under: " + jnlpInfDir);

            jnlpInfDir.mkdirs();

            File jnlpTemplate = new File(jnlpInfDir, "APPLICATION_TEMPLATE.JNLP");

            masterJnlpStr = masterJnlpStr.replaceAll("(<jnlp.*codebase\\ *=\\ *)\"((?!\").)*", "$1\"*")
                    .replaceAll("(<jnlp.*href\\ *=\\ *)\"((?!\").)*", "$1\"*");

            FileUtils.fileWrite(jnlpTemplate, masterJnlpStr);

            File startupMerged = new File(outputDirectory, "startup-jnlpinf.jar");

            Jar jar = (Jar) antProject.createTask("jar");
            jar.setDestFile(startupMerged);
            jar.setFilesetmanifest((FilesetManifestConfig) EnumeratedAttribute
                    .getInstance(FilesetManifestConfig.class, "merge"));

            FileSet jnlpInfDirectoryFileSet = new FileSet();
            jnlpInfDirectoryFileSet.setDir(outputDirectory);
            jnlpInfDirectoryFileSet.setIncludes("JNLP-INF/**");

            jar.addFileset(jnlpInfDirectoryFileSet);

            ZipFileSet startupJar = new ZipFileSet();
            startupJar.setSrc(startup);

            jar.addZipfileset(startupJar);

            jar.execute();

            startup = startupMerged;

            getLog().info("APPLICATION_TEMPLATE.JNLP generated - startup.jar: " + startup);
        }

        final JarsConfig startupConfig = new JarsConfig();

        ManifestEntries startupManifestEntries = new ManifestEntries();

        startupConfig.setManifestEntries(startupManifestEntries);

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        if (!validateJnlpDtd) {
            factory.setValidating(false);
            factory.setNamespaceAware(true);
            factory.setFeature("http://xml.org/sax/features/namespaces", false);
            factory.setFeature("http://xml.org/sax/features/validation", false);
            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        }
        DocumentBuilder builder = factory.newDocumentBuilder();

        final BufferedReader masterJnlpStrReader = new BufferedReader(new StringReader(masterJnlpStr));

        if (generateJnlpTimestamp) {
            masterJnlpStrReader.readLine();
        }

        Document doc = builder.parse(new InputSource(masterJnlpStrReader));

        Element jnlpRoot = doc.getDocumentElement();

        jarCodebase = jnlpRoot.getAttribute("codebase");

        if (jarCodebase.isEmpty()) {
            jarCodebase = "*";
        }

        startupManifestEntries.setCodebase(jarCodebase);

        XPath xpath = XPathFactory.newInstance().newXPath();

        Node jnlpSecurityPermission = (Node) xpath.evaluate(
                "(/jnlp/security/all-permissions | /jnlp/security/j2ee-application-client-permissions)[1]", doc,
                XPathConstants.NODE);

        if (jnlpSecurityPermission == null) {
            jarPermissions = "sandbox";
            jnlpSecurity = "";
        } else {
            jarPermissions = "all-permissions";
            jnlpSecurity = "<security><" + jnlpSecurityPermission.getNodeName() + "/></security>";
        }

        startupManifestEntries.setPermissions(jarPermissions);

        if (applicationName == null) {
            String jnlpApplicationTitle = (String) xpath.evaluate("/jnlp/information/title", doc,
                    XPathConstants.STRING);

            applicationName = jnlpApplicationTitle == null ? brandingToken : jnlpApplicationTitle;
        }

        startupManifestEntries.setApplicationName(applicationName);

        // +p

        if (autoManifestSecurityEntries) {
            if (jarsConfigs == null) {
                jarsConfigs = new ArrayList<JarsConfig>();
            }

            jarsConfigs.add(0, startupConfig);
        }

        final List<SignJar.JarsConfig> signJarJarsConfigs = buildSignJarJarsConfigs(jarsConfigs);

        File jnlpDestination = new File(webstartBuildDir.getAbsolutePath() + File.separator + "startup.jar");

        SignJar signTask = (SignJar) antProject.createTask("signjar");
        signTask.setKeystore(keystore);
        signTask.setStorepass(keystorepassword);
        signTask.setAlias(keystorealias);

        if (keystoretype != null) {
            signTask.setStoretype(keystoretype);
        }

        signTask.setForce(signingForce);
        signTask.setTsacert(signingTsaCert);
        signTask.setTsaurl(signingTsaUrl);
        signTask.setMaxmemory(signingMaxMemory);
        signTask.setRetryCount(signingRetryCount);

        signTask.setUnsignFirst(signingRemoveExistingSignatures);

        signTask.setJarsConfigs(buildSignJarJarsConfigs(Collections.singletonList(startupConfig)));

        signTask.setBasedir(nbmBuildDirFile);

        signTask.setSignedjar(jnlpDestination);

        signTask.setJar(startup);

        signTask.setPack200(pack200);
        signTask.setPack200Effort(pack200Effort);

        signTask.execute();
        // <-- all of this will be refactored soon ]--

        //            FileUtils.copyDirectoryStructureIfModified( nbmBuildDirFile, webstartBuildDir );

        MakeJnlp2 jnlpTask = (MakeJnlp2) antProject.createTask("makejnlp");
        jnlpTask.setOptimize(optimize);
        jnlpTask.setIncludelocales(includeLocales);
        jnlpTask.setDir(webstartBuildDir);
        jnlpTask.setCodebase(localCodebase);
        //TODO, how to figure verify excludes..
        jnlpTask.setVerify(false);
        jnlpTask.setPermissions(jnlpSecurity);
        jnlpTask.setSignJars(true);

        jnlpTask.setAlias(keystorealias);
        jnlpTask.setKeystore(keystore);
        jnlpTask.setStorePass(keystorepassword);
        if (keystoretype != null) {
            jnlpTask.setStoreType(keystoretype);
        }

        jnlpTask.setSigningForce(signingForce);
        jnlpTask.setSigningTsaCert(signingTsaCert);
        jnlpTask.setSigningTsaUrl(signingTsaUrl);
        jnlpTask.setUnsignFirst(signingRemoveExistingSignatures);
        jnlpTask.setJarsConfigs(signJarJarsConfigs);
        jnlpTask.setSigningMaxMemory(signingMaxMemory);
        jnlpTask.setSigningRetryCount(signingRetryCount);
        jnlpTask.setBasedir(nbmBuildDirFile);

        jnlpTask.setNbThreads(signingThreads);

        jnlpTask.setProcessJarVersions(processJarVersions);

        jnlpTask.setPack200(pack200);
        jnlpTask.setPack200Effort(pack200Effort);

        FileSet fs = jnlpTask.createModules();
        fs.setDir(nbmBuildDirFile);
        OrSelector or = new OrSelector();
        AndSelector and = new AndSelector();
        FilenameSelector inc = new FilenameSelector();
        inc.setName("*/modules/**/*.jar");
        or.addFilename(inc);
        inc = new FilenameSelector();
        inc.setName("*/lib/**/*.jar");
        or.addFilename(inc);
        inc = new FilenameSelector();
        inc.setName("*/core/**/*.jar");
        or.addFilename(inc);

        ModuleSelector ms = new ModuleSelector();
        Parameter included = new Parameter();
        included.setName("includeClusters");
        included.setValue("");
        Parameter excluded = new Parameter();
        excluded.setName("excludeClusters");
        excluded.setValue("");
        Parameter exModules = new Parameter();
        exModules.setName("excludeModules");
        exModules.setValue("");
        ms.setParameters(new Parameter[] { included, excluded, exModules });
        and.add(or);
        and.add(ms);
        fs.addAnd(and);
        jnlpTask.execute();

        Set<String> locales = jnlpTask.getExecutedLocales();

        String extSnippet = generateExtensions(fs, antProject, ""); // "netbeans/"

        //branding
        DirectoryScanner ds = new DirectoryScanner();
        ds.setBasedir(nbmBuildDirFile);

        final List<String> localeIncludes = new ArrayList<String>();
        final List<String> localeExcludes = new ArrayList<String>();

        localeIncludes.add("**/locale/*.jar");

        if (includeLocales != null) {
            List<String> excludes = Splitter.on(',').trimResults().omitEmptyStrings()
                    .splitToList(includeLocales);

            for (String exclude : (Collection<String>) CollectionUtils.subtract(locales, excludes)) {
                localeExcludes.add("**/locale/*_" + exclude + ".jar");
            }
        }

        ds.setIncludes(localeIncludes.toArray(new String[localeIncludes.size()]));
        ds.setExcludes(localeExcludes.toArray(new String[localeExcludes.size()]));
        ds.scan();
        String[] includes = ds.getIncludedFiles();

        StringBuilder brandRefs = new StringBuilder(
                "<property name=\"jnlp.packEnabled\" value=\"" + String.valueOf(pack200) + "\"/>\n");

        if (includes != null && includes.length > 0) {
            final File brandingDir = new File(webstartBuildDir, "branding");
            brandingDir.mkdirs();
            for (String incBran : includes) {
                File source = new File(nbmBuildDirFile, incBran);
                File dest = new File(brandingDir, source.getName());
                brandRefs.append("    <jar href=\'branding/").append(dest.getName()).append("\'/>\n");
            }

            final ExecutorService executorService = Executors.newFixedThreadPool(signingThreads);

            final List<Exception> threadException = new ArrayList<Exception>();

            for (final String toSign : includes) {
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            File toSignFile = new File(nbmBuildDirFile, toSign);

                            SignJar signTask = (SignJar) antProject.createTask("signjar");
                            if (keystoretype != null) {
                                signTask.setStoretype(keystoretype);
                            }
                            signTask.setKeystore(keystore);
                            signTask.setStorepass(keystorepassword);
                            signTask.setAlias(keystorealias);
                            signTask.setForce(signingForce);
                            signTask.setTsacert(signingTsaCert);
                            signTask.setTsaurl(signingTsaUrl);
                            signTask.setMaxmemory(signingMaxMemory);
                            signTask.setRetryCount(signingRetryCount);
                            signTask.setUnsignFirst(signingRemoveExistingSignatures);
                            signTask.setJarsConfigs(signJarJarsConfigs);
                            signTask.setJar(toSignFile);
                            signTask.setDestDir(brandingDir);
                            signTask.setBasedir(nbmBuildDirFile);
                            signTask.setDestFlatten(true);
                            signTask.setPack200(pack200);
                            signTask.setPack200Effort(pack200Effort);
                            signTask.execute();
                        } catch (Exception e) {
                            threadException.add(e);
                        }
                    }
                });
            }

            executorService.shutdown();

            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);

            if (!threadException.isEmpty()) {
                throw threadException.get(0);
            }
        }

        File modulesJnlp = new File(webstartBuildDir.getAbsolutePath() + File.separator + "modules.jnlp");
        props.setProperty("jnlp.branding.jars", brandRefs.toString());
        props.setProperty("jnlp.resources", extSnippet);
        filterCopy(null, /* filename is historical */"branding.jnlp", modulesJnlp, props);

        if (verifyJnlp) {
            getLog().info("Verifying generated webstartable content.");
            VerifyJNLP verifyTask = (VerifyJNLP) antProject.createTask("verifyjnlp");
            FileSet verify = new FileSet();
            verify.setFile(masterJnlp);
            verifyTask.addConfiguredFileset(verify);
            verifyTask.execute();
        }

        // create zip archive
        if (destinationFile.exists()) {
            destinationFile.delete();
        }
        ZipArchiver archiver = new ZipArchiver();
        if (codebase != null) {
            getLog().warn("Defining <codebase>/${nbm.webstart.codebase} is generally unnecessary");
            archiver.addDirectory(webstartBuildDir);
        } else {
            archiver.addDirectory(webstartBuildDir, null, new String[] { "**/*.jnlp" });
            for (final File jnlp : webstartBuildDir.listFiles()) {
                if (!jnlp.getName().endsWith(".jnlp")) {
                    continue;
                }
                archiver.addResource(new PlexusIoResource() {
                    public @Override InputStream getContents() throws IOException {
                        return new ByteArrayInputStream(FileUtils.fileRead(jnlp, "UTF-8")
                                .replace(localCodebase, "$$codebase").getBytes("UTF-8"));
                    }

                    public @Override long getLastModified() {
                        return jnlp.lastModified();
                    }

                    public @Override boolean isExisting() {
                        return true;
                    }

                    public @Override long getSize() {
                        return UNKNOWN_RESOURCE_SIZE;
                    }

                    public @Override URL getURL() throws IOException {
                        return null;
                    }

                    public @Override String getName() {
                        return jnlp.getAbsolutePath();
                    }

                    public @Override boolean isFile() {
                        return true;
                    }

                    public @Override boolean isDirectory() {
                        return false;
                    }
                }, jnlp.getName(), archiver.getDefaultFileMode());
            }
        }
        File jdkhome = new File(System.getProperty("java.home"));
        File servlet = new File(jdkhome, "sample/jnlp/servlet/jnlp-servlet.jar");
        if (!servlet.exists()) {
            servlet = new File(jdkhome.getParentFile(), "sample/jnlp/servlet/jnlp-servlet.jar");

            if (!servlet.exists()) {
                servlet = File.createTempFile("nbm_", "jnlp-servlet.jar");

                FileUtils.copyURLToFile(
                        Thread.currentThread().getContextClassLoader().getResource("jnlp-servlet.jar"),
                        servlet);
            }
        }
        if (servlet.exists()) {
            File servletDir = new File(webstartBuildDir, "WEB-INF/lib");

            servletDir.mkdirs();

            signTask = (SignJar) antProject.createTask("signjar");
            signTask.setKeystore(keystore);
            signTask.setStorepass(keystorepassword);
            signTask.setAlias(keystorealias);
            signTask.setForce(signingForce);
            signTask.setTsacert(signingTsaCert);
            signTask.setTsaurl(signingTsaUrl);
            signTask.setMaxmemory(signingMaxMemory);
            signTask.setRetryCount(signingRetryCount);
            signTask.setJar(servlet);
            signTask.setSignedjar(new File(servletDir, "jnlp-servlet.jar"));
            signTask.execute();

            //archiver.addFile( servlet, "WEB-INF/lib/jnlp-servlet.jar" );
            archiver.addResource(new PlexusIoResource() {
                public @Override InputStream getContents() throws IOException {
                    return new ByteArrayInputStream(("" + "<web-app>\n" + "    <servlet>\n"
                            + "        <servlet-name>JnlpDownloadServlet</servlet-name>\n"
                            + "        <servlet-class>jnlp.sample.servlet.JnlpDownloadServlet</servlet-class>\n"
                            + "    </servlet>\n" + "    <servlet-mapping>\n"
                            + "        <servlet-name>JnlpDownloadServlet</servlet-name>\n"
                            + "        <url-pattern>*.jnlp</url-pattern>\n" + "    </servlet-mapping>\n"
                            + "    <servlet-mapping>\n"
                            + "        <servlet-name>JnlpDownloadServlet</servlet-name>\n"
                            + "        <url-pattern>*.jar</url-pattern>\n" + "    </servlet-mapping>\n"
                            + "    <mime-mapping>\n" + "        <extension>jnlp</extension>\n"
                            + "        <mime-type>application/x-java-jnlp-file</mime-type>\n"
                            + "    </mime-mapping>\n" + "</web-app>\n").getBytes());
                }

                public @Override long getLastModified() {
                    return UNKNOWN_MODIFICATION_DATE;
                }

                public @Override boolean isExisting() {
                    return true;
                }

                public @Override long getSize() {
                    return UNKNOWN_RESOURCE_SIZE;
                }

                public @Override URL getURL() throws IOException {
                    return null;
                }

                public @Override String getName() {
                    return "web.xml";
                }

                public @Override boolean isFile() {
                    return true;
                }

                public @Override boolean isDirectory() {
                    return false;
                }
            }, "WEB-INF/web.xml", archiver.getDefaultFileMode());
        }
        archiver.setDestFile(destinationFile);
        archiver.createArchive();

        if (signWar) {
            signTask = (SignJar) antProject.createTask("signjar");
            signTask.setKeystore(keystore);
            signTask.setStorepass(keystorepassword);
            signTask.setAlias(keystorealias);
            signTask.setForce(signingForce);
            signTask.setTsacert(signingTsaCert);
            signTask.setTsaurl(signingTsaUrl);
            signTask.setMaxmemory(signingMaxMemory);
            signTask.setRetryCount(signingRetryCount);
            signTask.setJar(destinationFile);
            signTask.execute();
        }

        // attach standalone so that it gets installed/deployed
        projectHelper.attachArtifact(project, "war", webstartClassifier, destinationFile);

    } catch (Exception ex) {
        throw new MojoExecutionException("Error creating webstartable binary.", ex);
    }
}

From source file:org.apache.druid.indexing.overlord.ForkingTaskRunner.java

@Override
public ListenableFuture<TaskStatus> run(final Task task) {
    synchronized (tasks) {
        if (!tasks.containsKey(task.getId())) {
            tasks.put(task.getId(), new ForkingTaskRunnerWorkItem(task, exec.submit(new Callable<TaskStatus>() {
                @Override// w  w w .  j  ava 2 s.  c  om
                public TaskStatus call() {
                    final String attemptUUID = UUID.randomUUID().toString();
                    final File taskDir = taskConfig.getTaskDir(task.getId());
                    final File attemptDir = new File(taskDir, attemptUUID);

                    final ProcessHolder processHolder;
                    final String childHost = node.getHost();
                    int childPort = -1;
                    int tlsChildPort = -1;

                    if (node.isEnablePlaintextPort()) {
                        childPort = portFinder.findUnusedPort();
                    }

                    if (node.isEnableTlsPort()) {
                        tlsChildPort = portFinder.findUnusedPort();
                    }

                    final TaskLocation taskLocation = TaskLocation.create(childHost, childPort, tlsChildPort);

                    try {
                        final Closer closer = Closer.create();
                        try {
                            if (!attemptDir.mkdirs()) {
                                throw new IOE("Could not create directories: %s", attemptDir);
                            }

                            final File taskFile = new File(taskDir, "task.json");
                            final File statusFile = new File(attemptDir, "status.json");
                            final File logFile = new File(taskDir, "log");
                            final File reportsFile = new File(attemptDir, "report.json");

                            // time to adjust process holders
                            synchronized (tasks) {
                                final ForkingTaskRunnerWorkItem taskWorkItem = tasks.get(task.getId());

                                if (taskWorkItem.shutdown) {
                                    throw new IllegalStateException("Task has been shut down!");
                                }

                                if (taskWorkItem == null) {
                                    log.makeAlert("WTF?! TaskInfo disappeared!").addData("task", task.getId())
                                            .emit();
                                    throw new ISE("TaskInfo disappeared for task[%s]!", task.getId());
                                }

                                if (taskWorkItem.processHolder != null) {
                                    log.makeAlert("WTF?! TaskInfo already has a processHolder")
                                            .addData("task", task.getId()).emit();
                                    throw new ISE("TaskInfo already has processHolder for task[%s]!",
                                            task.getId());
                                }

                                final List<String> command = Lists.newArrayList();
                                final String taskClasspath;
                                if (task.getClasspathPrefix() != null && !task.getClasspathPrefix().isEmpty()) {
                                    taskClasspath = Joiner.on(File.pathSeparator)
                                            .join(task.getClasspathPrefix(), config.getClasspath());
                                } else {
                                    taskClasspath = config.getClasspath();
                                }

                                command.add(config.getJavaCommand());
                                command.add("-cp");
                                command.add(taskClasspath);

                                Iterables.addAll(command, new QuotableWhiteSpaceSplitter(config.getJavaOpts()));
                                Iterables.addAll(command, config.getJavaOptsArray());

                                // Override task specific javaOpts
                                Object taskJavaOpts = task
                                        .getContextValue(ForkingTaskRunnerConfig.JAVA_OPTS_PROPERTY);
                                if (taskJavaOpts != null) {
                                    Iterables.addAll(command,
                                            new QuotableWhiteSpaceSplitter((String) taskJavaOpts));
                                }

                                for (String propName : props.stringPropertyNames()) {
                                    for (String allowedPrefix : config.getAllowedPrefixes()) {
                                        // See https://github.com/apache/incubator-druid/issues/1841
                                        if (propName.startsWith(allowedPrefix)
                                                && !ForkingTaskRunnerConfig.JAVA_OPTS_PROPERTY.equals(propName)
                                                && !ForkingTaskRunnerConfig.JAVA_OPTS_ARRAY_PROPERTY
                                                        .equals(propName)) {
                                            command.add(StringUtils.format("-D%s=%s", propName,
                                                    props.getProperty(propName)));
                                        }
                                    }
                                }

                                // Override child JVM specific properties
                                for (String propName : props.stringPropertyNames()) {
                                    if (propName.startsWith(CHILD_PROPERTY_PREFIX)) {
                                        command.add(StringUtils.format("-D%s=%s",
                                                propName.substring(CHILD_PROPERTY_PREFIX.length()),
                                                props.getProperty(propName)));
                                    }
                                }

                                // Override task specific properties
                                final Map<String, Object> context = task.getContext();
                                if (context != null) {
                                    for (String propName : context.keySet()) {
                                        if (propName.startsWith(CHILD_PROPERTY_PREFIX)) {
                                            command.add(StringUtils.format("-D%s=%s",
                                                    propName.substring(CHILD_PROPERTY_PREFIX.length()),
                                                    task.getContextValue(propName)));
                                        }
                                    }
                                }

                                // Add dataSource, taskId and taskType for metrics or logging
                                command.add(
                                        StringUtils.format("-D%s%s=%s", MonitorsConfig.METRIC_DIMENSION_PREFIX,
                                                DruidMetrics.DATASOURCE, task.getDataSource()));
                                command.add(
                                        StringUtils.format("-D%s%s=%s", MonitorsConfig.METRIC_DIMENSION_PREFIX,
                                                DruidMetrics.TASK_ID, task.getId()));
                                command.add(
                                        StringUtils.format("-D%s%s=%s", MonitorsConfig.METRIC_DIMENSION_PREFIX,
                                                DruidMetrics.TASK_TYPE, task.getType()));

                                command.add(StringUtils.format("-Ddruid.host=%s", childHost));
                                command.add(StringUtils.format("-Ddruid.plaintextPort=%d", childPort));
                                command.add(StringUtils.format("-Ddruid.tlsPort=%d", tlsChildPort));
                                /**
                                 * These are not enabled per default to allow the user to either set or not set them
                                 * Users are highly suggested to be set in druid.indexer.runner.javaOpts
                                 * See org.apache.druid.concurrent.TaskThreadPriority#getThreadPriorityFromTaskPriority(int)
                                 * for more information
                                 command.add("-XX:+UseThreadPriorities");
                                 command.add("-XX:ThreadPriorityPolicy=42");
                                 */

                                command.add("org.apache.druid.cli.Main");
                                command.add("internal");
                                command.add("peon");
                                command.add(taskFile.toString());
                                command.add(statusFile.toString());
                                command.add(reportsFile.toString());
                                String nodeType = task.getNodeType();
                                if (nodeType != null) {
                                    command.add("--nodeType");
                                    command.add(nodeType);
                                }

                                if (!taskFile.exists()) {
                                    jsonMapper.writeValue(taskFile, task);
                                }

                                log.info("Running command: %s", Joiner.on(" ").join(command));
                                taskWorkItem.processHolder = new ProcessHolder(
                                        new ProcessBuilder(ImmutableList.copyOf(command))
                                                .redirectErrorStream(true).start(),
                                        logFile, taskLocation.getHost(), taskLocation.getPort(),
                                        taskLocation.getTlsPort());

                                processHolder = taskWorkItem.processHolder;
                                processHolder.registerWithCloser(closer);
                            }

                            TaskRunnerUtils.notifyLocationChanged(listeners, task.getId(), taskLocation);
                            TaskRunnerUtils.notifyStatusChanged(listeners, task.getId(),
                                    TaskStatus.running(task.getId()));

                            log.info("Logging task %s output to: %s", task.getId(), logFile);
                            boolean runFailed = true;

                            final ByteSink logSink = Files.asByteSink(logFile, FileWriteMode.APPEND);

                            // This will block for a while. So we append the thread information with more details
                            final String priorThreadName = Thread.currentThread().getName();
                            Thread.currentThread()
                                    .setName(StringUtils.format("%s-[%s]", priorThreadName, task.getId()));

                            try (final OutputStream toLogfile = logSink.openStream()) {
                                ByteStreams.copy(processHolder.process.getInputStream(), toLogfile);
                                final int statusCode = processHolder.process.waitFor();
                                log.info("Process exited with status[%d] for task: %s", statusCode,
                                        task.getId());
                                if (statusCode == 0) {
                                    runFailed = false;
                                }
                            } finally {
                                Thread.currentThread().setName(priorThreadName);
                                // Upload task logs
                                taskLogPusher.pushTaskLog(task.getId(), logFile);
                                if (reportsFile.exists()) {
                                    taskLogPusher.pushTaskReports(task.getId(), reportsFile);
                                }
                            }

                            TaskStatus status;
                            if (!runFailed) {
                                // Process exited successfully
                                status = jsonMapper.readValue(statusFile, TaskStatus.class);
                            } else {
                                // Process exited unsuccessfully
                                status = TaskStatus.failure(task.getId());
                            }

                            TaskRunnerUtils.notifyStatusChanged(listeners, task.getId(), status);
                            return status;
                        } catch (Throwable t) {
                            throw closer.rethrow(t);
                        } finally {
                            closer.close();
                        }
                    } catch (Throwable t) {
                        log.info(t, "Exception caught during execution");
                        throw Throwables.propagate(t);
                    } finally {
                        try {
                            synchronized (tasks) {
                                final ForkingTaskRunnerWorkItem taskWorkItem = tasks.remove(task.getId());
                                if (taskWorkItem != null && taskWorkItem.processHolder != null) {
                                    taskWorkItem.processHolder.process.destroy();
                                }
                                if (!stopping) {
                                    saveRunningTasks();
                                }
                            }

                            if (node.isEnablePlaintextPort()) {
                                portFinder.markPortUnused(childPort);
                            }
                            if (node.isEnableTlsPort()) {
                                portFinder.markPortUnused(tlsChildPort);
                            }

                            try {
                                if (!stopping && taskDir.exists()) {
                                    log.info("Removing task directory: %s", taskDir);
                                    FileUtils.deleteDirectory(taskDir);
                                }
                            } catch (Exception e) {
                                log.makeAlert(e, "Failed to delete task directory")
                                        .addData("taskDir", taskDir.toString()).addData("task", task.getId())
                                        .emit();
                            }
                        } catch (Exception e) {
                            log.error(e, "Suppressing exception caught while cleaning up task");
                        }
                    }
                }
            })));
        }
        saveRunningTasks();
        return tasks.get(task.getId()).getResult();
    }
}