Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable filtering of non-enabled i18n language files from being loaded… #160

Merged
merged 1 commit into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ApplicationBean {
public final static int VIVO_SEARCHBOX_SIZE = 20;

private final static String DEFAULT_APPLICATION_NAME = "Vitro";
private final static String DEFAULT_APPLICATION_AVAILABLE_LANGS_FILE = "available-langs";
private final static String DEFAULT_ROOT_LOGOTYPE_IMAGE = "";
private final static int DEFAULT_ROOT_LOGOTYPE_WIDTH = 0;
private final static int DEFAULT_ROOT_LOGOTYPE_HEIGHT = 0;
Expand All @@ -33,6 +34,7 @@ public class ApplicationBean {
private boolean initialized = false;
private String sessionIdStr = null;
private String applicationName = DEFAULT_APPLICATION_NAME;
private String availableLangsFile = DEFAULT_APPLICATION_AVAILABLE_LANGS_FILE;

private String rootLogotypeImage = DEFAULT_ROOT_LOGOTYPE_IMAGE;
private int rootLogotypeWidth = DEFAULT_ROOT_LOGOTYPE_WIDTH;
Expand All @@ -52,6 +54,7 @@ public String toString() {
output += " initialized from DB: [" + initialized + "]\n";
output += " session id: [" + sessionIdStr + "]\n";
output += " application name: [" + applicationName + "]\n";
output += " available langs file: [" + availableLangsFile + "]\n";
output += " root logotype image: [" + rootLogotypeImage + "]\n";
output += " root logotype width: [" + rootLogotypeWidth + "]\n";
output += " root logotype height: [" + rootLogotypeHeight + "]\n";
Expand Down Expand Up @@ -177,6 +180,10 @@ public String getShortHand() {
return "";
}

public String getAvailableLangsFile() {
return availableLangsFile;
}

/**
* Directory to find the images. Subdirectories include css, jsp and site_icons.
* Example: "themes/enhanced/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ private void setupModel(ServletContext ctx, String modelUri,

private void loadFirstTimeFiles(ServletContext ctx, String modelPath,
OntModel baseModel) {
RDFFilesLoader.loadFirstTimeFiles(modelPath, baseModel, baseModel.isEmpty());
RDFFilesLoader.loadFirstTimeFiles(ctx, modelPath, baseModel, baseModel.isEmpty());
}

private void loadEveryTimeFiles(ServletContext ctx, String modelPath,
OntModel memoryModel) {
RDFFilesLoader.loadEveryTimeFiles(modelPath, memoryModel);
RDFFilesLoader.loadEveryTimeFiles(ctx, modelPath, memoryModel);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ private void setUpJenaDataSource(ServletContext ctx) {

OntModel baseABoxModel = models.getOntModel(ABOX_ASSERTIONS);
if (firstTimeStartup) {
RDFFilesLoader.loadFirstTimeFiles("abox", baseABoxModel, true);
RDFFilesLoader.loadFirstTimeFiles(ctx, "abox", baseABoxModel, true);
}
RDFFilesLoader.loadEveryTimeFiles("abox", baseABoxModel);
RDFFilesLoader.loadEveryTimeFiles(ctx, "abox", baseABoxModel);

OntModel baseTBoxModel = models.getOntModel(TBOX_ASSERTIONS);
if (firstTimeStartup) {
RDFFilesLoader.loadFirstTimeFiles("tbox", baseTBoxModel, true);
RDFFilesLoader.loadFirstTimeFiles(ctx, "tbox", baseTBoxModel, true);
}
RDFFilesLoader.loadEveryTimeFiles("tbox", baseTBoxModel);
RDFFilesLoader.loadEveryTimeFiles(ctx, "tbox", baseTBoxModel);
}

private long secondsSince(long startTime) {
Expand All @@ -94,7 +94,7 @@ private long secondsSince(long startTime) {
private void initializeApplicationMetadata(ServletContext ctx,
Model applicationMetadataModel) {
OntModel temporaryAMModel = VitroModelFactory.createOntologyModel();
RDFFilesLoader.loadFirstTimeFiles("applicationMetadata", temporaryAMModel, true);
RDFFilesLoader.loadFirstTimeFiles(ctx, "applicationMetadata", temporaryAMModel, true);
setPortalUriOnFirstTime(temporaryAMModel, ctx);
applicationMetadataModel.add(temporaryAMModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@

package edu.cornell.mannlib.vitro.webapp.servlet.setup;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.TreeSet;

import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.i18n.VitroResourceBundle;
import edu.cornell.mannlib.vitro.webapp.i18n.selection.SelectedLocale;
import edu.cornell.mannlib.vitro.webapp.modelaccess.ModelAccess;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand All @@ -23,6 +33,8 @@

import edu.cornell.mannlib.vitro.webapp.application.ApplicationUtils;

import javax.servlet.ServletContext;

/**
* Help to load RDF files on first time and on every startup.
*/
Expand All @@ -34,8 +46,10 @@ public class RDFFilesLoader {
private static final String FIRST_TIME = "firsttime";
private static final String EVERY_TIME = "everytime";

private static List<String> omittedLocales;

/**
* Path filter that ignores sub-directories, hidden files, and markdown
* Path filter that ignores sub-directories, hidden files, markdown and non-enabled language
* files.
*/
private static final DirectoryStream.Filter<Path> RDF_FILE_FILTER = new DirectoryStream.Filter<Path>() {
Expand All @@ -52,6 +66,15 @@ public boolean accept(Path p) throws IOException {
if (p.toString().endsWith(".md")) {
return false;
}
// Skip language files that are not enabled
// ..Assumes all language files take the form: path/to/file/name-<locale>.extension
String basename = FilenameUtils.getBaseName(p.toString());
for (String omit : omittedLocales) {
if (basename.endsWith(omit)) {
log.info("Ignoring file not enabled in i18n configuration: " + p);
return false;
}
}
return true;
}
};
Expand All @@ -64,11 +87,11 @@ public boolean accept(Path p) throws IOException {
*
* The files from the directory are added to the model.
*/
public static void loadFirstTimeFiles(String modelPath, Model model,
public static void loadFirstTimeFiles(ServletContext ctx, String modelPath, Model model,
boolean firstTime) {
if (firstTime) {
String home = locateHomeDirectory();
Set<Path> paths = getPaths(home, RDF, modelPath, FIRST_TIME);
Set<Path> paths = getPaths(ctx, home, RDF, modelPath, FIRST_TIME);
for (Path p : paths) {
log.info("Loading " + relativePath(p, home));
readOntologyFileIntoModel(p, model);
Expand All @@ -87,11 +110,11 @@ public static void loadFirstTimeFiles(String modelPath, Model model,
*
* The files from the directory become a sub-model of the model.
*/
public static void loadEveryTimeFiles(String modelPath, OntModel model) {
public static void loadEveryTimeFiles(ServletContext ctx, String modelPath, OntModel model) {
OntModel everytimeModel = ModelFactory
.createOntologyModel(OntModelSpec.OWL_MEM);
String home = locateHomeDirectory();
Set<Path> paths = getPaths(home, RDF, modelPath, EVERY_TIME);
Set<Path> paths = getPaths(ctx, home, RDF, modelPath, EVERY_TIME);
for (Path p : paths) {
log.info("Loading " + relativePath(p, home));
readOntologyFileIntoModel(p, everytimeModel);
Expand All @@ -107,38 +130,14 @@ private static Path relativePath(Path p, String home) {
}
}

/**
* Create a model from all the RDF files in the specified directory.
*/
public static OntModel getModelFromDir(File dir) {
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
if (dir == null) {
log.warn("Must pass a File to getModelFromDir()");
return model;
}
if (!dir.isDirectory()) {
log.warn("Directory must be a File object for a directory");
return model;
}
if (!dir.canRead()) {
log.warn("getModelFromDir(): Directory "
+ " must be readable, check permissions on "
+ dir.getAbsolutePath());
return model;
}

Set<Path> paths = getPaths(dir.getPath());
for (Path p : paths) {
readOntologyFileIntoModel(p, model);
}
return model;
}

/**
* Find the paths to RDF files in this directory. Sub-directories, hidden
* files, and markdown files are ignored.
* files, markdown, and non-enabled language files are ignored.
*/
private static Set<Path> getPaths(String parentDir, String... strings) {
private static Set<Path> getPaths(ServletContext ctx, String parentDir, String... strings) {
// Ensure that omitted locales are loaded and available
loadOmittedLocales(ctx);

Path dir = Paths.get(parentDir, strings);

Set<Path> paths = new TreeSet<>();
Expand All @@ -158,6 +157,49 @@ private static Set<Path> getPaths(String parentDir, String... strings) {
return paths;
}

private static void loadOmittedLocales(ServletContext ctx) {
// Only load if 'omittedLocales' has not been initialized
if (omittedLocales == null) {
omittedLocales = new LinkedList<>();
List<String> enabledLocales = new LinkedList<>();

// Which locales are enabled in runtime.properties?
List<Locale> locales = SelectedLocale.getSelectableLocales(ctx);
for (Locale locale : locales) {
enabledLocales.add(locale.toLanguageTag().replace('-', '_'));
}

// Get ResourceBundle that contains listing of all available i18n languages
WebappDaoFactory wadf = ModelAccess.on(ctx).getWebappDaoFactory();
ApplicationBean app = wadf.getApplicationDao().getApplicationBean();
String availableLangsFilename = app.getAvailableLangsFile();
String themeI18nPath = "/not-used";
String appI18nPath = "/i18n";
VitroResourceBundle bundle = VitroResourceBundle.getBundle(
availableLangsFilename, ctx, appI18nPath, themeI18nPath,
ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_PROPERTIES));

// This should not happen
if (bundle == null) {
throw new RuntimeException("Unable to find bundle: " + availableLangsFilename);
}

// If no languages were enabled in runtime.properties, add 'en_US' as the default
if (enabledLocales.isEmpty()) {
enabledLocales.add("en_US");
}

// Omitted locales are the available locales minus the enabled locales
Enumeration langs = bundle.getKeys();
while (langs.hasMoreElements()) {
String available = (String) langs.nextElement();
if (!enabledLocales.contains(available)) {
omittedLocales.add(available);
}
}
}
}

private static void readOntologyFileIntoModel(Path p, Model model) {
String format = getRdfFormat(p);
log.debug("Loading " + p);
Expand Down
14 changes: 7 additions & 7 deletions installer/webapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
<type>war</type>
</overlay>
<!-- Overlays for multilingual support -->
<!-- overlay>
<overlay>
<groupId>org.vivoweb</groupId>
<artifactId>vitro-languages-webapp</artifactId>
<artifactId>vitro-languages-webapp-core</artifactId>
<type>war</type>
</overlay -->
</overlay>
</overlays>
<webResources>
<resource>
Expand Down Expand Up @@ -148,12 +148,12 @@
<type>war</type>
</dependency>
<!-- Dependency for multilingual support -->
<!-- dependency>
<dependency>
<groupId>org.vivoweb</groupId>
<artifactId>vitro-languages-webapp</artifactId>
<version>[2.0.0,2.1.0)</version>
<artifactId>vitro-languages-webapp-core</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency -->
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
Expand Down