Skip to content

Commit

Permalink
Add MFile::createIfExists to fix AggDataset relying on null MFile for…
Browse files Browse the repository at this point in the history
… nonexisting file
  • Loading branch information
tdrwenski committed Apr 26, 2023
1 parent bde5f82 commit b1241b1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions cdm/core/src/main/java/thredds/inventory/MFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected AggDataset(MFile mfile, @Nullable Object spiObject, @Nullable Element
protected AggDataset(String cacheLocation, String location, @Nullable String id,
@Nullable EnumSet<Enhance> 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;
Expand Down

0 comments on commit b1241b1

Please sign in to comment.