Skip to content

Commit d536174

Browse files
Eliminated the global resource loader instance
1 parent 129d4b3 commit d536174

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

ng-appserver/src/main/java/ng/appserver/NGResourceManager.java

+22-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import org.slf4j.LoggerFactory;
1111

1212
import ng.appserver.privates.NGResourceLoader;
13+
import ng.appserver.privates.NGResourceLoader.JavaClasspathResourceSource;
14+
import ng.appserver.privates.NGResourceLoader.StandardNamespace;
15+
import ng.appserver.privates.StandardResourceType;
1316

1417
/**
1518
* Experimental implementation of the resource manager.
@@ -20,6 +23,11 @@ public class NGResourceManager {
2023

2124
private static final Logger logger = LoggerFactory.getLogger( NGResourceManager.class );
2225

26+
/**
27+
* The resource loader that handles locating and loading of resources to be managed by this resource manager
28+
*/
29+
private NGResourceLoader _resourceLoader;
30+
2331
// FIXME: The eventual type of cache we're going to have? (including namespaces though) // Hugi 2024-02-24
2432
// private final Map<ResourceType, Map<String, Optional<byte[]>>> resourceCache = new ConcurrentHashMap<>();
2533

@@ -40,31 +48,37 @@ private static boolean _cachingEnabled() {
4048

4149
/**
4250
* @return The resource loader used by this manager
43-
*
44-
* FIXME: We're currently using a global instance, will eventually be replaced by a local instance // Hugi 2024-05-25
4551
*/
46-
private NGResourceLoader loader() {
47-
return NGResourceLoader.instance();
52+
private NGResourceLoader resourceLoader() {
53+
if( _resourceLoader == null ) {
54+
_resourceLoader = new NGResourceLoader();
55+
_resourceLoader.addResourceSource( StandardNamespace.App.identifier(), StandardResourceType.App, new JavaClasspathResourceSource( "app-resources" ) );
56+
_resourceLoader.addResourceSource( StandardNamespace.App.identifier(), StandardResourceType.WebServer, new JavaClasspathResourceSource( "webserver-resources" ) );
57+
_resourceLoader.addResourceSource( StandardNamespace.App.identifier(), StandardResourceType.Public, new JavaClasspathResourceSource( "public" ) );
58+
_resourceLoader.addResourceSource( StandardNamespace.App.identifier(), StandardResourceType.ComponentTemplate, new JavaClasspathResourceSource( "components" ) );
59+
}
60+
61+
return _resourceLoader;
4862
}
4963

5064
public Optional<byte[]> bytesForAppResourceNamed( final String resourceName ) {
5165
Objects.requireNonNull( resourceName );
52-
return bytesForAnyResource( resourceName, _appResourceCache, loader()::bytesForAppResource );
66+
return bytesForAnyResource( resourceName, _appResourceCache, resourceLoader()::bytesForAppResource );
5367
}
5468

5569
public Optional<byte[]> bytesForWebserverResourceNamed( final String resourceName ) {
5670
Objects.requireNonNull( resourceName );
57-
return bytesForAnyResource( resourceName, _webserverResourceCache, loader()::bytesForWebserverResource );
71+
return bytesForAnyResource( resourceName, _webserverResourceCache, resourceLoader()::bytesForWebserverResource );
5872
}
5973

6074
public Optional<byte[]> bytesForComponentResourceNamed( final String resourceName ) {
6175
Objects.requireNonNull( resourceName );
62-
return bytesForAnyResource( resourceName, _componentResourceCache, loader()::bytesForComponentResource );
76+
return bytesForAnyResource( resourceName, _componentResourceCache, resourceLoader()::bytesForComponentResource );
6377
}
6478

6579
public Optional<byte[]> bytesForPublicResourceNamed( final String resourceName ) {
6680
Objects.requireNonNull( resourceName );
67-
return bytesForAnyResource( resourceName, _publicResourceCache, loader()::bytesForPublicResource );
81+
return bytesForAnyResource( resourceName, _publicResourceCache, resourceLoader()::bytesForPublicResource );
6882
}
6983

7084
private static Optional<byte[]> bytesForAnyResource( final String resourceName, final Map<String, Optional<byte[]>> cacheMap, Function<String, Optional<byte[]>> readFunction ) {

ng-appserver/src/main/java/ng/appserver/privates/NGResourceLoader.java

+3-21
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,13 @@
1818
import org.slf4j.LoggerFactory;
1919

2020
/**
21-
* Utilities for reading resources
21+
* Utility class for reading resources
2222
*
23-
* FIXME: Consumers should really never be going through this class directly. Resource providers should be registered with NGResourceManager and resources then loaded from there // Hugi 2023-07-08
23+
* Consumers rarely use this class directly and should usually go through NGResourceManager instead, which handles caching and other general resource management
2424
*/
2525

2626
public class NGResourceLoader {
2727

28-
/**
29-
* FIXME: This instance is created merely as a placeholder while we work on changing NGResourceLoader's static nature // Hugi 2024-0525
30-
*/
31-
@Deprecated
32-
private static final NGResourceLoader _instance = new NGResourceLoader();
33-
34-
@Deprecated
35-
public static NGResourceLoader instance() {
36-
return _instance;
37-
}
38-
3928
public enum StandardNamespace {
4029
App( "app" );
4130

@@ -52,14 +41,7 @@ public String identifier() {
5241

5342
private Map<ResourceType, List<ResourceSource>> _resourceSources = new ConcurrentHashMap<>();
5443

55-
static {
56-
_instance.addResourceSource( StandardNamespace.App.identifier(), StandardResourceType.App, new JavaClasspathResourceSource( "app-resources" ) );
57-
_instance.addResourceSource( StandardNamespace.App.identifier(), StandardResourceType.WebServer, new JavaClasspathResourceSource( "webserver-resources" ) );
58-
_instance.addResourceSource( StandardNamespace.App.identifier(), StandardResourceType.Public, new JavaClasspathResourceSource( "public" ) );
59-
_instance.addResourceSource( StandardNamespace.App.identifier(), StandardResourceType.ComponentTemplate, new JavaClasspathResourceSource( "components" ) );
60-
}
61-
62-
private void addResourceSource( final String namespace, final ResourceType type, final ResourceSource source ) {
44+
public void addResourceSource( final String namespace, final ResourceType type, final ResourceSource source ) {
6345
List<ResourceSource> sources = _resourceSources.get( type );
6446

6547
if( sources == null ) {

0 commit comments

Comments
 (0)