Search in sources :

Example 6 with ClassLoaderFile

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);
}
Also used : ClassLoaderFiles(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles) FileOutputStream(java.io.FileOutputStream) URLClassLoader(java.net.URLClassLoader) File(java.io.File) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile) URL(java.net.URL) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile) Test(org.junit.Test)

Example 7 with ClassLoaderFile

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());
}
Also used : ClassLoaderFiles(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles) URLClassLoader(java.net.URLClassLoader) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile) Test(org.junit.Test)

Example 8 with ClassLoaderFile

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);
}
Also used : Kind(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile.Kind) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile)

Example 9 with ClassLoaderFile

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;
}
Also used : SourceFolder(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder) IOException(java.io.IOException) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile)

Example 10 with ClassLoaderFile

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;
}
Also used : IOException(java.io.IOException) File(java.io.File) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile)

Aggregations

ClassLoaderFile (org.springframework.boot.devtools.restart.classloader.ClassLoaderFile)13 Test (org.junit.Test)7 ClassLoaderFiles (org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles)6 File (java.io.File)5 URL (java.net.URL)5 URLClassLoader (java.net.URLClassLoader)4 SourceFolder (org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder)4 Resource (org.springframework.core.io.Resource)3 ServletContextResource (org.springframework.web.context.support.ServletContextResource)3 IOException (java.io.IOException)2 LinkedHashSet (java.util.LinkedHashSet)2 DeletedClassLoaderFileResource (org.springframework.boot.devtools.restart.ClassLoaderFilesResourcePatternResolver.DeletedClassLoaderFileResource)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 Kind (org.springframework.boot.devtools.restart.classloader.ClassLoaderFile.Kind)1 ClassLoaderFileURLStreamHandler (org.springframework.boot.devtools.restart.classloader.ClassLoaderFileURLStreamHandler)1 AbstractResource (org.springframework.core.io.AbstractResource)1 UrlResource (org.springframework.core.io.UrlResource)1 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)1