Skip to content

Commit

Permalink
added native image configuration and new method for registering core …
Browse files Browse the repository at this point in the history
…classes in ParaObjectUtils
  • Loading branch information
albogdano committed May 21, 2024
1 parent 60c560e commit 52192d5
Show file tree
Hide file tree
Showing 13 changed files with 2,518 additions and 72 deletions.
4 changes: 2 additions & 2 deletions para-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4jVer}</version>
</dependency>
<dependency>
<!-- <dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logbackVer}</version>
Expand All @@ -137,7 +137,7 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.4.14</version>
</dependency>
</dependency>-->

<!-- METRICS -->
<dependency>
Expand Down
9 changes: 9 additions & 0 deletions para-core/src/main/java/com/erudika/para/core/utils/Para.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.erudika.para.core.utils;

import com.erudika.para.core.App;
import com.erudika.para.core.ParaObject;
import com.erudika.para.core.cache.Cache;
import com.erudika.para.core.listeners.DestroyListener;
import com.erudika.para.core.listeners.IOListener;
Expand Down Expand Up @@ -377,6 +378,14 @@ public static ClassLoader getParaClassLoader() {
return paraClassLoader;
}

/**
* Explicitly registers core classes for reflection.
* @param classes a list of classes
*/
public static void registerCoreClasses(Class<? extends ParaObject>... classes) {
ParaObjectUtils.registerCoreClasses(classes);
}

/**
* Creates the root application and returns the credentials for it.
* @return credentials for the root app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@
*/
package com.erudika.para.core.utils;

