diff --git a/cdm/core/src/main/java/thredds/inventory/MFiles.java b/cdm/core/src/main/java/thredds/inventory/MFiles.java index 25ed49d2c3..c982da6952 100644 --- a/cdm/core/src/main/java/thredds/inventory/MFiles.java +++ b/cdm/core/src/main/java/thredds/inventory/MFiles.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.util.ServiceLoader; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import thredds.filesystem.MFileOS; @@ -48,4 +49,21 @@ public static MFile create(@Nonnull String location) { } return new MFileOS(location); } + + /** + * Create an {@link thredds.inventory.MFile} from a given location if it exists, otherwise return + * null. + * + * @param location location of file (local or remote) to be used to back the MFile object + * @return {@link thredds.inventory.MFile} + */ + @Nullable + public static MFile createIfExists(String location) { + if (location == null) { + return null; + } + + final MFile mFile = create(location); + return mFile.exists() ? mFile : null; + } } diff --git a/cdm/core/src/main/java/ucar/nc2/internal/ncml/AggDataset.java b/cdm/core/src/main/java/ucar/nc2/internal/ncml/AggDataset.java index cf540fbbc4..92557b33b5 100644 --- a/cdm/core/src/main/java/ucar/nc2/internal/ncml/AggDataset.java +++ b/cdm/core/src/main/java/ucar/nc2/internal/ncml/AggDataset.java @@ -72,7 +72,7 @@ protected AggDataset(MFile mfile, @Nullable Object spiObject, @Nullable Element protected AggDataset(String cacheLocation, String location, @Nullable String id, @Nullable EnumSet wantEnhance, @Nullable ucar.nc2.util.cache.FileFactory reader, @Nullable Object spiObject, @Nullable Element ncmlElem) { - this.mfile = location == null ? null : MFiles.create(location); + this.mfile = MFiles.createIfExists(location); this.cacheLocation = cacheLocation; this.id = id; this.enhance = (wantEnhance == null) ? NetcdfDataset.getEnhanceNone() : wantEnhance;