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

Simplify EmbeddedExtractor #906

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 27 additions & 112 deletions server/embedded/src/org/labkey/embedded/EmbeddedExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.labkey.bootstrap.ConfigException;
import org.springframework.util.StreamUtils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -78,60 +75,37 @@ private File verifyJar()

private boolean shouldExtract(File webAppLocation)
{
File existingVersionFile = new File(webAppLocation, "WEB-INF/classes/VERSION");
File existingDistributionFile = new File(webAppLocation, "WEB-INF/classes/distribution.properties");

LabKeyDistributionInfo incomingDistribution = getDistributionInfo();

// Fresh installation or upgrading from a pre-distribution.properties distribution
if (!existingVersionFile.exists() || !existingDistributionFile.exists())
if (!existingDistributionFile.exists())
{
LOG.info("Extracting new LabKey distribution - %s".formatted(incomingDistribution));
return true;
}

String existingVersion;
String existingBuildUrl;
String existingDistributionName;
LabKeyDistributionInfo existingDistribution;

try
{
try (InputStream is = Files.newInputStream(existingDistributionFile.toPath()))
{
LabKeyDistributionInfo info = getFromProperties(is);
existingVersion = info.version;
existingBuildUrl = info.buildUrl;
existingDistributionName = info.distributionName;
}

// TODO: Stop reading VERSION file
if (existingVersion.isEmpty())
{
String versionFileContents = Files.readString(existingVersionFile.toPath()).trim();
String[] splitVersion = versionFileContents.trim().split("\\n");
existingVersion = splitVersion[0];
if (splitVersion.length > 1)
{
existingBuildUrl = splitVersion[1];
}
else
{
existingBuildUrl = null;
}
existingDistribution = getFromProperties(is);
}
}
catch (IOException e)
{
throw new RuntimeException(e);
}

LabKeyDistributionInfo existingDistribution = new LabKeyDistributionInfo(existingVersion, existingBuildUrl, existingDistributionName);

if (!existingDistribution.equals(incomingDistribution))
{
LOG.info("Updating LabKey (%s -> %s)".formatted(existingDistribution, incomingDistribution));
return true;
}
else if (incomingDistribution.buildUrl == null)
else if (incomingDistribution.buildUrl() == null)
{
LOG.info("Extracting custom-build LabKey distribution (%s)".formatted(existingDistribution));
return true;
Expand All @@ -149,9 +123,7 @@ else if (incomingDistribution.buildUrl == null)
*/
private LabKeyDistributionInfo getDistributionInfo()
{
String version = "";
String buildUrl = null;
String distributionName = "";
LabKeyDistributionInfo info = null;

try
{
Expand All @@ -173,47 +145,15 @@ private LabKeyDistributionInfo getDistributionInfo()
while (zipEntry != null)
{
distributionDirs.add(zipEntry.getName().split("/", 2)[0]);
// TODO: Remove this branch once newest gradle plugins version is adopted
if (!zipEntry.isDirectory() && zipEntry.getName().equals(LABKEYWEBAPP + "/WEB-INF/classes/VERSION"))
{
// Don't overwrite values from distribution.properties
if (version.isEmpty())
{
String versionFileContents = StreamUtils.copyToString(zipIn, StandardCharsets.UTF_8).trim();

String[] splitVersion = versionFileContents.trim().split("\\n");
version = splitVersion[0];
if (splitVersion.length > 1)
{
buildUrl = splitVersion[1];
}
else
{
buildUrl = null;
}
}
}
else if (!zipEntry.isDirectory() && zipEntry.getName().equals(LABKEYWEBAPP + "/WEB-INF/classes/distribution.properties"))
if (!zipEntry.isDirectory() && zipEntry.getName().equals(LABKEYWEBAPP + "/WEB-INF/classes/distribution.properties"))
{
LabKeyDistributionInfo info = getFromProperties(zipIn);
distributionName = info.distributionName;
if (!info.version.isEmpty())
version = info.version;
if (info.buildUrl != null)
buildUrl = info.buildUrl;
info = getFromProperties(zipIn);
}
zipIn.closeEntry();
zipEntry = zipIn.getNextEntry();
}
}
if (version.isEmpty())
{
throw new ConfigException("Unable to determine version of distribution.");
}
if (distributionName.isEmpty())
{
throw new ConfigException("Unable to determine name of distribution.");
}

if (!distributionDirs.equals(EXPECTED_DIST_DIRS))
{
StringBuilder msg = new StringBuilder("Corrupted distribution; contents are not as expected.");
Expand All @@ -236,7 +176,11 @@ else if (!zipEntry.isDirectory() && zipEntry.getName().equals(LABKEYWEBAPP + "/W

throw new IllegalStateException(msg.toString());
}
return new LabKeyDistributionInfo(version, buildUrl, distributionName);

if (null == info)
throw new IllegalStateException("distribution.properties file was not found!");

return info;
}
}

Expand All @@ -258,7 +202,10 @@ private LabKeyDistributionInfo getFromProperties(InputStream in) throws IOExcept
String version = props.getProperty("version", "").trim();
String buildUrl = props.containsKey("buildUrl") ? props.getProperty("buildUrl").trim() : null;

return new LabKeyDistributionInfo(version, buildUrl, distributionName);
var info = new LabKeyDistributionInfo(version, buildUrl, distributionName);
LOG.info("LabKeyDistributionInfo: " + info);

return info;
}

public void extractDistribution(File webAppLocation)
Expand Down Expand Up @@ -416,50 +363,18 @@ private void deleteOldDistribution(File webAppLocation)
}
}

class LabKeyDistributionInfo
/**
* Build properties from 'distribution.properties' file
*
* @param version the LabKey version (e.g. 24.3-SNAPSHOT)
* @param buildUrl optional TeamCity BUILD_URL, if distribution was produced by TeamCity
* @param distributionName value of the 'name' property
*/
record LabKeyDistributionInfo(String version, String buildUrl, String distributionName)
{
final String version;
final String buildUrl;
final String distributionName;

/**
* Build properties from 'distribution.properties' file
* @param version the LabKey version (e.g. 24.3-SNAPSHOT)
* @param buildUrl TeamCity BUILD_URL, if distribution was produced by TeamCity
* @param distributionName value of the 'name' property
*/
public LabKeyDistributionInfo(String version, String buildUrl, String distributionName)
{
this.version = version;
this.buildUrl = buildUrl;
this.distributionName = distributionName;
}

@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

LabKeyDistributionInfo that = (LabKeyDistributionInfo) o;

if (!version.equals(that.version)) return false;
if (!Objects.equals(buildUrl, that.buildUrl)) return false;
return distributionName.equals(that.distributionName);
}

@Override
public int hashCode()
{
int result = version.hashCode();
result = 31 * result + (buildUrl != null ? buildUrl.hashCode() : 0);
result = 31 * result + distributionName.hashCode();
return result;
}

@Override
public String toString()
{
return distributionName + ":" + version;
return distributionName + ":" + version + (buildUrl != null ? ":" + buildUrl : "");
}
}