import com.erudika.para.core.annotations.Stored;
import com.erudika.para.core.Address;
import com.erudika.para.core.App;
import com.erudika.para.core.Linker;
import com.erudika.para.core.ParaObject;
import com.erudika.para.core.Sysprop;
import com.erudika.para.core.Tag;
import com.erudika.para.core.Translation;
import com.erudika.para.core.User;
import com.erudika.para.core.Vote;
import com.erudika.para.core.Webhook;
import com.erudika.para.core.annotations.Stored;
import static com.erudika.para.core.utils.Utils.getAllDeclaredFields;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
Expand All @@ -42,6 +49,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.bidimap.DualHashBidiMap;
Expand All @@ -58,8 +66,6 @@ public final class ParaObjectUtils {

private static final Logger logger = LoggerFactory.getLogger(ParaObjectUtils.class);
// maps plural to singular type definitions
private static final Map<String, String> CORE_TYPES = new DualHashBidiMap();
private static final Map<String, String> CORE_PARA_TYPES = new DualHashBidiMap();
// maps lowercase simple names to class objects
private static final Map<String, Class<? extends ParaObject>> CORE_CLASSES = new DualHashBidiMap();
private static final Map<String, Class<? extends ParaObject>> CORE_PARA_CLASSES = new DualHashBidiMap();
Expand All @@ -73,6 +79,16 @@ public final class ParaObjectUtils {
JSON_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
JSON_MAPPER.registerModule(new JavaTimeModule());
JSON_MAPPER.findAndRegisterModules();

CORE_PARA_CLASSES.put(Address.class.getSimpleName().toLowerCase(), Address.class);
CORE_PARA_CLASSES.put(App.class.getSimpleName().toLowerCase(), App.class);
CORE_PARA_CLASSES.put(Linker.class.getSimpleName().toLowerCase(), Linker.class);
CORE_PARA_CLASSES.put(Sysprop.class.getSimpleName().toLowerCase(), Sysprop.class);
CORE_PARA_CLASSES.put(Tag.class.getSimpleName().toLowerCase(), Tag.class);
CORE_PARA_CLASSES.put(Translation.class.getSimpleName().toLowerCase(), Translation.class);
CORE_PARA_CLASSES.put(User.class.getSimpleName().toLowerCase(), User.class);
CORE_PARA_CLASSES.put(Vote.class.getSimpleName().toLowerCase(), Vote.class);
CORE_PARA_CLASSES.put(Webhook.class.getSimpleName().toLowerCase(), Webhook.class);
}

private ParaObjectUtils() { }
Expand Down Expand Up @@ -123,36 +139,17 @@ public static ObjectWriter getJsonWriterNoIdent() {
* @return a map of type plural - type singular form
*/
public static Map<String, String> getCoreTypes() {
if (CORE_TYPES.isEmpty()) {
try {
for (Class<? extends ParaObject> clazz : getCoreClassesMap().values()) {
ParaObject p = clazz.getConstructor().newInstance();
CORE_TYPES.put(p.getPlural(), p.getType());
}
} catch (Exception ex) {
logger.error(null, ex);
}
}
return Collections.unmodifiableMap(CORE_TYPES);
return getCoreClassesMap().values().stream().
collect(Collectors.toMap(k -> Utils.singularToPlural(Utils.type(k)), v -> Utils.type(v)));
}

/**
* Returns a map of the core data types declared in Para Core only.
* @return a map of type plural - type singular form
*/
public static Map<String, String> getCoreParaTypes() {
if (CORE_PARA_TYPES.isEmpty()) {
try {
getCoreClassesMap();
for (Class<? extends ParaObject> clazz : CORE_PARA_CLASSES.values()) {
ParaObject p = clazz.getConstructor().newInstance();
CORE_PARA_TYPES.put(p.getPlural(), p.getType());
}
} catch (Exception ex) {
logger.error(null, ex);
}
}
return Collections.unmodifiableMap(CORE_PARA_TYPES);
return CORE_PARA_CLASSES.values().stream().
collect(Collectors.toMap(k -> Utils.singularToPlural(Utils.type(k)), v -> Utils.type(v)));
}

/**
Expand Down Expand Up @@ -500,17 +497,6 @@ public static Class<? extends ParaObject> toClass(String type, Class<? extends P
public static Map<String, Class<? extends ParaObject>> getCoreClassesMap() {
if (CORE_CLASSES.isEmpty()) {
try {
String corePackage = ParaObject.class.getPackage().getName();
ClassGraph cg1 = new ClassGraph().enableClassInfo().acceptPackages(corePackage);
try (ScanResult scanResult = cg1.scan()) {
ClassInfoList classes = scanResult.getClassesImplementing(ParaObject.class.getName()).
filter(ci -> !ci.isInterface() && !ci.isAbstract());
for (io.github.classgraph.ClassInfo clazz : classes) {
CORE_PARA_CLASSES.put(clazz.getSimpleName().toLowerCase(),
(Class<? extends ParaObject>) clazz.loadClass(true));
}
}

CORE_CLASSES.putAll(CORE_PARA_CLASSES);

if (!Para.getConfig().corePackageName().isEmpty()) {
Expand All @@ -532,6 +518,19 @@ public static Map<String, Class<? extends ParaObject>> getCoreClassesMap() {
return Collections.unmodifiableMap(CORE_CLASSES);
}

/**
* Explicitly registers core classes for reflection.
* @param classes a list of classes
*/
public static void registerCoreClasses(Class<? extends ParaObject>... classes) {
if (classes != null && classes.length > 0) {
getCoreClassesMap();
for (Class<? extends ParaObject> clazz : classes) {
CORE_CLASSES.putIfAbsent(clazz.getSimpleName().toLowerCase(), clazz);
}
}
}

/**
* Converts a JSON string to a domain object. If we can't match the JSON to a core object, we fall back to
* {@link com.erudika.para.core.Sysprop}.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"name":"com.sun.management.VMOption$Origin",
"fields":[{"name":"ATTACH_ON_DEMAND"}, {"name":"CONFIG_FILE"}, {"name":"DEFAULT"}, {"name":"ENVIRON_VAR"}, {"name":"ERGONOMIC"}, {"name":"MANAGEMENT"}, {"name":"OTHER"}, {"name":"VM_CREATION"}]
},
{
"name":"com.sun.management.internal.Flag",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","java.lang.Object","boolean","boolean","com.sun.management.VMOption$Origin"] }]
},
{
"name":"java.lang.Boolean",
"methods":[{"name":"<init>","parameterTypes":["boolean"] }, {"name":"getBoolean","parameterTypes":["java.lang.String"] }]
},
{
"name":"java.lang.Long",
"methods":[{"name":"<init>","parameterTypes":["long"] }]
},
{
"name":"sun.management.VMManagementImpl",
"fields":[{"name":"compTimeMonitoringSupport"}, {"name":"currentThreadCpuTimeSupport"}, {"name":"objectMonitorUsageSupport"}, {"name":"otherThreadCpuTimeSupport"}, {"name":"remoteDiagnosticCommandsSupport"}, {"name":"synchronizerUsageSupport"}, {"name":"threadAllocatedMemorySupport"}, {"name":"threadContentionMonitoringSupport"}]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Args=\
-H:+AddAllCharsets \
-H:+ReportExceptionStackTraces \
--initialize-at-build-time=ch.qos.logback,\
org.apache.log4j.Logger,\
org.slf4j,\
org.jsoup,\
com.typesafe.config,\
com.fasterxml.jackson,\
org.apache.commons.logging.impl.Jdk14Logger,\
org.apache.velocity.runtime.resource.ResourceManagerImpl,\
org.hibernate.validator.constraints.CodePointLength$NormalizationStrategy,\
com.vladsch.flexmark,\
com.vladsch.flexmark.util,\
com.vladsch.flexmark.parser,\
com.vladsch.flexmark.ext,\
com.vladsch.flexmark.html,\
com.vladsch.flexmark.ast.util,\
org.sqlite.util.ProcessRunner,\
org.apache.commons.collections.bidimap.DualHashBidiMap,\
org.apache.commons.collections.bidimap.AbstractDualBidiMap$EntrySet,\
org.apache.commons.collections.bidimap.AbstractDualBidiMap$Values,\
org.hibernate.validator.constraints.CodePointLength$NormalizationStrategy,\
org.apache.velocity.runtime.resource.ResourceManagerImpl,\
org.xml.sax.helpers,\
org.yaml.snakeyaml \
--initialize-at-run-time=com.vladsch.flexmark.util.sequence.Escaping

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"type":"agent-extracted",
"classes":[
]
}
]

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"interfaces":["java.lang.reflect.ParameterizedType","org.springframework.core.SerializableTypeWrapper$SerializableTypeProxy","java.io.Serializable"]
}
]
Loading

0 comments on commit 52192d5

Please sign in to comment.