Content of file ProcessExecutor.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>ProcessExecutor.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Zephyr - UI (JavaFX)</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-rca-ui-content-editor</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.rca.ui.contenteditor.impl</a> &gt; <span class="el_source">ProcessExecutor.java</span></div><h1>ProcessExecutor.java</h1><pre class="source lang-java linenums">/*
 * #%L
 * *********************************************************************************************************************
 *
 * NorthernWind - lightweight CMS
 * http://northernwind.tidalwave.it - git clone git@bitbucket.org:tidalwave/northernwind-rca-src.git
 * %%
 * Copyright (C) 2013 - 2021 Tidalwave s.a.s. (http://tidalwave.it)
 * %%
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * *********************************************************************************************************************
 *
 *
 * *********************************************************************************************************************
 * #L%
 */
package it.tidalwave.northernwind.rca.ui.contenteditor.impl;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.io.IOException;
import java.io.File;
import it.tidalwave.util.Callback;
import lombok.AllArgsConstructor;
import lombok.With;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc bnc" id="L46" title="All 2 branches missed.">@AllArgsConstructor @Slf4j</span>
public class ProcessExecutor
  {
    @Nonnull
    private final String executable;

<span class="nc bnc" id="L52" title="All 2 branches missed.">    @With</span>
    private final List&lt;String&gt; arguments;

<span class="nc bnc" id="L55" title="All 2 branches missed.">    @With</span>
    private final Callback postMortemTask;

    public ProcessExecutor()
      {
<span class="nc" id="L60">        this(&quot;&quot;, new ArrayList&lt;&gt;(), Callback.EMPTY);</span>
<span class="nc" id="L61">      }</span>

    @Nonnull
    public ProcessExecutor withArguments2 (@Nonnull final String ... arguments)
      {
<span class="nc" id="L66">        return withArguments(Arrays.asList(arguments));</span>
      }

    @Nonnull
    public static ProcessExecutor forExecutable (@Nonnull final String executable)
      {
<span class="nc" id="L72">        return new ProcessExecutor(executable, new ArrayList&lt;&gt;(), Callback.EMPTY);</span>
      }

    public void execute()
      {
<span class="nc" id="L77">        final List&lt;String&gt; args = new ArrayList&lt;&gt;();</span>
<span class="nc" id="L78">        args.add(&quot;open&quot;);</span>
<span class="nc" id="L79">        args.add(&quot;-W&quot;);</span>
<span class="nc" id="L80">        args.add(&quot;-a&quot;);</span>

<span class="nc" id="L82">        final String[] paths =</span>
          {
<span class="nc" id="L84">            &quot;/Applications/&quot;, System.getProperty(&quot;user.home&quot;) + &quot;/Applications/&quot;</span>
          };

<span class="nc bnc" id="L87" title="All 2 branches missed.">        for (final String path : paths)</span>
          {
<span class="nc" id="L89">            final File file = new File(new File(path), executable);</span>

<span class="nc bnc" id="L91" title="All 2 branches missed.">            if (file.exists())</span>
              {
<span class="nc" id="L93">                args.add(file.getAbsolutePath());</span>
<span class="nc" id="L94">                break;</span>
              }
          }

<span class="nc" id="L98">        args.addAll(arguments);</span>

        try
          {
<span class="nc" id="L102">            log.info(&quot;Executing {}&quot;, args);</span>
<span class="nc" id="L103">            final Process process = Runtime.getRuntime().exec(args.toArray(new String[0]));</span>
            // FIXME; inject the executor
; inject the executor
<span class="nc" id="L105"> Executors.newSingleThreadExecutor().submit(() -&gt;</span> { try { <span class="nc" id="L109"> log.debug(&quot;&gt;&gt;&gt;&gt; waiting for the process to terminate...&quot;);</span> <span class="nc" id="L110"> process.waitFor();</span> <span class="nc" id="L111"> log.debug(&quot;&gt;&gt;&gt;&gt; process terminated&quot;);</span> <span class="nc" id="L112"> postMortemTask.call();</span> } <span class="nc" id="L114"> catch (Throwable e)</span> { <span class="nc" id="L116"> log.error(&quot;&quot;, e);</span> <span class="nc" id="L117"> }</span> <span class="nc" id="L118"> });</span> } <span class="nc" id="L120"> catch (IOException e)</span> { <span class="nc" id="L122"> log.error(&quot;&quot;, e); // shouldn't happen</span> <span class="nc" id="L123"> }</span> <span class="nc" id="L124"> }</span> } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>