Skip to content

Commit

Permalink
play with "lookup" using dataverse.version (scratch work)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Sep 7, 2022
1 parent a64c138 commit 898dce1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public int numberOfVarArgs() {
* @return The setting as a String
*/
public String lookup() {
System.out.println("lookup() called");
return lookup(String.class);
}

Expand All @@ -233,13 +234,16 @@ public Optional<String> lookupOptional() {
* @throws IllegalArgumentException When the settings value could not be converted to target type.
*/
public <T> T lookup(Class<T> klass) {
System.out.println("lookup(String.class) called on " + klass);
if (needsVarArgs()) {
System.out.println("about to throw this: Cannot lookup a setting containing placeholders with this method");
throw new IllegalArgumentException("Cannot lookup a setting containing placeholders with this method.");
}

// This must be done with the full-fledged lookup, as we cannot store the config in an instance or static
// variable, as the alias config source depends on this enum (circular dependency). This is easiest
// avoided by looking up the static cached config at the cost of a method invocation.
System.out.println("calling ConfigProvider.getConfig().getValue(this.getScopedKey(), klass);");
return ConfigProvider.getConfig().getValue(this.getScopedKey(), klass);
}

Expand Down
126 changes: 5 additions & 121 deletions src/main/java/edu/harvard/iq/dataverse/util/SystemConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import edu.harvard.iq.dataverse.authorization.AuthenticationServiceBean;
import edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinAuthenticationProvider;
import edu.harvard.iq.dataverse.authorization.providers.oauth2.AbstractOAuth2AuthenticationProvider;
import edu.harvard.iq.dataverse.settings.JvmSettings;
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
import edu.harvard.iq.dataverse.validation.PasswordValidatorUtil;
import java.io.FileInputStream;
Expand Down Expand Up @@ -41,6 +42,8 @@

import org.passay.CharacterRule;
import org.apache.commons.io.IOUtils;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;

/**
* System-wide configuration
Expand All @@ -50,6 +53,7 @@
public class SystemConfig {

private static final Logger logger = Logger.getLogger(SystemConfig.class.getCanonicalName());
private static final Config config = ConfigProvider.getConfig();

@EJB
SettingsServiceBean settingsService;
Expand Down Expand Up @@ -132,127 +136,7 @@ public String getVersion() {
// candidate for being moved into some kind of an application-scoped caching
// service... some CachingService @Singleton - ? (L.A. 5.8)
public String getVersion(boolean withBuildNumber) {

if (appVersionString == null) {

// The Version Number is no longer supplied in a .properties file - so
// we can't just do
// return BundleUtil.getStringFromBundle("version.number", null, ResourceBundle.getBundle("VersionNumber", Locale.US));
//
// Instead, we'll rely on Maven placing the version number into the
// Manifest, and getting it from there:
// (this is considered a better practice, and will also allow us
// to maintain this number in only one place - the pom.xml file)
// -- L.A. 4.0.2

// One would assume, that once the version is in the MANIFEST.MF,
// as Implementation-Version:, it would be possible to obtain
// said version simply as
// appVersionString = getClass().getPackage().getImplementationVersion();
// alas - that's not working, for whatever reason. (perhaps that's
// only how it works with jar-ed packages; not with .war files).
// People on the interwebs suggest that one should instead
// open the Manifest as a resource, then extract its attributes.
// There were some complications with that too. Plus, relying solely
// on the MANIFEST.MF would NOT work for those of the developers who
// are using "in place deployment" (i.e., where
// Netbeans runs their builds directly from the local target
// directory, bypassing the war file deployment; and the Manifest
// is only available in the .war file). For that reason, I am
// going to rely on the pom.properties file, and use java.util.Properties
// to read it. We have to look for this file in 2 different places
// depending on whether this is a .war file deployment, or a
// developers build. (the app-level META-INF is only populated when
// a .war file is built; the "maven-archiver" directory, on the other
// hand, is only available when it's a local build deployment).
// So, long story short, I'm resorting to the convoluted steps below.
// It may look hacky, but it should actually be pretty solid and
// reliable.


// First, find the absolute path url of the application persistence file
// always supplied with the Dataverse app:
java.net.URL fileUrl = Thread.currentThread().getContextClassLoader().getResource("META-INF/persistence.xml");
String filePath = null;


if (fileUrl != null) {
filePath = fileUrl.getFile();
if (filePath != null) {
InputStream mavenPropertiesInputStream = null;
String mavenPropertiesFilePath;
Properties mavenProperties = new Properties();


filePath = filePath.replaceFirst("/[^/]*$", "/");
// Using a relative path, find the location of the maven pom.properties file.
// First, try to look for it in the app-level META-INF. This will only be
// available if it's a war file deployment:
mavenPropertiesFilePath = filePath.concat("../../../META-INF/maven/edu.harvard.iq/dataverse/pom.properties");

try {
mavenPropertiesInputStream = new FileInputStream(mavenPropertiesFilePath);
} catch (IOException ioex) {
// OK, let's hope this is a local dev. build.
// In that case the properties file should be available in
// the maven-archiver directory:

mavenPropertiesFilePath = filePath.concat("../../../../maven-archiver/pom.properties");

// try again:

try {
mavenPropertiesInputStream = new FileInputStream(mavenPropertiesFilePath);
} catch (IOException ioex2) {
logger.warning("Failed to find and/or open for reading the pom.properties file.");
mavenPropertiesInputStream = null;
}
}

if (mavenPropertiesInputStream != null) {
try {
mavenProperties.load(mavenPropertiesInputStream);
appVersionString = mavenProperties.getProperty("version");
} catch (IOException ioex) {
logger.warning("caught IOException trying to read and parse the pom properties file.");
} finally {
IOUtils.closeQuietly(mavenPropertiesInputStream);
}
}

} else {
logger.warning("Null file path representation of the location of persistence.xml in the webapp root directory!");
}
} else {
logger.warning("Could not find the location of persistence.xml in the webapp root directory!");
}


if (appVersionString == null) {
// still null? - defaulting to 4.0:
appVersionString = "4.0";
}
}

if (withBuildNumber) {
if (buildNumberString == null) {
// (build number is still in a .properties file in the source tree; it only
// contains a real build number if this war file was built by
// Jenkins)

try {
buildNumberString = ResourceBundle.getBundle("BuildNumber").getString("build.number");
} catch (MissingResourceException ex) {
buildNumberString = null;
}
}

if (buildNumberString != null && !buildNumberString.equals("")) {
return appVersionString + " build " + buildNumberString;
}
}

return appVersionString;
return JvmSettings.VERSION.lookup();
}

public String getSolrHostColonPort() {
Expand Down

0 comments on commit 898dce1

Please sign in to comment.