You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2021/01/18 10:27:26 UTC

[isis] branch master updated: ISIS-2489: new config entry in support of TranslationsResolverWicket

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new d2e48ba  ISIS-2489: new config entry in support of TranslationsResolverWicket
d2e48ba is described below

commit d2e48baf9f2af3a574eff38ace4907686d4c6323
Author: Andi Huber <ah...@apache.org>
AuthorDate: Mon Jan 18 11:27:10 2021 +0100

    ISIS-2489: new config entry in support of TranslationsResolverWicket
    
    also supposed fixes for potential NPEs
---
 .../apache/isis/core/config/IsisConfiguration.java |  9 ++++++
 .../services/TranslationsResolverWicket.java       | 32 ++++++++++------------
 2 files changed, 23 insertions(+), 18 deletions(-)

diff --git a/core/config/src/main/java/org/apache/isis/core/config/IsisConfiguration.java b/core/config/src/main/java/org/apache/isis/core/config/IsisConfiguration.java
index faf35ac..d2b65f6 100644
--- a/core/config/src/main/java/org/apache/isis/core/config/IsisConfiguration.java
+++ b/core/config/src/main/java/org/apache/isis/core/config/IsisConfiguration.java
@@ -1594,6 +1594,15 @@ public class IsisConfiguration {
             public static class Translation {
 
                 private final Po po = new Po();
+                
+                /**
+                 * Specifies the relative resource path to look for translation files.
+                 * <p>
+                 * If {@code null} uses {@code servletContext.getResource("/WEB-INF/")}.
+                 * <p>
+                 * Replaces the former Servlet context parameter 'isis.config.dir';
+                 */
+                private String resourceLocation = null;
 
                 @Data
                 public static class Po {
diff --git a/viewers/wicket/viewer/src/main/java/org/apache/isis/viewer/wicket/viewer/services/TranslationsResolverWicket.java b/viewers/wicket/viewer/src/main/java/org/apache/isis/viewer/wicket/viewer/services/TranslationsResolverWicket.java
index 695fbfe..9283d13 100644
--- a/viewers/wicket/viewer/src/main/java/org/apache/isis/viewer/wicket/viewer/services/TranslationsResolverWicket.java
+++ b/viewers/wicket/viewer/src/main/java/org/apache/isis/viewer/wicket/viewer/services/TranslationsResolverWicket.java
@@ -29,6 +29,7 @@ import java.util.List;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
+import javax.inject.Inject;
 import javax.inject.Named;
 import javax.servlet.ServletContext;
 
@@ -39,32 +40,35 @@ import org.springframework.stereotype.Service;
 import org.apache.isis.applib.annotation.OrderPrecedence;
 import org.apache.isis.applib.services.i18n.TranslationsResolver;
 import org.apache.isis.commons.internal.base._Text;
-import org.apache.isis.viewer.wicket.viewer.wicketapp.IsisWicketApplication;
+import org.apache.isis.core.config.IsisConfiguration;
 
+import lombok.RequiredArgsConstructor;
 import lombok.val;
 import lombok.extern.log4j.Log4j2;
 
 
 /**
  * An implementation that reads from /WEB-INF/...
+ * TODO this Service is also required by the VaadinViewer, could be moved to a shared module
+ * TODO ... or (ideally) let Spring handle translations
  */
 @Service
 @Named("isisWicketViewer.TranslationsResolverWicket")
 @Order(OrderPrecedence.MIDPOINT)
 @Qualifier("Wicket")
+@RequiredArgsConstructor(onConstructor_ = {@Inject})
 @Log4j2
 public class TranslationsResolverWicket implements TranslationsResolver {
 
-    /**
-     * Servlet context parameter name used to specify the location for translations.
-     */
-    public static final String CONFIG_DIR_PARAM = "isis.config.dir";
+    private final ServletContext servletContext;
+    private final IsisConfiguration isisConfiguration;
 
     @Override
     public List<String> readLines(final String fileName) {
-        final ServletContext servletContext = getServletContext();
-
-        final String configLocation = servletContext.getInitParameter(CONFIG_DIR_PARAM);
+        
+        final String configLocation = 
+                isisConfiguration.getCore().getRuntimeServices().getTranslation().getResourceLocation();
+        
         try {
             if(configLocation != null) {
                 log.info( "Reading translations relative to config override location: {}", configLocation );
@@ -75,7 +79,7 @@ public class TranslationsResolverWicket implements TranslationsResolver {
                 return readLines(url);
             }
         } catch (final RuntimeException | IOException ignored) {
-            return null;
+            return Collections.emptyList();
         }
     }
 
@@ -85,15 +89,11 @@ public class TranslationsResolverWicket implements TranslationsResolver {
         return path.resolve(fileName);
     }
 
-    protected ServletContext getServletContext() {
-        return getIsisWicketApplication().getServletContext();
-    }
-
     private static final Pattern nonEmpty = Pattern.compile("^(#:|msgid|msgstr).+$");
     
     private static List<String> readLines(final URL url) throws IOException {
         if(url == null) {
-            return null;
+            return Collections.emptyList();
         }
         
         val acceptedLines = _Text.readLinesFromUrl(url, StandardCharsets.UTF_8)
@@ -104,8 +104,4 @@ public class TranslationsResolverWicket implements TranslationsResolver {
         return Collections.unmodifiableList(acceptedLines);
     }
 
-    protected IsisWicketApplication getIsisWicketApplication() {
-        return IsisWicketApplication.get();
-    }
-
 }