├── .gitignore ├── tests ├── src │ ├── main │ │ ├── resources │ │ │ └── io │ │ │ │ └── xpipe │ │ │ │ └── modulefs │ │ │ │ └── tests │ │ │ │ ├── empty_file.txt │ │ │ │ └── test_resource.txt │ │ └── java │ │ │ ├── module-info.java │ │ │ └── io │ │ │ └── xpipe │ │ │ └── modulefs │ │ │ └── tests │ │ │ ├── Main.java │ │ │ ├── ExplodedTests.java │ │ │ └── CommonTests.java │ └── test │ │ ├── resources │ │ └── io │ │ │ └── xpipe │ │ │ └── modulefs │ │ │ └── tests │ │ │ └── junit │ │ │ ├── empty_file.txt │ │ │ └── test_resource.txt │ │ └── java │ │ ├── module-info.java │ │ └── io │ │ └── xpipe │ │ └── modulefs │ │ └── tests │ │ └── junit │ │ └── ExplodedModuleTest.java └── build.gradle ├── jlink_tests.bat ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── modulefs ├── src │ ├── main │ │ └── java │ │ │ ├── module-info.java │ │ │ └── io │ │ │ └── xpipe │ │ │ └── modulefs │ │ │ ├── ModuleDirectoryStream.java │ │ │ ├── ExplodedModuleFileSystem.java │ │ │ ├── JrtModuleFileSystem.java │ │ │ ├── ModuleFileStore.java │ │ │ ├── JarModuleFileSystem.java │ │ │ ├── ModuleFileSystem.java │ │ │ ├── ModulePath.java │ │ │ └── ModuleFileSystemProvider.java │ └── test │ │ └── java │ │ └── io │ │ └── xpipe │ │ └── modulefs │ │ └── Examples.java ├── build.gradle └── publish.gradle ├── .github └── workflows │ └── publish.yml ├── LICENSE.md ├── gradlew.bat ├── gradlew └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle/ 2 | build/ 3 | .idea -------------------------------------------------------------------------------- /tests/src/main/resources/io/xpipe/modulefs/tests/empty_file.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /jlink_tests.bat: -------------------------------------------------------------------------------- 1 | CALL tests\build\image\bin\modulefs.bat 2 | pause -------------------------------------------------------------------------------- /tests/src/test/resources/io/xpipe/modulefs/tests/junit/empty_file.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/src/main/resources/io/xpipe/modulefs/tests/test_resource.txt: -------------------------------------------------------------------------------- 1 | resource -------------------------------------------------------------------------------- /tests/src/test/resources/io/xpipe/modulefs/tests/junit/test_resource.txt: -------------------------------------------------------------------------------- 1 | resource -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'modulefs' 2 | include 'modulefs' 3 | include 'tests' -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xpipe-io/modulefs/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /tests/src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | module io.xpipe.modulefs.tests { 2 | exports io.xpipe.modulefs.tests; 3 | requires io.xpipe.modulefs; 4 | requires org.junit.jupiter.api; 5 | } -------------------------------------------------------------------------------- /tests/src/test/java/module-info.java: -------------------------------------------------------------------------------- 1 | module io.xpipe.modulefs.tests.junit { 2 | exports io.xpipe.modulefs.tests.junit; 3 | 4 | requires org.junit.jupiter.api; 5 | requires io.xpipe.modulefs.tests; 6 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-9.0.0-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /modulefs/src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | import io.xpipe.modulefs.ModuleFileSystemProvider; 2 | 3 | import java.nio.file.spi.FileSystemProvider; 4 | 5 | module io.xpipe.modulefs { 6 | requires transitive jdk.zipfs; 7 | 8 | exports io.xpipe.modulefs; 9 | provides FileSystemProvider with ModuleFileSystemProvider; 10 | } -------------------------------------------------------------------------------- /tests/src/test/java/io/xpipe/modulefs/tests/junit/ExplodedModuleTest.java: -------------------------------------------------------------------------------- 1 | package io.xpipe.modulefs.tests.junit; 2 | 3 | import io.xpipe.modulefs.tests.Main; 4 | import org.junit.jupiter.api.Test; 5 | 6 | public class ExplodedModuleTest { 7 | 8 | @Test 9 | public void testFileSystemRead() throws Exception { 10 | Main.main(new String[0]); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/src/main/java/io/xpipe/modulefs/tests/Main.java: -------------------------------------------------------------------------------- 1 | package io.xpipe.modulefs.tests; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) throws Exception { 6 | var ct = new CommonTests(); 7 | for (var method : CommonTests.class.getDeclaredMethods()) { 8 | if (method.isSynthetic()) { 9 | continue; 10 | } 11 | 12 | System.out.println("Running " + method.getName()); 13 | method.invoke(ct); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/src/main/java/io/xpipe/modulefs/tests/ExplodedTests.java: -------------------------------------------------------------------------------- 1 | package io.xpipe.modulefs.tests; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | 5 | import java.io.IOException; 6 | import java.net.URI; 7 | import java.nio.file.FileSystems; 8 | import java.nio.file.Files; 9 | import java.util.Map; 10 | 11 | public class ExplodedTests { 12 | 13 | public void testClose() throws IOException { 14 | // TODO 15 | try (var fs = FileSystems.newFileSystem( 16 | URI.create("module:/io.xpipe.modulefs.tests"), Map.of())) { 17 | var p1 = fs.getPath("io/xpipe/modulefs/tests/test_resource.txt"); 18 | Assertions.assertEquals(Files.readString(p1), "resource"); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /modulefs/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java-library' 3 | id 'maven-publish' 4 | id 'signing' 5 | } 6 | 7 | version '0.1.7' 8 | group 'io.xpipe' 9 | base.archivesName = 'modulefs' 10 | 11 | apply from: 'publish.gradle' 12 | 13 | repositories { 14 | mavenCentral() 15 | } 16 | 17 | tasks.withType(JavaCompile).configureEach { 18 | sourceCompatibility = JavaVersion.VERSION_17 19 | targetCompatibility = JavaVersion.VERSION_17 20 | modularity.inferModulePath = true 21 | options.encoding = 'UTF-8' 22 | options.compilerArgs << "-Xlint:unchecked" 23 | } 24 | 25 | javadoc { 26 | source = sourceSets.main.allJava 27 | options { 28 | addStringOption('-release', '17') 29 | addStringOption('link', 'https://docs.oracle.com/en/java/javase/17/docs/api/') 30 | addBooleanOption('html5', true) 31 | } 32 | } 33 | 34 | test { 35 | failOnNoDiscoveredTests = false 36 | } 37 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Git checkout 13 | uses: actions/checkout@v4 14 | 15 | - name: Set up GraalVM 16 | uses: graalvm/setup-graalvm@v1 17 | with: 18 | version: '21.3.0' 19 | java-version: '17' 20 | 21 | - name: Publish Maven Libraries 22 | run: ./gradlew publishToSonatype closeAndReleaseSonatypeStagingRepository 23 | env: 24 | GPG_KEY_ID: ${{ secrets.GPG_KEY_ID }} 25 | GPG_KEY: ${{ secrets.GPG_KEY }} 26 | GPG_KEY_PASSWORD: ${{ secrets.GPG_KEY_PASSWORD }} 27 | SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }} 28 | SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} 29 | 30 | - name: Upload artifact 31 | uses: actions/upload-artifact@v4 32 | with: 33 | name: modulefs 34 | path: modulefs/build/libs 35 | -------------------------------------------------------------------------------- /modulefs/src/main/java/io/xpipe/modulefs/ModuleDirectoryStream.java: -------------------------------------------------------------------------------- 1 | package io.xpipe.modulefs; 2 | 3 | import java.io.IOException; 4 | import java.nio.file.DirectoryStream; 5 | import java.nio.file.Path; 6 | import java.util.Iterator; 7 | 8 | public class ModuleDirectoryStream implements DirectoryStream { 9 | 10 | private ModuleFileSystem fs; 11 | private DirectoryStream wrapped; 12 | 13 | public ModuleDirectoryStream(ModuleFileSystem fs, DirectoryStream wrapped) { 14 | this.fs = fs; 15 | this.wrapped = wrapped; 16 | } 17 | 18 | @Override 19 | public Iterator iterator() { 20 | var it = wrapped.iterator(); 21 | return new Iterator<>() { 22 | 23 | @Override 24 | public boolean hasNext() { 25 | return it.hasNext(); 26 | } 27 | 28 | @Override 29 | public Path next() { 30 | return new ModulePath(fs, it.next()); 31 | } 32 | }; 33 | } 34 | 35 | @Override 36 | public void close() throws IOException { 37 | wrapped.close(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /modulefs/src/main/java/io/xpipe/modulefs/ExplodedModuleFileSystem.java: -------------------------------------------------------------------------------- 1 | package io.xpipe.modulefs; 2 | 3 | import java.io.IOException; 4 | import java.net.URI; 5 | import java.nio.file.Path; 6 | import java.nio.file.spi.FileSystemProvider; 7 | import java.util.Optional; 8 | 9 | public class ExplodedModuleFileSystem extends ModuleFileSystem { 10 | 11 | static Optional create(String module, FileSystemProvider provider, URI location) throws IOException { 12 | if (location.getScheme().equals("file")) { 13 | var basePath = Path.of(location); 14 | return Optional.of(new ExplodedModuleFileSystem(module, basePath, provider)); 15 | } 16 | return Optional.empty(); 17 | } 18 | 19 | private boolean open = true; 20 | 21 | ExplodedModuleFileSystem(String module, Path basePath, FileSystemProvider provider) { 22 | super(module, basePath, provider); 23 | } 24 | 25 | @Override 26 | public void close() throws IOException { 27 | open = false; 28 | } 29 | 30 | @Override 31 | public boolean isOpen() { 32 | return open; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | ### MIT License 2 | 3 | Copyright (c) 2021 Christopher Schnick 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /modulefs/src/main/java/io/xpipe/modulefs/JrtModuleFileSystem.java: -------------------------------------------------------------------------------- 1 | package io.xpipe.modulefs; 2 | 3 | import java.io.IOException; 4 | import java.net.URI; 5 | import java.nio.file.FileSystems; 6 | import java.nio.file.Path; 7 | import java.nio.file.spi.FileSystemProvider; 8 | import java.util.Map; 9 | import java.util.Optional; 10 | 11 | public final class JrtModuleFileSystem extends ModuleFileSystem { 12 | 13 | static Optional create( 14 | String module, 15 | FileSystemProvider provider, 16 | URI uri, 17 | URI location) throws IOException { 18 | if (location.getScheme().equals("jrt")) { 19 | String moduleName = uri.getPath().substring(1); 20 | var fs = FileSystems.newFileSystem(URI.create("jrt:/"), Map.of()); 21 | var basePath = fs.getPath("modules", moduleName); 22 | return Optional.of(new JrtModuleFileSystem(module, basePath, provider)); 23 | } 24 | return Optional.empty(); 25 | } 26 | 27 | private boolean open = true; 28 | 29 | JrtModuleFileSystem(String module, Path basePath, FileSystemProvider provider) { 30 | super(module, basePath, provider); 31 | } 32 | 33 | @Override 34 | public void close() throws IOException { 35 | open = false; 36 | } 37 | 38 | @Override 39 | public boolean isOpen() { 40 | return open; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /modulefs/src/test/java/io/xpipe/modulefs/Examples.java: -------------------------------------------------------------------------------- 1 | package io.xpipe.modulefs; 2 | 3 | import java.io.IOException; 4 | import java.net.URI; 5 | import java.net.URL; 6 | import java.nio.file.*; 7 | import java.nio.file.attribute.BasicFileAttributes; 8 | import java.util.Map; 9 | 10 | public class Examples { 11 | 12 | public static void complexExample() throws IOException { 13 | try (var fs = FileSystems.newFileSystem( 14 | URI.create("module:/com.myorg.mymodule"), Map.of())) { 15 | 16 | var filePath = fs.getPath("com/myorg/mymodule/assets"); 17 | Files.walkFileTree(filePath, new SimpleFileVisitor<>() { 18 | @Override 19 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 20 | // Do anything you want with the file 21 | return FileVisitResult.CONTINUE; 22 | } 23 | }); 24 | } 25 | } 26 | 27 | public static void urlExample() throws IOException { 28 | try (ModuleFileSystem fs = 29 | ModuleFileSystem.create("module:/com.myorg.mymodule")) { 30 | ModulePath modulePath = fs.getPath("com/myorg/mymodule/test_resource.txt"); 31 | // Get the internal path of the module path 32 | Path internalPath = modulePath.getWrappedPath(); 33 | URL usableUrl = internalPath.toUri().toURL(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /modulefs/publish.gradle: -------------------------------------------------------------------------------- 1 | java { 2 | withJavadocJar() 3 | withSourcesJar() 4 | } 5 | 6 | publishing { 7 | publications { 8 | mavenJava(MavenPublication) { 9 | from components.java 10 | 11 | pom { 12 | name = 'ModuleFS' 13 | description = 'The ModuleFS library provides a simple file system implementation to access the contents of Java modules in a unified way.' 14 | url = 'https://github.com/xpipe-io/modulefs' 15 | licenses { 16 | license { 17 | name = 'The MIT License (MIT)' 18 | url = 'https://github.com/xpipe-io/modulefs/LICENSE.md' 19 | } 20 | } 21 | developers { 22 | developer { 23 | id = 'crschnick' 24 | name = 'Christopher Schnick' 25 | email = 'crschnick@xpipe.io' 26 | } 27 | } 28 | scm { 29 | connection = 'scm:git:git://github.com/xpipe-io/modulefs.git' 30 | developerConnection = 'scm:git:ssh://github.com/xpipe-io/modulefs.git' 31 | url = 'https://github.com/xpipe-io/modulefs' 32 | } 33 | } 34 | } 35 | } 36 | } 37 | 38 | def signingKeyId = project.hasProperty('signingKeyId') ? project.property("signingKeyId") : System.getenv('GPG_KEY_ID') 39 | def signingKey = project.hasProperty('signingKeyFile') ? file(project.property("signingKeyFile")).text : System.getenv('GPG_KEY') 40 | def signingPassword = project.hasProperty('signingKeyPassword') ? project.property("signingKeyPassword") : System.getenv('GPG_KEY_PASSWORD') 41 | 42 | signing { 43 | useInMemoryPgpKeys(signingKeyId, signingKey, signingPassword != null ? signingPassword : '') 44 | 45 | sign publishing.publications.mavenJava 46 | } 47 | -------------------------------------------------------------------------------- /modulefs/src/main/java/io/xpipe/modulefs/ModuleFileStore.java: -------------------------------------------------------------------------------- 1 | package io.xpipe.modulefs; 2 | 3 | import java.io.IOException; 4 | import java.nio.file.FileStore; 5 | import java.nio.file.attribute.FileAttributeView; 6 | import java.nio.file.attribute.FileStoreAttributeView; 7 | 8 | public final class ModuleFileStore extends FileStore { 9 | 10 | private final FileStore wrappedStore; 11 | private final ModuleFileSystem fs; 12 | 13 | ModuleFileStore(FileStore wrappedStore, ModuleFileSystem fs) { 14 | this.wrappedStore = wrappedStore; 15 | this.fs = fs; 16 | } 17 | 18 | @Override 19 | public String name() { 20 | return fs.toString(); 21 | } 22 | 23 | @Override 24 | public String type() { 25 | return "modulefs"; 26 | } 27 | 28 | @Override 29 | public boolean isReadOnly() { 30 | return wrappedStore.isReadOnly(); 31 | } 32 | 33 | @Override 34 | public long getTotalSpace() throws IOException { 35 | return wrappedStore.getTotalSpace(); 36 | } 37 | 38 | @Override 39 | public long getUsableSpace() throws IOException { 40 | return wrappedStore.getUsableSpace(); 41 | } 42 | 43 | @Override 44 | public long getUnallocatedSpace() throws IOException { 45 | return wrappedStore.getUnallocatedSpace(); 46 | } 47 | 48 | @Override 49 | public boolean supportsFileAttributeView(Class type) { 50 | return wrappedStore.supportsFileAttributeView(type); 51 | } 52 | 53 | @Override 54 | public boolean supportsFileAttributeView(String name) { 55 | return wrappedStore.supportsFileAttributeView(name); 56 | } 57 | 58 | @Override 59 | public V getFileStoreAttributeView(Class type) { 60 | return wrappedStore.getFileStoreAttributeView(type); 61 | } 62 | 63 | @Override 64 | public Object getAttribute(String attribute) throws IOException { 65 | return wrappedStore.getAttribute(attribute); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /tests/build.gradle: -------------------------------------------------------------------------------- 1 | import org.beryx.jlink.JlinkTask 2 | import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform 3 | 4 | plugins { 5 | id 'application' 6 | id 'org.beryx.jlink' version '3.1.3' 7 | } 8 | 9 | setVersion '1.0-SNAPSHOT' 10 | 11 | application { 12 | mainModule = 'io.xpipe.modulefs.tests' 13 | mainClass = 'io.xpipe.modulefs.tests.Main' 14 | } 15 | 16 | repositories { 17 | mavenCentral() 18 | } 19 | 20 | dependencies { 21 | implementation project(':modulefs') 22 | testImplementation project(':modulefs') 23 | implementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' 24 | testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' 25 | testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0' 26 | } 27 | 28 | test { 29 | useJUnitPlatform() 30 | } 31 | 32 | sourceSets { 33 | main { 34 | output.resourcesDir = "build/classes/java/main" 35 | } 36 | test { 37 | output.resourcesDir = "build/classes/java/test" 38 | } 39 | } 40 | 41 | tasks.withType(JavaCompile).configureEach { 42 | sourceCompatibility = JavaVersion.VERSION_17 43 | targetCompatibility = JavaVersion.VERSION_17 44 | modularity.inferModulePath = true 45 | options.encoding = 'UTF-8' 46 | options.compilerArgs << "-Xlint:unchecked" 47 | } 48 | 49 | task copyModules(type: Copy) { 50 | into "${buildDir}/modules" 51 | from configurations.runtimeClasspath 52 | } 53 | 54 | task copyOutput(type: Copy) { 55 | into "${buildDir}/modules" 56 | from "${buildDir}/libs" 57 | } 58 | 59 | jlink { 60 | imageDir = file("$buildDir/image") 61 | options = [ 62 | // '--strip-debug', 63 | '--compress', '2', 64 | '--no-header-files', 65 | '--no-man-pages'] 66 | launcher { 67 | name = 'modulefs' 68 | } 69 | 70 | customImage { 71 | appModules = [ 72 | 'io.xpipe.modulefs.tests', 73 | ] 74 | } 75 | } 76 | 77 | task createImage(type: JlinkTask, dependsOn: [copyModules, copyOutput]) { 78 | } 79 | -------------------------------------------------------------------------------- /modulefs/src/main/java/io/xpipe/modulefs/JarModuleFileSystem.java: -------------------------------------------------------------------------------- 1 | package io.xpipe.modulefs; 2 | 3 | import java.io.IOException; 4 | import java.lang.reflect.InaccessibleObjectException; 5 | import java.net.URI; 6 | import java.nio.file.FileSystem; 7 | import java.nio.file.FileSystems; 8 | import java.nio.file.Path; 9 | import java.nio.file.spi.FileSystemProvider; 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | import java.util.Optional; 13 | 14 | public final class JarModuleFileSystem extends ModuleFileSystem { 15 | 16 | static Optional create( 17 | String module, FileSystemProvider provider, URI modUri) throws IOException { 18 | if (modUri.getPath().endsWith(".jar")) { 19 | var fsUri = URI.create("jar:" + modUri.toString()); 20 | FileSystem jarFs; 21 | 22 | Path modFilePath = Path.of(modUri); 23 | synchronized (openFsCounts) { 24 | if (openFsCounts.containsKey(modFilePath)) { 25 | jarFs = FileSystems.getFileSystem(fsUri); 26 | } else { 27 | jarFs = FileSystems.newFileSystem(fsUri, Map.of()); 28 | try { 29 | var m = jarFs.getClass().getDeclaredField("readOnly"); 30 | m.setAccessible(true); 31 | m.set(jarFs, true); 32 | } catch (IllegalAccessException | NoSuchFieldException | InaccessibleObjectException ignored) { 33 | } catch (Exception e) { 34 | throw new RuntimeException("Unable to make file " + modFilePath + " read-only", e); 35 | } 36 | } 37 | return Optional.of(new JarModuleFileSystem(module, jarFs.getPath("/"), modFilePath, provider)); 38 | } 39 | } 40 | return Optional.empty(); 41 | } 42 | 43 | private static final Map openFsCounts = new HashMap<>(); 44 | 45 | private final Path jarFilePath; 46 | 47 | JarModuleFileSystem(String module, Path basePath, Path jarFilePath, FileSystemProvider provider) { 48 | super(module, basePath, provider); 49 | this.jarFilePath = jarFilePath; 50 | synchronized (openFsCounts) { 51 | openFsCounts.put(jarFilePath, openFsCounts.getOrDefault(jarFilePath, 0) + 1); 52 | } 53 | } 54 | 55 | @Override 56 | public void close() throws IOException { 57 | synchronized (openFsCounts) { 58 | int openCount = openFsCounts.get(this.jarFilePath); 59 | openFsCounts.put(this.jarFilePath, --openCount); 60 | if (openCount == 0) { 61 | basePath.getFileSystem().close(); 62 | openFsCounts.remove(this.jarFilePath); 63 | } 64 | } 65 | } 66 | 67 | @Override 68 | public boolean isOpen() { 69 | return basePath.getFileSystem().isOpen(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /modulefs/src/main/java/io/xpipe/modulefs/ModuleFileSystem.java: -------------------------------------------------------------------------------- 1 | package io.xpipe.modulefs; 2 | 3 | import java.io.IOException; 4 | import java.lang.module.ModuleReference; 5 | import java.net.URI; 6 | import java.nio.file.*; 7 | import java.nio.file.attribute.UserPrincipalLookupService; 8 | import java.nio.file.spi.FileSystemProvider; 9 | import java.util.List; 10 | import java.util.Map; 11 | import java.util.Set; 12 | 13 | public abstract class ModuleFileSystem extends FileSystem { 14 | 15 | public static ModuleFileSystem create(String uri) throws IOException { 16 | ModuleFileSystemProvider fsp = FileSystemProvider.installedProviders().stream() 17 | .filter(p -> p instanceof ModuleFileSystemProvider) 18 | .map(p -> (ModuleFileSystemProvider) p) 19 | .findFirst() 20 | .orElseThrow(() -> new ProviderNotFoundException("modulefs provider not found")); 21 | return fsp.newFileSystem(URI.create(uri), Map.of()); 22 | } 23 | 24 | public static ModuleFileSystem create(ModuleReference reference) throws IOException { 25 | ModuleFileSystemProvider fsp = FileSystemProvider.installedProviders().stream() 26 | .filter(p -> p instanceof ModuleFileSystemProvider) 27 | .map(p -> (ModuleFileSystemProvider) p) 28 | .findFirst() 29 | .orElseThrow(() -> new ProviderNotFoundException("modulefs provider not found")); 30 | var location = reference.location().orElseThrow(() -> new IllegalArgumentException("Module reference location is unknown")); 31 | var name = reference.descriptor().name(); 32 | var uri = URI.create("module:/" + name); 33 | return fsp.newFileSystem(uri, Map.of("location", location)); 34 | } 35 | 36 | private final String module; 37 | private final FileSystemProvider provider; 38 | protected Path basePath; 39 | 40 | ModuleFileSystem(String module, Path basePath, FileSystemProvider provider) { 41 | this.module = module; 42 | this.basePath = basePath; 43 | this.provider = provider; 44 | } 45 | 46 | @Override 47 | public FileSystemProvider provider() { 48 | return provider; 49 | } 50 | 51 | FileSystemProvider getBaseProvider() { 52 | return basePath.getFileSystem().provider(); 53 | } 54 | 55 | ModulePath getRoot() { 56 | return new ModulePath(this, basePath); 57 | } 58 | 59 | @Override 60 | public boolean isReadOnly() { 61 | return basePath.getFileSystem().isReadOnly(); 62 | } 63 | 64 | @Override 65 | public String getSeparator() { 66 | return basePath.getFileSystem().getSeparator(); 67 | } 68 | 69 | @Override 70 | public Iterable getRootDirectories() { 71 | return Set.of(new ModulePath(this, basePath)); 72 | } 73 | 74 | @Override 75 | public Iterable getFileStores() { 76 | var store = basePath.getFileSystem().getFileStores().iterator().next(); 77 | return List.of(new ModuleFileStore(store, this)); 78 | } 79 | 80 | @Override 81 | public Set supportedFileAttributeViews() { 82 | return basePath.getFileSystem().supportedFileAttributeViews(); 83 | } 84 | 85 | @Override 86 | public ModulePath getPath(String first, String... more) { 87 | var current = basePath.resolve(first); 88 | for (var m : more) { 89 | current = current.resolve(m); 90 | } 91 | return new ModulePath(this, current); 92 | } 93 | 94 | @Override 95 | public PathMatcher getPathMatcher(String syntaxAndPattern) { 96 | return basePath.getFileSystem().getPathMatcher(syntaxAndPattern); 97 | } 98 | 99 | @Override 100 | public UserPrincipalLookupService getUserPrincipalLookupService() { 101 | return basePath.getFileSystem().getUserPrincipalLookupService(); 102 | } 103 | 104 | @Override 105 | public WatchService newWatchService() throws IOException { 106 | return basePath.getFileSystem().newWatchService(); 107 | } 108 | 109 | public String getModule() { 110 | return module; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /tests/src/main/java/io/xpipe/modulefs/tests/CommonTests.java: -------------------------------------------------------------------------------- 1 | package io.xpipe.modulefs.tests; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | 5 | import java.io.IOException; 6 | import java.net.URI; 7 | import java.nio.file.FileSystemNotFoundException; 8 | import java.nio.file.FileSystems; 9 | import java.nio.file.Files; 10 | import java.nio.file.Path; 11 | import java.util.Arrays; 12 | import java.util.Map; 13 | 14 | public class CommonTests { 15 | 16 | public void testFileSystemRead() throws IOException { 17 | try (var fs = FileSystems.newFileSystem( 18 | URI.create("module:/io.xpipe.modulefs.tests"), Map.of())) { 19 | var p1 = fs.getPath("io/xpipe/modulefs/tests/test_resource.txt"); 20 | Assertions.assertEquals("resource", Files.readString(p1)); 21 | } 22 | } 23 | 24 | public void testFileSystemWrite() throws IOException { 25 | try (var fs = FileSystems.newFileSystem( 26 | URI.create("module:/io.xpipe.modulefs.tests"), Map.of())) { 27 | var p1 = fs.getPath("io/xpipe/modulefs/tests/test_resource.txt"); 28 | Assertions.assertThrows(UnsupportedOperationException.class, () -> Files.writeString(p1, "test")); 29 | } 30 | } 31 | 32 | public void testMultipleSequentialFileSystemRead() throws IOException { 33 | var mod = "module:/io.xpipe.modulefs.tests"; 34 | try (var fs = FileSystems.newFileSystem( 35 | URI.create(mod), Map.of())) { 36 | var p = fs.getPath("io/xpipe/modulefs/tests/test_resource.txt"); 37 | Assertions.assertEquals("resource", Files.readString(p)); 38 | } 39 | 40 | try (var fs = FileSystems.newFileSystem( 41 | URI.create(mod), Map.of())) { 42 | var p = fs.getPath("io/xpipe/modulefs/tests/empty_file.txt"); 43 | Assertions.assertEquals("", Files.readString(p)); 44 | } 45 | } 46 | 47 | public void testMultipleParallelFileSystemRead() throws IOException { 48 | var mod = "module:/io.xpipe.modulefs.tests"; 49 | try (var fs = FileSystems.newFileSystem( 50 | URI.create(mod), Map.of())) { 51 | 52 | try (var fs2 = FileSystems.newFileSystem( 53 | URI.create(mod), Map.of())) { 54 | var p2 = fs2.getPath("io/xpipe/modulefs/tests/empty_file.txt"); 55 | Assertions.assertEquals("", Files.readString(p2)); 56 | } 57 | 58 | var p = fs.getPath("io/xpipe/modulefs/tests/test_resource.txt"); 59 | Assertions.assertEquals("resource", Files.readString(p)); 60 | } 61 | } 62 | 63 | public void testInvalidURIs() { 64 | Arrays.stream(new String[] { 65 | "module:/io.xpipe.modulefs.tests/", 66 | "module:/io.xpipe.modulefs.tests/org/../", 67 | "module:io.xpipe.modulefs.tests", 68 | "module://io.xpipe.modulefs.tests", 69 | "module:/io.xpipe.modulefs.tests#frag", 70 | "module:/abc/io.xpipe.modulefs.tests", 71 | "module:/io.xpipe.modulefs.tests?abc=d" 72 | 73 | }).forEach(s -> { 74 | Assertions.assertThrows(IllegalArgumentException.class, () -> FileSystems.newFileSystem( 75 | URI.create(s), Map.of())); 76 | }); 77 | } 78 | 79 | public void testGetFileSystem() throws IOException { 80 | Assertions.assertThrows(FileSystemNotFoundException.class, 81 | () -> FileSystems.getFileSystem(URI.create("module:/io.xpipe.modulefs.tests"))); 82 | 83 | var fs = FileSystems.newFileSystem(URI.create("module:/io.xpipe.modulefs.tests"), Map.of()); 84 | var gotFs = FileSystems.getFileSystem(URI.create("module:/io.xpipe.modulefs.tests")); 85 | Assertions.assertEquals(fs, gotFs); 86 | 87 | fs.close(); 88 | Assertions.assertThrows(FileSystemNotFoundException.class, 89 | () -> FileSystems.getFileSystem(URI.create("module:/io.xpipe.modulefs.tests"))); 90 | } 91 | 92 | public void testInvalidModule() throws IOException { 93 | Assertions.assertThrows(FileSystemNotFoundException.class, () -> Path.of(URI.create( 94 | "module:/invalid.module/"))); 95 | } 96 | 97 | public void testReadFromURI() throws IOException { 98 | var path = Path.of(URI.create( 99 | "module:/io.xpipe.modulefs.tests/io/xpipe/modulefs/tests/test_resource.txt")); 100 | Assertions.assertEquals(Files.readString(path), "resource"); 101 | path.getFileSystem().close(); 102 | } 103 | 104 | public void testDirectoryFromURI() throws IOException { 105 | var path = Path.of(URI.create("module:/io.xpipe.modulefs.tests/io/xpipe/modulefs/tests/")); 106 | Assertions.assertTrue(Files.list(path).anyMatch(p -> p.getFileName().toString().equals("empty_file.txt"))); 107 | path.getFileSystem().close(); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /modulefs/src/main/java/io/xpipe/modulefs/ModulePath.java: -------------------------------------------------------------------------------- 1 | package io.xpipe.modulefs; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.net.URI; 6 | import java.nio.file.*; 7 | import java.util.Iterator; 8 | import java.util.Objects; 9 | 10 | public class ModulePath implements Path { 11 | 12 | private final ModuleFileSystem fs; 13 | private final Path wrappedPath; 14 | 15 | ModulePath(ModuleFileSystem fs, Path wrappedPath) { 16 | this.fs = fs; 17 | this.wrappedPath = wrappedPath; 18 | } 19 | 20 | ModuleFileSystem getModuleFileSystem() { 21 | return fs; 22 | } 23 | 24 | public Path getWrappedPath() { 25 | return wrappedPath; 26 | } 27 | 28 | @Override 29 | public FileSystem getFileSystem() { 30 | return fs; 31 | } 32 | 33 | @Override 34 | public boolean isAbsolute() { 35 | return wrappedPath.isAbsolute(); 36 | } 37 | 38 | @Override 39 | public Path getRoot() { 40 | return fs.getRoot(); 41 | } 42 | 43 | @Override 44 | public Path getFileName() { 45 | return wrappedPath.getFileName(); 46 | } 47 | 48 | @Override 49 | public Path getParent() { 50 | return wrappedPath.getParent(); 51 | } 52 | 53 | @Override 54 | public int getNameCount() { 55 | return wrappedPath.getNameCount(); 56 | } 57 | 58 | @Override 59 | public Path getName(int index) { 60 | return wrappedPath.getName(index); 61 | } 62 | 63 | @Override 64 | public Path subpath(int beginIndex, int endIndex) { 65 | return wrappedPath.subpath(beginIndex, endIndex); 66 | } 67 | 68 | private Path getNullableWrappedPathInternal(Path other) { 69 | Objects.requireNonNull(other, "other"); 70 | if (!(other instanceof ModulePath)) { 71 | return null; 72 | } 73 | var cast = (ModulePath) other; 74 | if (!cast.fs.equals(fs)) { 75 | return null; 76 | } 77 | 78 | return cast.wrappedPath; 79 | } 80 | 81 | private Path getNonNullWrappedPathInternal(Path other) { 82 | Objects.requireNonNull(other, "other"); 83 | if (!(other instanceof ModulePath)) { 84 | throw new ProviderMismatchException(); 85 | } 86 | var cast = (ModulePath) other; 87 | return cast.wrappedPath; 88 | } 89 | 90 | @Override 91 | public String toString() { 92 | return wrappedPath.toString(); 93 | } 94 | 95 | @Override 96 | public boolean startsWith(Path other) { 97 | var wp = getNullableWrappedPathInternal(other); 98 | return wp != null && wrappedPath.startsWith(wp); 99 | } 100 | 101 | @Override 102 | public boolean startsWith(String other) { 103 | return startsWith(fs.getPath(other)); 104 | } 105 | 106 | @Override 107 | public boolean endsWith(Path other) { 108 | var wp = getNullableWrappedPathInternal(other); 109 | return wp != null && wrappedPath.endsWith(wp); 110 | } 111 | 112 | @Override 113 | public boolean endsWith(String other) { 114 | return endsWith(fs.getPath(other)); 115 | } 116 | 117 | @Override 118 | public Path normalize() { 119 | return new ModulePath(fs, wrappedPath.normalize()); 120 | } 121 | 122 | @Override 123 | public Path resolve(Path other) { 124 | var wp = getNonNullWrappedPathInternal(other); 125 | if (wp.isAbsolute()) { 126 | return other; 127 | } 128 | 129 | return new ModulePath(fs, wrappedPath.resolve(wp)); 130 | } 131 | 132 | @Override 133 | public Path resolve(String other) { 134 | return new ModulePath(fs, wrappedPath.resolve(other)); 135 | } 136 | 137 | @Override 138 | public Path resolveSibling(Path other) { 139 | var wp = getNonNullWrappedPathInternal(other); 140 | if (wp.isAbsolute()) { 141 | return other; 142 | } 143 | 144 | return new ModulePath(fs, wrappedPath.resolveSibling(wp)); 145 | } 146 | 147 | @Override 148 | public Path resolveSibling(String other) { 149 | return new ModulePath(fs, wrappedPath.resolveSibling(other)); 150 | } 151 | 152 | @Override 153 | public Path relativize(Path other) { 154 | var wp = getNonNullWrappedPathInternal(other); 155 | return new ModulePath(fs, wrappedPath.relativize(wp)); 156 | } 157 | 158 | @Override 159 | public URI toUri() { 160 | return URI.create("module:/" + fs.getModule() + "!/" + fs.basePath.relativize(wrappedPath).toString()); 161 | } 162 | 163 | @Override 164 | public Path toAbsolutePath() { 165 | return new ModulePath(fs, wrappedPath.toAbsolutePath()); 166 | } 167 | 168 | @Override 169 | public Path toRealPath(LinkOption... options) throws IOException { 170 | return new ModulePath(fs, wrappedPath.toRealPath(options)); 171 | } 172 | 173 | @Override 174 | public File toFile() { 175 | return wrappedPath.toFile(); 176 | } 177 | 178 | @Override 179 | public WatchKey register( 180 | WatchService watcher, 181 | WatchEvent.Kind[] events, 182 | WatchEvent.Modifier... modifiers) throws IOException { 183 | return wrappedPath.register(watcher, events, modifiers); 184 | } 185 | 186 | @Override 187 | public WatchKey register(WatchService watcher, WatchEvent.Kind... events) throws IOException { 188 | return wrappedPath.register(watcher, events); 189 | } 190 | 191 | @Override 192 | public Iterator iterator() { 193 | return new Iterator<>() { 194 | private final Iterator wrappedIterator = wrappedPath.iterator(); 195 | 196 | @Override 197 | public boolean hasNext() { 198 | return wrappedIterator.hasNext(); 199 | } 200 | 201 | @Override 202 | public Path next() { 203 | return new ModulePath(fs, wrappedIterator.next()); 204 | } 205 | }; 206 | } 207 | 208 | @Override 209 | public int compareTo(Path other) { 210 | var wp = getNonNullWrappedPathInternal(other); 211 | return wrappedPath.compareTo(wp); 212 | } 213 | 214 | 215 | } 216 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.xpipe/modulefs/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.xpipe/modulefs) 2 | [![javadoc](https://javadoc.io/badge2/io.xpipe/modulefs/javadoc.svg)](https://javadoc.io/doc/io.xpipe/modulefs) 3 | [![Build Status](https://github.com/xpipe-io/modulefs/actions/workflows/publish.yml/badge.svg)](https://github.com/xpipe-io/modulefs/actions/workflows/publish.yml) 4 | 5 | # ModuleFS library 6 | 7 | The ModuleFS library provides a simple file system implementation to access the contents of Java modules in a unified way. 8 | It also comes with a variety of neat features that will make working with modules more enjoyable for you. 9 | You can get the library through [maven central](https://search.maven.org/artifact/io.xpipe/modulefs). 10 | Note that at least Java 17 is required as it is the first LTS release that includes all necessary bug fixes for the internal module file systems. 11 | 12 | ## Installation 13 | 14 | To use ModuleFS with Maven you have to add it as a dependency: 15 | 16 | 17 | io.xpipe 18 | modulefs 19 | 0.1.7 20 | 21 | 22 | For gradle, add the following entries to your build.gradle file: 23 | 24 | dependencies { 25 | implementation group: 'io.xpipe', name: 'modulefs', version: '0.1.7' 26 | } 27 | 28 | Add the library to your project's module-info like this: 29 | 30 | requires io.xpipe.modulefs; 31 | 32 | Note that ModuleFS requires your project to be modularized. 33 | 34 | ## Motivation 35 | 36 | The [Path](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/Path.html) and 37 | [FileSystem](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/FileSystem.html) APIs 38 | introduced in Java 7 with NIO.2 makes working with files much more pleasant. 39 | One hurdle when trying to use these APIs with bundled application resources is that modules can be stored in different formats: 40 | - Exploded module directory, stored as directory tree (Used during development) 41 | - Module artifacts, mostly stored as jars (Used for both development and production) 42 | - Modules contained in a jlink image, which are stored in a proprietary jimage format (Only used for production) 43 | 44 | #### Implementation Differences 45 | 46 | While there are FileSystem implementations like `jdk.zipfs` for jar files and the internal `jrtfs` for jlink images, 47 | there is no unified interface, which results in you having to take care of FileSystem specific differences. 48 | For example, the first challenge is to reliably find out the storage format of a module. 49 | Then, you also have to adapt your code to handle unique properties of the storage format. 50 | For example, every format requires you to use different schemes and approaches to closing the underlying file 51 | system, which causes exceptions when not being done properly. 52 | As a result, how to access files in modules using FileSystems heavily depends on how the modules are stored 53 | and can therefore vary between development and production environments. 54 | The current solution to reliably access resources in a package of a jar, or now a module, is to use the old methods 55 | [getResource()](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Class.html#getResource(java.lang.String)) and 56 | [getResourceAsStream()](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Class.html#getResourceAsStream(java.lang.String)). 57 | These methods are however way more tedious to work with as it is lacking the many features of the Path API. 58 | 59 | #### Modules vs Classpaths 60 | 61 | The main difference between the traditional classpaths and 62 | modules is that a module is located at exactly one location, e.g. a jar archive or a directory. 63 | It is not possible to combine multiple different locations into 64 | one module as you could do with for example multiple directories and classpaths. 65 | This makes working with FileSystems on modules much easier as there is only one root. 66 | 67 | 68 | ## Features 69 | 70 | ModuleFS allows you to create a FileSystem using the `module` URI scheme. 71 | The created FileSystem instance conforms to standard FileSystem behaviours 72 | with some limitations on writability as modules are intended to be read-only. 73 | A simple file reading example could look like this: 74 | 75 | ````java 76 | try (var fs = FileSystems.newFileSystem( 77 | URI.create("module:/com.myorg.mymodule"), Map.of())) { 78 | // The file system paths start from the root of the module, 79 | // so you have to include packages in your paths! 80 | var filePath = fs.getPath("com/myorg/mymodule/test_resource.txt"); 81 | var fileContent = Files.readString(filePath); 82 | } 83 | ```` 84 | 85 | The Path API allows for more complex applications than just parsing the contents of a single file. 86 | For example, we can also easily recursively iterative over all files in a directory that exists inside your module: 87 | 88 | ````java 89 | try (var fs = FileSystems.newFileSystem( 90 | URI.create("module:/com.myorg.mymodule"), Map.of())) { 91 | 92 | var filePath = fs.getPath("com/myorg/mymodule/assets"); 93 | Files.walkFileTree(filePath, new SimpleFileVisitor<>() { 94 | @Override 95 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 96 | // Do anything you want with the file 97 | return FileVisitResult.CONTINUE; 98 | } 99 | }); 100 | } 101 | ```` 102 | 103 | Basically, you can make use of any method in the 104 | [Files](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/Files.html) class. 105 | 106 | 107 | ### Module Layers 108 | 109 | In case you are using custom [ModuleLayers](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/ModuleLayer.html), 110 | you will have to pass this information to the file system to correctly locate all modules of this layer: 111 | 112 | ````java 113 | ModuleLayer customLayer = ... ; 114 | try (var fs = FileSystems.newFileSystem( 115 | URI.create("module:/com.myorg.mymodule"), Map.of("layer", customLayer))) { 116 | ... 117 | } 118 | ```` 119 | 120 | ### Using URLs 121 | 122 | Many other Java methods take [URLs](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/net/URL.html) as an input. 123 | One problem with many libraries, even standard libraries, is that they hardcode/expect 124 | URLs with a certain protocol, e.g. `file:` or `jar:`. 125 | To adapt ModuleFS to this limitation, the ModuleFS library does not come with custom 126 | [URLStreamHandler](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/net/URLStreamHandler.html) 127 | implementations required to make use of `module: ...` URLs. 128 | 129 | The easiest way of obtaining a usable URL is to access the wrapped internal Path and access its URL: 130 | 131 | ````java 132 | try (ModuleFileSystem fs = ModuleFileSystem.create("module:/com.myorg.mymodule")) { 133 | ModulePath modulePath = fs.getPath("com/myorg/mymodule/test_resource.txt"); 134 | // Get the internal path of the module path 135 | Path internalPath = modulePath.getWrappedPath(); 136 | URL usableUrl = internalPath.toUri().toURL(); 137 | } 138 | ```` 139 | 140 | In the above example, we explicitly use the ModuleFileSystem class to automatically get ModulePath instances when creating paths. 141 | This allows us to use the `getWrappedPath()` method to obtain the internal path, which returns an `file:`, `jar:`, or `jrt:` URL. 142 | You can then use this URL to access any resources of the module in a normal fashion by passing the URL. 143 | Note that this requires a file system to be created through the `ModuleFileSystem` class, not the `FileSystem` class. 144 | 145 | ### Bypassing Encapsulation 146 | 147 | One common problem you might encounter when working with modules our permission issues. 148 | Unless a package is open by a certain module, you are not allowed to access the contained resources. 149 | However, this mechanism is implemented on a very high level, 150 | i.e. you can easily bypass it by accessing the underlying file system instead methods like 151 | [getResource()](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Class.html#getResource(java.lang.String)). 152 | As ModuleFS does only work through the underlying file systems, 153 | you will not run into any permission issues when using ModuleFS, i.e. 154 | you can even access resources from modules that are not open at all. 155 | 156 | ### Module References 157 | 158 | In case you are loading modules at runtime and want to access the file system of a module before a proper module layer is created, 159 | you can also create a module file system for a 160 | [ModuleReference](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/ModuleReference.html) like this: 161 | 162 | ````java 163 | Path path = ...; 164 | var finder = ModuleFinder.of(path); 165 | var moduleReference = finder.find("myorg.mymodule") 166 | .orElseThrow(() -> new IllegalArgumentException("Module not found")); 167 | try (var fs = ModuleFileSystem.create(moduleReference)) { 168 | ... 169 | } 170 | ```` 171 | 172 | 173 | ## Development 174 | 175 | Testing is made more difficult by the fact that we also have to run all tests in a jlink image to achieve good coverage. 176 | Furthermore, junit uses the exploded module structure, so we also have to separately run tests in a module jar file. 177 | Therefore, the tests are defined in the `tests` subproject in the main source directory, which are then called from 178 | the three different environments, exploded module, module jar, and jlink image. 179 | 180 | To run all tests, we need to run the following commands: 181 | - `gradle :tests:test` (Exploded module) 182 | - `gradle :tests:run` (Module jar) 183 | - `gradle :tests:createImage` and then run `jlink_tests.bat` (jlink image) 184 | 185 | Some features and use cases have not been tested yet. 186 | For example, an application that uses a mix of full modules, 187 | automatic modules, and non-modular jars has not been tested yet. 188 | -------------------------------------------------------------------------------- /modulefs/src/main/java/io/xpipe/modulefs/ModuleFileSystemProvider.java: -------------------------------------------------------------------------------- 1 | package io.xpipe.modulefs; 2 | 3 | import java.io.IOException; 4 | import java.io.UncheckedIOException; 5 | import java.lang.module.ModuleReference; 6 | import java.lang.module.ResolvedModule; 7 | import java.net.URI; 8 | import java.nio.channels.SeekableByteChannel; 9 | import java.nio.file.*; 10 | import java.nio.file.attribute.BasicFileAttributes; 11 | import java.nio.file.attribute.FileAttribute; 12 | import java.nio.file.attribute.FileAttributeView; 13 | import java.nio.file.spi.FileSystemProvider; 14 | import java.util.*; 15 | import java.util.stream.Stream; 16 | 17 | public class ModuleFileSystemProvider extends FileSystemProvider { 18 | 19 | private final Map filesystems = new HashMap<>(); 20 | 21 | public ModuleFileSystemProvider() { 22 | } 23 | 24 | @Override 25 | public String getScheme() { 26 | return "module"; 27 | } 28 | 29 | private void checkUri(URI uri) { 30 | if (!uri.getScheme().equalsIgnoreCase(getScheme())) { 31 | throw new IllegalArgumentException("URI does not match this provider"); 32 | } 33 | if (uri.getAuthority() != null) { 34 | throw new IllegalArgumentException("Authority component present"); 35 | } 36 | if (uri.getPath() == null) { 37 | throw new IllegalArgumentException("Path component is undefined"); 38 | } 39 | String path = uri.getPath(); 40 | if (!path.startsWith("/")) { 41 | throw new IllegalArgumentException("Path component should start with '/'"); 42 | } 43 | if (path.substring(1).contains("/")) { 44 | throw new IllegalArgumentException("Path component should contain '/' other than at the start"); 45 | } 46 | if (path.contains("..")) { 47 | throw new IllegalArgumentException("Invalid path component"); 48 | } 49 | 50 | if (uri.getQuery() != null) { 51 | throw new IllegalArgumentException("Query component present"); 52 | } 53 | if (uri.getFragment() != null) { 54 | throw new IllegalArgumentException("Fragment component present"); 55 | } 56 | } 57 | 58 | private Optional resolveModule(String name, ModuleLayer l) { 59 | var found = l.configuration().modules().stream() 60 | .filter(r -> r.name().equals(name)) 61 | .findFirst(); 62 | if (found.isPresent()) { 63 | return found; 64 | } 65 | 66 | for (var p : l.parents()) { 67 | var r = resolveModule(name, p); 68 | if (r.isPresent()) { 69 | return r; 70 | } 71 | } 72 | 73 | return Optional.empty(); 74 | } 75 | 76 | @Override 77 | public ModuleFileSystem newFileSystem(URI uri, Map env) throws IOException { 78 | checkUri(uri); 79 | 80 | var layer = env.containsKey("layer") ? (ModuleLayer) env.get("layer") : ModuleLayer.boot(); 81 | var moduleLocation = env.containsKey("location") ? (URI) env.get("location") : null; 82 | String moduleName = uri.getPath().substring(1); 83 | 84 | if (moduleLocation == null) { 85 | var loc = resolveModule(moduleName, layer) 86 | .orElseThrow(() -> new FileSystemNotFoundException( 87 | "Module " + moduleName + " was not resolved")); 88 | moduleLocation = loc.reference().location().orElseThrow(() -> new IllegalArgumentException( 89 | "Location of module " + moduleName + " is unknown")); 90 | } 91 | 92 | var scheme = moduleLocation.getScheme(); 93 | var fs = Stream.of( 94 | JrtModuleFileSystem.create(moduleName, this, uri, moduleLocation), 95 | JarModuleFileSystem.create(moduleName, this, moduleLocation), 96 | ExplodedModuleFileSystem.create(moduleName, this, moduleLocation)) 97 | .flatMap(Optional::stream) 98 | .findFirst() 99 | .orElseThrow(() -> new IllegalArgumentException( 100 | "Unsupported module file system type " + scheme)); 101 | filesystems.put(moduleName, fs); 102 | return fs; 103 | } 104 | 105 | @Override 106 | public FileSystem getFileSystem(URI uri) { 107 | checkUri(uri); 108 | 109 | synchronized (filesystems) { 110 | var moduleName = uri.getPath().substring(1); 111 | var fs = filesystems.get(moduleName); 112 | if (fs == null) { 113 | throw new FileSystemNotFoundException("No FileSystem for module " + moduleName + " found"); 114 | } 115 | 116 | if (!fs.isOpen()) { 117 | filesystems.remove(moduleName); 118 | throw new FileSystemNotFoundException("Existing FileSystem for module " + moduleName + " is closed"); 119 | } 120 | 121 | return fs; 122 | } 123 | } 124 | 125 | @Override 126 | public Path getPath(URI uri) { 127 | var path = uri.getPath().substring(1); 128 | var moduleName = path; 129 | var moduleNameEnd = path.indexOf("/"); 130 | if (moduleNameEnd != -1) { 131 | moduleName = moduleName.substring(0, moduleNameEnd); 132 | } 133 | var inModulePath = path.substring(moduleNameEnd + 1); 134 | 135 | try { 136 | var fs = getFileSystem(URI.create("module:/" + moduleName)); 137 | return fs.getPath(inModulePath); 138 | } catch (FileSystemNotFoundException e) { 139 | try { 140 | var fs = newFileSystem(URI.create("module:/" + moduleName), Map.of()); 141 | return fs.getPath(inModulePath); 142 | } catch (IOException ioException) { 143 | throw new UncheckedIOException(ioException); 144 | } 145 | } 146 | 147 | } 148 | 149 | @Override 150 | public SeekableByteChannel newByteChannel(Path path, Set options, FileAttribute... attrs) throws IOException { 151 | if (!options.isEmpty() && !options.equals(Set.of(StandardOpenOption.READ))) { 152 | throw new UnsupportedOperationException(); 153 | } 154 | 155 | var mp = ((ModulePath) path); 156 | return mp.getModuleFileSystem().getBaseProvider().newByteChannel(mp.getWrappedPath(), options, attrs); 157 | } 158 | 159 | @Override 160 | public DirectoryStream newDirectoryStream(Path dir, DirectoryStream.Filter filter) throws IOException { 161 | var mp = ((ModulePath) dir); 162 | var ds = mp.getModuleFileSystem().getBaseProvider().newDirectoryStream(mp.getWrappedPath(), filter); 163 | return new ModuleDirectoryStream(mp.getModuleFileSystem(), ds); 164 | } 165 | 166 | private ModulePath getModulePath(Path path) { 167 | Objects.requireNonNull(path, "path"); 168 | if (!(path instanceof ModulePath)) { 169 | throw new ProviderMismatchException(); 170 | } 171 | return (ModulePath) path; 172 | } 173 | 174 | private FileSystemProvider getBaseProvider(Path path) { 175 | return getModulePath(path).getModuleFileSystem().getBaseProvider(); 176 | } 177 | 178 | @Override 179 | public void createDirectory(Path dir, FileAttribute... attrs) throws IOException { 180 | getBaseProvider(dir).createDirectory(getModulePath(dir).getWrappedPath(), attrs); 181 | } 182 | 183 | @Override 184 | public void delete(Path path) throws IOException { 185 | getBaseProvider(path).delete(getModulePath(path).getWrappedPath()); 186 | } 187 | 188 | @Override 189 | public void copy(Path source, Path target, CopyOption... options) throws IOException { 190 | getBaseProvider(source).copy(getModulePath(source).getWrappedPath(), 191 | getModulePath(target).getWrappedPath(), options); 192 | } 193 | 194 | @Override 195 | public void move(Path source, Path target, CopyOption... options) throws IOException { 196 | getBaseProvider(source).move(getModulePath(source).getWrappedPath(), 197 | getModulePath(target).getWrappedPath(), options); 198 | } 199 | 200 | @Override 201 | public boolean isSameFile(Path path, Path path2) throws IOException { 202 | return getBaseProvider(path).isSameFile( 203 | getModulePath(path).getWrappedPath(), 204 | getModulePath(path2).getWrappedPath()); 205 | } 206 | 207 | @Override 208 | public boolean isHidden(Path path) throws IOException { 209 | return getBaseProvider(path).isHidden(getModulePath(path).getWrappedPath()); 210 | } 211 | 212 | @Override 213 | public FileStore getFileStore(Path path) throws IOException { 214 | if (!Files.exists(path)) { 215 | throw new NoSuchFileException(path.toString()); 216 | } 217 | 218 | return getModulePath(path).getFileSystem().getFileStores().iterator().next(); 219 | } 220 | 221 | @Override 222 | public void checkAccess(Path path, AccessMode... modes) throws IOException { 223 | getBaseProvider(path).checkAccess(getModulePath(path).getWrappedPath(), modes); 224 | } 225 | 226 | @Override 227 | public V getFileAttributeView(Path path, Class type, LinkOption... options) { 228 | return getBaseProvider(path).getFileAttributeView(getModulePath(path).getWrappedPath(), type, options); 229 | } 230 | 231 | @Override 232 | public A readAttributes(Path path, Class type, LinkOption... options) throws IOException { 233 | return getBaseProvider(path).readAttributes(getModulePath(path).getWrappedPath(), type, options); 234 | } 235 | 236 | @Override 237 | public Map readAttributes(Path path, String attributes, LinkOption... options) throws IOException { 238 | return getBaseProvider(path).readAttributes(getModulePath(path).getWrappedPath(), attributes, options); 239 | } 240 | 241 | @Override 242 | public void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException { 243 | getBaseProvider(path).setAttribute(getModulePath(path).getWrappedPath(), attribute, value, options); 244 | } 245 | } 246 | --------------------------------------------------------------------------------