Use system properties to simplify backend configuration.
LocationBasedServices/mowesta/pipeline/head There was a failure building this commit

This commit is contained in:
Marcus Handte
2024-03-13 18:16:13 +01:00
parent 85a250bcf4
commit ee26a8180b
8 changed files with 58 additions and 135 deletions
+7
View File
@@ -14,3 +14,10 @@ To build the project, you will have to install [Java](http://www.oracle.com/tech
Pull requests and new commits to any branch are automatically built by a Jenkins service. A Gitea webhook informs the service about changes to the repository. In addition, the service will automatically scan the lbs Gitea organization for new repositories every hour. Successful builds of new commits are indicated by a green tick on the commit page. The build status of pull requests is currently not shown in Gitea. However, you can go to the [Jenkins](https://project.nes.uni-due.de/jenkins/job/LocationBasedServices/job/mowesta/) service to view the result of builds including the console output. To modify the build process, you can change the Jenkinsfile groovy script in the root of the repository, if necessary.
# Configuration
The MoWeSta backend uses a number of system properties to configure the location of input and output folders, the base url of videos and the forecast downlods. You can set them when starting wildfly by passing a ```-DpropretyName=propertyValue``` switch to the standalone.sh. The configurations are:
- *baseDirectory*: Defines where the input and output folders are placed. Default is working directory.
- *limitedMode*: Defines whether to limit the forecast download to cosmo europe. Default is false.
- *videoUrl*: Define the url to prefix the file names with. Default is https://www.mowesta.com/data/video.
@@ -1,18 +1,13 @@
package com.mowesta.service.injection;
import java.io.File;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;
import jakarta.inject.Qualifier;
/**
@@ -54,7 +49,7 @@ public class Settings {
@Qualifier
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface InfinispanFolder { }
public @interface ForecastFolder { }
/**
* The annotation to fetch the video base folder.
@@ -81,50 +76,36 @@ public class Settings {
@Retention(RetentionPolicy.RUNTIME)
public @interface LimitPredictions { }
/**
* The system property to access the base directory.
*/
private static final String PROPERTY_BASE_DIRECTORY = "baseDirectory";
/**
* The base folder to store lucene indexes. Note that this folder
* is also contained in the persistence.xml as a property for
* the hibernate search configuration.
* The system property to set the video url.
*/
private static final NameValuePair<String> FOLDER_LUCENCE = new NameValuePair<>("java:global/mowesta/folderLucene", "/Users/marcus/Data/mowesta/lucene");
private static final String PROPERTY_VIDEO_URL = "videoUrl";
/**
* The folder that contains the nominatim data to import.
* The system property to enable the limited predictions mode for testing.
*/
private static final NameValuePair<String> FOLDER_NOMINATIM = new NameValuePair<>("java:global/mowesta/folderNominatim", "/Users/marcus/Data/mowesta/nominatim");
private static final String PROPERTY_LIMITED_MODE = "limitedMode";
/**
* The base folder that contains the data from the dwd that is downloaded
* periodically and imported.
* The default base directory.
*/
private static final NameValuePair<String> FOLDER_DWD = new NameValuePair<>("java:global/mowesta/folderDwd", "/Users/marcus/Data/mowesta/dwd");
private static final String DEFAULT_BASE_DIRECTORY = ".";
/**
* The base folder that contains the infinispan data cache on the machine.
* The default value for the limited mode.
*/
private static final NameValuePair<String> FOLDER_INFINISPAN = new NameValuePair<>("java:global/mowesta/folderInfinispan", "/Users/marcus/Data/mowesta/infinispan");
private static final String DEFAULT_LIMITED_MODE = "false";
/**
* The folder that contains the generated video files.
* The default value for the video url.
*/
private static final NameValuePair<String> FOLDER_VIDEO = new NameValuePair<>("java:global/mowesta/folderVideo", "/Users/marcus/Data/mowesta/video");
/**
* The base url for videos.
*/
private static final NameValuePair<String> URL_VIDEO = new NameValuePair<>("java:global/mowesta/urlVideo", "https://www.mowesta.com/data/video/");
/**
* A boolean flag to limit the amount of predictions to process.
*/
private static final NameValuePair<Boolean> LIMIT_PREDICTIONS = new NameValuePair<>("java:global/mowesta/limitPredictions", Boolean.FALSE);
/**
* The logger.
*/
@Inject
private Logger logger;
private static final String DEFAULT_VIDEO_URL = "https://www.mowesta.com/data/video";
/**
* Produces the video url.
@@ -134,7 +115,7 @@ public class Settings {
@Produces
@VideoUrl
public String getVideoURL() {
return lookup(URL_VIDEO);
return System.getProperty(PROPERTY_VIDEO_URL, DEFAULT_VIDEO_URL);
}
/**
@@ -143,8 +124,10 @@ public class Settings {
* @return The infinispan folder.
*/
@Produces
@InfinispanFolder
public String getInfinispanFolder() { return lookup(FOLDER_INFINISPAN); }
@ForecastFolder
public String getForecastFolder() {
return getFolder("forecast");
}
/**
* Produces the lucene folder.
@@ -153,7 +136,9 @@ public class Settings {
*/
@Produces
@LuceneFolder
public String getLuceneFolder() { return lookup(FOLDER_LUCENCE); }
public String getLuceneFolder() {
return getFolder("lucene");
}
/**
* Produces the video folder.
@@ -162,7 +147,7 @@ public class Settings {
*/
@Produces
@VideoFolder
public String getVideoFolder() { return lookup(FOLDER_VIDEO); }
public String getVideoFolder() { return getFolder("video"); }
/**
* Produces the dwd folder.
@@ -171,7 +156,7 @@ public class Settings {
*/
@Produces
@DwdFolder
public String getDwdFolder() { return lookup(FOLDER_DWD); }
public String getDwdFolder() { return getFolder("dwd"); }
/**
* Produces the nominatim folder.
@@ -180,7 +165,7 @@ public class Settings {
*/
@Produces
@NominatimFolder
public String getNominatimFolder() { return lookup(FOLDER_NOMINATIM); }
public String getNominatimFolder() { return getFolder("nominatim"); }
/**
* A flag to reduce the amount of predictions to process.
@@ -191,62 +176,22 @@ public class Settings {
*/
@Produces
@LimitPredictions
public boolean isLimitPredicitons() { return lookup(LIMIT_PREDICTIONS); }
/**
* Performs a lookup of a name using JNDI and returns
* a value, or the default value if not present.
*
* @param nameValuePair The name value pair.
* @return The value bound to the name or the default value.
* @throws RuntimeException Thrown if something fails.
*/
@SuppressWarnings("unchecked")
private <T> T lookup(NameValuePair<T> nameValuePair) {
try {
InitialContext context = new InitialContext();
try {
return (T)context.lookup(nameValuePair.name);
} catch (NamingException e) {
// may happen, if name is not bound
} catch (RuntimeException e) {
logger.log(Level.SEVERE, "Found an unexpected data type when resolving " + nameValuePair.name + ". Consider adjusting it.");
}
logger.log(Level.INFO, "Using default value to resolve " + nameValuePair.name + ". Consider setting it explicitly.");
return nameValuePair.value;
} catch (NamingException e) {
throw new RuntimeException("Failed to resolve initial context.", e);
}
public boolean isLimitPredicitons() {
return Boolean.valueOf(System.getProperty(PROPERTY_LIMITED_MODE, DEFAULT_LIMITED_MODE));
}
/**
* An internal class to represent a name with a default value.
* Creates and returns the folder in the base directory.
*
* @param folder The folder.
* @return The absolute path to the folder inside the base directory.
*/
private static final class NameValuePair<T> {
/**
* The name.
*/
final String name;
/**
* The value.
*/
final T value;
/**
* Creates a new name value pair.
*
* @param name The name.
* @param value The value.
*/
private NameValuePair(String name, T value) {
this.name = name;
this.value = value;
}
private String getFolder(String folder) {
File file = new File(System.getProperty(PROPERTY_BASE_DIRECTORY, DEFAULT_BASE_DIRECTORY), folder);
file.mkdirs();
return file.getAbsolutePath();
}
}
@@ -60,7 +60,7 @@ public class CacheSessionBean {
/**
* The name of the index.
*/
private static final String CACHE_INDEX = "index";
private static final String INDEX_FORECAST = "forecast";
/**
* The logger for debug output.
@@ -78,8 +78,8 @@ public class CacheSessionBean {
* The folder containing the infinispan caches.
*/
@Inject
@Settings.InfinispanFolder
protected String infinispanFolder;
@Settings.ForecastFolder
protected String forecastFolder;
/**
* The folder containing the lucene indexes.
@@ -150,7 +150,7 @@ public class CacheSessionBean {
.make();
tempCache = temp.hashMap(CACHE_TEMP, Serializer.STRING, Serializer.DOUBLE_ARRAY).create();
try {
index = new ForecastIndex(getLocation(CACHE_INDEX));
index = new ForecastIndex(getIndexBase(INDEX_FORECAST));
} catch (IOException e) {
throw new RuntimeException("Failed to create forecast index.", e);
}
@@ -164,16 +164,17 @@ public class CacheSessionBean {
* @return The file system location.
*/
protected String getLocation(String cacheName) {
return new File(infinispanFolder, cacheName).getAbsolutePath();
return new File(forecastFolder, cacheName).getAbsolutePath();
}
/**
* Returns the base index location for lucene stores.
*
* @param name The namee
* @return The index base.
*/
protected String getIndexBase() {
return new File(luceneFolder).getAbsolutePath();
protected String getIndexBase(String name) {
return new File(luceneFolder, name).getAbsolutePath();
}
/**
@@ -1,28 +0,0 @@
package com.mowesta.service.session.test;
import com.mowesta.service.session.CacheSessionBean;
import jakarta.ejb.Singleton;
import jakarta.enterprise.inject.Alternative;
/**
* A bean that changes the file system location of
* the infinispan caches and lucene directories for testing.
*
* @author Marcus
*/
@Singleton
@Alternative
public class CacheTestBean extends CacheSessionBean {
@Override
protected String getIndexBase() {
return ".";
}
@Override
protected String getLocation(String cacheName) {
return cacheName;
}
}
@@ -31,7 +31,7 @@
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.jdbc.batch_size" value="1000"/>
<property name="hibernate.search.backend.type" value="lucene"/>
<property name="hibernate.search.backend.directory.root" value="/Users/marcus/Data/mowesta/search"/>
<property name="hibernate.search.backend.directory.root" value="${baseDirectory}/lucene"/>
<property name="hibernate.search.backend.directory.type" value="local-filesystem"/>
<property name="hibernate.search.backend.analysis.configurer" value="class:com.mowesta.service.injection.SearchConfigurer"/>
<property name="hibernate.search.backend.lucene_version" value="LATEST"/>
@@ -6,7 +6,6 @@
<alternatives>
<!-- enable testing beans to load test data synchronously and not send emails -->
<class>com.mowesta.service.session.test.DataTestBean</class>
<class>com.mowesta.service.session.test.CacheTestBean</class>
<class>com.mowesta.service.session.test.EmailTestBean</class>
<class>com.mowesta.service.session.test.LocationSyncTestBean</class>
<class>com.mowesta.service.session.test.PeriodicSyncTestBean</class>
@@ -6,7 +6,6 @@
<alternatives>
<!-- enable testing beans to load test data synchronously and not send emails -->
<class>com.mowesta.service.session.test.DataTestBean</class>
<class>com.mowesta.service.session.test.CacheTestBean</class>
<class>com.mowesta.service.session.test.EmailTestBean</class>
<class>com.mowesta.service.session.test.LocationSyncTestBean</class>
<class>com.mowesta.service.session.test.PeriodicSyncTestBean</class>
@@ -13,7 +13,7 @@
<property name="managementPort">10990</property>
<!-- reconfigure all port by offset of 1000 -->
<!-- <property name="javaVmArguments">-Djboss.socket.binding.port-offset=1000 -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y</property> -->
<property name="javaVmArguments">-Djboss.socket.binding.port-offset=1000 --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED</property>
<property name="javaVmArguments">-Djboss.socket.binding.port-offset=1000 -DbaseDirectory=./build/test-workspace --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED</property>
<!-- configure the wildfly location and the logger -->
<property name="jbossHome">../service-wildfly/wildfly-31.0.1.Final</property>
<property name="serverConfig">wildfly.xml</property>