-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#198 implement reading routes file from classpath (not only from loca…
…l file) it allows users to extract route files to modules (a jar file added to the project as a dependency).
- Loading branch information
Showing
7 changed files
with
93 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package play; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import play.exceptions.UnexpectedException; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.regex.Pattern; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
public class ClasspathResource { | ||
private static final Pattern RE_JAR_FILE_NAME = Pattern.compile("file:(.+\\.jar)!.+"); | ||
private final URL url; | ||
|
||
public static ClasspathResource file(String fileName) { | ||
return new ClasspathResource(Thread.currentThread().getContextClassLoader().getResource(fileName)); | ||
} | ||
|
||
ClasspathResource(URL url) { | ||
this.url = url; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "File " + url; | ||
} | ||
|
||
public String content() { | ||
try { | ||
return IOUtils.toString(url, UTF_8); | ||
} catch (IOException e) { | ||
throw new UnexpectedException(e); | ||
} | ||
} | ||
|
||
public boolean isModifiedAfter(long timestamp) { | ||
switch (url.getProtocol()) { | ||
case "file": { | ||
return new File(url.getFile()).lastModified() > timestamp; | ||
} | ||
case "jar": { | ||
return new File(getJarFilePath()).lastModified() > timestamp; | ||
} | ||
default: { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
String getJarFilePath() { | ||
return RE_JAR_FILE_NAME.matcher(url.getPath()).replaceFirst("$1"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package play; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class ClasspathResourceTest { | ||
@Test | ||
void extractsNameOfJarFile_from_classpathURL() throws MalformedURLException { | ||
assertThat(new ClasspathResource(new URL("jar:file:/Users/toomas/replay/build/libs/app-1.2.3.jar!/routes.yml")).getJarFilePath()) | ||
.isEqualTo("/Users/toomas/replay/build/libs/app-1.2.3.jar"); | ||
} | ||
} |
File renamed without changes.