Skip to content

Commit

Permalink
Continue adapter implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
fvilla committed Dec 29, 2024
1 parent fa1d4ba commit 3424fd9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ private List<Schema> findSchemata(Map<String, List<Schema>> schemata, String sch
return ret;
}

public void registerImportSchema(ServiceInfo serviceInfo) {
public Schema registerImportSchema(ServiceInfo serviceInfo) {

// create the schema
var namespace = Utils.Paths.getLeading(serviceInfo.getName(), '.');
Expand All @@ -361,9 +361,10 @@ public void registerImportSchema(ServiceInfo serviceInfo) {
schema = schema.with(arg.getName(), arg.getType().getFirst(), arg.isOptional());
}
addImport(namespace, schema);
return schema;
}

public void registerExportSchema(ServiceInfo serviceInfo) {
public Schema registerExportSchema(ServiceInfo serviceInfo) {
// create the schema
var namespace = Utils.Paths.getLeading(serviceInfo.getName(), '.');
var type = serviceInfo.listArguments().isEmpty() ? Schema.Type.STREAM : Schema.Type.PROPERTIES;
Expand All @@ -376,6 +377,7 @@ public void registerExportSchema(ServiceInfo serviceInfo) {
}

addExport(namespace, schema);
return schema;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface Adapter {
*
* @return
*/
Artifact.Type getResourceType(Urn urn);
Artifact.Type resourceType(Urn urn);

/**
* Version. Cannot be null. Multiple versions of the same adapter may coexist in a service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@

public class ComponentRegistry {

public static final String ADAPTER_PREFIX = "klab.resource.adapter";
public static final String LOCAL_SERVICE_COMPONENT = "internal.local.service.component";
private final BaseService service;
private PluginManager componentManager;
Expand Down Expand Up @@ -405,8 +404,7 @@ private void registerLibrary(Library annotation, Class<?> cls, List<LibraryDescr
var serviceInfo = createVerbPrototype(namespacePrefix, clss.getAnnotation(Verb.class));
verbs.add(Pair.of(serviceInfo, createFunctionDescriptor(serviceInfo, clss, null)));
} else if (clss.isAnnotationPresent(KlabAnnotation.class)) {
var serviceInfo = createPrototype(namespacePrefix,
clss.getAnnotation(KlabAnnotation.class));
var serviceInfo = createPrototype(namespacePrefix, clss.getAnnotation(KlabAnnotation.class));
annotations.add(Pair.of(serviceInfo, createFunctionDescriptor(serviceInfo, clss, null)));
}
}
Expand All @@ -425,13 +423,11 @@ private void registerLibrary(Library annotation, Class<?> cls, List<LibraryDescr
var serviceInfo = createVerbPrototype(namespacePrefix, method.getAnnotation(Verb.class));
verbs.add(Pair.of(serviceInfo, createFunctionDescriptor(serviceInfo, cls, method)));
} else if (method.isAnnotationPresent(Importer.class)) {
var serviceInfo = createPrototype(namespacePrefix,
method.getAnnotation(Importer.class));
var serviceInfo = createPrototype(namespacePrefix, method.getAnnotation(Importer.class));
prototypes.add(Pair.of(serviceInfo, createFunctionDescriptor(serviceInfo, cls, method)));
ResourceTransport.INSTANCE.registerImportSchema(serviceInfo);
} else if (method.isAnnotationPresent(Exporter.class)) {
var serviceInfo = createPrototype(namespacePrefix,
method.getAnnotation(Exporter.class));
var serviceInfo = createPrototype(namespacePrefix, method.getAnnotation(Exporter.class));
prototypes.add(Pair.of(serviceInfo, createFunctionDescriptor(serviceInfo, cls, method)));
ResourceTransport.INSTANCE.registerExportSchema(serviceInfo);
}
Expand Down Expand Up @@ -628,12 +624,11 @@ private void registerAdapter(ResourceAdapter annotation, Class<?> cls, List<Adap

try {
var adapter = new AdapterImpl(cls, annotation);
this.adapters.put(adapter.getName(), adapter);
} catch (Throwable t) {
Logging.INSTANCE.error(
"Adapter " + annotation.name() + " caused errors when loading and was " + "rejected", t);
Logging.INSTANCE.error(t);
}

System.out.println("ZIO PORCO UN ADAPTER " + annotation.name());
}

/**
Expand Down Expand Up @@ -1017,7 +1012,10 @@ public String getName() {
}

@Override
public Artifact.Type getResourceType(Urn urn) {
public Artifact.Type resourceType(Urn urn) {
if (typeAttributor != null) {
// TODO
}
return this.resourceType;
}

Expand Down Expand Up @@ -1072,35 +1070,81 @@ public Data encode(Resource resource, Geometry geometry, Object... contextParame

private void scanAdapterClass(Class<?> adapterClass) {

var namespace = ADAPTER_PREFIX + "." + name;

// annotated methods
for (Method method : adapterClass.getDeclaredMethods()) {

if (Modifier.isPublic(method.getModifiers()) && method.isAnnotationPresent(
ResourceAdapter.Encoder.class)) {

this.encoder = createServiceImplementation(method, method.getAnnotation(
ResourceAdapter.Encoder.class));

} else if (method.isAnnotationPresent(ResourceAdapter.Contextualizer.class)) {
// var serviceInfo = createAnnotationPrototype(namespacePrefix,
// method.getAnnotation
// (KlabAnnotation.class));
// annotations.add(Pair.of(serviceInfo, createFunctionDescriptor
// (serviceInfo, cls, method)));
if (!Resource.class.isAssignableFrom(method.getReturnType())) {
throw new KlabIllegalStateException(
"Adapter methods annotated with @Contextualizer must return a Resource");
}

this.contextualizer = createServiceImplementation(method, method.getAnnotation(
ResourceAdapter.Contextualizer.class));

} else if (method.isAnnotationPresent(ResourceAdapter.Inspector.class)) {
this.inspector = createServiceImplementation(method, method.getAnnotation(
ResourceAdapter.Inspector.class));
} else if (method.isAnnotationPresent(ResourceAdapter.Publisher.class)) {
this.publisher = createServiceImplementation(method, method.getAnnotation(
ResourceAdapter.Publisher.class));
} else if (method.isAnnotationPresent(ResourceAdapter.Sanitizer.class)) {
this.sanitizer = createServiceImplementation(method, method.getAnnotation(
ResourceAdapter.Sanitizer.class));
} else if (method.isAnnotationPresent(ResourceAdapter.Validator.class)) {
this.validator = createServiceImplementation(method, method.getAnnotation(
ResourceAdapter.Validator.class));
} else if (method.isAnnotationPresent(ResourceAdapter.Type.class)) {

if (!Artifact.Type.class.isAssignableFrom(method.getReturnType())) {
throw new KlabIllegalStateException(
"Adapter methods annotated with @Type must return an Artifact.Type");
}
this.typeAttributor = createServiceImplementation(method, method.getAnnotation(
ResourceAdapter.Type.class));

} else if (method.isAnnotationPresent(Importer.class)) {
var serviceInfo = createPrototype(namespace,
method.getAnnotation(Importer.class));
ResourceTransport.INSTANCE.registerImportSchema(serviceInfo);
var serviceInfo = createPrototype(name, method.getAnnotation(Importer.class));
var schema = ResourceTransport.INSTANCE.registerImportSchema(serviceInfo);
schema.setAdapter(name);
serviceImplementations.put(schema.getSchemaId(), createServiceImplementation(method, method.getAnnotation(
Importer.class)));
} else if (method.isAnnotationPresent(Exporter.class)) {
var serviceInfo = createPrototype(namespace,
method.getAnnotation(Exporter.class));
ResourceTransport.INSTANCE.registerExportSchema(serviceInfo);
var serviceInfo = createPrototype(name, method.getAnnotation(Exporter.class));
var schema = ResourceTransport.INSTANCE.registerExportSchema(serviceInfo);
schema.setAdapter(name);
serviceImplementations.put(schema.getSchemaId(), createServiceImplementation(method, method.getAnnotation(
Exporter.class)));
}
}

if (this.encoder == null) {
throw new KlabIllegalStateException(
"Cannot load adapter " + name + ": missing encoder method");
}
if ((this.resourceType == null || this.resourceType == Artifact.Type.VOID) && typeAttributor == null) {
throw new KlabIllegalStateException(
"Cannot load adapter " + name + ": missing type attribution in annotation or " +
"methods");
}
}

private ServiceImplementation createServiceImplementation(Method method, Annotation annotation) {
ServiceImplementation ret = new ServiceImplementation();
ret.method = method;
// TODO instances, reentrancy etc
return ret;
}
}

Expand Down

0 comments on commit 3424fd9

Please sign in to comment.