Skip to content

Commit

Permalink
Namespace support in NGResourceLoader, skeleton for usage in the manager
Browse files Browse the repository at this point in the history
  • Loading branch information
hugithordarson committed Jun 14, 2024
1 parent 765a4a3 commit 5d2fbe7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,31 @@ public String identifier() {
}
}

private Map<ResourceType, List<ResourceSource>> _resourceSources = new ConcurrentHashMap<>();
private Map<String, Map<ResourceType, List<ResourceSource>>> _allResourceSources = new ConcurrentHashMap<>();

public void addResourceSource( final String namespace, final ResourceType type, final ResourceSource source ) {
List<ResourceSource> sources = _resourceSources.get( type );

if( sources == null ) {
sources = new ArrayList<>();
_resourceSources.put( type, sources );
}

sources.add( source );
_resourceSources.put( type, sources );
_allResourceSources
.computeIfAbsent( namespace, _unused -> new ConcurrentHashMap<>() )
.computeIfAbsent( type, _unused -> new ArrayList<>() )
.add( source );
}

/**
* @return The named resource if it exists, an empty optional if not found
*/
private Optional<byte[]> readResource( final ResourceType type, String resourcePath ) {
Objects.requireNonNull( type );
private Optional<byte[]> readResource( final String namespace, final ResourceType resourceType, String resourcePath ) {
Objects.requireNonNull( resourceType );
Objects.requireNonNull( resourcePath );

final List<ResourceSource> list = _resourceSources.get( type );
final Map<ResourceType, List<ResourceSource>> sourceMapForNamespace = _allResourceSources.get( namespace );

if( sourceMapForNamespace == null ) {
return Optional.empty();
}

final List<ResourceSource> sourceListForType = sourceMapForNamespace.get( resourceType );

if( list == null ) {
if( sourceListForType == null ) {
return Optional.empty();
}

Expand All @@ -73,7 +74,7 @@ private Optional<byte[]> readResource( final ResourceType type, String resourceP
resourcePath = resourcePath.substring( 1 );
}

for( ResourceSource source : list ) {
for( ResourceSource source : sourceListForType ) {
final Optional<byte[]> result = source.bytesForResourceWithPath( resourcePath );

// FIXME: We should (optionally?) allow iterating through all registered sources to check for duplicates // Hugi 2024-06-14
Expand All @@ -88,8 +89,8 @@ private Optional<byte[]> readResource( final ResourceType type, String resourceP
/**
* @return The named resource if it exists, an empty optional if not found
*/
public Optional<byte[]> bytesForResource( final ResourceType type, String resourcePath ) {
return readResource( type, resourcePath );
public Optional<byte[]> bytesForResource( final String namespace, final ResourceType type, String resourcePath ) {
return readResource( namespace, type, resourcePath );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,22 @@ private NGResourceLoader resourceLoader() {
}

public Optional<byte[]> bytesForAppResourceNamed( final String resourceName ) {
return bytesForAnyResource( resourceName, StandardResourceType.App );
return bytesForAnyResource( "app", resourceName, StandardResourceType.App );
}

public Optional<byte[]> bytesForWebserverResourceNamed( final String resourceName ) {
return bytesForAnyResource( resourceName, StandardResourceType.WebServer );
return bytesForAnyResource( "app", resourceName, StandardResourceType.WebServer );
}

public Optional<byte[]> bytesForComponentResourceNamed( final String resourceName ) {
return bytesForAnyResource( resourceName, StandardResourceType.ComponentTemplate );
return bytesForAnyResource( "app", resourceName, StandardResourceType.ComponentTemplate );
}

public Optional<byte[]> bytesForPublicResourceNamed( final String resourceName ) {
return bytesForAnyResource( resourceName, StandardResourceType.Public );
return bytesForAnyResource( "app", resourceName, StandardResourceType.Public );
}

private Optional<byte[]> bytesForAnyResource( final String resourceName, ResourceType resourceType ) {
private Optional<byte[]> bytesForAnyResource( final String namespace, final String resourceName, ResourceType resourceType ) {
Objects.requireNonNull( resourceName );
Objects.requireNonNull( resourceType );

Expand All @@ -84,12 +84,12 @@ private Optional<byte[]> bytesForAnyResource( final String resourceName, Resourc

// FIXME: Applies to both non-existing and un-cached resources. Add an "I already checked this, it doesn't exist" resource cache entry
if( resource == null ) {
resource = resourceLoader().bytesForResource( resourceType, resourceName );
resource = resourceLoader().bytesForResource( namespace, resourceType, resourceName );
cacheMap.put( resourceName, resource );
}
}
else {
resource = resourceLoader().bytesForResource( resourceType, resourceName );
resource = resourceLoader().bytesForResource( namespace, resourceType, resourceName );
}

return resource;
Expand Down

0 comments on commit 5d2fbe7

Please sign in to comment.