Search in sources :

Example 1 with SourceFolder

use of org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder in project spring-boot by spring-projects.

the class ClassPathChangeUploaderTests method verifyUploadRequest.

private void verifyUploadRequest(File sourceFolder, MockClientHttpRequest request) throws IOException, ClassNotFoundException {
    ClassLoaderFiles classLoaderFiles = deserialize(request.getBodyAsBytes());
    Collection<SourceFolder> sourceFolders = classLoaderFiles.getSourceFolders();
    assertThat(sourceFolders.size()).isEqualTo(1);
    SourceFolder classSourceFolder = sourceFolders.iterator().next();
    assertThat(classSourceFolder.getName()).isEqualTo(sourceFolder.getAbsolutePath());
    Iterator<ClassLoaderFile> classFiles = classSourceFolder.getFiles().iterator();
    assertClassFile(classFiles.next(), "File1", ClassLoaderFile.Kind.ADDED);
    assertClassFile(classFiles.next(), "File2", ClassLoaderFile.Kind.MODIFIED);
    assertClassFile(classFiles.next(), null, ClassLoaderFile.Kind.DELETED);
    assertThat(classFiles.hasNext()).isFalse();
}
Also used : SourceFolder(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder) ClassLoaderFiles(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile)

Example 2 with SourceFolder

use of org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder in project spring-boot by spring-projects.

the class ClassLoaderFilesTests method getSourceFolders.

@Test
public void getSourceFolders() throws Exception {
    ClassLoaderFile file1 = new ClassLoaderFile(Kind.ADDED, new byte[10]);
    ClassLoaderFile file2 = new ClassLoaderFile(Kind.MODIFIED, new byte[10]);
    ClassLoaderFile file3 = new ClassLoaderFile(Kind.MODIFIED, new byte[10]);
    ClassLoaderFile file4 = new ClassLoaderFile(Kind.MODIFIED, new byte[10]);
    this.files.addFile("a", "myfile1", file1);
    this.files.addFile("a", "myfile2", file2);
    this.files.addFile("b", "myfile3", file3);
    this.files.addFile("b", "myfile4", file4);
    Iterator<SourceFolder> sourceFolders = this.files.getSourceFolders().iterator();
    SourceFolder sourceFolder1 = sourceFolders.next();
    SourceFolder sourceFolder2 = sourceFolders.next();
    assertThat(sourceFolders.hasNext()).isFalse();
    assertThat(sourceFolder1.getName()).isEqualTo("a");
    assertThat(sourceFolder2.getName()).isEqualTo("b");
    assertThat(sourceFolder1.getFiles()).containsOnly(file1, file2);
    assertThat(sourceFolder2.getFiles()).containsOnly(file3, file4);
}
Also used : SourceFolder(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder) Test(org.junit.Test)

Example 3 with SourceFolder

use of org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder in project spring-boot by spring-projects.

the class ClassLoaderFilesResourcePatternResolver method getAdditionalResources.

private List<Resource> getAdditionalResources(String locationPattern) throws MalformedURLException {
    List<Resource> additionalResources = new ArrayList<>();
    String trimmedLocationPattern = trimLocationPattern(locationPattern);
    for (SourceFolder sourceFolder : this.classLoaderFiles.getSourceFolders()) {
        for (Entry<String, ClassLoaderFile> entry : sourceFolder.getFilesEntrySet()) {
            String name = entry.getKey();
            ClassLoaderFile file = entry.getValue();
            if (file.getKind() == Kind.ADDED && this.antPathMatcher.match(trimmedLocationPattern, name)) {
                URL url = new URL("reloaded", null, -1, "/" + name, new ClassLoaderFileURLStreamHandler(file));
                UrlResource resource = new UrlResource(url);
                additionalResources.add(resource);
            }
        }
    }
    return additionalResources;
}
Also used : SourceFolder(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder) ClassLoaderFileURLStreamHandler(org.springframework.boot.devtools.restart.classloader.ClassLoaderFileURLStreamHandler) UrlResource(org.springframework.core.io.UrlResource) ArrayList(java.util.ArrayList) UrlResource(org.springframework.core.io.UrlResource) ServletContextResource(org.springframework.web.context.support.ServletContextResource) Resource(org.springframework.core.io.Resource) AbstractResource(org.springframework.core.io.AbstractResource) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile) URL(java.net.URL)

Example 4 with SourceFolder

use of org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder in project spring-boot by spring-projects.

the class RestartServer method updateAndRestart.

/**
	 * Update the current running application with the specified {@link ClassLoaderFiles}
	 * and trigger a reload.
	 * @param files updated class loader files
	 */
public void updateAndRestart(ClassLoaderFiles files) {
    Set<URL> urls = new LinkedHashSet<>();
    Set<URL> classLoaderUrls = getClassLoaderUrls();
    for (SourceFolder folder : files.getSourceFolders()) {
        for (Entry<String, ClassLoaderFile> entry : folder.getFilesEntrySet()) {
            for (URL url : classLoaderUrls) {
                if (updateFileSystem(url, entry.getKey(), entry.getValue())) {
                    urls.add(url);
                }
            }
        }
        urls.addAll(getMatchingUrls(classLoaderUrls, folder.getName()));
    }
    updateTimeStamp(urls);
    restart(urls, files);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SourceFolder(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder) URL(java.net.URL) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile)

Example 5 with SourceFolder

use of org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder 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)

Aggregations

SourceFolder (org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder)6 ClassLoaderFile (org.springframework.boot.devtools.restart.classloader.ClassLoaderFile)4 URL (java.net.URL)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 ClassLoaderFileURLStreamHandler (org.springframework.boot.devtools.restart.classloader.ClassLoaderFileURLStreamHandler)1 ClassLoaderFiles (org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles)1 AbstractResource (org.springframework.core.io.AbstractResource)1 Resource (org.springframework.core.io.Resource)1 UrlResource (org.springframework.core.io.UrlResource)1 ServletContextResource (org.springframework.web.context.support.ServletContextResource)1