From ecb0d2c826f460821ec43e53d5b4422529cc08b5 Mon Sep 17 00:00:00 2001 From: Dustin Johnson Date: Fri, 15 Mar 2013 10:56:14 -0500 Subject: [PATCH] Issue #1794 Consolidate common filename filters into FilenameFilters class Change-Id: I29d7267d79164e005e3c202a23267767c853bf36 Former-commit-id: 0e4c9f6ef22bf59f18ce3aab6539c52192539106 --- .../formatterlauncher/ZoneCombinerComp.java | 41 ++-- edexOsgi/build.edex/esb/conf/modes.xml | 5 + .../edex/colormap/ColorMapManager.java | 4 +- .../edex/utility/EDEXLocalizationAdapter.java | 3 +- .../edex/monitors/AEdexDirMonitor.java | 19 +- .../raytheon/edex/plugin/gfe/isc/GfeIRT.java | 9 +- .../edex/plugin/gfe/isc/IscReceiveSrv.java | 12 +- .../edex/plugin/gfe/reference/MapManager.java | 17 +- .../EnsembleGridAssembler.java | 23 +- .../edex/util/grib/GribTableLookup.java | 32 +-- .../META-INF/MANIFEST.MF | 1 + .../com/raytheon/uf/common/util/FileUtil.java | 4 +- .../uf/common/util/file/FilenameFilters.java | 228 ++++++++++++++++++ .../crawler/FileCommunicationStrategy.java | 11 +- .../uf/edex/plugin/qc/common/QCDataQuery.java | 11 +- .../common/dao/NcepPointDataPluginDao.java | 4 +- .../edex/plugin/ncgrib/NcgridAssembler.java | 40 ++- .../ncgrib/spatial/NcgribSpatialCache.java | 33 +-- .../edex/util/ncgrib/NcgribTableLookup.java | 35 ++- .../common/util/file/FilenameFiltersTest.java | 153 ++++++++++++ 20 files changed, 521 insertions(+), 164 deletions(-) create mode 100644 edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/file/FilenameFilters.java create mode 100644 tests/unit/com/raytheon/uf/common/util/file/FilenameFiltersTest.java diff --git a/cave/com.raytheon.viz.gfe/src/com/raytheon/viz/gfe/dialogs/formatterlauncher/ZoneCombinerComp.java b/cave/com.raytheon.viz.gfe/src/com/raytheon/viz/gfe/dialogs/formatterlauncher/ZoneCombinerComp.java index e9e9614d0f..42b9b6f317 100755 --- a/cave/com.raytheon.viz.gfe/src/com/raytheon/viz/gfe/dialogs/formatterlauncher/ZoneCombinerComp.java +++ b/cave/com.raytheon.viz.gfe/src/com/raytheon/viz/gfe/dialogs/formatterlauncher/ZoneCombinerComp.java @@ -75,11 +75,12 @@ import com.raytheon.uf.common.status.IUFStatusHandler; import com.raytheon.uf.common.status.UFStatus; import com.raytheon.uf.common.status.UFStatus.Priority; import com.raytheon.uf.common.util.FileUtil; +import com.raytheon.uf.common.util.file.FilenameFilters; import com.raytheon.uf.viz.core.RGBColors; import com.raytheon.uf.viz.core.VizApp; import com.raytheon.uf.viz.core.exception.VizException; import com.raytheon.viz.gfe.Activator; -import com.raytheon.viz.gfe.core.DataManager; +import com.raytheon.viz.gfe.core.DataManagerUIFactory; import com.raytheon.viz.gfe.textformatter.CombinationsFileGenerator; import com.raytheon.viz.gfe.textformatter.CombinationsFileUtil; import com.raytheon.viz.gfe.textformatter.TextProductManager; @@ -99,6 +100,7 @@ import com.raytheon.viz.gfe.ui.zoneselector.ZoneSelector; * Changes for non-blocking SaveDeleteComboDlg. * Changes for non-blocking ShuffleZoneGroupsDialog. * Changes for non-blocking ZoneColorEditorDlg. + * Mar 14, 2013 1794 djohnson Consolidate common FilenameFilter implementations. * * * @@ -114,7 +116,7 @@ public class ZoneCombinerComp extends Composite implements /** * Parent composite. */ - private Composite parent; + private final Composite parent; /** * Tool bar that mimics a menu bar. @@ -174,7 +176,7 @@ public class ZoneCombinerComp extends Composite implements /** * Product name. */ - private String productName; + private final String productName; /** * Load sub menu. This gets generated when the program runs. @@ -186,7 +188,7 @@ public class ZoneCombinerComp extends Composite implements */ private ZoneSelector zoneSelector; - private IProductTab callBack; + private final IProductTab callBack; protected TextProductManager textProductMgr; @@ -201,7 +203,7 @@ public class ZoneCombinerComp extends Composite implements Matcher matcher; - private String theSaved = ""; + private final String theSaved = ""; private Composite mapCompCtrl; @@ -217,7 +219,7 @@ public class ZoneCombinerComp extends Composite implements private String currentComboFile = null; - private LocalizationFile comboDir; + private final LocalizationFile comboDir; private boolean includeAllZones = false; @@ -802,8 +804,8 @@ public class ZoneCombinerComp extends Composite implements .getProductDefinition(productName).get("subDomainUGCs"); // First thing, give the zone resource a bounding geometry. - GridLocation gloc = DataManager.getCurrentInstance().getParmManager() - .compositeGridLocation(); + GridLocation gloc = DataManagerUIFactory.getCurrentInstance() + .getParmManager().compositeGridLocation(); zoneSelector = new ZoneSelector(controlComp, gloc, this); @@ -855,25 +857,8 @@ public class ZoneCombinerComp extends Composite implements String comboDirName = "saved"; String[] combos; File localFile; - FilenameFilter filter = new FilenameFilter() { - - /** - * Accept any file whose name ends with ".py". - * - * @param dir - * The directory in which the file exists - * @param name - * the file name - * @return true if name ends with ".py", false otherwise. - */ - @Override - public boolean accept(File dir, String name) { - if (name.endsWith(".py")) { - return true; - } - return false; - } - }; + // Accept any file whose name ends with ".py". + FilenameFilter filter = FilenameFilters.byFileExtension(".py"); if (level == null) { // Aggregate the filenames for all levels. @@ -1083,10 +1068,12 @@ public class ZoneCombinerComp extends Composite implements } } + @Override public void setStatusText(String significance, String message) { callBack.updateStatus(significance, message); } + @Override public void applyButtonState(final boolean enabled) { if (this.applyZoneComboBtn != null && !this.applyZoneComboBtn.isDisposed()) { diff --git a/edexOsgi/build.edex/esb/conf/modes.xml b/edexOsgi/build.edex/esb/conf/modes.xml index 0e6f707cb4..f23faebf11 100644 --- a/edexOsgi/build.edex/esb/conf/modes.xml +++ b/edexOsgi/build.edex/esb/conf/modes.xml @@ -111,6 +111,7 @@ qpf-ingest.xml fssobs-ingest.xml cpgsrv-spring.xml + .*sbn-simulator.* time-common.xml @@ -202,6 +203,9 @@ ebxml.*\.xml statsTemplate dataDeliveryTemplate + + .*sbn-simulator.* + .*datadelivery-ncf.* harvester-* crawler-* @@ -297,6 +301,7 @@ pointDataRetrievel-spring.xml q2FileProcessor-spring.xml satpre-spring.xml + .*sbn-simulator.* auth-request.xml diff --git a/edexOsgi/com.raytheon.edex.common/src/com/raytheon/edex/colormap/ColorMapManager.java b/edexOsgi/com.raytheon.edex.common/src/com/raytheon/edex/colormap/ColorMapManager.java index a61460330b..abc722e939 100644 --- a/edexOsgi/com.raytheon.edex.common/src/com/raytheon/edex/colormap/ColorMapManager.java +++ b/edexOsgi/com.raytheon.edex.common/src/com/raytheon/edex/colormap/ColorMapManager.java @@ -25,6 +25,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import com.raytheon.edex.exception.ColorTableException; import com.raytheon.uf.common.colormap.CMapFilenameFilter; @@ -52,6 +53,7 @@ import com.raytheon.uf.common.util.FileUtil; * Aug 20, 2008 dglazesk JiBX replaced with JaXB * Aug 20, 2008 dglazesk Updated for the new ColorMap interface * Feb 15, 2013 1638 mschenke Moved IndexColorModel creation to common.colormap utility + * Mar 14, 2013 1794 djohnson FileUtil.listFiles now returns List. * * * @@ -136,7 +138,7 @@ public class ColorMapManager { */ public String[] listColorMaps() { File colormapsDir = new File(baseColormapDir); - ArrayList files = FileUtil.listFiles(colormapsDir, + List files = FileUtil.listFiles(colormapsDir, new CMapFilenameFilter(), true); ArrayList colormaps = new ArrayList(); for (int i = 0; i < files.size(); i++) { diff --git a/edexOsgi/com.raytheon.edex.common/src/com/raytheon/edex/utility/EDEXLocalizationAdapter.java b/edexOsgi/com.raytheon.edex.common/src/com/raytheon/edex/utility/EDEXLocalizationAdapter.java index cfe47da18d..e7c79b6a09 100644 --- a/edexOsgi/com.raytheon.edex.common/src/com/raytheon/edex/utility/EDEXLocalizationAdapter.java +++ b/edexOsgi/com.raytheon.edex.common/src/com/raytheon/edex/utility/EDEXLocalizationAdapter.java @@ -57,6 +57,7 @@ import com.raytheon.uf.edex.core.props.PropertiesFactory; * Date Ticket# Engineer Description * ------------ ---------- ----------- -------------------------- * Jul 11, 2008 1250 jelkins Initial creation + * Mar 14, 2013 1794 djohnson FileUtil.listFiles now returns List. * * * @author jelkins @@ -257,7 +258,7 @@ public class EDEXLocalizationAdapter implements ILocalizationAdapter { for (LocalizationContext ctx : context) { - ArrayList fileList = com.raytheon.uf.common.util.FileUtil + List fileList = com.raytheon.uf.common.util.FileUtil .listFiles(getPath(ctx, path), null, recursive); for (File file : fileList) { diff --git a/edexOsgi/com.raytheon.edex.monitorsrv/src/com/raytheon/edex/monitors/AEdexDirMonitor.java b/edexOsgi/com.raytheon.edex.monitorsrv/src/com/raytheon/edex/monitors/AEdexDirMonitor.java index 4ecb668189..0f0dcaf4f1 100644 --- a/edexOsgi/com.raytheon.edex.monitorsrv/src/com/raytheon/edex/monitors/AEdexDirMonitor.java +++ b/edexOsgi/com.raytheon.edex.monitorsrv/src/com/raytheon/edex/monitors/AEdexDirMonitor.java @@ -19,23 +19,26 @@ **/ package com.raytheon.edex.monitors; -import java.io.File; import java.io.FilenameFilter; +import com.raytheon.uf.common.util.file.FilenameFilters; + /** * An abstract base class for directory monitors. + * *
- *
+ * 
  * SOFTWARE HISTORY
