@@ -136,7 +136,7 @@ public class SftpFileSystemProvider extends FileSystemProvider {
136136
137137 protected final Logger log ;
138138
139- private final SshClient clientInstance ;
139+ private SshClient clientInstance ;
140140 private final SftpClientFactory factory ;
141141 private final SftpVersionSelector versionSelector ;
142142 private final SftpErrorDataHandler errorDataHandler ;
@@ -183,11 +183,6 @@ public SftpFileSystemProvider(SshClient client, SftpClientFactory factory,
183183 this .factory = factory ;
184184 this .versionSelector = selector ;
185185 this .errorDataHandler = errorDataHandler ;
186- if (client == null ) {
187- // TODO: make this configurable using system properties
188- client = SshClient .setUpDefaultClient ();
189- client .start ();
190- }
191186 this .clientInstance = client ;
192187 }
193188
@@ -204,10 +199,20 @@ public SftpErrorDataHandler getSftpErrorDataHandler() {
204199 return errorDataHandler ;
205200 }
206201
207- public final SshClient getClientInstance () {
202+ public final synchronized SshClient getClientInstance () {
203+ if (clientInstance == null ) {
204+ clientInstance = createClient ();
205+ }
208206 return clientInstance ;
209207 }
210208
209+ private SshClient createClient () {
210+ // TODO: make this configurable using system properties
211+ SshClient client = SshClient .setUpDefaultClient ();
212+ client .start ();
213+ return client ;
214+ }
215+
211216 public SftpClientFactory getSftpClientFactory () {
212217 return factory ;
213218 }
@@ -561,10 +566,30 @@ public SftpFileSystem getFileSystem(String id) {
561566 }
562567 }
563568
569+ private SftpFileSystem getOrCreateFileSystem (URI uri ) throws IOException {
570+ String id = getFileSystemIdentifier (uri );
571+ synchronized (fileSystems ) {
572+ SftpFileSystem fs = fileSystems .get (id );
573+ if (fs == null ) {
574+ fs = newFileSystem (uri , Collections .emptyMap ());
575+ }
576+ return fs ;
577+ }
578+ }
579+
564580 @ Override
565581 public Path getPath (URI uri ) {
566- FileSystem fs = getFileSystem (uri );
567- return fs .getPath (uri .getPath ());
582+ if (!getScheme ().equalsIgnoreCase (uri .getScheme ())) {
583+ throw new IllegalArgumentException ("Not a " + getScheme () + " URI: " + uri );
584+ }
585+ try {
586+ FileSystem fs = getOrCreateFileSystem (uri );
587+ return fs .getPath (uri .getPath ());
588+ } catch (IOException e ) {
589+ FileSystemNotFoundException fe = new FileSystemNotFoundException ("No file system for URI " + uri );
590+ fe .initCause (e );
591+ throw fe ;
592+ }
568593 }
569594
570595 @ Override
0 commit comments