Skip to content

Commit 5b995d5

Browse files
Moved suppling of pages/elements from NGApplication to NGElementManager
1 parent 62e6574 commit 5b995d5

File tree

5 files changed

+139
-107
lines changed

5 files changed

+139
-107
lines changed

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

Lines changed: 10 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44
import java.io.FileNotFoundException;
55
import java.io.FileOutputStream;
66
import java.io.PrintStream;
7-
import java.lang.reflect.Constructor;
87
import java.lang.reflect.InvocationTargetException;
98
import java.time.LocalDateTime;
109
import java.util.ArrayList;
1110
import java.util.List;
12-
import java.util.Map;
13-
import java.util.Objects;
1411
import java.util.Optional;
1512
import java.util.ServiceLoader;
1613
import java.util.ServiceLoader.Provider;
@@ -33,7 +30,6 @@
3330
import ng.appserver.resources.StandardResourceType;
3431
import ng.appserver.routing.NGRouteTable;
3532
import ng.appserver.templating.NGElementManager;
36-
import ng.appserver.templating.NGElementNotFoundException;
3733
import ng.appserver.wointegration.NGDefaultLifeBeatThread;
3834
import ng.appserver.wointegration.WOMPRequestHandler;
3935
import ng.classes.NGClassManager;
@@ -356,6 +352,10 @@ public NGResourceManager resourceManager() {
356352
return _resourceManager;
357353
}
358354

