Skip to content

Commit

Permalink
Make NGResourceManager provide NGResources instead of byte arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
hugithordarson committed Jun 26, 2024
1 parent 3db6cbc commit 62340ed
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 37 deletions.
8 changes: 5 additions & 3 deletions ng-appserver/src/main/java/ng/appserver/NGApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import ng.appserver.NGProperties.PropertiesSourceArguments;
import ng.appserver.NGProperties.PropertiesSourceResource;
import ng.appserver.directactions.NGDirectActionRequestHandler;
import ng.appserver.resources.NGResource;
import ng.appserver.resources.NGResourceManager;
import ng.appserver.resources.NGResourceManagerDynamic;
import ng.appserver.resources.StandardNamespace;
Expand Down Expand Up @@ -442,16 +443,17 @@ private NGResponse noHandlerResponse( final NGRequest request ) {
return new NGResponse( "No resource name specified", 400 );
}

final Optional<byte[]> resourceBytes = resourceManager().bytesForPublicResourceNamed( resourcePath );
// FIXME: We've not decided what to do about namespacing and public resources // Hugi 2024-06-26
final Optional<NGResource> resource = resourceManager().obtainPublicResource( resourcePath );

// FIXME: Shouldn't we allow the user to customize the response for a non-existent resource? // Hugi 2024-02-05
if( resourceBytes.isEmpty() ) {
if( resource.isEmpty() ) {
final NGResponse errorResponse = new NGResponse( "public resource '" + resourcePath + "' does not exist", 404 );
errorResponse.setHeader( "content-type", "text/html" );
return errorResponse;
}

return NGResourceRequestHandler.responseForResource( resourceBytes, resourcePath );
return NGResourceRequestHandler.responseForResource( Optional.of( resource.get().bytes() ), resourcePath );
}

/**
Expand Down
7 changes: 4 additions & 3 deletions ng-appserver/src/main/java/ng/appserver/NGProperties.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ng.appserver;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
Expand All @@ -16,6 +15,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ng.appserver.resources.NGResource;

/**
* Handles properties loading
*
Expand Down Expand Up @@ -115,7 +116,7 @@ public String resourcePath() {

@Override
public Map<String, String> readAll() {
final Optional<byte[]> propertyBytes = NGApplication.application().resourceManager().bytesForAppResourceNamed( _namespace, _resourcePath );
final Optional<NGResource> propertyBytes = NGApplication.application().resourceManager().obtainAppResource( _namespace, _resourcePath );

if( !propertyBytes.isPresent() ) {
logger.warn( "No default properties file found {}::{}", _namespace, _resourcePath );
Expand All @@ -124,7 +125,7 @@ public Map<String, String> readAll() {

try {
final Properties p = new Properties();
p.load( new ByteArrayInputStream( propertyBytes.get() ) );
p.load( propertyBytes.get().inputStream() ); // FIXME: WE should probably close this stream. Look into once resource loading is done // Hugi 2024-06-25
return (Map)p;
}
catch( IOException e ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public NGResponse handleRequest( final NGRequest request ) {
return responseForResource( resourceBytes, resourcePath );
}

// FIXME: Replace (or at least accompany) with an NGResource-invoking method // Hugi 2024-06-26
@Deprecated
public static NGResponse responseForResource( Optional<byte[]> resourceBytes, final String resourcePath ) {

// FIXME: Shouldn't we allow the user to customize the response for a non-existent resource? // Hugi 2021-12-06
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,6 @@ public void addResourceSource( final String namespace, final ResourceType resour
.add( resourceSource );
}

/**
* @return The named resource if it exists, an empty optional if not found
*/
@Deprecated
public Optional<byte[]> bytesForResource( final String namespace, final ResourceType resourceType, String resourcePath ) {
Optional<NGResource> resource = obtainResource( namespace, resourceType, resourcePath );

if( !resource.isPresent() ) {
return Optional.empty();
}

return Optional.of( resource.get().bytes() );
}

/**
* @return The named resource if it exists, an empty optional if not found
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class NGResourceManager {
/**
* Cache storing resources in-memory by namespace -> resource type -> resource path
*/
private final Map<String, Map<ResourceType, Map<String, Optional<byte[]>>>> resourceCache = new ConcurrentHashMap<>();
private final Map<String, Map<ResourceType, Map<String, Optional<NGResource>>>> resourceCache = new ConcurrentHashMap<>();

