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

Implement ResourceCodeResolver.getLastModified #186

Merged
merged 1 commit into from Nov 21, 2022
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
16 changes: 8 additions & 8 deletions DOCUMENTATION.md
Expand Up @@ -439,7 +439,7 @@ Sometimes it is required to output text as is. The `@raw` keyword can be used to

### For a regular website

When using the `DirectoryCodeResolver`, hot reloading is supported out of the box. Before a template is resolved, the modification timestamp of the template file and all of its dependencies is checked. If there is any modification detected, the template is recompiled and the old one discarded to GC.
When using the `DirectoryCodeResolver` (or the `ResourceCodeResolver` with resources located outside of JAR files), hot reloading is supported out of the box. Before a template is resolved, the modification timestamp of the template file and all of its dependencies is checked. If there is any modification detected, the template is recompiled and the old one discarded to GC.

> It makes sense to do this on your local development environment only. When running in production, for maximum performance and security [precompiled templates](#precompiling-templates) are recommended instead.

Expand Down Expand Up @@ -654,7 +654,7 @@ There is a [Maven plugin](https://github.com/casid/jte-maven-compiler-plugin) yo

#### Gradle

Since 1.6.0 there is a <a href="https://plugins.gradle.org/plugin/gg.jte.gradle">Gradle plugin</a> you can use to generate all templates during the Gradle build. Please note that paths specified in Java need to match those specified in Gradle.
Since 1.6.0 there is a <a href="https://plugins.gradle.org/plugin/gg.jte.gradle">Gradle plugin</a> you can use to generate all templates during the Gradle build. Please note that paths specified in Java need to match those specified in Gradle.

> Make sure that the jte gradle plugin version always matches the jte dependency version.

Expand Down Expand Up @@ -756,15 +756,15 @@ Let's implement `gg.jte.support.LocalizationSupport`. There's only one method to

```java
public static class JteLocalizer implements gg.jte.support.LocalizationSupport {

private final OtherFrameworkLocalizer frameworkLocalizer;
private final Locale locale;

public JteLocalizer(OtherFrameworkLocalizer frameworkLocalizer, Locale locale) {
this.frameworkLocalizer = frameworkLocalizer;
this.locale = locale;
}

@Override
public String lookup(String key) {
// However this works in your localization framework
Expand Down Expand Up @@ -793,15 +793,15 @@ public class JteContext {
public static Content localize(String key) {
context.get().localize(key);
}

public static Content localize(String key, Object... params) {
context.get().localize(key, params);
}

static void init(JteLocalizer localizer) {
context.set(localizer);
}

static void dispose() {
context.remove();
}
Expand Down
15 changes: 14 additions & 1 deletion jte/src/main/java/gg/jte/resolve/ResourceCodeResolver.java
Expand Up @@ -3,9 +3,12 @@
import gg.jte.CodeResolver;
import gg.jte.compiler.IoUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URISyntaxException;
import java.net.URL;

/**
* Resolves template code within a given resources root.
Expand Down Expand Up @@ -46,7 +49,17 @@ public boolean exists(String name) {

@Override
public long getLastModified(String name) {
return 0;
URL res = getClassLoader().getResource(root + name);
if (res == null) {
return 0;
}

try {
// Returns 0 if the file does not exist or an I/O error occurs.
return new File(res.toURI()).lastModified();
} catch (IllegalArgumentException | URISyntaxException e) {
return 0;
}
}

private ClassLoader getClassLoader() {
Expand Down
Expand Up @@ -36,12 +36,12 @@ void notFound() {
}

@Test
void lastModifiedNotSupported() {
void lastModified() {
resourceCodeResolver = new ResourceCodeResolver("benchmark");

long lastModified = resourceCodeResolver.getLastModified("welcome.jte");

assertThat(lastModified).isEqualTo(0L);
assertThat(lastModified).isNotEqualTo(0L);
}

@Test
Expand All @@ -68,4 +68,4 @@ void noRootDirectory() {
assertThat(resourceCodeResolver.exists("root-template.jte")).isTrue();
assertThat(resourceCodeResolver.exists("doesNotExist.jte")).isFalse();
}
}
}