355+
public NGElementManager elementManager() {
356+
return _elementManager;
357+
}
358+
359359
public NGResourceManagerDynamic resourceManagerDynamic() {
360360
return _resourceManagerDynamic;
361361
}
@@ -648,112 +648,19 @@ public void terminate() {
648648

649649
/**
650650
* @return The named component, where [componentName] can be either the component's simple class name or full class name.
651+
*
652+
* CHECKME: The implementation of this method is in elementManager, we're currently nly keeping this method around for API compatibility with older projects.
651653
*/
652654
public NGComponent pageWithName( final String componentName, final NGContext context ) {
653-
Objects.requireNonNull( componentName, "'componentName' must not be null. I can't create components from nothing." );
654-
Objects.requireNonNull( context, "'context' must not be null. What's life without context?" );
655-
656-
final NGComponentDefinition definition = _componentDefinition( componentName );
657-
return pageWithName( definition, context );
655+
return elementManager().pageWithName( componentName, context );
658656
}
659657

660658
/**
661659
* @return A new instance of [componentClass] in the given [context]
662-
*/
663-
@SuppressWarnings("unchecked") // Our cast to the component class is fine
664-
public <E extends NGComponent> E pageWithName( final Class<E> componentClass, final NGContext context ) {
665-
Objects.requireNonNull( componentClass, "'componentClass' must not be null. I can't create components from nothing." );
666-
Objects.requireNonNull( context, "'context' must not be null. What's life without context?" );
667-
668-
final NGComponentDefinition definition = _componentDefinition( componentClass );
669-
return (E)pageWithName( definition, context );
670-
}
671-
672-
/**
673-
* @return A new instance of [componentDefinition] in the given [context]
674-
*/
675-
private NGComponent pageWithName( final NGComponentDefinition componentDefinition, final NGContext context ) {
676-
Objects.requireNonNull( componentDefinition );
677-
Objects.requireNonNull( context );
678-
679-
return componentDefinition.componentInstanceInContext( context );
680-
}
681-
682-
/**
683-
* @return The componentDefinition corresponding to the given NGComponent class.
684-
*
685-
* FIXME: This should not be static // Hugi 2023-04-14
686-
*/
687-
private static NGComponentDefinition _componentDefinition( final Class<? extends NGComponent> componentClass ) {
688-
Objects.requireNonNull( componentClass );
689-
return NGComponentDefinition.get( componentClass );
690-
}
691-
692-
/**
693-
* @return The componentDefinition corresponding to the named NGComponent
694-
*
695-
* FIXME: This should not be static // Hugi 2023-04-14
696-
*/
697-
public static NGComponentDefinition _componentDefinition( final String componentName ) {
698-
Objects.requireNonNull( componentName );
699-
return NGComponentDefinition.get( componentName );
700-
}
701-
702-
/**
703-
* FIXME: This should not be static // Hugi 2023-04-14
704-
*
705-
* @param name The name identifying what element we're getting
706-
* @param associations Associations used to bind the generated element to it's parent
707-
* @param contentTemplate The content wrapped by the element (if a container element)
708660
*
709-
* @return An instance of the named dynamic element. This can be a classless component (in which case it's the template name), a simple class name or a full class name
661+
* CHECKME: The implementation of this method is in elementManager, we're currently nly keeping this method around for API compatibility with older projects.
710662
*/
711-
public static NGElement dynamicElementWithName( final String elementIdentifier, final Map<String, NGAssociation> associations, final NGElement contentTemplate ) {
712-
Objects.requireNonNull( elementIdentifier );
713-
Objects.requireNonNull( associations );
714-
715-
// First we're going to check if we have a tag alias present.
716-
final String elementName = NGElementUtils.tagShortcutMap().getOrDefault( elementIdentifier, elementIdentifier );
717-
718-
// Check if we can find a class representing the element we're going to render.
719-
final Class<? extends NGElement> elementClass = NGElementUtils.classWithNameNullIfNotFound( elementName );
720-
721-
// If we don't find a class for the element, we're going to try going down the route of a classless component.
722-
if( elementClass == null ) {
723-
final NGComponentDefinition componentDefinition = _componentDefinition( elementName );
724-
return componentDefinition.componentReferenceWithAssociations( associations, contentTemplate );
725-
}
726-
727-
// First we check if this is a dynamic element
728-
if( NGDynamicElement.class.isAssignableFrom( elementClass ) ) {
729-
return createDynamicElementInstance( elementClass, elementName, associations, contentTemplate );
730-
}
731-
732-
// If it's not an element, let's move on to creating a component reference instead
733-
if( NGComponent.class.isAssignableFrom( elementClass ) ) {
734-
final NGComponentDefinition componentDefinition = _componentDefinition( (Class<? extends NGComponent>)elementClass );
735-
return componentDefinition.componentReferenceWithAssociations( associations, contentTemplate );
736-
}
737-
738-
// We should never end up here unless we got an incorrect/non-existent element name
739-
throw new NGElementNotFoundException( "I could not construct a dynamic element named '%s'".formatted( elementName ), elementName );
740-
}
741-
742-
/**
743-
* @return A new NGDynamicElement constructed using the given parameters
744-
*
745-
* CHECKME: Not sure this is the final home for this functionality. It's just a method shortcut to invoking the dynamic element's constructor via reflection.
746-
*/
747-
private static <E extends NGElement> E createDynamicElementInstance( final Class<E> elementClass, final String name, final Map<String, NGAssociation> associations, final NGElement contentTemplate ) {
748-
final Class<?>[] parameterTypes = { String.class, Map.class, NGElement.class };
749-
final Object[] parameters = { name, associations, contentTemplate };
750-
751-
try {
752-
final Constructor<E> constructor = elementClass.getDeclaredConstructor( parameterTypes );
753-
return constructor.newInstance( parameters );
754-
}
755-
catch( NoSuchMethodException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e ) {
756-
throw new RuntimeException( e );
757-
}
663+
public <E extends NGComponent> E pageWithName( final Class<E> componentClass, final NGContext context ) {
664+
return elementManager().pageWithName( componentClass, context );
758665
}
759666
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private void beforeComponent( final NGContext context ) {
5757
// If no instance was obtained, we need to create the component instance and insert it into the parent's child map.
5858
if( newComponentInstance == null ) {
5959
// Load up our component's definition
60-
final NGComponentDefinition componentDefinition = NGApplication.application()._componentDefinition( _componentName );
60+
final NGComponentDefinition componentDefinition = NGApplication.application().elementManager()._componentDefinition( _componentName );
6161

6262
// ...and obtain an instance of the component
6363
newComponentInstance = componentDefinition.componentInstanceInContext( context );

ng-appserver/src/main/java/ng/appserver/elements/NGSwitchComponent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public NGElement _realComponentWithName( final String name, final String element
8383

8484
// No component instance found so we're going to have to construct a new one
8585
if( localWOElement == null ) {
86-
localWOElement = NGApplication.application().dynamicElementWithName( name, _componentAssociations, _contentTemplate );
86+
localWOElement = NGApplication.application().elementManager().dynamicElementWithName( name, _componentAssociations, _contentTemplate );
8787

8888
if( localWOElement == null ) {
8989
throw new RuntimeException( "<" + getClass().getName() + "> : cannot find component or dynamic element named " + name );
Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,128 @@
11
package ng.appserver.templating;
22

3-
public class NGElementManager {}
3+
import java.lang.reflect.Constructor;
4+
import java.lang.reflect.InvocationTargetException;
5+
import java.util.Map;
6+
import java.util.Objects;
7+
8+
import ng.appserver.NGAssociation;
9+
import ng.appserver.NGComponent;
10+
import ng.appserver.NGComponentDefinition;
11+
import ng.appserver.NGContext;
12+
import ng.appserver.NGDynamicElement;
13+
import ng.appserver.NGElement;
14+
import ng.appserver.NGElementUtils;
15+
16+
public class NGElementManager {
17+
18+
/**
19+
* @return The named component, where [componentName] can be either the component's simple class name or full class name.
20+
*/
21+
public NGComponent pageWithName( final String componentName, final NGContext context ) {
22+
Objects.requireNonNull( componentName, "'componentName' must not be null. I can't create components from nothing." );
23+
Objects.requireNonNull( context, "'context' must not be null. What's life without context?" );
24+
25+
final NGComponentDefinition definition = _componentDefinition( componentName );
26+
return pageWithName( definition, context );
27+
}
28+
29+
/**
30+
* @return A new instance of [componentClass] in the given [context]
31+
*/
32+
@SuppressWarnings("unchecked") // Our cast to the component class is fine
33+
public <E extends NGComponent> E pageWithName( final Class<E> componentClass, final NGContext context ) {
34+
Objects.requireNonNull( componentClass, "'componentClass' must not be null. I can't create components from nothing." );
35+
Objects.requireNonNull( context, "'context' must not be null. What's life without context?" );
36+
37+
final NGComponentDefinition definition = _componentDefinition( componentClass );
38+
return (E)pageWithName( definition, context );
39+
}
40+
41+
/**
42+
* @return A new instance of [componentDefinition] in the given [context]
43+
*/
44+
private NGComponent pageWithName( final NGComponentDefinition componentDefinition, final NGContext context ) {
45+
Objects.requireNonNull( componentDefinition );
46+
Objects.requireNonNull( context );
47+
48+
return componentDefinition.componentInstanceInContext( context );
49+
}
50+
51+
/**
52+
* @return The componentDefinition corresponding to the given NGComponent class.
53+
*
54+
* FIXME: This should not be static // Hugi 2023-04-14
55+
*/
56+
private static NGComponentDefinition _componentDefinition( final Class<? extends NGComponent> componentClass ) {
57+
Objects.requireNonNull( componentClass );
58+
return NGComponentDefinition.get( componentClass );
59+
}
60+
61+
/**
62+
* @return The componentDefinition corresponding to the named NGComponent
63+
*
64+
* FIXME: This should not be static // Hugi 2023-04-14
65+
*/
66+
public static NGComponentDefinition _componentDefinition( final String componentName ) {
67+
Objects.requireNonNull( componentName );
68+
return NGComponentDefinition.get( componentName );
69+
}
70+
71+
/**
72+
* FIXME: This should not be static // Hugi 2023-04-14
73+
*
74+
* @param name The name identifying what element we're getting
75+
* @param associations Associations used to bind the generated element to it's parent
76+
* @param contentTemplate The content wrapped by the element (if a container element)
77+
*
78+
* @return An instance of the named dynamic element. This can be a classless component (in which case it's the template name), a simple class name or a full class name
79+
*/
80+
public static NGElement dynamicElementWithName( final String elementIdentifier, final Map<String, NGAssociation> associations, final NGElement contentTemplate ) {
81+
Objects.requireNonNull( elementIdentifier );
82+
Objects.requireNonNull( associations );
83+
84+
// First we're going to check if we have a tag alias present.
85+
final String elementName = NGElementUtils.tagShortcutMap().getOrDefault( elementIdentifier, elementIdentifier );
86+
87+
// Check if we can find a class representing the element we're going to render.
88+
final Class<? extends NGElement> elementClass = NGElementUtils.classWithNameNullIfNotFound( elementName );
89+
90+
// If we don't find a class for the element, we're going to try going down the route of a classless component.
91+
if( elementClass == null ) {
92+
final NGComponentDefinition componentDefinition = _componentDefinition( elementName );
93+
return componentDefinition.componentReferenceWithAssociations( associations, contentTemplate );
94+
}
95+
96+
// First we check if this is a dynamic element
97+
if( NGDynamicElement.class.isAssignableFrom( elementClass ) ) {
98+
return createDynamicElementInstance( elementClass, elementName, associations, contentTemplate );
99+
}
100+
101+
// If it's not an element, let's move on to creating a component reference instead
102+
if( NGComponent.class.isAssignableFrom( elementClass ) ) {
103+
final NGComponentDefinition componentDefinition = _componentDefinition( (Class<? extends NGComponent>)elementClass );
104+
return componentDefinition.componentReferenceWithAssociations( associations, contentTemplate );
105+
}
106+
107+
// We should never end up here unless we got an incorrect/non-existent element name
108+
throw new NGElementNotFoundException( "I could not construct a dynamic element named '%s'".formatted( elementName ), elementName );
109+
}
110+
111+
/**
112+
* @return A new NGDynamicElement constructed using the given parameters
113+
*
114+
* Really just a shortcut for invoking a dynamic element class' constructor via reflection.
115+
*/
116+
private static <E extends NGElement> E createDynamicElementInstance( final Class<E> elementClass, final String name, final Map<String, NGAssociation> associations, final NGElement contentTemplate ) {
117+
final Class<?>[] parameterTypes = { String.class, Map.class, NGElement.class };
118+
final Object[] parameters = { name, associations, contentTemplate };
119+
120+
try {
121+
final Constructor<E> constructor = elementClass.getDeclaredConstructor( parameterTypes );
122+
return constructor.newInstance( parameters );
123+
}
124+
catch( NoSuchMethodException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e ) {
125+
throw new RuntimeException( e );
126+
}
127+
}
128+
}

ng-appserver/src/main/java/ng/appserver/templating/NGTemplateParserProxy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private static NGElement toDynamicElement( final PBasicNode node ) {
7373
final NGElement childTemplate = toTemplate( node.children() );
7474

7575
try {
76-
return NGApplication.dynamicElementWithName( type, associations, childTemplate );
76+
return NGApplication.application().elementManager().dynamicElementWithName( type, associations, childTemplate );
7777
}
7878
catch( NGElementNotFoundException e ) {
7979
// FIXME: Experimental functionality, probably doesn't belong with the parser part of the framework.

0 commit comments

Comments
 (0)