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 :: Git</a> > <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-core-filesystem-basic</a> > <a href="index.source.html" class="el_package">it.tidalwave.northernwind.frontend.filesystem.basic</a> > <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 "License"); 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 "AS IS" 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 = "rootPath")</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 = "";
<span class="nc" id="L64"> private LocalFileSystemProvider targetProvider = new LocalFileSystemProvider();</span>
@Inject @Named("applicationMessageBus")
private MessageBus messageBus;
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
<span class="nc" id="L73"> private final Listener<ResourceFileSystemChangedEvent> sourceProviderChangeListener =</span>
new Listener<>()
<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("Detected file change, regenerating local file system...");</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("While resetting site: ", 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("initialize()");</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("generateLocalFileSystem()");</span>
<span class="nc bnc" id="L130" title="All 2 branches missed."> if (!new File(rootPath).mkdirs()) // TODO: use NwFileSystem API</span>
{
<span class="nc" id="L132"> throw new IOException("Cannot create dirs for " + 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(">>>> scratching {} ...", path);</span>
<span class="nc" id="L141"> emptyFolder(targetRoot);</span>
<span class="nc" id="L142"> log.info(">>>> copying files to {} ...", 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("emptyFolder({}", 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("copyFolder({}, {}", 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(">>>> copying {} into {} ...", 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>