Content of file LocalCopyFileSystemProvider.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>LocalCopyFileSystemProvider.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">NorthernWind :: Filesystems :: SCM :: Mercurial</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-core-filesystem-basic</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.frontend.filesystem.basic</a> &gt; <span class="el_source">LocalCopyFileSystemProvider.java</span></div><h1>LocalCopyFileSystemProvider.java</h1><pre class="source lang-java linenums">/*
 * #%L
 * *********************************************************************************************************************
 *
 * NorthernWind - lightweight CMS
 * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
 * %%
 * Copyright (C) 2011 - 2023 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.frontend.filesystem.basic;

import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import java.time.ZonedDateTime;
import java.io.File;
import java.io.IOException;
import it.tidalwave.messagebus.MessageBus;
import it.tidalwave.messagebus.MessageBus.Listener;
import it.tidalwave.northernwind.core.model.ResourceFile;
import it.tidalwave.northernwind.core.model.ResourceFileSystem;
import it.tidalwave.northernwind.core.model.ResourceFileSystemChangedEvent;
import it.tidalwave.northernwind.core.model.ResourceFileSystemProvider;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 *
 * A provider for the {@link ResourceFileSystemProvider} that clones a source provider into a local
 * {@code ResourceFileSystemProvider} for performance purposes...
 *
 * @author Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L55">@Slf4j @ToString(of = &quot;rootPath&quot;)</span>
<span class="nc" id="L56">public class LocalCopyFileSystemProvider implements ResourceFileSystemProvider</span>
  {
<span class="nc" id="L58">    @Getter @Setter @Nonnull</span>
    private ResourceFileSystemProvider sourceProvider;

<span class="nc" id="L61">    @Getter @Setter @Nonnull</span>
    private String rootPath = &quot;&quot;;

<span class="nc" id="L64">    private LocalFileSystemProvider targetProvider = new LocalFileSystemProvider();</span>

    @Inject @Named(&quot;applicationMessageBus&quot;)
    private MessageBus messageBus;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
<span class="nc" id="L73">    private final Listener&lt;ResourceFileSystemChangedEvent&gt; sourceProviderChangeListener =</span>
      new Listener&lt;&gt;()
<span class="nc" id="L75">        {</span>
          @Override
          public void notify (@Nonnull final ResourceFileSystemChangedEvent event)
            {
<span class="nc bnc" id="L79" title="All 2 branches missed.">              if (event.getFileSystemProvider() == sourceProvider)</span>
                {
                  try
                    {
<span class="nc" id="L83">                      log.info(&quot;Detected file change, regenerating local file system...&quot;);</span>
<span class="nc" id="L84">                      generateLocalFileSystem();</span>
<span class="nc" id="L85">                      messageBus.publish(new ResourceFileSystemChangedEvent(LocalCopyFileSystemProvider.this,</span>
<span class="nc" id="L86">                                                                            ZonedDateTime.now()));</span>
                    }
<span class="nc" id="L88">                  catch (IOException e)</span>
                    {
<span class="nc" id="L90">                      log.error(&quot;While resetting site: &quot;, e);</span>
<span class="nc" id="L91">                    }</span>
                }
<span class="nc" id="L93">            }</span>
        };

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public synchronized ResourceFileSystem getFileSystem()
            throws IOException
      {
<span class="nc" id="L105">        return targetProvider.getFileSystem();</span>
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @PostConstruct
    /* package */ void initialize()
            throws IOException
      {
<span class="nc" id="L116">        log.info(&quot;initialize()&quot;);</span>
<span class="nc" id="L117">        generateLocalFileSystem();</span>
<span class="nc" id="L118">        messageBus.subscribe(ResourceFileSystemChangedEvent.class, sourceProviderChangeListener);</span>
<span class="nc" id="L119">      }</span>

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void generateLocalFileSystem()
            throws IOException
      {
<span class="nc" id="L128">        log.info(&quot;generateLocalFileSystem()&quot;);</span>

<span class="nc bnc" id="L130" title="All 2 branches missed.">        if (!new File(rootPath).mkdirs()) // TODO: use NwFileSystem API</span>
use NwFileSystem API
{ <span class="nc" id="L132"> throw new IOException(&quot;Cannot create dirs for &quot; + rootPath);</span> } // FIXME: shouldn't be needed, but otherwise after a second call to this method won't find files <span class="nc" id="L136"> targetProvider = new LocalFileSystemProvider();</span> <span class="nc" id="L137"> targetProvider.setRootPath(rootPath);</span> <span class="nc" id="L138"> final var targetRoot = targetProvider.getFileSystem().getRoot();</span> <span class="nc" id="L139"> final var path = targetRoot.toFile().getAbsolutePath();</span> <span class="nc" id="L140"> log.info(&quot;&gt;&gt;&gt;&gt; scratching {} ...&quot;, path);</span> <span class="nc" id="L141"> emptyFolder(targetRoot);</span> <span class="nc" id="L142"> log.info(&quot;&gt;&gt;&gt;&gt; copying files to {} ...&quot;, path);</span> <span class="nc" id="L143"> copyFolder(sourceProvider.getFileSystem().getRoot(), targetRoot);</span> // targetProvider.getFileSystem().refresh(true); <span class="nc" id="L145"> }</span> /******************************************************************************************************************* * * ******************************************************************************************************************/ private static void emptyFolder (@Nonnull final ResourceFile folder) throws IOException { <span class="nc" id="L154"> log.trace(&quot;emptyFolder({}&quot;, folder);</span> <span class="nc bnc" id="L156" title="All 2 branches missed."> for (final var child : folder.findChildren().results())</span> { <span class="nc" id="L158"> child.delete();</span> <span class="nc" id="L159"> }</span> <span class="nc" id="L160"> }</span> /******************************************************************************************************************* * * ******************************************************************************************************************/ private static void copyFolder (@Nonnull final ResourceFile sourceFolder, @Nonnull final ResourceFile targetFolder) throws IOException { <span class="nc" id="L169"> log.trace(&quot;copyFolder({}, {}&quot;, sourceFolder, targetFolder);</span> <span class="nc bnc" id="L171" title="All 2 branches missed."> for (final var sourceChild : sourceFolder.findChildren().results())</span> { <span class="nc bnc" id="L173" title="All 2 branches missed."> if (!sourceChild.isFolder())</span> { <span class="nc" id="L175"> log.trace(&quot;&gt;&gt;&gt;&gt; copying {} into {} ...&quot;, sourceChild, targetFolder);</span> <span class="nc" id="L176"> sourceChild.copyTo(targetFolder);</span> } <span class="nc" id="L178"> }</span> <span class="nc bnc" id="L180" title="All 2 branches missed."> for (final var sourceChild : sourceFolder.findChildren().results())</span> { <span class="nc bnc" id="L182" title="All 2 branches missed."> if (sourceChild.isFolder())</span> { <span class="nc" id="L184"> copyFolder(sourceChild, targetFolder.createFolder(sourceChild.getName()));</span> } <span class="nc" id="L186"> }</span> <span class="nc" id="L187"> }</span> } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.9.202303310957</span></div></body></html>