mirror of
https://github.com/apple/pkl.git
synced 2026-06-08 23:02:45 +02:00
Untangle external reader code (#776)
- move the following classes into package externalreader: - ExternalModuleResolver - ExternalResourceResolver - MessageTransportModuleResolver (renamed to ExternalModuleResolverImpl, made package-private) - MessageTransportResourceResolver (renamed to ExternalResourceResolverImpl, made package-private) - replace interface ExternalModuleResolver.Spec with record ExternalModuleReaderSpec - replace interface ExternalResourceResolver.Spec with record ExternalResourceReaderSpec - translate between messaging.ResourceReaderSpec and ExternalResourceReaderSpec (eliminates dependency from messaging on higher layer) - translate between messaging.ResourceResolverSpec and ExternalResourceResolverSpec (eliminates dependency from messaging on higher layer) - add ServerMessages.ExternalReader and translate between this message component and the PklEvaluatorSettings.ExternalReader API - add ServerMessages.Proxy and translate between this message component and the PklEvaluatorSettings.Proxy API - change type of CreateEvaluatorRequest.allowedModules/allowedResources from List<Pattern>? to List<String>? - removes a lot of code - should not need to create a Pattern object to send a message - deprecate method evaluatorSettings.PklEvaluatorSettings.Proxy.create() - only seems useful internally, inlined Co-authored-by: Dan Chao <dan.chao@apple.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -137,6 +137,7 @@ public record PklEvaluatorSettings(
|
||||
}
|
||||
|
||||
public record Proxy(@Nullable URI address, @Nullable List<String> noProxy) {
|
||||
@Deprecated(forRemoval = true)
|
||||
public static Proxy create(@Nullable String address, @Nullable List<String> noProxy) {
|
||||
URI addressUri;
|
||||
try {
|
||||
@@ -147,14 +148,19 @@ public record PklEvaluatorSettings(
|
||||
return new Proxy(addressUri, noProxy);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static @Nullable Proxy parse(Value input) {
|
||||
if (input instanceof PNull) {
|
||||
return null;
|
||||
} else if (input instanceof PObject proxy) {
|
||||
var address = (String) proxy.get("address");
|
||||
@SuppressWarnings("unchecked")
|
||||
var noProxy = (List<String>) proxy.get("noProxy");
|
||||
return create(address, noProxy);
|
||||
try {
|
||||
var addressUri = address == null ? null : new URI(address);
|
||||
return new Proxy(addressUri, noProxy);
|
||||
} catch (URISyntaxException e) {
|
||||
throw new PklException(ErrorMessages.create("invalidUri", address));
|
||||
}
|
||||
} else {
|
||||
throw PklBugException.unreachableCode();
|
||||
}
|
||||
|
||||
+6
-11
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -13,24 +13,19 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.core.module;
|
||||
package org.pkl.core.externalreader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import org.pkl.core.SecurityManager;
|
||||
import org.pkl.core.SecurityManagerException;
|
||||
import org.pkl.core.messaging.MessageTransport;
|
||||
import org.pkl.core.module.PathElement;
|
||||
|
||||
public interface ExternalModuleResolver {
|
||||
|
||||
interface Spec {
|
||||
boolean hasHierarchicalUris();
|
||||
|
||||
boolean isGlobbable();
|
||||
|
||||
boolean isLocal();
|
||||
|
||||
String scheme();
|
||||
static ExternalModuleResolver of(MessageTransport transport, long evaluatorId) {
|
||||
return new ExternalModuleResolverImpl(transport, evaluatorId);
|
||||
}
|
||||
|
||||
String resolveModule(SecurityManager securityManager, URI uri)
|
||||
+7
-5
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.core.messaging;
|
||||
package org.pkl.core.externalreader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
@@ -26,21 +26,23 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Future;
|
||||
import org.pkl.core.SecurityManager;
|
||||
import org.pkl.core.SecurityManagerException;
|
||||
import org.pkl.core.messaging.MessageTransport;
|
||||
import org.pkl.core.messaging.MessageTransports;
|
||||
import org.pkl.core.messaging.Messages.ListModulesRequest;
|
||||
import org.pkl.core.messaging.Messages.ListModulesResponse;
|
||||
import org.pkl.core.messaging.Messages.ReadModuleRequest;
|
||||
import org.pkl.core.messaging.Messages.ReadModuleResponse;
|
||||
import org.pkl.core.module.ExternalModuleResolver;
|
||||
import org.pkl.core.messaging.ProtocolException;
|
||||
import org.pkl.core.module.PathElement;
|
||||
|
||||
public class MessageTransportModuleResolver implements ExternalModuleResolver {
|
||||
final class ExternalModuleResolverImpl implements ExternalModuleResolver {
|
||||
private final MessageTransport transport;
|
||||
private final long evaluatorId;
|
||||
private final Map<URI, Future<String>> readResponses = new ConcurrentHashMap<>();
|
||||
private final Map<URI, Future<List<PathElement>>> listResponses = new ConcurrentHashMap<>();
|
||||
private final Random requestIdGenerator = new Random();
|
||||
|
||||
public MessageTransportModuleResolver(MessageTransport transport, long evaluatorId) {
|
||||
ExternalModuleResolverImpl(MessageTransport transport, long evaluatorId) {
|
||||
this.transport = transport;
|
||||
this.evaluatorId = evaluatorId;
|
||||
}
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,35 +23,33 @@ import org.pkl.core.util.Nullable;
|
||||
final class ExternalReaderMessages {
|
||||
private ExternalReaderMessages() {}
|
||||
|
||||
public record InitializeModuleReaderRequest(long requestId, String scheme)
|
||||
implements Server.Request {
|
||||
record InitializeModuleReaderRequest(long requestId, String scheme) implements Server.Request {
|
||||
public Type type() {
|
||||
return Type.INITIALIZE_MODULE_READER_REQUEST;
|
||||
}
|
||||
}
|
||||
|
||||
public record InitializeResourceReaderRequest(long requestId, String scheme)
|
||||
implements Server.Request {
|
||||
record InitializeResourceReaderRequest(long requestId, String scheme) implements Server.Request {
|
||||
public Type type() {
|
||||
return Type.INITIALIZE_RESOURCE_READER_REQUEST;
|
||||
}
|
||||
}
|
||||
|
||||
public record InitializeModuleReaderResponse(long requestId, @Nullable ModuleReaderSpec spec)
|
||||
record InitializeModuleReaderResponse(long requestId, @Nullable ModuleReaderSpec spec)
|
||||
implements Client.Response {
|
||||
public Type type() {
|
||||
return Type.INITIALIZE_MODULE_READER_RESPONSE;
|
||||
}
|
||||
}
|
||||
|
||||
public record InitializeResourceReaderResponse(long requestId, @Nullable ResourceReaderSpec spec)
|
||||
record InitializeResourceReaderResponse(long requestId, @Nullable ResourceReaderSpec spec)
|
||||
implements Client.Response {
|
||||
public Type type() {
|
||||
return Type.INITIALIZE_RESOURCE_READER_RESPONSE;
|
||||
}
|
||||
}
|
||||
|
||||
public record CloseExternalProcess() implements Server.OneWay {
|
||||
record CloseExternalProcess() implements Server.OneWay {
|
||||
public Type type() {
|
||||
return Type.CLOSE_EXTERNAL_PROCESS;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,8 +17,6 @@ package org.pkl.core.externalreader;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.pkl.core.evaluatorSettings.PklEvaluatorSettings.ExternalReader;
|
||||
import org.pkl.core.module.ExternalModuleResolver;
|
||||
import org.pkl.core.resource.ExternalResourceResolver;
|
||||
import org.pkl.core.util.Nullable;
|
||||
|
||||
/** An external process that reads Pkl modules and resources. */
|
||||
@@ -56,7 +54,8 @@ public interface ExternalReaderProcess extends AutoCloseable {
|
||||
* @throws IllegalStateException if this process has already been {@linkplain #close closed}
|
||||
* @throws IOException if an I/O error occurs
|
||||
*/
|
||||
ExternalModuleResolver.@Nullable Spec getModuleReaderSpec(String scheme) throws IOException;
|
||||
@Nullable
|
||||
ModuleReaderSpec getModuleReaderSpec(String scheme) throws IOException;
|
||||
|
||||
/**
|
||||
* Returns the spec, if available, of this process's resource reader with the given scheme.
|
||||
@@ -64,7 +63,8 @@ public interface ExternalReaderProcess extends AutoCloseable {
|
||||
* @throws IllegalStateException if this process has already been {@linkplain #close closed}
|
||||
* @throws IOException if an I/O error occurs
|
||||
*/
|
||||
ExternalResourceResolver.@Nullable Spec getResourceReaderSpec(String scheme) throws IOException;
|
||||
@Nullable
|
||||
ResourceReaderSpec getResourceReaderSpec(String scheme) throws IOException;
|
||||
|
||||
/**
|
||||
* Closes this process, releasing any associated resources.
|
||||
|
||||
@@ -30,12 +30,8 @@ import javax.annotation.concurrent.GuardedBy;
|
||||
import org.pkl.core.evaluatorSettings.PklEvaluatorSettings.ExternalReader;
|
||||
import org.pkl.core.externalreader.ExternalReaderMessages.*;
|
||||
import org.pkl.core.messaging.MessageTransport;
|
||||
import org.pkl.core.messaging.MessageTransportModuleResolver;
|
||||
import org.pkl.core.messaging.MessageTransportResourceResolver;
|
||||
import org.pkl.core.messaging.MessageTransports;
|
||||
import org.pkl.core.messaging.ProtocolException;
|
||||
import org.pkl.core.module.ExternalModuleResolver;
|
||||
import org.pkl.core.resource.ExternalResourceResolver;
|
||||
import org.pkl.core.util.ErrorMessages;
|
||||
import org.pkl.core.util.LateInit;
|
||||
import org.pkl.core.util.Nullable;
|
||||
@@ -46,9 +42,9 @@ final class ExternalReaderProcessImpl implements ExternalReaderProcess {
|
||||
|
||||
private final ExternalReader spec;
|
||||
private final @Nullable String logPrefix;
|
||||
private final Map<String, Future<ExternalModuleResolver.@Nullable Spec>>
|
||||
initializeModuleReaderResponses = new ConcurrentHashMap<>();
|
||||
private final Map<String, Future<ExternalResourceResolver.@Nullable Spec>>
|
||||
private final Map<String, Future<@Nullable ModuleReaderSpec>> initializeModuleReaderResponses =
|
||||
new ConcurrentHashMap<>();
|
||||
private final Map<String, Future<@Nullable ResourceReaderSpec>>
|
||||
initializeResourceReaderResponses = new ConcurrentHashMap<>();
|
||||
private final Random requestIdGenerator = new Random();
|
||||
|
||||
@@ -80,13 +76,13 @@ final class ExternalReaderProcessImpl implements ExternalReaderProcess {
|
||||
@Override
|
||||
public ExternalModuleResolver getModuleResolver(long evaluatorId)
|
||||
throws ExternalReaderProcessException {
|
||||
return new MessageTransportModuleResolver(getTransport(), evaluatorId);
|
||||
return ExternalModuleResolver.of(getTransport(), evaluatorId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExternalResourceResolver getResourceResolver(long evaluatorId)
|
||||
throws ExternalReaderProcessException {
|
||||
return new MessageTransportResourceResolver(getTransport(), evaluatorId);
|
||||
return ExternalResourceResolver.of(getTransport(), evaluatorId);
|
||||
}
|
||||
|
||||
private MessageTransport getTransport() throws ExternalReaderProcessException {
|
||||
@@ -175,13 +171,12 @@ final class ExternalReaderProcessImpl implements ExternalReaderProcess {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExternalModuleResolver.@Nullable Spec getModuleReaderSpec(String uriScheme)
|
||||
throws IOException {
|
||||
public ModuleReaderSpec getModuleReaderSpec(String uriScheme) throws IOException {
|
||||
return MessageTransports.resolveFuture(
|
||||
initializeModuleReaderResponses.computeIfAbsent(
|
||||
uriScheme,
|
||||
(scheme) -> {
|
||||
var future = new CompletableFuture<ExternalModuleResolver.@Nullable Spec>();
|
||||
var future = new CompletableFuture<@Nullable ModuleReaderSpec>();
|
||||
var request =
|
||||
new InitializeModuleReaderRequest(requestIdGenerator.nextLong(), scheme);
|
||||
try {
|
||||
@@ -190,7 +185,15 @@ final class ExternalReaderProcessImpl implements ExternalReaderProcess {
|
||||
request,
|
||||
(response) -> {
|
||||
if (response instanceof InitializeModuleReaderResponse resp) {
|
||||
future.complete(resp.spec());
|
||||
var spec =
|
||||
resp.spec() == null
|
||||
? null
|
||||
: new ModuleReaderSpec(
|
||||
resp.spec().scheme(),
|
||||
resp.spec().hasHierarchicalUris(),
|
||||
resp.spec().isLocal(),
|
||||
resp.spec().isGlobbable());
|
||||
future.complete(spec);
|
||||
} else {
|
||||
future.completeExceptionally(
|
||||
new ProtocolException("unexpected response"));
|
||||
@@ -204,13 +207,12 @@ final class ExternalReaderProcessImpl implements ExternalReaderProcess {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExternalResourceResolver.@Nullable Spec getResourceReaderSpec(String uriScheme)
|
||||
throws IOException {
|
||||
public ResourceReaderSpec getResourceReaderSpec(String uriScheme) throws IOException {
|
||||
return MessageTransports.resolveFuture(
|
||||
initializeResourceReaderResponses.computeIfAbsent(
|
||||
uriScheme,
|
||||
(scheme) -> {
|
||||
var future = new CompletableFuture<ExternalResourceResolver.@Nullable Spec>();
|
||||
var future = new CompletableFuture<@Nullable ResourceReaderSpec>();
|
||||
var request =
|
||||
new InitializeResourceReaderRequest(requestIdGenerator.nextLong(), scheme);
|
||||
try {
|
||||
@@ -220,7 +222,14 @@ final class ExternalReaderProcessImpl implements ExternalReaderProcess {
|
||||
(response) -> {
|
||||
log(response.toString());
|
||||
if (response instanceof InitializeResourceReaderResponse resp) {
|
||||
future.complete(resp.spec());
|
||||
var spec =
|
||||
resp.spec() == null
|
||||
? null
|
||||
: new ResourceReaderSpec(
|
||||
resp.spec().scheme(),
|
||||
resp.spec().hasHierarchicalUris(),
|
||||
resp.spec().isGlobbable());
|
||||
future.complete(spec);
|
||||
} else {
|
||||
future.completeExceptionally(
|
||||
new ProtocolException("unexpected response"));
|
||||
|
||||
+5
-9
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.core.resource;
|
||||
package org.pkl.core.externalreader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
@@ -21,16 +21,12 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.pkl.core.SecurityManager;
|
||||
import org.pkl.core.SecurityManagerException;
|
||||
import org.pkl.core.messaging.MessageTransport;
|
||||
import org.pkl.core.module.PathElement;
|
||||
|
||||
public interface ExternalResourceResolver {
|
||||
|
||||
interface Spec {
|
||||
boolean hasHierarchicalUris();
|
||||
|
||||
boolean isGlobbable();
|
||||
|
||||
String scheme();
|
||||
static ExternalResourceResolver of(MessageTransport transport, long evaluatorId) {
|
||||
return new ExternalResourceResolverImpl(transport, evaluatorId);
|
||||
}
|
||||
|
||||
Optional<Object> read(URI uri) throws IOException;
|
||||
+7
-5
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.core.messaging;
|
||||
package org.pkl.core.externalreader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
@@ -27,19 +27,21 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Future;
|
||||
import org.pkl.core.SecurityManager;
|
||||
import org.pkl.core.SecurityManagerException;
|
||||
import org.pkl.core.messaging.MessageTransport;
|
||||
import org.pkl.core.messaging.MessageTransports;
|
||||
import org.pkl.core.messaging.Messages.*;
|
||||
import org.pkl.core.messaging.ProtocolException;
|
||||
import org.pkl.core.module.PathElement;
|
||||
import org.pkl.core.resource.ExternalResourceResolver;
|
||||
import org.pkl.core.resource.Resource;
|
||||
|
||||
public class MessageTransportResourceResolver implements ExternalResourceResolver {
|
||||
final class ExternalResourceResolverImpl implements ExternalResourceResolver {
|
||||
private final MessageTransport transport;
|
||||
private final long evaluatorId;
|
||||
private final Map<URI, Future<byte[]>> readResponses = new ConcurrentHashMap<>();
|
||||
private final Map<URI, Future<List<PathElement>>> listResponses = new ConcurrentHashMap<>();
|
||||
private final Random requestIdGenerator = new Random();
|
||||
|
||||
public MessageTransportResourceResolver(MessageTransport transport, long evaluatorId) {
|
||||
ExternalResourceResolverImpl(MessageTransport transport, long evaluatorId) {
|
||||
this.transport = transport;
|
||||
this.evaluatorId = evaluatorId;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.core.externalreader;
|
||||
|
||||
public record ModuleReaderSpec(
|
||||
String scheme, boolean hasHierarchicalUris, boolean isLocal, boolean isGlobbable) {}
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.core.externalreader;
|
||||
|
||||
public record ResourceReaderSpec(String scheme, boolean hasHierarchicalUris, boolean isGlobbable) {}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,20 +20,17 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import org.pkl.core.messaging.Message.*;
|
||||
import org.pkl.core.module.ExternalModuleResolver;
|
||||
import org.pkl.core.module.PathElement;
|
||||
import org.pkl.core.resource.ExternalResourceResolver;
|
||||
import org.pkl.core.util.Nullable;
|
||||
|
||||
public final class Messages {
|
||||
private Messages() {}
|
||||
|
||||
public record ModuleReaderSpec(
|
||||
String scheme, boolean hasHierarchicalUris, boolean isLocal, boolean isGlobbable)
|
||||
implements ExternalModuleResolver.Spec {}
|
||||
String scheme, boolean hasHierarchicalUris, boolean isLocal, boolean isGlobbable) {}
|
||||
|
||||
public record ResourceReaderSpec(String scheme, boolean hasHierarchicalUris, boolean isGlobbable)
|
||||
implements ExternalResourceResolver.Spec {}
|
||||
public record ResourceReaderSpec(
|
||||
String scheme, boolean hasHierarchicalUris, boolean isGlobbable) {}
|
||||
|
||||
public record ListResourcesRequest(long requestId, long evaluatorId, URI uri)
|
||||
implements Server.Request {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -28,6 +28,7 @@ import java.util.Optional;
|
||||
import java.util.ServiceLoader;
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
import org.pkl.core.Closeables;
|
||||
import org.pkl.core.externalreader.ExternalModuleResolver;
|
||||
import org.pkl.core.externalreader.ExternalReaderProcess;
|
||||
import org.pkl.core.externalreader.ExternalReaderProcessException;
|
||||
import org.pkl.core.util.ErrorMessages;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -29,7 +29,9 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.pkl.core.SecurityManager;
|
||||
import org.pkl.core.SecurityManagerException;
|
||||
import org.pkl.core.externalreader.ExternalModuleResolver;
|
||||
import org.pkl.core.externalreader.ExternalReaderProcessException;
|
||||
import org.pkl.core.externalreader.ModuleReaderSpec;
|
||||
import org.pkl.core.packages.Dependency;
|
||||
import org.pkl.core.packages.Dependency.LocalDependency;
|
||||
import org.pkl.core.packages.PackageAssetUri;
|
||||
@@ -130,7 +132,7 @@ public final class ModuleKeys {
|
||||
|
||||
/** Creates a module key for an externally read module. */
|
||||
public static ModuleKey externalResolver(
|
||||
URI uri, ExternalModuleResolver.Spec spec, ExternalModuleResolver resolver) {
|
||||
URI uri, ModuleReaderSpec spec, ExternalModuleResolver resolver) {
|
||||
return new ExternalResolver(uri, spec, resolver);
|
||||
}
|
||||
|
||||
@@ -778,10 +780,10 @@ public final class ModuleKeys {
|
||||
public static class ExternalResolver implements ModuleKey {
|
||||
|
||||
private final URI uri;
|
||||
private final ExternalModuleResolver.Spec spec;
|
||||
private final ModuleReaderSpec spec;
|
||||
private final ExternalModuleResolver resolver;
|
||||
|
||||
ExternalResolver(URI uri, ExternalModuleResolver.Spec spec, ExternalModuleResolver resolver) {
|
||||
ExternalResolver(URI uri, ModuleReaderSpec spec, ExternalModuleResolver resolver) {
|
||||
this.uri = uri;
|
||||
this.spec = spec;
|
||||
this.resolver = resolver;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -31,6 +31,8 @@ import org.pkl.core.SecurityManager;
|
||||
import org.pkl.core.SecurityManagerException;
|
||||
import org.pkl.core.externalreader.ExternalReaderProcess;
|
||||
import org.pkl.core.externalreader.ExternalReaderProcessException;
|
||||
import org.pkl.core.externalreader.ExternalResourceResolver;
|
||||
import org.pkl.core.externalreader.ResourceReaderSpec;
|
||||
import org.pkl.core.module.FileResolver;
|
||||
import org.pkl.core.module.ModulePathResolver;
|
||||
import org.pkl.core.module.PathElement;
|
||||
@@ -162,7 +164,7 @@ public final class ResourceReaders {
|
||||
|
||||
/** Returns a reader for external and client reader resources. */
|
||||
public static ResourceReader externalResolver(
|
||||
ExternalResourceResolver.Spec spec, ExternalResourceResolver resolver) {
|
||||
ResourceReaderSpec spec, ExternalResourceResolver resolver) {
|
||||
return new ExternalResolver(spec, resolver);
|
||||
}
|
||||
|
||||
@@ -691,11 +693,10 @@ public final class ResourceReaders {
|
||||
}
|
||||
|
||||
private static final class ExternalResolver implements ResourceReader {
|
||||
private final ExternalResourceResolver.Spec readerSpec;
|
||||
private final ResourceReaderSpec readerSpec;
|
||||
private final ExternalResourceResolver resolver;
|
||||
|
||||
public ExternalResolver(
|
||||
ExternalResourceResolver.Spec readerSpec, ExternalResourceResolver resolver) {
|
||||
public ExternalResolver(ResourceReaderSpec readerSpec, ExternalResourceResolver resolver) {
|
||||
this.readerSpec = readerSpec;
|
||||
this.resolver = resolver;
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ import org.pkl.core.util.IoUtils;
|
||||
public class VmImportAnalyzer {
|
||||
@TruffleBoundary
|
||||
public static ImportGraph analyze(URI[] moduleUris, VmContext context)
|
||||
throws IOException, SecurityManagerException {
|
||||
throws IOException, SecurityManagerException, ExternalReaderProcessException {
|
||||
var imports = new TreeMap<URI, Set<ImportGraph.Import>>();
|
||||
var resolvedImports = new TreeMap<URI, URI>();
|
||||
for (var moduleUri : moduleUris) {
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.net.URISyntaxException;
|
||||
import org.pkl.core.ImportGraph;
|
||||
import org.pkl.core.ImportGraph.Import;
|
||||
import org.pkl.core.SecurityManagerException;
|
||||
import org.pkl.core.externalreader.ExternalReaderProcessException;
|
||||
import org.pkl.core.packages.PackageLoadError;
|
||||
import org.pkl.core.runtime.AnalyzeModule;
|
||||
import org.pkl.core.runtime.VmContext;
|
||||
@@ -91,7 +92,10 @@ public final class AnalyzeNodes {
|
||||
try {
|
||||
var results = VmImportAnalyzer.analyze(uris, context);
|
||||
return importGraphFactory.create(results);
|
||||
} catch (IOException | SecurityManagerException | PackageLoadError e) {
|
||||
} catch (IOException
|
||||
| SecurityManagerException
|
||||
| PackageLoadError
|
||||
| ExternalReaderProcessException e) {
|
||||
throw exceptionBuilder().withCause(e).build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,7 +18,7 @@ package org.pkl.core.externalreader
|
||||
import java.net.URI
|
||||
import org.pkl.core.messaging.Messages.ModuleReaderSpec
|
||||
|
||||
/** An external module reader, to be used with [ExternalReaderRuntime]. */
|
||||
/** An external module reader, to be used with [ExternalReaderClient]. */
|
||||
interface ExternalModuleReader : ExternalReaderBase {
|
||||
val isLocal: Boolean
|
||||
|
||||
|
||||
+8
-7
@@ -19,14 +19,15 @@ import java.io.IOException
|
||||
import org.pkl.core.externalreader.ExternalReaderMessages.*
|
||||
import org.pkl.core.messaging.Message
|
||||
import org.pkl.core.messaging.MessageTransport
|
||||
import org.pkl.core.messaging.Messages
|
||||
import org.pkl.core.messaging.Messages.*
|
||||
import org.pkl.core.messaging.ProtocolException
|
||||
import org.pkl.core.util.Nullable
|
||||
|
||||
/** An implementation of the client side of the external reader flow */
|
||||
class ExternalReaderRuntime(
|
||||
private val moduleReaders: List<ExternalModuleReader>,
|
||||
private val resourceReaders: List<ExternalResourceReader>,
|
||||
class ExternalReaderClient(
|
||||
private val externalModuleReaders: List<ExternalModuleReader>,
|
||||
private val externalResourceReaders: List<ExternalResourceReader>,
|
||||
private val transport: MessageTransport,
|
||||
) {
|
||||
/** Close the runtime and its transport. */
|
||||
@@ -35,7 +36,7 @@ class ExternalReaderRuntime(
|
||||
}
|
||||
|
||||
private fun findModuleReader(scheme: String): @Nullable ExternalModuleReader? {
|
||||
for (moduleReader in moduleReaders) {
|
||||
for (moduleReader in externalModuleReaders) {
|
||||
if (moduleReader.scheme.equals(scheme, ignoreCase = true)) {
|
||||
return moduleReader
|
||||
}
|
||||
@@ -44,7 +45,7 @@ class ExternalReaderRuntime(
|
||||
}
|
||||
|
||||
private fun findResourceReader(scheme: String): @Nullable ExternalResourceReader? {
|
||||
for (resourceReader in resourceReaders) {
|
||||
for (resourceReader in externalResourceReaders) {
|
||||
if (resourceReader.scheme.equals(scheme, ignoreCase = true)) {
|
||||
return resourceReader
|
||||
}
|
||||
@@ -72,7 +73,7 @@ class ExternalReaderRuntime(
|
||||
Message.Type.INITIALIZE_MODULE_READER_REQUEST -> {
|
||||
val req = msg as InitializeModuleReaderRequest
|
||||
val reader = findModuleReader(req.scheme)
|
||||
var spec: @Nullable ModuleReaderSpec? = null
|
||||
var spec: Messages.ModuleReaderSpec? = null
|
||||
if (reader != null) {
|
||||
spec = reader.spec
|
||||
}
|
||||
@@ -81,7 +82,7 @@ class ExternalReaderRuntime(
|
||||
Message.Type.INITIALIZE_RESOURCE_READER_REQUEST -> {
|
||||
val req = msg as InitializeResourceReaderRequest
|
||||
val reader = findResourceReader(req.scheme)
|
||||
var spec: @Nullable ResourceReaderSpec? = null
|
||||
var spec: Messages.ResourceReaderSpec? = null
|
||||
if (reader != null) {
|
||||
spec = reader.spec
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,7 +18,7 @@ package org.pkl.core.externalreader
|
||||
import java.net.URI
|
||||
import org.pkl.core.messaging.Messages.ResourceReaderSpec
|
||||
|
||||
/** An external resource reader, to be used with [ExternalReaderRuntime]. */
|
||||
/** An external resource reader, to be used with [ExternalReaderClient]. */
|
||||
interface ExternalResourceReader : ExternalReaderBase {
|
||||
fun read(uri: URI): ByteArray
|
||||
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,7 +23,7 @@ import org.msgpack.core.MessagePack
|
||||
import org.pkl.core.externalreader.ExternalReaderMessages.*
|
||||
import org.pkl.core.messaging.*
|
||||
|
||||
class ExternalProcessProcessReaderMessagePackCodecTest {
|
||||
class MessagePackCodecTest {
|
||||
private val encoder: MessageEncoder
|
||||
private val decoder: MessageDecoder
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -25,10 +25,7 @@ import java.util.concurrent.Future
|
||||
import kotlin.random.Random
|
||||
import org.pkl.core.externalreader.ExternalReaderMessages.*
|
||||
import org.pkl.core.messaging.MessageTransport
|
||||
import org.pkl.core.messaging.MessageTransportModuleResolver
|
||||
import org.pkl.core.messaging.MessageTransportResourceResolver
|
||||
import org.pkl.core.messaging.MessageTransports
|
||||
import org.pkl.core.messaging.Messages.*
|
||||
import org.pkl.core.messaging.ProtocolException
|
||||
|
||||
class TestExternalReaderProcess(private val transport: MessageTransport) : ExternalReaderProcess {
|
||||
@@ -42,11 +39,11 @@ class TestExternalReaderProcess(private val transport: MessageTransport) : Exter
|
||||
transport.close()
|
||||
}
|
||||
|
||||
override fun getModuleResolver(evaluatorId: Long): MessageTransportModuleResolver =
|
||||
MessageTransportModuleResolver(transport, evaluatorId)
|
||||
override fun getModuleResolver(evaluatorId: Long): ExternalModuleResolver =
|
||||
ExternalModuleResolver.of(transport, evaluatorId)
|
||||
|
||||
override fun getResourceResolver(evaluatorId: Long): MessageTransportResourceResolver =
|
||||
MessageTransportResourceResolver(transport, evaluatorId)
|
||||
override fun getResourceResolver(evaluatorId: Long): ExternalResourceResolver =
|
||||
ExternalResourceResolver.of(transport, evaluatorId)
|
||||
|
||||
fun run() {
|
||||
try {
|
||||
@@ -69,7 +66,11 @@ class TestExternalReaderProcess(private val transport: MessageTransport) : Exter
|
||||
transport.send(request) { response ->
|
||||
when (response) {
|
||||
is InitializeModuleReaderResponse -> {
|
||||
complete(response.spec)
|
||||
val spec =
|
||||
response.spec?.let {
|
||||
ModuleReaderSpec(it.scheme, it.hasHierarchicalUris, it.isLocal, it.isGlobbable)
|
||||
}
|
||||
complete(spec)
|
||||
}
|
||||
else -> completeExceptionally(ProtocolException("unexpected response"))
|
||||
}
|
||||
@@ -86,7 +87,11 @@ class TestExternalReaderProcess(private val transport: MessageTransport) : Exter
|
||||
transport.send(request) { response ->
|
||||
when (response) {
|
||||
is InitializeResourceReaderResponse -> {
|
||||
complete(response.spec)
|
||||
val spec =
|
||||
response.spec?.let {
|
||||
ResourceReaderSpec(it.scheme, it.hasHierarchicalUris, it.isGlobbable)
|
||||
}
|
||||
complete(spec)
|
||||
}
|
||||
else -> completeExceptionally(ProtocolException("unexpected response"))
|
||||
}
|
||||
@@ -97,9 +102,9 @@ class TestExternalReaderProcess(private val transport: MessageTransport) : Exter
|
||||
|
||||
companion object {
|
||||
fun initializeTestHarness(
|
||||
moduleReaders: List<ExternalModuleReader>,
|
||||
resourceReaders: List<ExternalResourceReader>,
|
||||
): Pair<TestExternalReaderProcess, ExternalReaderRuntime> {
|
||||
externalModuleReaders: List<ExternalModuleReader>,
|
||||
externalResourceReaders: List<ExternalResourceReader>,
|
||||
): Pair<TestExternalReaderProcess, ExternalReaderClient> {
|
||||
val rxIn = PipedInputStream(10240)
|
||||
val rxOut = PipedOutputStream(rxIn)
|
||||
val txIn = PipedInputStream(10240)
|
||||
@@ -115,13 +120,14 @@ class TestExternalReaderProcess(private val transport: MessageTransport) : Exter
|
||||
ExternalReaderMessagePackEncoder(rxOut),
|
||||
) {}
|
||||
|
||||
val runtime = ExternalReaderRuntime(moduleReaders, resourceReaders, clientTransport)
|
||||
val client =
|
||||
ExternalReaderClient(externalModuleReaders, externalResourceReaders, clientTransport)
|
||||
val proc = TestExternalReaderProcess(serverTransport)
|
||||
|
||||
Thread(runtime::run).start()
|
||||
Thread(client::run).start()
|
||||
Thread(proc::run).start()
|
||||
|
||||
return proc to runtime
|
||||
return proc to client
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
Reference in New Issue
Block a user