use of com.github.javaparser.ast.stmt.ThrowStmt in project automatiko-engine by automatiko-io.
the class ServiceTaskDescriptor method classDeclaration.
protected ClassOrInterfaceDeclaration classDeclaration() {
String unqualifiedName = StaticJavaParser.parseName(mangledName).removeQualifier().asString();
ClassOrInterfaceDeclaration cls = new ClassOrInterfaceDeclaration().setName(unqualifiedName).setModifiers(Modifier.Keyword.PUBLIC).addImplementedType(WorkItemHandler.class.getCanonicalName());
ClassOrInterfaceType serviceType = new ClassOrInterfaceType(null, interfaceName);
FieldDeclaration serviceField = new FieldDeclaration().addVariable(new VariableDeclarator(serviceType, "service"));
cls.addMember(serviceField);
ClassOrInterfaceType completionHandlerType = new ClassOrInterfaceType(null, WorkItemExecutionManager.class.getCanonicalName());
FieldDeclaration completionHandlerField = new FieldDeclaration().addVariable(new VariableDeclarator(completionHandlerType, "completionHandler"));
cls.addMember(completionHandlerField);
// executeWorkItem method
BlockStmt executeWorkItemBody = new BlockStmt();
MethodCallExpr callService = new MethodCallExpr(new NameExpr("service"), operationName);
int counter = 0;
for (Map.Entry<String, String> paramEntry : parameters.entrySet()) {
MethodCallExpr getParamMethod = new MethodCallExpr(new NameExpr("workItem"), "getParameter").addArgument(new StringLiteralExpr(paramEntry.getKey()));
if (isServerlessWorkflow) {
String paramType = opTypes.size() > counter ? opTypes.get(counter).getCanonicalName() : paramEntry.getValue();
MethodCallExpr extractValueMethod = new MethodCallExpr(new NameExpr("io.automatiko.engine.workflow.json.ValueExtractor"), "extract").addArgument(getParamMethod).addArgument(new NameExpr(paramType + ".class"));
callService.addArgument(new CastExpr(new ClassOrInterfaceType(null, paramType), extractValueMethod));
} else {
callService.addArgument(new CastExpr(new ClassOrInterfaceType(null, paramEntry.getValue()), getParamMethod));
}
counter++;
}
MethodCallExpr completeWorkItem = completeWorkItem(executeWorkItemBody, callService);
MethodCallExpr completionHandlerCompleteMethod = completeWorkItemViaHandler();
IfStmt handleCompletion = new IfStmt(new BinaryExpr(new NameExpr("completionHandler"), new NullLiteralExpr(), BinaryExpr.Operator.EQUALS), new BlockStmt().addStatement(completeWorkItem), new BlockStmt().addStatement(completionHandlerCompleteMethod));
executeWorkItemBody.addStatement(handleCompletion);
if (implementation.equalsIgnoreCase("##webservice")) {
BlockStmt catchbody = new BlockStmt();
catchbody.addStatement(new ThrowStmt(new ObjectCreationExpr(null, new ClassOrInterfaceType(null, "io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError"), NodeList.nodeList(new MethodCallExpr(new NameExpr("wex"), "getMessage"), new MethodCallExpr(new NameExpr("String"), "valueOf").addArgument(new MethodCallExpr(new MethodCallExpr(new NameExpr("wex"), "getResponse"), "getStatus")), new MethodCallExpr(new NameExpr("io.automatiko.engine.services.utils.IoUtils"), "valueOf").addArgument(new MethodCallExpr(new MethodCallExpr(new NameExpr("wex"), "getResponse"), "getEntity"))))));
CatchClause catchClause = new CatchClause(new Parameter(new ClassOrInterfaceType(null, "javax.ws.rs.WebApplicationException"), "wex"), catchbody);
BlockStmt unavailablecatchbody = new BlockStmt();
unavailablecatchbody.addStatement(new ThrowStmt(new ObjectCreationExpr(null, new ClassOrInterfaceType(null, "io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError"), NodeList.nodeList(new StringLiteralExpr("503"), new MethodCallExpr(new NameExpr("ex"), "getMessage"), new NameExpr("ex")))));
CatchClause unavailablecatchClause = new CatchClause(new Parameter(new ClassOrInterfaceType(null, "javax.ws.rs.ProcessingException"), "ex"), unavailablecatchbody);
TryStmt trystmt = new TryStmt(executeWorkItemBody, NodeList.nodeList(catchClause, unavailablecatchClause), null);
executeWorkItemBody = new BlockStmt().addStatement(trystmt);
}
Set<String> handledErrorCodes = collectHandledErrorCodes();
if (!handledErrorCodes.isEmpty()) {
// add exception wrapper to handle errors that have error handlers attached
BlockStmt runtimeCatchBody = new BlockStmt();
MethodCallExpr wrapMethodCall = new MethodCallExpr(new NameExpr("io.automatiko.engine.workflow.ErrorMapper"), "wrap").addArgument(new NameExpr("rex"));
for (String errorCode : handledErrorCodes) {
wrapMethodCall.addArgument(new StringLiteralExpr(errorCode));
}
runtimeCatchBody.addStatement(new ThrowStmt(wrapMethodCall));
CatchClause runtimeCatchClause = new CatchClause(new Parameter(new ClassOrInterfaceType(null, RuntimeException.class.getCanonicalName()), "rex"), runtimeCatchBody);
TryStmt wrapperTryCatch = new TryStmt(executeWorkItemBody, NodeList.nodeList(runtimeCatchClause), null);
executeWorkItemBody = new BlockStmt().addStatement(wrapperTryCatch);
}
MethodDeclaration executeWorkItem = new MethodDeclaration().setModifiers(Modifier.Keyword.PUBLIC).setType(void.class).setName("executeWorkItem").setBody(executeWorkItemBody).addParameter(WorkItem.class.getCanonicalName(), "workItem").addParameter(WorkItemManager.class.getCanonicalName(), "workItemManager");
// abortWorkItem method
BlockStmt abortWorkItemBody = new BlockStmt();
MethodDeclaration abortWorkItem = new MethodDeclaration().setModifiers(Modifier.Keyword.PUBLIC).setType(void.class).setName("abortWorkItem").setBody(abortWorkItemBody).addParameter(WorkItem.class.getCanonicalName(), "workItem").addParameter(WorkItemManager.class.getCanonicalName(), "workItemManager");
// getName method
MethodDeclaration getName = new MethodDeclaration().setModifiers(Modifier.Keyword.PUBLIC).setType(String.class).setName("getName").setBody(new BlockStmt().addStatement(new ReturnStmt(new StringLiteralExpr(mangledName))));
cls.addMember(executeWorkItem).addMember(abortWorkItem).addMember(getName);
return cls;
}
use of com.github.javaparser.ast.stmt.ThrowStmt in project kogito-runtimes by kiegroup.
the class QueryEndpointGenerator method wrapBodyAddingExceptionLogging.
private BlockStmt wrapBodyAddingExceptionLogging(BlockStmt body, String nameURL) {
TryStmt ts = new TryStmt();
ts.setTryBlock(body);
CatchClause cc = new CatchClause();
String exceptionName = "e";
cc.setParameter(new Parameter().setName(exceptionName).setType(Exception.class));
BlockStmt cb = new BlockStmt();
cb.addStatement(parseStatement(String.format("systemMetricsCollectorProvider.get().registerException(\"%s\", %s.getStackTrace()[0].toString());", nameURL, exceptionName)));
cb.addStatement(new ThrowStmt(new NameExpr(exceptionName)));
cc.setBody(cb);
ts.setCatchClauses(new NodeList<>(cc));
return new BlockStmt(new NodeList<>(ts));
}
use of com.github.javaparser.ast.stmt.ThrowStmt in project kogito-runtimes by kiegroup.
the class RuleUnitContainerGenerator method factoryByIdBody.
private BlockStmt factoryByIdBody() {
SwitchStmt switchStmt = new SwitchStmt();
switchStmt.setSelector(new NameExpr("fqcn"));
for (RuleUnitGenerator ruleUnit : ruleUnits) {
SwitchEntry switchEntry = new SwitchEntry();
switchEntry.getLabels().add(new StringLiteralExpr(ruleUnit.getRuleUnitDescription().getCanonicalName()));
ObjectCreationExpr ruleUnitConstructor = new ObjectCreationExpr().setType(ruleUnit.targetCanonicalName()).addArgument("application");
switchEntry.getStatements().add(new ReturnStmt(ruleUnitConstructor));
switchStmt.getEntries().add(switchEntry);
}
SwitchEntry defaultEntry = new SwitchEntry();
defaultEntry.getStatements().add(new ThrowStmt(new ObjectCreationExpr().setType(UnsupportedOperationException.class.getCanonicalName())));
switchStmt.getEntries().add(defaultEntry);
return new BlockStmt().addStatement(switchStmt);
}
Aggregations