-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor common SPI logic, handle SPI registrations with missing Enso…
… library (#11722) - Closes #11707 by filtering out SPI registrations that cannot load their corresponding Enso type.
- Loading branch information
Showing
34 changed files
with
232 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 17 additions & 32 deletions
49
std-bits/base/src/main/java/org/enso/base/enso_cloud/DataLinkSPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,38 @@ | ||
package org.enso.base.enso_cloud; | ||
|
||
import java.util.ServiceLoader; | ||
import java.util.stream.Collectors; | ||
import org.enso.base.polyglot.EnsoMeta; | ||
import java.util.Objects; | ||
import org.enso.base.spi.EnsoService; | ||
import org.enso.base.spi.EnsoServiceLoader; | ||
import org.graalvm.polyglot.Value; | ||
|
||
/** | ||
* An interface for data link parser providers. A class providing this interface can register an | ||
* Enso type that defines how to `parse` a specific type of datalink. The `parse` method on that | ||
* type should return a configured datalink instance that can later be `read`. | ||
*/ | ||
public abstract class DataLinkSPI { | ||
private static final ServiceLoader<DataLinkSPI> loader = | ||
ServiceLoader.load(DataLinkSPI.class, DataLinkSPI.class.getClassLoader()); | ||
public abstract class DataLinkSPI extends EnsoService { | ||
private static final EnsoServiceLoader<DataLinkSPI> loader = | ||
EnsoServiceLoader.load(DataLinkSPI.class); | ||
|
||
public void reload() { | ||
loader.reload(); | ||
} | ||
|
||
public static Value findDataLinkType(String name) { | ||
var providers = | ||
loader.stream().filter(provider -> provider.get().getLinkTypeName().equals(name)).toList(); | ||
if (providers.isEmpty()) { | ||
Objects.requireNonNull(name, "name must not be null/Nothing."); | ||
var found = | ||
loader.findSingleProvider(provider -> name.equals(provider.getLinkTypeName()), name); | ||
if (found == null) { | ||
return null; | ||
} | ||
|
||
if (providers.size() > 1) { | ||
var modules = | ||
providers.stream() | ||
.map(provider -> provider.get().getModuleName()) | ||
.collect(Collectors.joining(", ")); | ||
throw new IllegalStateException( | ||
"Error: Multiple Data Link providers found for type: " | ||
+ name | ||
+ ". The clashing definitions are in the following modules: " | ||
+ modules | ||
+ "."); | ||
} | ||
|
||
return providers.get(0).get().getTypeObject(); | ||
return found.getTypeObject(); | ||
} | ||
|
||
public Value getTypeObject() { | ||
return EnsoMeta.getType(getModuleName(), getTypeName()); | ||
} | ||
|
||
protected abstract String getModuleName(); | ||
|
||
protected abstract String getTypeName(); | ||
|
||
/** | ||
* Defines the name of the data link type associated with this SPI registration. | ||
* | ||
* <p>This is the same value as the `type` property of the corresponding variant in | ||
* `dataLinkSchema.json`. | ||
*/ | ||
protected abstract String getLinkTypeName(); | ||
} |
2 changes: 1 addition & 1 deletion
2
.../base/enso_cloud/EnsoFileDataLinkSPI.java → ...base/enso_cloud/EnsoFileDataLinkImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../enso/base/file_format/ByteFormatSPI.java → .../base/file_format/ByteFileFormatImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 13 additions & 36 deletions
49
std-bits/base/src/main/java/org/enso/base/file_format/FileFormatSPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../enso/base/file_format/JSONFormatSPI.java → .../base/file_format/JSONFileFormatImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../enso/base/file_format/TextFormatSPI.java → .../base/file_format/TextFileFormatImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...g/enso/base/file_format/XMLFormatSPI.java → ...o/base/file_format/XMLFileFormatImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 22 additions & 24 deletions
46
std-bits/base/src/main/java/org/enso/base/file_system/FileSystemSPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,41 @@ | ||
package org.enso.base.file_system; | ||
|
||
import java.util.ServiceLoader; | ||
import org.enso.base.polyglot.EnsoMeta; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import org.enso.base.spi.EnsoService; | ||
import org.enso.base.spi.EnsoServiceLoader; | ||
import org.graalvm.polyglot.Value; | ||
|
||
public abstract class FileSystemSPI { | ||
private static final ServiceLoader<org.enso.base.file_system.FileSystemSPI> loader = | ||
ServiceLoader.load( | ||
org.enso.base.file_system.FileSystemSPI.class, | ||
org.enso.base.file_format.FileFormatSPI.class.getClassLoader()); | ||
public abstract class FileSystemSPI extends EnsoService { | ||
private static final EnsoServiceLoader<FileSystemSPI> loader = | ||
EnsoServiceLoader.load(FileSystemSPI.class); | ||
|
||
public static Value get_type(String protocol, boolean refresh) { | ||
Objects.requireNonNull(protocol, "protocol must not be null/Nothing."); | ||
|
||
if (refresh) { | ||
loader.reload(); | ||
} | ||
|
||
var first = | ||
loader.stream() | ||
.filter(provider -> provider.get().getProtocol().equals(protocol)) | ||
.findFirst(); | ||
return first | ||
.map(fileSystemSPIProvider -> fileSystemSPIProvider.get().getTypeObject()) | ||
.orElse(null); | ||
var found = | ||
loader.findSingleProvider(provider -> protocol.equals(provider.getProtocol()), protocol); | ||
if (found == null) { | ||
return null; | ||
} | ||
return found.getTypeObject(); | ||
} | ||
|
||
public static Value[] get_types(boolean refresh) { | ||
public static List<Value> get_types(boolean refresh) { | ||
if (refresh) { | ||
loader.reload(); | ||
} | ||
return loader.stream().map(provider -> provider.get().getTypeObject()).toArray(Value[]::new); | ||
} | ||
|
||
public Value getTypeObject() { | ||
return EnsoMeta.getType(getModuleName(), getTypeName()); | ||
return loader.getTypeObjects(); | ||
} | ||
|
||
protected abstract String getModuleName(); | ||
|
||
protected abstract String getTypeName(); | ||
|
||
/** | ||
* Defines the protocol that this file system provider is responsible for. | ||
* | ||
* <p>For example "enso" protocol is used for handling Enso Cloud `enso://` paths. | ||
*/ | ||
protected abstract String getProtocol(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...enso/base/read/BaseReadManyReturnSPI.java → ...nso/base/read/BaseReadManyReturnImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.