org.bidib.wizard.nodescript.resources.syncer.NodescriptResourcesSyncer Maven / Gradle / Ivy
package org.bidib.wizard.nodescript.resources.syncer;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.tasks.SyncTask;
import org.apache.tools.ant.Project;
import org.bidib.wizard.api.model.event.ConsoleMessageEvent;
import org.bidib.wizard.api.service.console.ConsoleColor;
import org.bidib.wizard.common.service.SettingsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
public class NodescriptResourcesSyncer implements ResourcesSyncer {
private static final Logger LOGGER = LoggerFactory.getLogger(NodescriptResourcesSyncer.class);
private static final String HELP_PATH = "data/help";
private static final String INSTRUCTION_PATH = "data/wizard/help/instruction";
private final SettingsService settingsService;
private final ApplicationEventPublisher applicationEventPublisher;
public NodescriptResourcesSyncer(final SettingsService settingsService,
final ApplicationEventPublisher applicationEventPublisher) {
this.settingsService = settingsService;
this.applicationEventPublisher = applicationEventPublisher;
}
@Override
public void syncResources() throws FileSystemException {
LOGGER.info("Sync the nodescript resources.");
final FileSystemManager fsManager = VFS.getManager();
// check the user dir
String targetPath = getUserPath(INSTRUCTION_PATH);
syncDirectory(fsManager, "res:" + HELP_PATH, targetPath, "instruction/**");
LOGGER.info("Sync the nodescript resources has finished.");
}
private void syncDirectory(final FileSystemManager fsManager, String source, String target, String includes)
throws FileSystemException {
// the 'res' prefix is registered to support spring-boot repackaged jar
final FileObject sourceDir = fsManager.resolveFile(source);
LOGGER.info("Current sourceDir: {}", sourceDir);
final FileObject targetDir = fsManager.resolveFile(target);
LOGGER.info("Current targetDir: {}", targetDir.getPath());
if (!targetDir.exists()) {
LOGGER.info("The targetDir does nit exist. Create the targetDir: {}", targetDir.getPath());
try {
Files.createDirectories(targetDir.getPath());
}
catch (IOException ex) {
LOGGER.warn("Create targetDir failed.", ex);
throw new FileSystemException("Create targetDir failed.", ex);
}
}
final SyncTask syncTask = new SyncTask();
syncTask.setProject(new Project());
syncTask.setSrcDir(sourceDir.getPublicURIString());
syncTask.setDestDir(targetDir.getPublicURIString());
syncTask.setIncludes(includes);
syncTask.execute();
}
private String getUserPath(String subDir) {
String labelPath = settingsService.getMiscSettings().getBidibConfigDir();
File fileUser = new File(labelPath, subDir);
String searchPathUser = fileUser.getPath();
return searchPathUser;
}
@EventListener(ApplicationReadyEvent.class)
public void handleApplicationReadyEvent() {
LOGGER.info("The application is ready. Start the resource syncer.");
try {
syncResources();
}
catch (FileSystemException ex) {
LOGGER.warn("Sync the nodescript resources has failed.", ex);
this.applicationEventPublisher
.publishEvent(new ConsoleMessageEvent(ConsoleColor.red,
"Sync the nodescript resources has failed. Reason: " + ex.getMessage(), true));
}
}
}