use of org.wso2.ballerinalang.compiler.tree.BLangEndpoint in project ballerina by ballerina-lang.
the class TreeVisitor method visit.
public void visit(BLangService serviceNode) {
if (!ScopeResolverConstants.getResolverByClass(cursorPositionResolver).isCursorBeforeNode(serviceNode.getPosition(), serviceNode, this, this.documentServiceContext)) {
BSymbol serviceSymbol = serviceNode.symbol;
SymbolEnv serviceEnv = SymbolEnv.createPkgLevelSymbolEnv(serviceNode, serviceSymbol.scope, symbolEnv);
// Reset the previous node
this.setPreviousNode(null);
if (!(serviceNode.resources.isEmpty() && serviceNode.vars.isEmpty() && serviceNode.endpoints.isEmpty())) {
// Visit the endpoints
serviceNode.endpoints.forEach(bLangEndpoint -> this.acceptNode(bLangEndpoint, serviceEnv));
// Since the service does not contains a block statement, we consider the block owner only.
// Here it is service
this.blockOwnerStack.push(serviceNode);
serviceNode.vars.forEach(v -> {
this.cursorPositionResolver = ServiceScopeResolver.class;
this.acceptNode(v, serviceEnv);
});
serviceNode.resources.forEach(r -> {
this.cursorPositionResolver = ServiceScopeResolver.class;
this.acceptNode(r, serviceEnv);
});
if (terminateVisitor) {
this.acceptNode(null, null);
}
this.blockOwnerStack.pop();
} else {
this.isCursorWithinBlock(serviceNode.getPosition(), serviceEnv);
}
}
}
use of org.wso2.ballerinalang.compiler.tree.BLangEndpoint in project kubernetes by ballerinax.
the class KubernetesPlugin method process.
@Override
public void process(EndpointNode endpointNode, List<AnnotationAttachmentNode> annotations) {
String endpointName = endpointNode.getName().getValue();
ServiceModel serviceModel = null;
setCanProcess(true);
for (AnnotationAttachmentNode attachmentNode : annotations) {
String annotationKey = attachmentNode.getAnnotationName().getValue();
try {
switch(annotationKey) {
case "Service":
serviceModel = kubernetesAnnotationProcessor.processServiceAnnotation(endpointName, attachmentNode);
kubernetesDataHolder.addServiceModel(endpointName, serviceModel);
break;
case "Deployment":
kubernetesDataHolder.setDeploymentModel(kubernetesAnnotationProcessor.processDeployment(attachmentNode));
break;
default:
break;
}
} catch (KubernetesPluginException e) {
dlog.logDiagnostic(Diagnostic.Kind.ERROR, endpointNode.getPosition(), e.getMessage());
}
}
List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangEndpoint) endpointNode).configurationExpr).getKeyValuePairs();
for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
String key = keyValue.getKey().toString();
switch(key) {
case "port":
int port = Integer.parseInt(keyValue.getValue().toString());
kubernetesDataHolder.addPort(port);
if (serviceModel != null) {
serviceModel.setPort(port);
}
break;
case "secureSocket":
List<BLangRecordLiteral.BLangRecordKeyValue> sslKeyValues = ((BLangRecordLiteral) keyValue.valueExpr).getKeyValuePairs();
try {
Set<SecretModel> secretModels = kubernetesAnnotationProcessor.processSecureSocketAnnotation(endpointName, sslKeyValues);
kubernetesDataHolder.addEndpointSecret(endpointName, secretModels);
kubernetesDataHolder.addSecrets(secretModels);
} catch (KubernetesPluginException e) {
dlog.logDiagnostic(Diagnostic.Kind.ERROR, null, e.getMessage());
}
break;
default:
break;
}
}
}
use of org.wso2.ballerinalang.compiler.tree.BLangEndpoint in project ballerina by ballerina-lang.
the class AnnotationDesugar method rewritePackageAnnotations.
protected void rewritePackageAnnotations(BLangPackage pkgNode) {
BLangFunction initFunction = pkgNode.initFunction;
// Remove last return statement. we will add it later. TODO : Fix this properly.
initFunction.body.stmts.remove(initFunction.body.stmts.size() - 1);
// This is the variable which store all package level annotations.
BLangVariable annotationMap = createGlobalAnnotationMapVar(pkgNode);
// handle Service Annotations.
for (BLangService service : pkgNode.services) {
generateAnnotations(service, service.name.value, initFunction, annotationMap);
for (BLangResource resource : service.resources) {
String key = service.name.value + DOT + resource.name.value;
generateAnnotations(resource, key, initFunction, annotationMap);
}
}
// Handle Function Annotations.
for (BLangFunction function : pkgNode.functions) {
generateAnnotations(function, function.symbol.name.value, initFunction, annotationMap);
}
// Handle Connector Annotations.
for (BLangConnector connector : pkgNode.connectors) {
generateAnnotations(connector, connector.name.value, initFunction, annotationMap);
for (BLangAction action : connector.actions) {
String key = connector.name.value + DOT + action.name.value;
generateAnnotations(connector, key, initFunction, annotationMap);
}
}
// Handle Struct Annotations.
for (BLangStruct struct : pkgNode.structs) {
generateAnnotations(struct, struct.name.value, initFunction, annotationMap);
for (BLangVariable field : struct.fields) {
String key = struct.name.value + DOT + field.name.value;
generateAnnotations(field, key, initFunction, annotationMap);
}
}
for (BLangEndpoint globalEndpoint : pkgNode.globalEndpoints) {
generateAnnotations(globalEndpoint, globalEndpoint.name.value, initFunction, annotationMap);
}
ASTBuilderUtil.createReturnStmt(pkgNode.pos, initFunction.body);
}
use of org.wso2.ballerinalang.compiler.tree.BLangEndpoint in project ballerina by ballerina-lang.
the class EndpointDesugar method rewriteEndpoint.
void rewriteEndpoint(BLangEndpoint endpoint, SymbolEnv env) {
final BSymbol encSymbol, varSymbol;
final BLangBlockStmt initBlock, startBlock;
BLangBlockStmt stopBlock = null;
if (env.enclInvokable != null) {
// Function, Action, Resource. Code generate to its body directly.
encSymbol = varSymbol = env.enclInvokable.symbol;
initBlock = startBlock = ((BLangInvokableNode) env.node).body;
} else if (env.enclService != null) {
encSymbol = env.enclService.symbol;
varSymbol = ((BLangService) env.node).initFunction.symbol;
initBlock = startBlock = ((BLangService) env.node).initFunction.body;
} else {
// Pkg level endpoint.
encSymbol = env.enclPkg.symbol;
varSymbol = ((BLangPackage) env.node).initFunction.symbol;
initBlock = ((BLangPackage) env.node).initFunction.body;
startBlock = ((BLangPackage) env.node).startFunction.body;
stopBlock = ((BLangPackage) env.node).stopFunction.body;
}
BLangBlockStmt genInit, genInitCall, genStartCall, genStopCall;
genInit = generateEndpointInit(endpoint, env, encSymbol);
genInitCall = generateEndpointInitFunctionCall(endpoint, env, encSymbol, varSymbol);
genStartCall = generateEndpointStartOrStop(endpoint, endpoint.symbol.startFunction, env, encSymbol);
genStopCall = generateEndpointStartOrStop(endpoint, endpoint.symbol.stopFunction, env, encSymbol);
if (env.enclInvokable != null) {
ASTBuilderUtil.prependStatements(genStartCall, startBlock);
ASTBuilderUtil.prependStatements(genInitCall, initBlock);
// TODO : Implement stop.
ASTBuilderUtil.prependStatements(genInit, initBlock);
} else if (env.enclService != null) {
ASTBuilderUtil.appendStatements(genInit, initBlock);
ASTBuilderUtil.appendStatements(genInitCall, initBlock);
ASTBuilderUtil.appendStatements(genStartCall, startBlock);
// TODO : Implement stop.
} else {
ASTBuilderUtil.appendStatements(genInit, initBlock);
ASTBuilderUtil.appendStatements(genInitCall, initBlock);
ASTBuilderUtil.appendStatements(genStartCall, startBlock);
ASTBuilderUtil.appendStatements(genStopCall, Objects.requireNonNull(stopBlock));
}
}
use of org.wso2.ballerinalang.compiler.tree.BLangEndpoint in project ballerina by ballerina-lang.
the class TreeVisitor method visit.
public void visit(BLangFunction funcNode) {
// Check for native functions
BSymbol funcSymbol = funcNode.symbol;
if (Symbols.isNative(funcSymbol)) {
return;
}
String functionName = funcNode.getName().getValue();
SymbolEnv funcEnv = SymbolEnv.createFunctionEnv(funcNode, funcSymbol.scope, symbolEnv);
if (isWithinParameterContext(functionName, NODE_TYPE_FUNCTION)) {
this.populateSymbols(this.resolveAllVisibleSymbols(funcEnv), funcEnv);
setTerminateVisitor(true);
} else if (!ScopeResolverConstants.getResolverByClass(cursorPositionResolver).isCursorBeforeNode(funcNode.getPosition(), funcNode, this, this.documentServiceContext)) {
// Visit the endpoints
funcNode.endpoints.forEach(bLangEndpoint -> this.acceptNode(bLangEndpoint, funcEnv));
this.blockOwnerStack.push(funcNode);
// Cursor position is calculated against the Block statement scope resolver
cursorPositionResolver = BlockStatementScopeResolver.class;
this.acceptNode(funcNode.body, funcEnv);
this.blockOwnerStack.pop();
// Process workers
if (terminateVisitor && !funcNode.workers.isEmpty()) {
this.setTerminateVisitor(false);
}
funcNode.workers.forEach(e -> this.symbolEnter.defineNode(e, funcEnv));
funcNode.workers.forEach(e -> this.acceptNode(e, funcEnv));
}
}
Aggregations