Skip to content

Commit

Permalink
fix: Fix startup exception and theme imports in dev environment (halo…
Browse files Browse the repository at this point in the history
…-dev#916)

* Log error instead of throwing while starting halo

* Provide unknown version support

* Fix code format
  • Loading branch information
JohnNiang authored Jun 15, 2020
1 parent 7336286 commit 658f0a2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 24 deletions.
4 changes: 1 addition & 3 deletions src/main/java/run/halo/app/listener/StartedListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public void onApplicationEvent(ApplicationStartedEvent event) {

private void printStartInfo() {
String blogUrl = optionService.getBlogBaseUrl();

log.info(AnsiOutput.toString(AnsiColor.BRIGHT_BLUE, "Halo started at ", blogUrl));
log.info(AnsiOutput.toString(AnsiColor.BRIGHT_BLUE, "Halo admin started at ", blogUrl, "/", haloProperties.getAdminPath()));
if (!haloProperties.isDocDisabled()) {
Expand Down Expand Up @@ -149,7 +148,7 @@ private void initThemes() {
log.debug("Skipped copying theme folder due to existence of theme folder");
}
} catch (Exception e) {
throw new RuntimeException("Initialize internal theme to user path error", e);
log.error("Initialize internal theme to user path error!", e);
}
}

Expand Down Expand Up @@ -188,7 +187,6 @@ private void initDirectory() {
Files.createDirectories(dataExportPath);
log.info("Created data export directory: [{}]", dataExportPath);
}

} catch (IOException ie) {
throw new RuntimeException("Failed to initialize directories", ie);
}
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/run/halo/app/model/support/HaloConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.http.HttpHeaders;

import java.io.File;
import java.util.Optional;

/**
* <pre>
Expand Down Expand Up @@ -139,6 +140,12 @@ public class HaloConst {
* Version constant. (Available in production environment)
*/
public static final String HALO_VERSION;

/**
* Unknown version: unknown
*/
public static final String UNKNOWN_VERSION = "unknown";

/**
* Database product name.
*/
Expand All @@ -150,6 +157,6 @@ public class HaloConst {

static {
// Set version
HALO_VERSION = HaloConst.class.getPackage().getImplementationVersion();
HALO_VERSION = Optional.ofNullable(HaloConst.class.getPackage().getImplementationVersion()).orElse(UNKNOWN_VERSION);
}
}
36 changes: 29 additions & 7 deletions src/main/java/run/halo/app/utils/VersionUtil.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,63 @@
package run.halo.app.utils;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import run.halo.app.model.support.HaloConst;

import java.util.Objects;
import java.util.StringTokenizer;

/**
* @author ryanwang
* @date 2020-02-03
* @see com.sun.xml.internal.ws.util.VersionUtil
* @see "com.sun.xml.internal.ws.util.VersionUtil"
*/
@Slf4j
public class VersionUtil {

public VersionUtil() {
private static final String UNDERLINE = "_";

private VersionUtil() {
}

public static int[] getCanonicalVersion(String version) {
Assert.hasText(version, "Version must not be blank");

if (Objects.equals(version, HaloConst.UNKNOWN_VERSION)) {
log.warn("Unknown version will be converted to {}.{}.{}.{}",
Integer.MAX_VALUE,
Integer.MAX_VALUE,
Integer.MAX_VALUE,
Integer.MAX_VALUE);
return new int[] {Integer.MAX_VALUE,
Integer.MAX_VALUE,
Integer.MAX_VALUE,
Integer.MAX_VALUE};
}

int[] canonicalVersion = new int[] {1, 1, 0, 0};
StringTokenizer tokenizer = new StringTokenizer(version, ".");
String token = tokenizer.nextToken();
canonicalVersion[0] = Integer.parseInt(token);
token = tokenizer.nextToken();
StringTokenizer subTokenizer;
if (!token.contains(StrUtil.UNDERLINE)) {
if (!token.contains(UNDERLINE)) {
canonicalVersion[1] = Integer.parseInt(token);
} else {
subTokenizer = new StringTokenizer(token, "_");
subTokenizer = new StringTokenizer(token, UNDERLINE);
canonicalVersion[1] = Integer.parseInt(subTokenizer.nextToken());
canonicalVersion[3] = Integer.parseInt(subTokenizer.nextToken());
}

if (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
if (!token.contains(StrUtil.UNDERLINE)) {
if (!token.contains(UNDERLINE)) {
canonicalVersion[2] = Integer.parseInt(token);
if (tokenizer.hasMoreTokens()) {
canonicalVersion[3] = Integer.parseInt(tokenizer.nextToken());
}
} else {
subTokenizer = new StringTokenizer(token, "_");
subTokenizer = new StringTokenizer(token, UNDERLINE);
canonicalVersion[2] = Integer.parseInt(subTokenizer.nextToken());
canonicalVersion[3] = Integer.parseInt(subTokenizer.nextToken());
}
Expand All @@ -47,6 +67,8 @@ public static int[] getCanonicalVersion(String version) {
}

public static int compare(String version1, String version2) {
log.debug("Comparing version [{}] with [{}]", version1, version2);

int[] canonicalVersion1 = getCanonicalVersion(version1);
int[] canonicalVersion2 = getCanonicalVersion(version2);
if (canonicalVersion1[0] < canonicalVersion2[0]) {
Expand Down
45 changes: 32 additions & 13 deletions src/test/java/run/halo/app/utils/VersionUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
package run.halo.app.utils;

import org.junit.Assert;
import org.junit.Test;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Test;
import run.halo.app.model.support.HaloConst;

import static org.junit.jupiter.api.Assertions.*;

/**
* @author ryanwang
* @date 2020-02-03
*/
public class VersionUtilTest {
class VersionUtilTest {

@Test
public void compareVersion() {
Assert.assertTrue(VersionUtil.compareVersion("1.2.0", "1.1.1"));

Assert.assertTrue(VersionUtil.compareVersion("1.2.1", "1.2.0"));

Assert.assertTrue(VersionUtil.compareVersion("1.2.0", "1.1.1.0"));

Assert.assertTrue(VersionUtil.compareVersion("1.2.0", "0.4.4"));
void compareVersion() {
assertTrue(VersionUtil.compareVersion("1.2.0", "1.1.1"));
assertTrue(VersionUtil.compareVersion("1.2.1", "1.2.0"));
assertTrue(VersionUtil.compareVersion("1.2.0", "1.1.1.0"));
assertTrue(VersionUtil.compareVersion("1.2.0", "0.4.4"));
assertFalse(VersionUtil.compareVersion("1.1.1", "1.2.0"));
assertFalse(VersionUtil.compareVersion("0.0.1", "1.2.0"));
}

Assert.assertFalse(VersionUtil.compareVersion("1.1.1", "1.2.0"));
@Test
void unknownVersionCompareTest() {
// build a random version
String randomVersion = String.join(".",
RandomStringUtils.randomNumeric(1),
RandomStringUtils.randomNumeric(2),
RandomStringUtils.randomNumeric(3));
VersionUtil.compareVersion(HaloConst.UNKNOWN_VERSION, randomVersion);
}

Assert.assertFalse(VersionUtil.compareVersion("0.0.1", "1.2.0"));
@Test
void unknownOrEmptyCanonicalVersionTest() {
assertThrows(IllegalArgumentException.class, () -> VersionUtil.getCanonicalVersion(null));
int[] version = VersionUtil.getCanonicalVersion(HaloConst.UNKNOWN_VERSION);
assertNotNull(version);
assertEquals(4, version.length);
for (int v : version) {
assertEquals(Integer.MAX_VALUE, v);
}
}
}

0 comments on commit 658f0a2

Please sign in to comment.