/**
* Specifies if we want to use the resources cache.
Expand Down Expand Up @@ -63,8 +63,8 @@ private NGResourceLoader resourceLoader() {
/**
* @return The specified app resource
*/
public Optional<byte[]> bytesForAppResourceNamed( final String namespace, final String resourcePath ) {
return bytesForResource( namespace, StandardResourceType.App, resourcePath );
public Optional<NGResource> obtainAppResource( final String namespace, final String resourcePath ) {
return obtainResource( namespace, StandardResourceType.App, resourcePath );
}

/**
Expand All @@ -78,8 +78,8 @@ public Optional<byte[]> bytesForAppResourceNamed( final String resourcePath ) {
/**
* @return The specified webserver resource
*/
public Optional<byte[]> bytesForWebserverResourceNamed( final String namespace, final String resourcePath ) {
return bytesForResource( namespace, StandardResourceType.WebServer, resourcePath );
public Optional<NGResource> obtainWebserverResource( final String namespace, final String resourcePath ) {
return obtainResource( namespace, StandardResourceType.WebServer, resourcePath );
}

/**
Expand All @@ -93,8 +93,8 @@ public Optional<byte[]> bytesForWebserverResourceNamed( final String resourcePat
/**
* @return The specified component template resource
*/
public Optional<byte[]> bytesForComponentTemplateResourceNamed( final String namespace, final String resourcePath ) {
return bytesForResource( namespace, StandardResourceType.ComponentTemplate, resourcePath );
public Optional<NGResource> obtainComponentTemplateResource( final String namespace, final String resourcePath ) {
return obtainResource( namespace, StandardResourceType.ComponentTemplate, resourcePath );
}

/**
Expand All @@ -108,22 +108,22 @@ public Optional<byte[]> bytesForComponentTemplateResourceNamed( final String res
/**
* @return The specified public resource
*/
public Optional<byte[]> bytesForPublicResourceNamed( final String namespace, final String resourcePath ) {
return bytesForResource( namespace, StandardResourceType.Public, resourcePath );
public Optional<NGResource> obtainPublicResource( final String namespace, final String resourcePath ) {
return obtainResource( namespace, StandardResourceType.Public, resourcePath );
}

/**
* @return The specified public resource resource by searching in all namespaces
*/
@Deprecated
public Optional<byte[]> bytesForPublicResourceNamed( final String resourcePath ) {
return bytesForResourceSearchingAllNamespaces( StandardResourceType.Public, resourcePath );
public Optional<NGResource> obtainPublicResource( final String resourcePath ) {
return obtainResourceSearchingAllNamespaces( StandardResourceType.Public, resourcePath );
}

/**
* @return The bytes for the named resource, looking for a cached copy first if caching is enabled (i.e. if we're in production mode)
*/
private Optional<byte[]> bytesForResource( final String namespace, final ResourceType resourceType, final String resourcePath ) {
private Optional<NGResource> obtainResource( final String namespace, final ResourceType resourceType, final String resourcePath ) {
Objects.requireNonNull( namespace );
Objects.requireNonNull( resourcePath );
Objects.requireNonNull( resourceType );
Expand All @@ -134,22 +134,36 @@ private Optional<byte[]> bytesForResource( final String namespace, final Resourc
return resourceCache
.computeIfAbsent( namespace, _unused -> new ConcurrentHashMap<>() )
.computeIfAbsent( resourceType, _unused -> new ConcurrentHashMap<>() )
.computeIfAbsent( resourcePath, _unused -> resourceLoader().bytesForResource( namespace, resourceType, resourcePath ) );
.computeIfAbsent( resourcePath, _unused -> resourceLoader().obtainResource( namespace, resourceType, resourcePath ) );
}

return resourceLoader().bytesForResource( namespace, resourceType, resourcePath );
return resourceLoader().obtainResource( namespace, resourceType, resourcePath );
}

/**
* @return bytes for the specified resource by searching all namespaces
*/
@Deprecated
private Optional<byte[]> bytesForResourceSearchingAllNamespaces( final ResourceType resourceType, final String resourcePath ) {
final Optional<NGResource> resource = obtainResourceSearchingAllNamespaces( resourceType, resourcePath );

if( !resource.isEmpty() ) {
return Optional.of( resource.get().bytes() );
}

return Optional.empty();
}

/**
* @return the specified resource by searching all namespaces
*/
@Deprecated
private Optional<NGResource> obtainResourceSearchingAllNamespaces( final ResourceType resourceType, final String resourcePath ) {
for( String namespace : resourceLoader().namespaces() ) {
final Optional<byte[]> resourceBytes = bytesForResource( namespace, resourceType, resourcePath );
final Optional<NGResource> resource = obtainResource( namespace, resourceType, resourcePath );

if( !resourceBytes.isEmpty() ) {
return resourceBytes;
if( !resource.isEmpty() ) {
return resource;
}
}

Expand Down

0 comments on commit 62340ed

Please sign in to comment.