Skip to content

Commit

Permalink
fixed various issues around the native build and added working native…
Browse files Browse the repository at this point in the history
… image configuration
  • Loading branch information
albogdano committed May 17, 2024
1 parent 8ff3fba commit 3ed4bd0
Show file tree
Hide file tree
Showing 21 changed files with 5,223 additions and 94 deletions.
25 changes: 7 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@
<url>https://github.com/Erudika/scoold/issues</url>
</issueManagement>

<ciManagement>
<system>Travis CI</system>
<url>https://travis-ci.org/Erudika/scoold</url>
</ciManagement>

<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand All @@ -65,7 +60,7 @@
<dependency>
<groupId>com.erudika</groupId>
<artifactId>para-client</artifactId>
<version>1.50.0</version>
<version>1.50.1-SNAPSHOT</version>
</dependency>

<!-- VELOCITY -->
Expand All @@ -79,7 +74,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- <exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
Expand All @@ -88,7 +83,7 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
</exclusions>
</exclusions>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -400,16 +395,10 @@
</executions>
<configuration>
<buildArgs>
<buildArg>--initialize-at-build-time=ch.qos.logback</buildArg>
<buildArg>--initialize-at-build-time=org.slf4j</buildArg>
<buildArg>--initialize-at-build-time=org.apache.velocity.runtime.resource.ResourceManagerImpl</buildArg>
<buildArg>--initialize-at-build-time=org.hibernate.validator.constraints.CodePointLength$NormalizationStrategy</buildArg>
<buildArg>--initialize-at-build-time=com.vladsch.flexmark.html.HtmlRenderer</buildArg>
<buildArg>--initialize-at-build-time=com.vladsch.flexmark.util.sequence.LineAppendable</buildArg>
<buildArg>--initialize-at-build-time=com.vladsch.flexmark.util.data</buildArg>
<buildArg>--initialize-at-build-time=com.fasterxml.jackson</buildArg>
<buildArg>--initialize-at-build-time=org.xml.sax.helpers</buildArg>
<buildArg>-H:IncludeResources="velocity-engine-core:.*/velocity.properties"</buildArg>
<!--THIS CAUSES SOME CONFLICTS WITH AZURE CORE WHERE IT'S INITIALIZED AT BUILD TIME-->
<buildArg>--initialize-at-run-time=org.slf4j.LoggerFactory</buildArg>
<buildArg>-H:+IncludeAllLocales</buildArg>
<buildArg>-H:ConfigurationFileDirectories=src/main/resources/META-INF/native-image/com.erudika/scoold/</buildArg>
</buildArgs>
<mainClass>com.erudika.scoold.ScooldServer</mainClass>
<agentConfiguration>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/erudika/scoold/ScooldServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@
import com.erudika.para.core.utils.Config;
import com.erudika.para.core.utils.Para;
import com.erudika.para.core.utils.Utils;
import com.erudika.scoold.utils.CoreUtils;
import com.erudika.scoold.utils.ScooldEmailer;
import com.erudika.scoold.utils.ScooldRequestInterceptor;
import com.erudika.scoold.utils.ScooldUtils;
import com.erudika.scoold.velocity.VelocityConfigurer;
import com.erudika.scoold.velocity.VelocityViewResolver;
import com.typesafe.config.ConfigFactory;
import jakarta.inject.Named;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import jakarta.inject.Named;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.runtime.RuntimeConstants;
import org.slf4j.Logger;
Expand Down Expand Up @@ -70,7 +71,7 @@ public class ScooldServer extends SpringBootServletInitializer {

static {
// tells ParaClient where to look for classes that implement ParaObject
System.setProperty("para.core_package_name", "com.erudika.scoold.core");
CoreUtils.registerCoreClasses();
System.setProperty("server.port", String.valueOf(CONF.serverPort()));
System.setProperty("server.servlet.context-path", CONF.serverContextPath());
System.setProperty("server.use-forward-headers", String.valueOf(CONF.inProduction()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.erudika.scoold.utils.ScooldUtils;
import jakarta.inject.Inject;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -41,9 +42,10 @@ public NotFoundController(ScooldUtils utils) {
}

@GetMapping
public String get(HttpServletRequest req, Model model) {
public String get(HttpServletRequest req, HttpServletResponse res, Model model) {
model.addAttribute("path", "notfound.vm");
model.addAttribute("title", utils.getLang(req).get("notfound.title"));
res.setStatus(404);
return "base";
}
}
101 changes: 101 additions & 0 deletions src/main/java/com/erudika/scoold/utils/CoreUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2013-2024 Erudika. https://erudika.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For issues and patches go to: https://github.com/erudika
*/
package com.erudika.scoold.utils;

import com.erudika.para.core.Address;
import com.erudika.para.core.ParaObject;
import com.erudika.para.core.Sysprop;
import com.erudika.para.core.Tag;
import com.erudika.para.core.User;
import com.erudika.para.core.Vote;
import com.erudika.para.core.utils.Para;
import com.erudika.para.core.utils.Utils;
import com.erudika.scoold.core.Badge;
import com.erudika.scoold.core.Comment;
import com.erudika.scoold.core.Feedback;
import com.erudika.scoold.core.Profile;
import com.erudika.scoold.core.Question;
import com.erudika.scoold.core.Reply;
import com.erudika.scoold.core.Report;
import com.erudika.scoold.core.Revision;
import com.erudika.scoold.core.Sticky;
import com.erudika.scoold.core.UnapprovedQuestion;
import com.erudika.scoold.core.UnapprovedReply;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections.bidimap.DualHashBidiMap;

/**
* Core utils.
* @author Alex Bogdanovski [[email protected]]
*/
@SuppressWarnings("unchecked")
public final class CoreUtils {

private static final Set<String> CORE_TYPES;

private static final Map<String, String> CORE_PARA_TYPES = new DualHashBidiMap();
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();

private CoreUtils() { }

static {
CORE_TYPES = new HashSet<>(Arrays.asList(
Utils.type(Badge.class),
Utils.type(Comment.class),
Utils.type(Feedback.class),
Utils.type(Profile.class),
Utils.type(Question.class),
Utils.type(Reply.class),
Utils.type(Report.class),
Utils.type(Revision.class),
//Utils.type(Sticky.class),
Utils.type(UnapprovedQuestion.class),
Utils.type(UnapprovedReply.class),
// Para core types
Utils.type(Address.class),
Utils.type(Sysprop.class),
Utils.type(Tag.class),
Utils.type(User.class),
Utils.type(Vote.class)
));
}

public static void registerCoreClasses() {
Para.registerCoreClasses(
Badge.class,
Comment.class,
Feedback.class,
Profile.class,
Question.class,
Reply.class,
Report.class,
Revision.class,
Sticky.class,
UnapprovedQuestion.class,
UnapprovedReply.class);
}

public static Set<String> getCoreTypes() {
return Set.copyOf(CORE_TYPES);
}

}
14 changes: 7 additions & 7 deletions src/main/java/com/erudika/scoold/utils/LanguageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.erudika.para.core.Sysprop;
import com.erudika.para.core.Translation;
import com.erudika.para.core.utils.Para;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -30,8 +32,6 @@
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import org.apache.commons.lang3.LocaleUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -114,7 +114,7 @@ public Map<String, String> readLanguage(String langCode) {
}
}
LANG_CACHE.put(langCode, lang);
return Collections.unmodifiableMap(lang);
return Map.copyOf(lang);
}

/**
Expand Down Expand Up @@ -144,7 +144,7 @@ public Map<String, String> getDefaultLanguage() {
// initialize the language cache maps
LANG_CACHE.put(getDefaultLanguageCode(), readLanguageFromFileAndUpdateProgress(getDefaultLanguageCode()));
}
return Collections.unmodifiableMap(LANG_CACHE.get(getDefaultLanguageCode()));
return LANG_CACHE.get(getDefaultLanguageCode());
}

/**
Expand All @@ -161,22 +161,22 @@ public String getDefaultLanguageCode() {
*/
public Map<String, Integer> getTranslationProgressMap() {
if (!LANG_PROGRESS_CACHE.isEmpty() && LANG_PROGRESS_CACHE.size() > 2) { // en + default user lang
return Collections.unmodifiableMap(LANG_PROGRESS_CACHE);
return LANG_PROGRESS_CACHE;
}
for (String langCode : ALL_LOCALES.keySet()) {
if (!langCode.equals(getDefaultLanguageCode())) {
LANG_CACHE.put(langCode, readLanguageFromFileAndUpdateProgress(langCode));
}
}
return Collections.unmodifiableMap(LANG_PROGRESS_CACHE);
return LANG_PROGRESS_CACHE;
}

/**
* Returns a map of all language codes and their locales.
* @return a map of language codes to locales
*/
public Map<String, Locale> getAllLocales() {
return Collections.unmodifiableMap(ALL_LOCALES);
return ALL_LOCALES;
}

private int calculateProgressPercent(double approved, double defsize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@
*/
package com.erudika.scoold.utils;

import java.net.URI;
import java.util.Set;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import org.springframework.web.servlet.resource.NoResourceFoundException;

/**
* HttpRequestMethodNotSupportedException handler - suppress spammy log messages.
Expand All @@ -48,4 +51,9 @@ protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequest
return new ResponseEntity<>(null, headers, status);
}

@Override
protected ResponseEntity<Object> handleNoResourceFoundException(
NoResourceFoundException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
return ResponseEntity.status(HttpStatus.PERMANENT_REDIRECT).location(URI.create("/not-found")).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
import static com.erudika.scoold.ScooldServer.*;
import com.erudika.scoold.core.Profile;
import com.erudika.scoold.core.Report.ReportType;
import java.net.ConnectException;
import java.util.Collections;
import java.util.Locale;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.net.ConnectException;
import java.util.Collections;
import java.util.Locale;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -216,16 +216,16 @@ public void postHandle(HttpServletRequest request, HttpServletResponse response,
// CSP, HSTS, etc, headers. See https://securityheaders.com
utils.setSecurityHeaders(cspNonce, request, response);
// default metadata for social meta tags
if (StringUtils.isBlank(modelAndView.getModel().getOrDefault("title", "").toString())) {
if (StringUtils.isBlank(modelAndView.getModel().getOrDefault("title", "") + "")) {
modelAndView.addObject("title", CONF.appName());
}
if (StringUtils.isBlank(modelAndView.getModel().getOrDefault("description", "").toString())) {
if (StringUtils.isBlank(modelAndView.getModel().getOrDefault("description", "") + "")) {
modelAndView.addObject("description", CONF.metaDescription());
}
if (StringUtils.isBlank(modelAndView.getModel().getOrDefault("keywords", "").toString())) {
if (StringUtils.isBlank(modelAndView.getModel().getOrDefault("keywords", "") + "")) {
modelAndView.addObject("keywords", CONF.metaKeywords());
}
if (StringUtils.isBlank(modelAndView.getModel().getOrDefault("ogimage", "").toString())) {
if (StringUtils.isBlank(modelAndView.getModel().getOrDefault("ogimage", "") + "")) {
modelAndView.addObject("ogimage", CONF.metaAppIconUrl());
}
}
Expand Down
Loading

0 comments on commit 3ed4bd0

Please sign in to comment.