- *
+ * 
  * Date         Ticket#    Engineer    Description
  * ------------ ---------- ----------- --------------------------
  * 08May2008    1113       MW Fegan    Initial creation.
+ * Mar 14, 2013 1794       djohnson    Consolidate common FilenameFilter implementations.
  * 
  * 
- * + * * @author mfegan - * @version 1.0 + * @version 1.0 */ public abstract class AEdexDirMonitor extends AEdexMonitor { @@ -48,11 +51,7 @@ public abstract class AEdexDirMonitor extends AEdexMonitor { protected String dirToCheck = "."; /* file filter that screens out "dot" files */ - protected FilenameFilter filter = new FilenameFilter() { - public boolean accept(final File dir, final String name) { - return !name.startsWith("."); - } - }; + protected FilenameFilter filter = FilenameFilters.NO_LINUX_HIDDEN_FILES; /** * Constructor. Takes no action. diff --git a/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/isc/GfeIRT.java b/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/isc/GfeIRT.java index 607e138391..c723397310 100644 --- a/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/isc/GfeIRT.java +++ b/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/isc/GfeIRT.java @@ -59,6 +59,7 @@ import com.raytheon.uf.common.util.FileUtil; * Date Ticket# Engineer Description * ------------ ---------- ----------- -------------------------- * 07/14/09 1995 bphillip Initial creation + * Mar 14, 2013 1794 djohnson FileUtil.listFiles now returns List. * * * @@ -72,13 +73,13 @@ public class GfeIRT extends Thread { .getHandler(GfeIRT.class); /** The site ID associated with this IRT thread */ - private String siteID; + private final String siteID; /** The MHS ID associated with this IRT thread */ - private String mhsID; + private final String mhsID; /** The script file name */ - private String scriptFile; + private final String scriptFile; /** The Python script object */ private PythonScript script; @@ -186,7 +187,7 @@ public class GfeIRT extends Thread { return name.trim().matches("ISC_\\p{Alnum}{3}\\.xml"); } }; - ArrayList editAreas = FileUtil.listFiles(editAreaDir, + List editAreas = FileUtil.listFiles(editAreaDir, filter, false); String name = ""; diff --git a/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/isc/IscReceiveSrv.java b/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/isc/IscReceiveSrv.java index d662ce4442..0176cb6fa4 100644 --- a/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/isc/IscReceiveSrv.java +++ b/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/isc/IscReceiveSrv.java @@ -43,6 +43,7 @@ import com.raytheon.uf.common.status.IUFStatusHandler; import com.raytheon.uf.common.status.UFStatus; import com.raytheon.uf.common.status.UFStatus.Priority; import com.raytheon.uf.common.util.FileUtil; +import com.raytheon.uf.common.util.file.FilenameFilters; import com.raytheon.uf.edex.site.SiteAwareRegistry; /** @@ -57,6 +58,7 @@ import com.raytheon.uf.edex.site.SiteAwareRegistry; * ------------ ---------- ----------- -------------------------- * Mar 05, 2012 #361 dgilling Initial creation * Mar 12, 2013 #1759 dgilling Re-implement using IscScript. + * Mar 14, 2013 1794 djohnson Consolidate common FilenameFilter implementations. * * * @@ -70,12 +72,8 @@ public class IscReceiveSrv { private static final String METHOD_NAME = "main"; - private static final FilenameFilter docFileFilter = new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return name.endsWith(".doc"); - } - }; + private static final FilenameFilter docFileFilter = FilenameFilters + .byFileExtension(".doc"); private static final IPythonJobListener jobListener = new IPythonJobListener() { @@ -249,7 +247,7 @@ public class IscReceiveSrv { statusHandler.error("Unable to delete " + xmlFileName); } } - ArrayList docFiles = FileUtil.listFiles( + List docFiles = FileUtil.listFiles( xmlFile.getParentFile(), docFileFilter, false); for (File docFile : docFiles) { docFile.delete(); diff --git a/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/reference/MapManager.java b/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/reference/MapManager.java index b0c3a1b81c..c7eb2f450d 100644 --- a/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/reference/MapManager.java +++ b/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/reference/MapManager.java @@ -73,6 +73,7 @@ import com.raytheon.uf.common.status.IUFStatusHandler; import com.raytheon.uf.common.status.UFStatus; import com.raytheon.uf.common.status.UFStatus.Priority; import com.raytheon.uf.common.util.FileUtil; +import com.raytheon.uf.common.util.file.FilenameFilters; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; @@ -96,6 +97,7 @@ import com.vividsolutions.jts.simplify.TopologyPreservingSimplifier; * Jun 25, 2008 #1210 randerso Modified to get directories from UtilityContext * Oct 13, 2008 #1607 njensen Added genCombinationsFiles() * Sep 18, 2012 #1091 randerso Changed to use Maps.py and localMaps.py + * Mar 14, 2013 1794 djohnson Consolidate common FilenameFilter implementations. * * * @@ -119,13 +121,13 @@ public class MapManager { private List _mapErrors; - private Map> editAreaMap = new HashMap>(); + private final Map> editAreaMap = new HashMap>(); - private Map> editAreaAttrs = new HashMap>(); + private final Map> editAreaAttrs = new HashMap>(); - private List iscMarkersID = new ArrayList(); + private final List iscMarkersID = new ArrayList(); - private List iscMarkers = new ArrayList(); + private final List iscMarkers = new ArrayList(); private final String commonStaticConfigDir; @@ -357,14 +359,9 @@ public class MapManager { } d = new File(FileUtil.join(commonStaticConfigDir, SAMPLE_SETS_DIR)); - FilenameFilter filter = new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return name.trim().startsWith("ISC_Marker_Set"); - } - }; if (d.exists()) { + final FilenameFilter filter = FilenameFilters.byFilePrefix("ISC_Marker_Set"); for (File file : FileUtil.listFiles(d, filter, false)) { file.delete(); } diff --git a/edexOsgi/com.raytheon.edex.plugin.grib/src/com/raytheon/edex/plugin/grib/decoderpostprocessors/EnsembleGridAssembler.java b/edexOsgi/com.raytheon.edex.plugin.grib/src/com/raytheon/edex/plugin/grib/decoderpostprocessors/EnsembleGridAssembler.java index 6ebc0d9d0a..cad738554b 100644 --- a/edexOsgi/com.raytheon.edex.plugin.grib/src/com/raytheon/edex/plugin/grib/decoderpostprocessors/EnsembleGridAssembler.java +++ b/edexOsgi/com.raytheon.edex.plugin.grib/src/com/raytheon/edex/plugin/grib/decoderpostprocessors/EnsembleGridAssembler.java @@ -22,7 +22,6 @@ package com.raytheon.edex.plugin.grib.decoderpostprocessors; import java.io.File; import java.io.FilenameFilter; -import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; @@ -50,6 +49,7 @@ import com.raytheon.uf.common.status.IUFStatusHandler; import com.raytheon.uf.common.status.UFStatus; import com.raytheon.uf.common.status.UFStatus.Priority; import com.raytheon.uf.common.util.FileUtil; +import com.raytheon.uf.common.util.file.FilenameFilters; import com.raytheon.uf.edex.core.EDEXUtil; import com.raytheon.uf.edex.database.cluster.ClusterLockUtils; import com.raytheon.uf.edex.database.cluster.ClusterLockUtils.LockState; @@ -69,6 +69,7 @@ import com.raytheon.uf.edex.plugin.grid.dao.GridDao; * Date Ticket# Engineer Description * ------------ ---------- ----------- -------------------------- * 4/09/10 4638 bphillip Initial Creation + * Mar 14, 2013 1794 djohnson FileUtil.listFiles now returns List. * * * @@ -102,20 +103,19 @@ public class EnsembleGridAssembler implements IDecoderPostProcessor { File commonPath = pm.getFile(pm.getContext( LocalizationType.EDEX_STATIC, LocalizationLevel.BASE), "/grib/thinnedModels"); - FilenameFilter filter = new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return (!new File(dir.getPath() + File.separator + name) - .isDirectory() && name.endsWith(".xml")); - } - }; - ArrayList thinnedModelFiles = FileUtil.listFiles(commonPath, + + FilenameFilter filter = FilenameFilters.byFilters( + FilenameFilters.ACCEPT_FILES, + FilenameFilters.byFileExtension(".xml")); + + List thinnedModelFiles = FileUtil.listFiles(commonPath, filter, false); for (File file : thinnedModelFiles) { try { - CompositeModel model = (CompositeModel) SerializationUtil - .jaxbUnmarshalFromXmlFile(file.getPath()); + CompositeModel model = SerializationUtil + .jaxbUnmarshalFromXmlFile(CompositeModel.class, + file.getPath()); thinnedModels.put(model.getModelName(), model); } catch (SerializationException e) { statusHandler.handle(Priority.PROBLEM, @@ -124,6 +124,7 @@ public class EnsembleGridAssembler implements IDecoderPostProcessor { } } + @Override public GridRecord[] process(GridRecord rec) throws GribException { Map newRecords = new HashMap(); String compositeModel = getCompositeModel(rec.getDatasetId()); diff --git a/edexOsgi/com.raytheon.edex.plugin.grib/src/com/raytheon/edex/util/grib/GribTableLookup.java b/edexOsgi/com.raytheon.edex.plugin.grib/src/com/raytheon/edex/util/grib/GribTableLookup.java index fb4cf4b765..87bc0de644 100644 --- a/edexOsgi/com.raytheon.edex.plugin.grib/src/com/raytheon/edex/util/grib/GribTableLookup.java +++ b/edexOsgi/com.raytheon.edex.plugin.grib/src/com/raytheon/edex/util/grib/GribTableLookup.java @@ -25,8 +25,8 @@ import java.io.File; import java.io.FileReader; import java.io.FilenameFilter; import java.io.IOException; -import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; @@ -45,6 +45,7 @@ import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel import com.raytheon.uf.common.localization.LocalizationContext.LocalizationType; import com.raytheon.uf.common.localization.PathManagerFactory; import com.raytheon.uf.common.util.FileUtil; +import com.raytheon.uf.common.util.file.FilenameFilters; /** * Class used to access and manage data from any defined tables. @@ -56,6 +57,7 @@ import com.raytheon.uf.common.util.FileUtil; * Date Ticket# Engineer Description * ------------ ---------- ----------- -------------------------- * 4/7/09 1994 bphillip Initial Creation + * Mar 14, 2013 1794 djohnson Consolidate common FilenameFilter implementations. * * * @@ -73,7 +75,7 @@ public class GribTableLookup { private static final int NO_SUBCENTER = -1; /** The map of defined tables */ - private Map> tableMap; + private final Map> tableMap; /** The singleton instance */ private static GribTableLookup instance; @@ -219,31 +221,19 @@ public class GribTableLookup { } private void initTablesFromPath(String commonPath) { - FilenameFilter filter = new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return new File(dir.getPath() + File.separator + name) - .isDirectory(); - } - }; + FilenameFilter tableFilter = FilenameFilters.byFileExtension(".table"); - FilenameFilter tableFilter = new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return name.endsWith(".table"); - } - }; - - ArrayList files = FileUtil.listFiles(new File(commonPath), - filter, false); + List files = FileUtil.listFiles(new File(commonPath), + FilenameFilters.ACCEPT_DIRECTORIES, false); for (File f : files) { int center = 0; try { center = Integer.parseInt(f.getPath().substring( f.getPath().lastIndexOf("/") + 1)); - ArrayList files2 = FileUtil.listFiles(new File(commonPath - + File.separator + String.valueOf(center)), filter, + List files2 = FileUtil.listFiles(new File(commonPath + + File.separator + String.valueOf(center)), + FilenameFilters.ACCEPT_DIRECTORIES, true); int subcenter = 0; @@ -251,7 +241,7 @@ public class GribTableLookup { if (!f2.getPath().contains(".svn")) { subcenter = Integer.parseInt(f2.getPath().substring( f2.getPath().lastIndexOf("/") + 1)); - ArrayList tableFiles = FileUtil.listFiles(f2, + List tableFiles = FileUtil.listFiles(f2, tableFilter, false); String tableName = null; for (File table : tableFiles) { diff --git a/edexOsgi/com.raytheon.uf.common.util/META-INF/MANIFEST.MF b/edexOsgi/com.raytheon.uf.common.util/META-INF/MANIFEST.MF index f21072c209..7ee7146d7c 100644 --- a/edexOsgi/com.raytheon.uf.common.util/META-INF/MANIFEST.MF +++ b/edexOsgi/com.raytheon.uf.common.util/META-INF/MANIFEST.MF @@ -12,6 +12,7 @@ Require-Bundle: org.junit;bundle-version="1.0.0", Export-Package: com.raytheon.uf.common.util, com.raytheon.uf.common.util.algorithm, com.raytheon.uf.common.util.cache, + com.raytheon.uf.common.util.file, com.raytheon.uf.common.util.header, com.raytheon.uf.common.util.mapping, com.raytheon.uf.common.util.registry, 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 1bb04bfdec..8b812e98a8 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,6 +31,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.channels.FileChannel; import java.util.ArrayList; +import java.util.List; import java.util.regex.Pattern; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; @@ -55,6 +56,7 @@ import java.util.zip.GZIPOutputStream; * 02/15/2013 #1597 randerso Fixed error when copying empty files * Feb 15, 2013 1638 mschenke Moved EOL field from edex.common Util * Mar 11, 2013 1645 djohnson Added file modification watcher. + * Mar 14, 2013 1794 djohnson FileUtil.listFiles now returns List. * * * @@ -114,7 +116,7 @@ public class FileUtil { * whether or not to go into subdirectories * @return the files that match the filter */ - public static ArrayList listFiles(File directory, + public static List listFiles(File directory, FilenameFilter filter, boolean recurse) { // List of files / directories ArrayList files = new ArrayList(); diff --git a/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/file/FilenameFilters.java b/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/file/FilenameFilters.java new file mode 100644 index 0000000000..6aa5abac44 --- /dev/null +++ b/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/file/FilenameFilters.java @@ -0,0 +1,228 @@ +/** + * This software was developed and / or modified by Raytheon Company, + * pursuant to Contract DG133W-05-CQ-1067 with the US Government. + * + * U.S. EXPORT CONTROLLED TECHNICAL DATA + * This software product contains export-restricted data whose + * export/transfer/disclosure is restricted by U.S. law. Dissemination + * to non-U.S. persons whether in the United States or abroad requires + * an export license or other authorization. + * + * Contractor Name: Raytheon Company + * Contractor Address: 6825 Pine Street, Suite 340 + * Mail Stop B8 + * Omaha, NE 68106 + * 402.291.0100 + * + * See the AWIPS II Master Rights File ("Master Rights File.pdf") for + * further licensing information. + **/ +package com.raytheon.uf.common.util.file; + +import java.io.File; +import java.io.FilenameFilter; + +/** + * Consolidates common filename filters. + * + *
+ * 
+ * SOFTWARE HISTORY
+ * 
+ * Date         Ticket#    Engineer    Description
+ * ------------ ---------- ----------- --------------------------
+ * Mar 14, 2013 1794       djohnson     Initial creation
+ * 
+ * 
+ * + * @author djohnson + * @version 1.0 + */ + +public final class FilenameFilters { + + /** + * Reverses a {@link FilenameFilter} + */ + private static class ReverseFilter implements FilenameFilter { + private final FilenameFilter filter; + + private ReverseFilter(FilenameFilter filter) { + this.filter = filter; + } + + @Override + public boolean accept(File dir, String name) { + return !filter.accept(dir, name); + } + } + + /** + * {@link FilenameFilter} that matches files with the specified extension. + */ + private static class FileExtensionFilenameFilter implements FilenameFilter { + + private final String fileExtension; + + /** + * @param fileExtension + */ + private FileExtensionFilenameFilter(String fileExtension) { + this.fileExtension = fileExtension; + } + + @Override + public boolean accept(File dir, String name) { + return name.endsWith(fileExtension); + } + } + + /** + * {@link FilenameFilter} that matches files with the specified prefix. + */ + private static class StartsWithPrefix implements FilenameFilter { + private final String prefix; + + private StartsWithPrefix(String prefix) { + this.prefix = prefix; + } + + @Override + public boolean accept(final File dir, final String name) { + return name != null && name.trim().startsWith(prefix); + } + }; + + /** + * {@link FilenameFilter} that denies files with the specified prefix. + */ + private static class SumFilter implements FilenameFilter { + private final FilenameFilter[] filters; + + private SumFilter(FilenameFilter[] filters) { + this.filters = filters; + } + + @Override + public boolean accept(final File dir, final String name) { + for (FilenameFilter filter : filters) { + if (!filter.accept(dir, name)) { + return false; + } + } + + return true; + } + }; + + /** + * No construction. + */ + private FilenameFilters() { + } + + /** + * Accepts all files. + */ + public static final FilenameFilter ACCEPT_ALL = new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return true; + } + }; + + /** + * Denies all files. + */ + public static final FilenameFilter ACCEPT_NONE = new ReverseFilter( + ACCEPT_ALL); + + /** + * Denies all files starting with "." + */ + public static final FilenameFilter NO_LINUX_HIDDEN_FILES = new ReverseFilter( + byFilePrefix(".")); + + /** + * Accepts directories. + */ + public static final FilenameFilter ACCEPT_DIRECTORIES = new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return new File(dir.getPath(), name).isDirectory(); + } + }; + + /** + * Accepts directories. + */ + public static final FilenameFilter ACCEPT_FILES = new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return new File(dir.getPath(), name).isFile(); + } + }; + + /** + * Returns a {@link FilenameFilter} that matches the specified file + * extension. + * + * @param fileExtension + * the file extension to match + * @return the file name filter + */ + public static FilenameFilter byFileExtension(final String fileExtension) { + return new FileExtensionFilenameFilter(fileExtension); + } + + /** + * Returns a {@link FilenameFilter} that matches the specified file name + * prefix. + * + * @param prefix + * the file prefix to match + * @return the file name filter + */ + public static FilenameFilter byFilePrefix(final String prefix) { + return new StartsWithPrefix(prefix); + } + + /** + * Returns a {@link FilenameFilter} that returns true only if all filters + * return true. + * + * @param filters + * the filters + * @return the file name filter + */ + public static FilenameFilter byFilters(FilenameFilter filters) { + // This method version forces at least one filter to supplied + return byFilters(new FilenameFilter[] { filters }); + } + + /** + * Returns a {@link FilenameFilter} that returns true only if all filters + * return true. + * + * @param filters + * the filters + * @return the file name filter + */ + public static FilenameFilter byFilters(FilenameFilter... filters) { + return new SumFilter(filters); + } + + /** + * Returns a {@link FilenameFilter} that returns the reverse of the + * specified filter. For instance, if the supplied filter would return true + * if the file ends with .xml this version returns a filter that would + * return false. + * + * @param filter + * the filter to reverse + * @return the reversed filter + */ + public static FilenameFilter reverse(FilenameFilter filter) { + return new ReverseFilter(filter); + } +} diff --git a/edexOsgi/com.raytheon.uf.edex.datadelivery.harvester/src/com/raytheon/uf/edex/datadelivery/harvester/crawler/FileCommunicationStrategy.java b/edexOsgi/com.raytheon.uf.edex.datadelivery.harvester/src/com/raytheon/uf/edex/datadelivery/harvester/crawler/FileCommunicationStrategy.java index be02cea283..bc51d2815f 100644 --- a/edexOsgi/com.raytheon.uf.edex.datadelivery.harvester/src/com/raytheon/uf/edex/datadelivery/harvester/crawler/FileCommunicationStrategy.java +++ b/edexOsgi/com.raytheon.uf.edex.datadelivery.harvester/src/com/raytheon/uf/edex/datadelivery/harvester/crawler/FileCommunicationStrategy.java @@ -20,7 +20,6 @@ package com.raytheon.uf.edex.datadelivery.harvester.crawler; import java.io.File; -import java.io.FilenameFilter; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -53,6 +52,7 @@ import com.raytheon.uf.common.time.util.TimeUtil; import com.raytheon.uf.common.util.CollectionUtil; import com.raytheon.uf.common.util.FileUtil; import com.raytheon.uf.common.util.StringUtil; +import com.raytheon.uf.common.util.file.FilenameFilters; import com.raytheon.uf.edex.datadelivery.harvester.CrawlMetaDataHandler; import com.raytheon.uf.edex.datadelivery.harvester.config.CrawlAgent; import com.raytheon.uf.edex.datadelivery.harvester.config.HarvesterConfig; @@ -72,6 +72,7 @@ import com.raytheon.uf.edex.datadelivery.retrieval.ProviderCollectionLinkStore; * Jul 17, 2012 740 djohnson Initial creation * Aug 06, 2012 1022 djohnson Add shutdown(), write out millis with filename to prevent overwriting. * Sep 10, 2012 1154 djohnson Use JAXB instead of thrift, allowing introspection of links, return files in ascending order. + * Mar 14, 2013 1794 djohnson Consolidate common FilenameFilter implementations. * * * @@ -306,12 +307,8 @@ class FileCommunicationStrategy implements CommunicationStrategy { private File[] readFilesInDir(final File dir, final String fileExtension) { if (dir.isDirectory()) { - File[] files = dir.listFiles(new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return name.endsWith(fileExtension); - } - }); + File[] files = dir.listFiles(FilenameFilters + .byFileExtension(fileExtension)); // order the files in time order Arrays.sort(files, new Comparator() { diff --git a/edexOsgi/com.raytheon.uf.edex.plugin.qc/src/com/raytheon/uf/edex/plugin/qc/common/QCDataQuery.java b/edexOsgi/com.raytheon.uf.edex.plugin.qc/src/com/raytheon/uf/edex/plugin/qc/common/QCDataQuery.java index 626d35752c..bcd61a56ae 100644 --- a/edexOsgi/com.raytheon.uf.edex.plugin.qc/src/com/raytheon/uf/edex/plugin/qc/common/QCDataQuery.java +++ b/edexOsgi/com.raytheon.uf.edex.plugin.qc/src/com/raytheon/uf/edex/plugin/qc/common/QCDataQuery.java @@ -47,6 +47,7 @@ import com.raytheon.uf.edex.pointdata.PointDataQuery; * Date Ticket# Engineer Description * ------------ ---------- ----------- -------------------------- * 12/04/09 3408 bphillip Initial creation + * Mar 14, 2013 1794 djohnson FileUtil.listFiles now returns List. * * * @@ -56,7 +57,7 @@ import com.raytheon.uf.edex.pointdata.PointDataQuery; public class QCDataQuery extends PointDataQuery { /** List of query parameters for "querying" the netCDF file */ - private List queryParameters; + private final List queryParameters; /** * Constructs a new QCDataQuery @@ -71,7 +72,8 @@ public class QCDataQuery extends PointDataQuery { queryParameters = new ArrayList(); } - public void addParameter(String name, String value, String operand) { + @Override + public void addParameter(String name, String value, String operand) { queryParameters.add(name + " " + value + " " + operand); } @@ -82,13 +84,14 @@ public class QCDataQuery extends PointDataQuery { * @throws Exception * if errors occur while retrieving the data */ - public PointDataContainer execute() throws Exception { + @Override + public PointDataContainer execute() throws Exception { List containers = new ArrayList(); // Gets the available files for querying Map pathMap = QCPaths.getPaths(); for (File dir : pathMap.values()) { - ArrayList files = FileUtil.listFiles(dir, null, false); + List files = FileUtil.listFiles(dir, null, false); if (!files.isEmpty()) { String[] fileParams = ((QCDao) dao).getParameters(files.get(0)); diff --git a/ncep/gov.noaa.nws.ncep.edex.common/src/gov/noaa/nws/ncep/edex/common/dao/NcepPointDataPluginDao.java b/ncep/gov.noaa.nws.ncep.edex.common/src/gov/noaa/nws/ncep/edex/common/dao/NcepPointDataPluginDao.java index 5438c26887..5ce37e18c9 100644 --- a/ncep/gov.noaa.nws.ncep.edex.common/src/gov/noaa/nws/ncep/edex/common/dao/NcepPointDataPluginDao.java +++ b/ncep/gov.noaa.nws.ncep.edex.common/src/gov/noaa/nws/ncep/edex/common/dao/NcepPointDataPluginDao.java @@ -14,7 +14,7 @@ import com.raytheon.uf.common.dataplugin.persist.IPersistable; import com.raytheon.uf.common.datastorage.IDataStore; import com.raytheon.uf.common.util.FileUtil; import com.raytheon.uf.edex.core.hdf5.HDF5PluginFilenameFilter; -import com.raytheon.uf.edex.pointdata.*; +import com.raytheon.uf.edex.pointdata.PointDataPluginDao; public abstract class NcepPointDataPluginDao extends PointDataPluginDao{ protected List tableClassNameList= new ArrayList(); @@ -84,7 +84,7 @@ public abstract class NcepPointDataPluginDao extends */ public void purgeAllData() throws PluginException { purgeAllTables(); - ArrayList files = FileUtil.listFiles(new File(PLUGIN_HDF5_DIR), + List files = FileUtil.listFiles(new File(PLUGIN_HDF5_DIR), new HDF5PluginFilenameFilter(pluginName), true); for (File file : files) { file.delete(); diff --git a/ncep/gov.noaa.nws.ncep.edex.plugin.ncgrib/src/gov/noaa/nws/ncep/edex/plugin/ncgrib/NcgridAssembler.java b/ncep/gov.noaa.nws.ncep.edex.plugin.ncgrib/src/gov/noaa/nws/ncep/edex/plugin/ncgrib/NcgridAssembler.java index 0386f0c128..45d8b7769b 100644 --- a/ncep/gov.noaa.nws.ncep.edex.plugin.ncgrib/src/gov/noaa/nws/ncep/edex/plugin/ncgrib/NcgridAssembler.java +++ b/ncep/gov.noaa.nws.ncep.edex.plugin.ncgrib/src/gov/noaa/nws/ncep/edex/plugin/ncgrib/NcgridAssembler.java @@ -20,37 +20,37 @@ package gov.noaa.nws.ncep.edex.plugin.ncgrib; +import gov.noaa.nws.ncep.common.dataplugin.ncgrib.NcgribModel; +import gov.noaa.nws.ncep.common.dataplugin.ncgrib.NcgribRecord; +import gov.noaa.nws.ncep.common.dataplugin.ncgrib.exception.GribException; +import gov.noaa.nws.ncep.common.dataplugin.ncgrib.spatial.projections.LatLonNcgridCoverage; +import gov.noaa.nws.ncep.common.dataplugin.ncgrib.spatial.projections.NcgridCoverage; +import gov.noaa.nws.ncep.edex.plugin.ncgrib.dao.NcgribDao; +import gov.noaa.nws.ncep.edex.plugin.ncgrib.spatial.NcgribSpatialCache; +import gov.noaa.nws.ncep.edex.plugin.ncgrib.util.NcgribModelCache; +import gov.noaa.nws.ncep.edex.util.ncgrib.NccompositeModel; + import java.io.File; import java.io.FilenameFilter; -import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; -import com.raytheon.uf.common.dataplugin.PluginException; import com.raytheon.edex.util.Util; +import com.raytheon.uf.common.dataplugin.PluginException; import com.raytheon.uf.common.datastorage.records.FloatDataRecord; import com.raytheon.uf.common.localization.IPathManager; -import com.raytheon.uf.common.localization.PathManagerFactory; import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel; import com.raytheon.uf.common.localization.LocalizationContext.LocalizationType; +import com.raytheon.uf.common.localization.PathManagerFactory; import com.raytheon.uf.common.serialization.SerializationException; import com.raytheon.uf.common.serialization.SerializationUtil; import com.raytheon.uf.common.util.FileUtil; +import com.raytheon.uf.common.util.file.FilenameFilters; import com.raytheon.uf.edex.database.DataAccessLayerException; import com.raytheon.uf.edex.database.plugin.PluginFactory; -import gov.noaa.nws.ncep.edex.util.ncgrib.NccompositeModel; -import gov.noaa.nws.ncep.edex.plugin.ncgrib.dao.NcgribDao; -import gov.noaa.nws.ncep.edex.plugin.ncgrib.spatial.NcgribSpatialCache; -import gov.noaa.nws.ncep.edex.plugin.ncgrib.util.NcgribModelCache; -import gov.noaa.nws.ncep.common.dataplugin.ncgrib.NcgribModel; -import gov.noaa.nws.ncep.common.dataplugin.ncgrib.NcgribRecord; -import gov.noaa.nws.ncep.common.dataplugin.ncgrib.exception.GribException; -import gov.noaa.nws.ncep.common.dataplugin.ncgrib.spatial.projections.NcgridCoverage; -import gov.noaa.nws.ncep.common.dataplugin.ncgrib.spatial.projections.LatLonNcgridCoverage; - /** * The GridAssmebler class is part of the ingest process for grib data. Some * grib model come in as octants. This class will combine those octants into a @@ -63,6 +63,7 @@ import gov.noaa.nws.ncep.common.dataplugin.ncgrib.spatial.projections.LatLonNcgr * Date Ticket# Engineer Description * ------------ ---------- ----------- -------------------------- * 4/09/10 4638 bphillip Initial Creation + * Mar 14, 2013 1794 djohnson FileUtil.listFiles now returns List. * * * @@ -97,16 +98,11 @@ public class NcgridAssembler { //System.out.println(" load thin models commonPath=" + commonPath); - FilenameFilter filter = new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - //System.out.println(" load thin models dir=" + dir.getPath() +"/" + name); + FilenameFilter filter = FilenameFilters.byFilters( + FilenameFilters.ACCEPT_FILES, + FilenameFilters.byFileExtension(".xml")); - return (!new File(dir.getPath() + File.separator + name) - .isDirectory() && name.endsWith(".xml")); - } - }; - ArrayList thinnedModelFiles = FileUtil.listFiles(commonPath, + List thinnedModelFiles = FileUtil.listFiles(commonPath, filter, false); for (File file : thinnedModelFiles) { diff --git a/ncep/gov.noaa.nws.ncep.edex.plugin.ncgrib/src/gov/noaa/nws/ncep/edex/plugin/ncgrib/spatial/NcgribSpatialCache.java b/ncep/gov.noaa.nws.ncep.edex.plugin.ncgrib/src/gov/noaa/nws/ncep/edex/plugin/ncgrib/spatial/NcgribSpatialCache.java index 0b417ebc06..f4445a295b 100644 --- a/ncep/gov.noaa.nws.ncep.edex.plugin.ncgrib/src/gov/noaa/nws/ncep/edex/plugin/ncgrib/spatial/NcgribSpatialCache.java +++ b/ncep/gov.noaa.nws.ncep.edex.plugin.ncgrib/src/gov/noaa/nws/ncep/edex/plugin/ncgrib/spatial/NcgribSpatialCache.java @@ -20,9 +20,15 @@ package gov.noaa.nws.ncep.edex.plugin.ncgrib.spatial; +import gov.noaa.nws.ncep.common.dataplugin.ncgrib.exception.GribException; +import gov.noaa.nws.ncep.common.dataplugin.ncgrib.spatial.projections.NcgridCoverage; +import gov.noaa.nws.ncep.common.dataplugin.ncgrib.subgrid.SubNcgrid; +import gov.noaa.nws.ncep.common.dataplugin.ncgrib.util.NcgridModel; +import gov.noaa.nws.ncep.edex.plugin.ncgrib.dao.INcgridCoverageDao; +import gov.noaa.nws.ncep.edex.util.ncgrib.NcgribModelLookup; + import java.io.File; import java.io.FilenameFilter; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -31,9 +37,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.raytheon.uf.common.localization.IPathManager; -import com.raytheon.uf.common.localization.PathManagerFactory; import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel; import com.raytheon.uf.common.localization.LocalizationContext.LocalizationType; +import com.raytheon.uf.common.localization.PathManagerFactory; import com.raytheon.uf.common.serialization.SerializationUtil; import com.raytheon.uf.common.util.FileUtil; import com.raytheon.uf.edex.core.EDEXUtil; @@ -41,13 +47,6 @@ import com.raytheon.uf.edex.database.DataAccessLayerException; import com.raytheon.uf.edex.database.dao.CoreDao; import com.raytheon.uf.edex.database.dao.DaoConfig; -import gov.noaa.nws.ncep.edex.plugin.ncgrib.dao.INcgridCoverageDao; -import gov.noaa.nws.ncep.edex.util.ncgrib.NcgribModelLookup; -import gov.noaa.nws.ncep.common.dataplugin.ncgrib.exception.GribException; -import gov.noaa.nws.ncep.common.dataplugin.ncgrib.spatial.projections.NcgridCoverage; -import gov.noaa.nws.ncep.common.dataplugin.ncgrib.subgrid.SubNcgrid; -import gov.noaa.nws.ncep.common.dataplugin.ncgrib.util.NcgridModel; - /** * Cache used for holding GridCoverage objects. Since creating geometries and * CRS objects are expensive operations, this cache is used to store @@ -60,6 +59,7 @@ import gov.noaa.nws.ncep.common.dataplugin.ncgrib.util.NcgridModel; * Date Ticket# Engineer Description * ------------ ---------- ----------- -------------------------- * 4/7/09 1994 bphillip Initial Creation + * Mar 14, 2013 1794 djohnson FileUtil.listFiles now returns List. * * * @@ -79,7 +79,7 @@ public class NcgribSpatialCache { * The key for this map is the id field of the GridCoverage object stored as * the value of the map */ - private Map ncspatialMap; + private final Map ncspatialMap; /** * Map containing the GridCoverages
@@ -87,17 +87,17 @@ public class NcgribSpatialCache { * as the value of the map. This is only used internally for lookup of a * coverage by name aka gridId. */ - private Map ncspatialNameMap; + private final Map ncspatialNameMap; /** * Map containing the subGrid coverage based on a model name. */ - private Map subNcgridCoverageMap; + private final Map subNcgridCoverageMap; /** * Map containing the subGrid definition based on a model name. */ - private Map definedSubNcgridMap; + private final Map definedSubNcgridMap; /** * Gets the singleton instance of NcgribSpatialCache @@ -272,7 +272,7 @@ public class NcgribSpatialCache { //System.out.println("ncep default sitePah=" + sitePath); - ArrayList files = FileUtil.listFiles(new File(basePath), filter, + List files = FileUtil.listFiles(new File(basePath), filter, true); // Add any spatial information defined by the site @@ -285,8 +285,9 @@ public class NcgribSpatialCache { for (File file : files) { try { //System.out.println ("ncep default filePath=" + file.getPath()); - NcgridCoverage grid = (NcgridCoverage) SerializationUtil - .jaxbUnmarshalFromXmlFile(file.getPath()); + NcgridCoverage grid = SerializationUtil + .jaxbUnmarshalFromXmlFile(NcgridCoverage.class, + file.getPath()); putGrid(grid, true); } catch (Exception e) { // Log error but do not throw exception diff --git a/ncep/gov.noaa.nws.ncep.edex.plugin.ncgrib/src/gov/noaa/nws/ncep/edex/util/ncgrib/NcgribTableLookup.java b/ncep/gov.noaa.nws.ncep.edex.plugin.ncgrib/src/gov/noaa/nws/ncep/edex/util/ncgrib/NcgribTableLookup.java index eef4f52923..a4196eee10 100644 --- a/ncep/gov.noaa.nws.ncep.edex.plugin.ncgrib/src/gov/noaa/nws/ncep/edex/util/ncgrib/NcgribTableLookup.java +++ b/ncep/gov.noaa.nws.ncep.edex.plugin.ncgrib/src/gov/noaa/nws/ncep/edex/util/ncgrib/NcgribTableLookup.java @@ -20,33 +20,33 @@ package gov.noaa.nws.ncep.edex.util.ncgrib; +import gov.noaa.nws.ncep.common.dataplugin.ncgrib.NcGenProcess; +import gov.noaa.nws.ncep.common.dataplugin.ncgrib.Ncgrib1Parameter; +import gov.noaa.nws.ncep.common.dataplugin.ncgrib.NcgribLevel; +import gov.noaa.nws.ncep.common.dataplugin.ncgrib.NcgribParameter; +import gov.noaa.nws.ncep.common.dataplugin.ncgrib.exception.GribException; +import gov.noaa.nws.ncep.common.dataplugin.ncgrib.util.Ncgrib1ParameterLookup; + import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FilenameFilter; import java.io.IOException; -import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import gov.noaa.nws.ncep.common.dataplugin.ncgrib.NcGenProcess; - -import gov.noaa.nws.ncep.common.dataplugin.ncgrib.NcgribLevel; -import gov.noaa.nws.ncep.common.dataplugin.ncgrib.NcgribParameter; -import gov.noaa.nws.ncep.common.dataplugin.ncgrib.exception.GribException; -import gov.noaa.nws.ncep.common.dataplugin.ncgrib.Ncgrib1Parameter; -import gov.noaa.nws.ncep.common.dataplugin.ncgrib.util.Ncgrib1ParameterLookup; - import com.raytheon.uf.common.dataplugin.level.LevelFactory; import com.raytheon.uf.common.dataplugin.level.MasterLevel; import com.raytheon.uf.common.localization.IPathManager; -import com.raytheon.uf.common.localization.PathManagerFactory; import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel; import com.raytheon.uf.common.localization.LocalizationContext.LocalizationType; +import com.raytheon.uf.common.localization.PathManagerFactory; import com.raytheon.uf.common.util.FileUtil; +import com.raytheon.uf.common.util.file.FilenameFilters; import com.raytheon.uf.edex.database.dao.CoreDao; import com.raytheon.uf.edex.database.dao.DaoConfig; @@ -77,7 +77,7 @@ public class NcgribTableLookup { private static final int NO_SUBCENTER = -1; /** The map of defined tables */ - private Map> tableMap; + private final Map> tableMap; /** The singleton instance */ private static NcgribTableLookup instance; @@ -236,14 +236,9 @@ public class NcgribTableLookup { } }; - FilenameFilter tableFilter = new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return name.endsWith(".table"); - } - }; + FilenameFilter tableFilter = FilenameFilters.byFileExtension(".table"); - ArrayList files = FileUtil.listFiles(new File(commonPath), + List files = FileUtil.listFiles(new File(commonPath), filter, false); for (File f : files) { int center = 0; @@ -251,7 +246,7 @@ public class NcgribTableLookup { center = Integer.parseInt(f.getPath().substring( f.getPath().lastIndexOf("/") + 1)); - ArrayList files2 = FileUtil.listFiles(new File(commonPath + List files2 = FileUtil.listFiles(new File(commonPath + File.separator + String.valueOf(center)), filter, true); @@ -260,7 +255,7 @@ public class NcgribTableLookup { if (!f2.getPath().contains(".svn")) { subcenter = Integer.parseInt(f2.getPath().substring( f2.getPath().lastIndexOf("/") + 1)); - ArrayList tableFiles = FileUtil.listFiles(f2, + List tableFiles = FileUtil.listFiles(f2, tableFilter, false); String tableName = null; for (File table : tableFiles) { diff --git a/tests/unit/com/raytheon/uf/common/util/file/FilenameFiltersTest.java b/tests/unit/com/raytheon/uf/common/util/file/FilenameFiltersTest.java new file mode 100644 index 0000000000..41d4c987ab --- /dev/null +++ b/tests/unit/com/raytheon/uf/common/util/file/FilenameFiltersTest.java @@ -0,0 +1,153 @@ +/** + * This software was developed and / or modified by Raytheon Company, + * pursuant to Contract DG133W-05-CQ-1067 with the US Government. + * + * U.S. EXPORT CONTROLLED TECHNICAL DATA + * This software product contains export-restricted data whose + * export/transfer/disclosure is restricted by U.S. law. Dissemination + * to non-U.S. persons whether in the United States or abroad requires + * an export license or other authorization. + * + * Contractor Name: Raytheon Company + * Contractor Address: 6825 Pine Street, Suite 340 + * Mail Stop B8 + * Omaha, NE 68106 + * 402.291.0100 + * + * See the AWIPS II Master Rights File ("Master Rights File.pdf") for + * further licensing information. + **/ +package com.raytheon.uf.common.util.file; + +import static com.raytheon.uf.common.util.file.FilenameFilters.reverse; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; + +import org.junit.Test; + +import com.raytheon.uf.common.util.TestUtil; + +/** + * Test {@link FilenameFilters}. + * + *
+ * 
+ * SOFTWARE HISTORY
+ * 
+ * Date         Ticket#    Engineer    Description
+ * ------------ ---------- ----------- --------------------------
+ * Mar 14, 2013 1794       djohnson     Initial creation
+ * 
+ * 
+ * + * @author djohnson + * @version 1.0 + */ +public class FilenameFiltersTest { + + private static final File UNUSED_DIR = new File("."); + + @Test + public void acceptAllAlwaysReturnsTrue() { + assertTrue(FilenameFilters.ACCEPT_ALL.accept(null, null)); + } + + @Test + public void acceptNoneAlwaysReturnsFalse() { + assertFalse(FilenameFilters.ACCEPT_NONE.accept(null, null)); + } + + @Test + public void reverseWillReturnOppositeOfFilter() { + assertEquals(!FilenameFilters.ACCEPT_ALL.accept(null, null), + reverse(FilenameFilters.ACCEPT_ALL).accept(null, null)); + assertEquals(!FilenameFilters.ACCEPT_NONE.accept(null, null), + reverse(FilenameFilters.ACCEPT_NONE).accept(null, null)); + } + + @Test + public void byFileExtensionAcceptsFileWithExtension() { + assertTrue(FilenameFilters.byFileExtension(".xml").accept(UNUSED_DIR, + "blah.xml")); + } + + @Test + public void byFileExtensionDoesNotAcceptFileWithoutExtension() { + assertFalse(FilenameFilters.byFileExtension(".xml").accept(UNUSED_DIR, + "blah.txt")); + } + + @Test + public void noLinuxHiddenFilesAcceptsNonHidden() { + assertTrue(FilenameFilters.NO_LINUX_HIDDEN_FILES.accept(UNUSED_DIR, + "nothidden")); + } + + @Test + public void noLinuxHiddenFilesDoesNotAcceptHidden() { + assertFalse(FilenameFilters.NO_LINUX_HIDDEN_FILES.accept(UNUSED_DIR, + ".hidden")); + } + + @Test + public void byFiltersReturnsTrueIfAllFiltersAccept() { + FilenameFilter sumFilter = FilenameFilters.byFilters( + FilenameFilters.NO_LINUX_HIDDEN_FILES, + FilenameFilters.ACCEPT_ALL); + assertTrue(sumFilter.accept(UNUSED_DIR, "nothidden")); + } + + @Test + public void byFiltersReturnsFalseIfAnyFilterDoesNotAccept() { + FilenameFilter sumFilter = FilenameFilters.byFilters( + FilenameFilters.NO_LINUX_HIDDEN_FILES, + FilenameFilters.ACCEPT_ALL); + assertFalse(sumFilter.accept(UNUSED_DIR, ".hidden")); + } + + @Test + public void acceptFilesReturnsTrueForFile() throws IOException { + File testDir = TestUtil.setupTestClassDir(FilenameFiltersTest.class); + File file = new File(testDir, "realFile.txt"); + file.createNewFile(); + + assertTrue(FilenameFilters.ACCEPT_FILES.accept(file.getParentFile(), + file.getName())); + } + + @Test + public void acceptFilesReturnsFalseForDirectory() throws IOException { + File testDir = TestUtil.setupTestClassDir(FilenameFiltersTest.class); + File file = new File(testDir, "directory"); + file.mkdirs(); + + assertFalse(FilenameFilters.ACCEPT_FILES.accept(file.getParentFile(), + file.getName())); + } + + @Test + public void acceptDirectoriesReturnsTrueForDirectory() throws IOException { + File testDir = TestUtil.setupTestClassDir(FilenameFiltersTest.class); + File file = new File(testDir, "directory"); + file.mkdirs(); + + assertTrue(FilenameFilters.ACCEPT_DIRECTORIES.accept( + file.getParentFile(), file.getName())); + } + + @Test + public void acceptDirectoriesReturnsFalseForFile() throws IOException { + File testDir = TestUtil.setupTestClassDir(FilenameFiltersTest.class); + + File file = new File(testDir, "realFile.txt"); + file.createNewFile(); + + assertFalse(FilenameFilters.ACCEPT_DIRECTORIES.accept( + file.getParentFile(), file.getName())); + } +}