Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,24 @@

import io.ballerina.runtime.api.Runtime;
import io.ballerina.runtime.api.concurrent.StrandMetadata;
import io.ballerina.runtime.api.creators.ValueCreator;
import io.ballerina.runtime.api.types.MethodType;
import io.ballerina.runtime.api.types.ObjectType;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.utils.TypeUtils;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.runtime.api.values.BString;
import io.ballerina.stdlib.file.utils.FileConstants;
import io.ballerina.stdlib.file.utils.ModuleUtils;
import org.wso2.transport.localfilesystem.server.connector.contract.LocalFileSystemEvent;
import org.wso2.transport.localfilesystem.server.connector.contract.LocalFileSystemListener;

import java.util.HashMap;
import java.util.Map;

import static io.ballerina.stdlib.file.service.DirectoryListenerConstants.FILE_SYSTEM_EVENT;

/**
* File System connector listener for Ballerina.
*/
Expand All @@ -45,19 +53,29 @@ public FSListener(Runtime runtime) {
@Override
public void onMessage(LocalFileSystemEvent fileEvent) {
Thread.startVirtualThread(() -> {
Object[] parameters = getJvmSignatureParameters(fileEvent);
for (Map.Entry<BObject, Map<String, MethodType>> serviceEntry: serviceRegistry.entrySet()) {
MethodType serviceFunction = serviceEntry.getValue().get(fileEvent.getEvent());
if (serviceFunction != null) {
String functionName = serviceFunction.getName();
BObject service = serviceEntry.getKey();
ObjectType type = (ObjectType) TypeUtils.getReferredType(TypeUtils.getType(service));
boolean isConcurrentSafe = type.isIsolated() && type.isIsolated(functionName);
runtime.callMethod(service, functionName, new StrandMetadata(isConcurrentSafe, null));
runtime.callMethod(service, functionName, new StrandMetadata(isConcurrentSafe, null), parameters);
}
}
});
}

private Object[] getJvmSignatureParameters(LocalFileSystemEvent fileEvent) {
BMap<BString, Object> eventStruct = ValueCreator.createRecordValue(ModuleUtils.getModule(), FILE_SYSTEM_EVENT);
eventStruct.put(StringUtils.fromString(FileConstants.FILE_EVENT_NAME),
StringUtils.fromString(fileEvent.getFileName()));
eventStruct.put(StringUtils.fromString(FileConstants.FILE_EVENT_OPERATION),
StringUtils.fromString(fileEvent.getEvent()));
return new Object[] {eventStruct};
}

public void addService(BObject service, Map<String, MethodType> attachedFunctions) {
this.serviceRegistry.put(service, attachedFunctions);
}
Expand Down