public static void addNativeToRecode(Object module, BMap<String, BValue> bStruct) { bStruct.addNativeData(NATIVE_KEY, module); }
@Override public void addNativeData(String key, Object data) { value.addNativeData(key, data); }
public static void addCarbonMsg(BMap<String, BValue> struct, HttpCarbonMessage httpCarbonMessage) { struct.addNativeData(TRANSPORT_MESSAGE, httpCarbonMessage); }
public static BMap<String, BValue> createSessionStruct(Context context, Session session) { BMap<String, BValue> sessionStruct = ConnectorUtils .createAndGetStruct(context, HttpConstants.PROTOCOL_PACKAGE_HTTP, HttpConstants.SESSION); //Add session to the struct as a native data sessionStruct.addNativeData(HttpConstants.HTTP_SESSION, session); return sessionStruct; }
private static void methodInvocationCheck(BMap<String, BValue> bStruct, HttpCarbonMessage reqMsg, int statusCode) { if (bStruct.getNativeData(METHOD_ACCESSED) != null || reqMsg == null) { throw new IllegalStateException("illegal function invocation"); } if (!is100ContinueRequest(reqMsg, statusCode)) { bStruct.addNativeData(METHOD_ACCESSED, true); } }
/** * Populates the HTTP caller with native data. * * @param caller Represents the HTTP caller * @param inboundMsg Represents carbon message * @param config Represents service endpoint configuration */ public static void enrichHttpCallerWithNativeData(BMap<String, BValue> caller, HttpCarbonMessage inboundMsg, Struct config) { caller.addNativeData(HttpConstants.TRANSPORT_MESSAGE, inboundMsg); caller.put(HttpConstants.HTTP_CONNECTOR_CONFIG_FIELD, (BMap<String, BValue>) config.getVMValue()); }
@Override public void onResponseHandle(ResponseHandle responseHandle) { BMap<String, BValue> httpFuture = BLangConnectorSPIUtil.createBStruct(this.dataContext.context, HttpConstants.PROTOCOL_PACKAGE_HTTP, HttpConstants.HTTP_FUTURE); httpFuture.addNativeData(HttpConstants.TRANSPORT_HANDLE, responseHandle); this.dataContext.notifyInboundResponseStatus(httpFuture, null); }
/** * Returns endpoint instance which is used to respond to the caller. * * @param responseObserver client responder instance. * @return instance of endpoint type. */ private BValue getConnectionParameter(Resource resource, StreamObserver responseObserver) { ProgramFile programFile = getProgramFile(resource); // generate client responder struct on request message with response observer and response msg type. BMap<String, BValue> clientEndpoint = BLangConnectorSPIUtil.createBStruct(programFile, GrpcConstants.PROTOCOL_STRUCT_PACKAGE_GRPC, GrpcConstants.CALLER); clientEndpoint.put(CALLER_ID, new BInteger(responseObserver.hashCode())); clientEndpoint.addNativeData(GrpcConstants.RESPONSE_OBSERVER, responseObserver); clientEndpoint.addNativeData(GrpcConstants.RESPONSE_MESSAGE_DEFINITION, methodDescriptor.getOutputType()); return clientEndpoint; }
@Override public void execute(Context context) { BMap bStruct = (BMap) context.getRefArgument(0); Counter counter = (Counter) bStruct.getNativeData(ObserveNativeImplConstants.METRIC_NATIVE_INSTANCE_KEY); Counter registeredCounter = counter.register(); bStruct.addNativeData(ObserveNativeImplConstants.METRIC_NATIVE_INSTANCE_KEY, registeredCounter); } }
@Override public void execute(Context context) { BMap bStruct = (BMap) context.getRefArgument(0); Gauge gauge = (Gauge) bStruct.getNativeData(ObserveNativeImplConstants.METRIC_NATIVE_INSTANCE_KEY); Gauge registeredCounter = gauge.register(); bStruct.addNativeData(ObserveNativeImplConstants.METRIC_NATIVE_INSTANCE_KEY, registeredCounter); } }
private BValue[] getSignatureParameters(Message jmsMessage) { ProgramFile programFile = resource.getResourceInfo().getPackageInfo().getProgramFile(); BMap<String, BValue> message = BLangConnectorSPIUtil.createBStruct(programFile, Constants.BALLERINA_PACKAGE_JMS, Constants.JMS_MESSAGE_STRUCT_NAME); message.addNativeData(Constants.JMS_MESSAGE_OBJECT, jmsMessage); List<ParamDetail> paramDetails = resource.getParamDetails(); BValue[] bValues = new BValue[paramDetails.size()]; bValues[0] = queueConsumerBObject; bValues[1] = message; return bValues; }
private static BMap<String, BValue> createSQLDataSource(Context context, SQLDatasource.SQLDatasourceParams sqlDatasourceParams) { SQLDatasource datasource = new SQLDatasource(); datasource.init(sqlDatasourceParams); BMap<String, BValue> sqlClient = BLangConnectorSPIUtil .createBStruct(context.getProgramFile(), Constants.SQL_PACKAGE_PATH, Constants.SQL_CLIENT); sqlClient.addNativeData(Constants.SQL_CLIENT, datasource); return sqlClient; }
BValue[] computeMessageParams(Resource resource, Message request, StreamObserver responseObserver) { List<ParamDetail> paramDetails = resource.getParamDetails(); BValue[] signatureParams = new BValue[paramDetails.size()]; signatureParams[0] = getConnectionParameter(resource, responseObserver); BMap<String, BValue> headerStruct = getHeaderStruct(resource); if (headerStruct != null) { headerStruct.addNativeData(MESSAGE_HEADERS, request.getHeaders()); } BValue requestParam = request != null ? request.getbMessage() : null; if (requestParam != null) { signatureParams[1] = requestParam; } if (headerStruct != null) { signatureParams[signatureParams.length - 1] = headerStruct; } return signatureParams; }
public static void populateInboundRequest(BMap<String, BValue> inboundRequestStruct, BMap<String, BValue> entity, BMap<String, BValue> mediaType, HttpCarbonMessage inboundRequestMsg, ProgramFile programFile) { inboundRequestStruct.addNativeData(TRANSPORT_MESSAGE, inboundRequestMsg); inboundRequestStruct.addNativeData(REQUEST, true); enrichWithInboundRequestInfo(inboundRequestStruct, inboundRequestMsg); enrichWithInboundRequestHeaders(inboundRequestStruct, inboundRequestMsg); populateEntity(entity, mediaType, inboundRequestMsg); inboundRequestStruct.put(REQUEST_ENTITY_FIELD, entity); inboundRequestStruct.addNativeData(IS_BODY_BYTE_CHANNEL_ALREADY_SET, false); String cacheControlHeader = inboundRequestMsg.getHeader(CACHE_CONTROL.toString()); if (cacheControlHeader != null) { BMap<String, BValue> cacheControlStruct = BLangConnectorSPIUtil.createBStruct(programFile, PROTOCOL_PACKAGE_HTTP, REQUEST_CACHE_CONTROL); RequestCacheControlStruct requestCacheControl = new RequestCacheControlStruct(cacheControlStruct); requestCacheControl.populateStruct(cacheControlHeader); inboundRequestStruct.put(REQUEST_CACHE_CONTROL_FIELD, requestCacheControl.getStruct()); } }
@Override public void execute(Context context) { BMap<String, BValue> headerValues = (BMap<String, BValue>) context.getRefArgument(0); HttpHeaders headers = (HttpHeaders) headerValues.getNativeData(MESSAGE_HEADERS); String headerName = context.getStringArgument(0); String headerValue = context.getStringArgument(1); // Only initialize ctx if not yet initialized headers = headers != null ? headers : new DefaultHttpHeaders(); headers.add(headerName, headerValue); headerValues.addNativeData(MESSAGE_HEADERS, headers); context.setReturnValues(); } }
@Override public void execute(Context context) { BMap<String, BValue> headerValues = (BMap<String, BValue>) context.getRefArgument(0); HttpHeaders headers = (HttpHeaders) headerValues.getNativeData(MESSAGE_HEADERS); String headerName = context.getStringArgument(0); String headerValue = context.getStringArgument(1); // Only initialize ctx if not yet initialized headers = headers != null ? headers : new DefaultHttpHeaders(); headers.set(headerName, headerValue); headerValues.addNativeData(MESSAGE_HEADERS, headers); context.setReturnValues(); } }
/** * Populates the push promise struct from native {@code Http2PushPromise}. * * @param pushPromiseStruct the push promise struct * @param pushPromise the native Http2PushPromise */ public static void populatePushPromiseStruct(BMap<String, BValue> pushPromiseStruct, Http2PushPromise pushPromise) { pushPromiseStruct.addNativeData(HttpConstants.TRANSPORT_PUSH_PROMISE, pushPromise); pushPromiseStruct.put(HttpConstants.PUSH_PROMISE_PATH_FIELD, new BString(pushPromise.getPath())); pushPromiseStruct.put(HttpConstants.PUSH_PROMISE_METHOD_FIELD, new BString(pushPromise.getMethod())); }
@Override public void execute(Context context) { BMap<String, BValue> bStruct = (BMap<String, BValue>) context.getRefArgument(0); Counter counter = Counter.builder(bStruct.get(ObserveNativeImplConstants.NAME_FIELD).stringValue()) .description(bStruct.get(ObserveNativeImplConstants.DESCRIPTION_FIELD).stringValue()) .tags(Utils.toStringMap((BMap) bStruct.get(ObserveNativeImplConstants.TAGS_FIELD))) .build(); bStruct.addNativeData(ObserveNativeImplConstants.METRIC_NATIVE_INSTANCE_KEY, counter); } }
@Override @SuppressWarnings("unchecked") public BRefType getBRefType(ProgramFile programFile, SerializableState state, Deserializer deserializer) { PackageInfo packageInfo = programFile.getPackageInfo(pkgPath); BMap<K, V> bMap; if (packageInfo != null) { StructureTypeInfo structInfo = packageInfo.getStructInfo(structName); if (structInfo == null) { throw new BallerinaException(structName + " not found in module " + pkgPath); } bMap = new BMap<>(structInfo.getType()); } else { bMap = new BMap<>(); } nativeData.forEach((s, o) -> bMap.addNativeData(s, state.deserialize(o, programFile, deserializer))); map.forEach((k, v) -> bMap.put(k, (V) state.deserialize(v, programFile, deserializer))); return bMap; } }
@Override public void execute(Context context, CallableUnitCallback callableUnitCallback) { Topic jmsDestination; Struct sessionBObject = BallerinaAdapter.getReceiverObject(context); Session session = BallerinaAdapter.getNativeObject(sessionBObject, Constants.JMS_SESSION, Session.class, context); BMap<String, BValue> bStruct = BLangConnectorSPIUtil.createBStruct(context, Constants.BALLERINA_PACKAGE_JMS, Constants.JMS_DESTINATION_STRUCT_NAME); try { jmsDestination = session.createTemporaryTopic(); bStruct.addNativeData(Constants.JMS_DESTINATION_OBJECT, jmsDestination); bStruct.put(Constants.DESTINATION_NAME, new BString(jmsDestination.getTopicName())); bStruct.put(Constants.DESTINATION_TYPE, new BString("topic")); } catch (JMSException e) { BallerinaAdapter.returnError("Failed to create temporary destination.", context, e); } context.setReturnValues(bStruct); } }