use of org.springframework.boot.devtools.restart.classloader.ClassLoaderFile in project spring-boot by spring-projects.
the class RestartServerTests method updateSetsJarLastModified.
@Test
public void updateSetsJarLastModified() throws Exception {
long startTime = System.currentTimeMillis();
File folder = this.temp.newFolder();
File jarFile = new File(folder, "module-a.jar");
new FileOutputStream(jarFile).close();
jarFile.setLastModified(0);
URL url = jarFile.toURI().toURL();
URLClassLoader classLoader = new URLClassLoader(new URL[] { url });
SourceFolderUrlFilter filter = new DefaultSourceFolderUrlFilter();
MockRestartServer server = new MockRestartServer(filter, classLoader);
ClassLoaderFiles files = new ClassLoaderFiles();
ClassLoaderFile fileA = new ClassLoaderFile(Kind.ADDED, new byte[0]);
files.addFile("my/module-a", "ClassA.class", fileA);
server.updateAndRestart(files);
assertThat(jarFile.lastModified()).isGreaterThan(startTime - 1000);
}
use of org.springframework.boot.devtools.restart.classloader.ClassLoaderFile in project spring-boot by spring-projects.
the class RestarterTests method addClassLoaderFiles.
@Test
public void addClassLoaderFiles() throws Exception {
ClassLoaderFiles classLoaderFiles = new ClassLoaderFiles();
classLoaderFiles.addFile("f", new ClassLoaderFile(Kind.ADDED, "abc".getBytes()));
Restarter restarter = Restarter.getInstance();
restarter.addClassLoaderFiles(classLoaderFiles);
restarter.restart();
ClassLoader classLoader = ((TestableRestarter) restarter).getRelaunchClassLoader();
assertThat(FileCopyUtils.copyToByteArray(classLoader.getResourceAsStream("f"))).isEqualTo("abc".getBytes());
}
use of org.springframework.boot.devtools.restart.classloader.ClassLoaderFile in project spring-boot by spring-projects.
the class ClassPathChangeUploader method asClassLoaderFile.
private ClassLoaderFile asClassLoaderFile(ChangedFile changedFile) throws IOException {
ClassLoaderFile.Kind kind = TYPE_MAPPINGS.get(changedFile.getType());
byte[] bytes = (kind == Kind.DELETED ? null : FileCopyUtils.copyToByteArray(changedFile.getFile()));
long lastModified = (kind == Kind.DELETED ? System.currentTimeMillis() : changedFile.getFile().lastModified());
return new ClassLoaderFile(kind, lastModified, bytes);
}
use of org.springframework.boot.devtools.restart.classloader.ClassLoaderFile in project spring-boot by spring-projects.
the class ClassLoaderFilesResourcePatternResolver method isDeleted.
private boolean isDeleted(Resource resource) {
for (SourceFolder sourceFolder : this.classLoaderFiles.getSourceFolders()) {
for (Entry<String, ClassLoaderFile> entry : sourceFolder.getFilesEntrySet()) {
try {
String name = entry.getKey();
ClassLoaderFile file = entry.getValue();
if (file.getKind() == Kind.DELETED && resource.exists() && resource.getURI().toString().endsWith(name)) {
return true;
}
} catch (IOException ex) {
throw new IllegalStateException("Failed to retrieve URI from '" + resource + "'", ex);
}
}
}
return false;
}
use of org.springframework.boot.devtools.restart.classloader.ClassLoaderFile in project spring-boot by spring-projects.
the class RestartServer method updateFileSystem.
private boolean updateFileSystem(URL url, String name, ClassLoaderFile classLoaderFile) {
if (!isFolderUrl(url.toString())) {
return false;
}
try {
File folder = ResourceUtils.getFile(url);
File file = new File(folder, name);
if (file.exists() && file.canWrite()) {
if (classLoaderFile.getKind() == Kind.DELETED) {
return file.delete();
}
FileCopyUtils.copy(classLoaderFile.getContents(), file);
return true;
}
} catch (IOException ex) {
// Ignore
}
return false;
}
Aggregations