From f1480cfc627e16a32b5ec304038b8947057c393f Mon Sep 17 00:00:00 2001 From: Brad Gonzales Date: Tue, 29 Oct 2013 15:09:38 -0500 Subject: [PATCH] Issue #2267 Remove java.nio.file dependencies from the SbnSimulator. Fixed handler setting for UFStatus in test env. Change-Id: I8245eef53573b6596546e17a7900a1538455cee2 Former-commit-id: 6b8ff162be029543c8b9a7fcb3f8a359962aa6d7 --- .../raytheon/uf/common/status/UFStatus.java | 14 ++-- .../com/raytheon/uf/common/util/FileUtil.java | 69 ++++++++-------- .../bandwidth/sbn/SbnSimulator.java | 79 ++++++++++++++----- .../bandwidth/sbn/SbnSimulatorTest.java | 6 +- 4 files changed, 97 insertions(+), 71 deletions(-) diff --git a/edexOsgi/com.raytheon.uf.common.status/src/com/raytheon/uf/common/status/UFStatus.java b/edexOsgi/com.raytheon.uf.common.status/src/com/raytheon/uf/common/status/UFStatus.java index 88e22b582d..96cf7b7512 100644 --- a/edexOsgi/com.raytheon.uf.common.status/src/com/raytheon/uf/common/status/UFStatus.java +++ b/edexOsgi/com.raytheon.uf.common.status/src/com/raytheon/uf/common/status/UFStatus.java @@ -23,8 +23,6 @@ import java.util.Iterator; import java.util.ServiceLoader; import java.util.concurrent.atomic.AtomicReference; -import com.raytheon.uf.common.status.UFStatus.Priority; - /** * The principal mechanism for representing the outcome of an operation.
* @@ -103,23 +101,22 @@ public class UFStatus { protected final String message; /** handler factory */ - private static AtomicReference handlerFactoryRef = createHandlerFactory(); + private static AtomicReference handlerFactoryRef = new AtomicReference(); - private static final AtomicReference createHandlerFactory() { + static { ServiceLoader loader = ServiceLoader.load( IUFStatusHandlerFactory.class, IUFStatusHandlerFactory.class.getClassLoader()); Iterator handlerIterator = loader.iterator(); - IUFStatusHandlerFactory factory = null; if (handlerIterator.hasNext()) { - factory = handlerIterator.next(); + UFStatus.setHandlerFactory(handlerIterator.next()); } else { - factory = new DefaultStatusHandlerFactory(); + UFStatus.setHandlerFactory(new DefaultStatusHandlerFactory()); Exception e = new RuntimeException("No " + IUFStatusHandlerFactory.class.getName() + " found.\nUsing default handler."); - factory.getInstance() + UFStatus.getHandler() .handle(Priority.CRITICAL, e.getLocalizedMessage() + "\nPlease ignore if you are in a unit test environment\n"); @@ -130,7 +127,6 @@ public class UFStatus { + IUFStatusHandlerFactory.class.getName() + " handlers defined"); } - return new AtomicReference(factory); } /** diff --git a/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/FileUtil.java b/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/FileUtil.java index dedcb82118..5f06f0578c 100644 --- a/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/FileUtil.java +++ b/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/FileUtil.java @@ -31,13 +31,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.channels.FileChannel; -import java.nio.file.DirectoryStream; -import java.nio.file.FileSystems; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.ArrayList; -import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.regex.Pattern; import java.util.zip.GZIPInputStream; @@ -932,36 +926,37 @@ public class FileUtil { return size; } - /** - * List files/directories that match a FileFilter. - * - * @param directory - * @param filter - * @param recurse - * @return - * @throws IOException - */ - public static List listPaths(File directory, - DirectoryStream.Filter filter, boolean recurse) - throws IOException { - // List of files / directories - List files = new LinkedList(); - - // Get files / directories in the directory accepted by the filter. - Path dirPath = FileSystems.getDefault().getPath( - directory.getAbsolutePath()); - DirectoryStream stream = null; - try { - stream = Files.newDirectoryStream(dirPath, filter); - for (final Iterator it = stream.iterator(); it.hasNext();) { - files.add(it.next()); - } - } finally { - if (stream != null) { - stream.close(); - } - } - return files; - } + // TODO Java 1.7 potential code + // /** + // * List files/directories that match a FileFilter. + // * + // * @param directory + // * @param filter + // * @param recurse + // * @return + // * @throws IOException + // */ + // public static List listPaths(File directory, + // DirectoryStream.Filter filter, boolean recurse) + // throws IOException { + // // List of files / directories + // List files = new LinkedList(); + // + // // Get files / directories in the directory accepted by the filter. + // Path dirPath = FileSystems.getDefault().getPath( + // directory.getAbsolutePath()); + // DirectoryStream stream = null; + // try { + // stream = Files.newDirectoryStream(dirPath, filter); + // for (final Iterator it = stream.iterator(); it.hasNext();) { + // files.add(it.next()); + // } + // } finally { + // if (stream != null) { + // stream.close(); + // } + // } + // return files; + // } } diff --git a/edexOsgi/com.raytheon.uf.edex.datadelivery.bandwidth.ncf/src/com/raytheon/uf/edex/datadelivery/bandwidth/sbn/SbnSimulator.java b/edexOsgi/com.raytheon.uf.edex.datadelivery.bandwidth.ncf/src/com/raytheon/uf/edex/datadelivery/bandwidth/sbn/SbnSimulator.java index 02b7017252..f01563cf23 100644 --- a/edexOsgi/com.raytheon.uf.edex.datadelivery.bandwidth.ncf/src/com/raytheon/uf/edex/datadelivery/bandwidth/sbn/SbnSimulator.java +++ b/edexOsgi/com.raytheon.uf.edex.datadelivery.bandwidth.ncf/src/com/raytheon/uf/edex/datadelivery/bandwidth/sbn/SbnSimulator.java @@ -21,9 +21,6 @@ package com.raytheon.uf.edex.datadelivery.bandwidth.sbn; import java.io.File; import java.io.IOException; -import java.nio.file.FileSystems; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; import java.util.List; import com.google.common.annotations.VisibleForTesting; @@ -95,6 +92,8 @@ public class SbnSimulator { private final IFileProcessor fileProcessor; + private String site; + /** * Private constructor. */ @@ -115,6 +114,7 @@ public class SbnSimulator { this.sitesDirectory = new File(directoryToScan, "sbnSimulator"); this.localSiteDirectory = new File(sitesDirectory, site); this.localSiteDirectory.mkdirs(); + this.site = site; } /** @@ -126,8 +126,8 @@ public class SbnSimulator { final List files = FileUtil.listFiles(localSiteDirectory, FilenameFilters.ACCEPT_VISIBLE_FILES, false); - statusHandler.info("Found [" + files.size() + "] files for " - + SiteUtil.getSite() + " from the SBN..."); + statusHandler.info("Found [" + files.size() + "] files for " + site + + " from the SBN..."); for (File file : files) { @@ -150,36 +150,73 @@ public class SbnSimulator { * @throws IOException */ public void distributeToSiteDirs() throws IOException { - final List undistributedFiles = FileUtil.listPaths( + final List undistributedFiles = FileUtil.listFiles( directoryToScan, - FilenameFilters.ACCEPT_PATH_FILES, false); + FilenameFilters.ACCEPT_FILES, false); // get list of site dirs - final List sites = FileUtil.listPaths(sitesDirectory, - FilenameFilters.ACCEPT_PATH_DIRECTORIES, false); + final List sites = FileUtil.listFiles(sitesDirectory, + FilenameFilters.ACCEPT_DIRECTORIES, false); statusHandler.info("Found [" + undistributedFiles.size() + "] files to distribute..."); // distribute to site specific directories - for (Path file : undistributedFiles) { + for (File file : undistributedFiles) { statusHandler.info("Distributing file [" + file + "]"); - for (Path siteDir : sites) { - Path dest = FileSystems.getDefault().getPath( - siteDir.toString(), file.getFileName().toString()); - Path hiddenDest = FileSystems.getDefault() - .getPath(siteDir.toString(), - "." + file.getFileName().toString()); + for (File siteDir : sites) { + File dest = new File(siteDir, file.getName().toString()); + File hiddenDest = new File(siteDir, "." + + file.getName().toString()); // move to site sbn directory as hidden - java.nio.file.Files.copy(file, hiddenDest, - StandardCopyOption.REPLACE_EXISTING); + FileUtil.copyFile(file, hiddenDest); // rename dest to un-hidden - java.nio.file.Files.move(hiddenDest, dest, - StandardCopyOption.ATOMIC_MOVE); + hiddenDest.renameTo(dest); statusHandler.info("===> to file [" + dest + "]"); } // delete source file - java.nio.file.Files.delete(file); + file.delete(); } } + // TODO Java 1.7 version of the distributeToSiteDirs() method + // /** + // * Distribute to the site directories. Enables all site client registries + // * to ingest shared data. + // * + // * @throws IOException + // */ + // public void distributeToSiteDirs() throws IOException { + // final List undistributedFiles = FileUtil.listPaths( + // directoryToScan, + // FilenameFilters.ACCEPT_PATH_FILES, false); + // // get list of site dirs + // final List sites = FileUtil.listPaths(sitesDirectory, + // FilenameFilters.ACCEPT_PATH_DIRECTORIES, false); + // + // statusHandler.info("Found [" + undistributedFiles.size() + + // "] files to distribute..."); + // + // // distribute to site specific directories + // for (Path file : undistributedFiles) { + // statusHandler.info("Distributing file [" + file + "]"); + // for (Path siteDir : sites) { + // Path dest = FileSystems.getDefault().getPath( + // siteDir.toString(), file.getFileName().toString()); + // Path hiddenDest = FileSystems.getDefault() + // .getPath(siteDir.toString(), + // "." + file.getFileName().toString()); + // + // // move to site sbn directory as hidden + // java.nio.file.Files.copy(file, hiddenDest, + // StandardCopyOption.REPLACE_EXISTING); + // // rename dest to un-hidden + // java.nio.file.Files.move(hiddenDest, dest, + // StandardCopyOption.ATOMIC_MOVE); + // statusHandler.info("===> to file [" + dest + "]"); + // } + // // delete source file + // java.nio.file.Files.delete(file); + // } + // } + } diff --git a/tests/unit/com/raytheon/uf/edex/datadelivery/bandwidth/sbn/SbnSimulatorTest.java b/tests/unit/com/raytheon/uf/edex/datadelivery/bandwidth/sbn/SbnSimulatorTest.java index aa87aa0ccf..a767a3dae3 100644 --- a/tests/unit/com/raytheon/uf/edex/datadelivery/bandwidth/sbn/SbnSimulatorTest.java +++ b/tests/unit/com/raytheon/uf/edex/datadelivery/bandwidth/sbn/SbnSimulatorTest.java @@ -117,10 +117,8 @@ public class SbnSimulatorTest { @Test public void errorOnOneFileDoesNotStopTheOthers() throws IOException { - File fileOneExpectedResult = createTestFileGetExpected(testDir, - "fileOne.txt", "AA1"); - File fileTwoExpectedResult = createTestFileGetExpected(testDir, - "fileTwo.txt", "AA2"); + createTestFileGetExpected(testDir, "fileOne.txt", "AA1"); + createTestFileGetExpected(testDir, "fileTwo.txt", "AA2"); doThrow(new IOException()).when(fileProcessor).processFile( any(File.class));