use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.
the class ProgramFile method addAttributeInfo.
@Override
public void addAttributeInfo(AttributeInfo.Kind attributeKind, AttributeInfo attributeInfo) {
attributeInfoMap.put(attributeKind, attributeInfo);
if (attributeKind == AttributeInfo.Kind.VARIABLE_TYPE_COUNT_ATTRIBUTE) {
// TODO Move this out of the program file to a program context.. Runtime representation of a program.
// TODO ProgramFile is the static program data.
VarTypeCountAttributeInfo varTypeCountAttribInfo = (VarTypeCountAttributeInfo) attributeInfo;
int[] globalVarCount = varTypeCountAttribInfo.getVarTypeCount();
// TODO Introduce an abstraction for memory blocks
// Initialize global memory block
BStructType dummyType = new BStructType(null, "", "", 0);
dummyType.setFieldTypeCount(globalVarCount);
this.globalMemoryBlock = new BStruct(dummyType);
}
}
use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.
the class BLangFunctions method invokeNonNativeCallable.
public static WorkerExecutionContext invokeNonNativeCallable(CallableUnitInfo callableUnitInfo, WorkerExecutionContext parentCtx, int[] argRegs, int[] retRegs, boolean waitForResponse, int flags) {
WorkerSet workerSet = callableUnitInfo.getWorkerSet();
int generalWorkersCount = workerSet.generalWorkers.length;
CallableWorkerResponseContext respCtx = createWorkerResponseContext(callableUnitInfo.getRetParamTypes(), generalWorkersCount);
WaitForResponseCallback respCallback = null;
if (waitForResponse) {
respCallback = new WaitForResponseCallback();
respCtx.registerResponseCallback(respCallback);
}
if (TraceManagerWrapper.getInstance().isTraceEnabled() && FunctionFlags.isObserved(flags)) {
respCtx.registerResponseCallback(new TraceableCallback(parentCtx));
}
respCtx.joinTargetContextInfo(parentCtx, retRegs);
WorkerDataIndex wdi = callableUnitInfo.retWorkerIndex;
/* execute the init worker and extract the local variables created by it */
WorkerData initWorkerLocalData = null;
CodeAttributeInfo initWorkerCAI = null;
if (workerSet.initWorker != null) {
initWorkerLocalData = executeInitWorker(parentCtx, argRegs, callableUnitInfo, workerSet.initWorker, wdi);
if (initWorkerLocalData == null) {
handleError(parentCtx);
return null;
}
initWorkerCAI = workerSet.initWorker.getCodeAttributeInfo();
}
for (int i = 1; i < generalWorkersCount; i++) {
executeWorker(respCtx, parentCtx, argRegs, callableUnitInfo, workerSet.generalWorkers[i], wdi, initWorkerLocalData, initWorkerCAI, false);
}
WorkerExecutionContext runInCallerCtx = executeWorker(respCtx, parentCtx, argRegs, callableUnitInfo, workerSet.generalWorkers[0], wdi, initWorkerLocalData, initWorkerCAI, true);
if (waitForResponse) {
BLangScheduler.executeNow(runInCallerCtx);
respCallback.waitForResponse();
// An error in the context at this point means an unhandled runtime error has propagated
// all the way up to the entry point. Hence throw a {@link BLangRuntimeException} and
// terminate the execution.
BStruct error = parentCtx.getError();
if (error != null) {
handleError(parentCtx);
}
return null;
} else {
return runInCallerCtx;
}
}
use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.
the class AbstractHTTPAction method send.
/**
* Send outbound request through the client connector. If the Content-Type is multipart, check whether the boundary
* exist. If not get a new boundary string and add it as a parameter to Content-Type, just before sending header
* info through wire. If a boundary string exist at this point, serialize multipart entity body, else serialize
* entity body which can either be a message data source or a byte channel.
*
* @param dataContext holds the ballerina context and callback
* @param outboundRequestMsg Outbound request that needs to be sent across the wire
* @param async whether a handle should be return
* @return connector future for this particular request
* @throws Exception When an error occurs while sending the outbound request via client connector
*/
private void send(DataContext dataContext, HTTPCarbonMessage outboundRequestMsg, boolean async) throws Exception {
BStruct bConnector = (BStruct) dataContext.context.getRefArgument(0);
Struct httpClient = BLangConnectorSPIUtil.toStruct(bConnector);
HttpClientConnector clientConnector = (HttpClientConnector) httpClient.getNativeData(HttpConstants.HTTP_CLIENT);
String contentType = HttpUtil.getContentTypeFromTransportMessage(outboundRequestMsg);
String boundaryString = null;
if (HeaderUtil.isMultipart(contentType)) {
boundaryString = HttpUtil.addBoundaryIfNotExist(outboundRequestMsg, contentType);
}
HttpMessageDataStreamer outboundMsgDataStreamer = new HttpMessageDataStreamer(outboundRequestMsg);
OutputStream messageOutputStream = outboundMsgDataStreamer.getOutputStream();
RetryConfig retryConfig = getRetryConfiguration(httpClient);
HTTPClientConnectorListener httpClientConnectorLister = new HTTPClientConnectorListener(dataContext, retryConfig, outboundRequestMsg, outboundMsgDataStreamer);
HttpResponseFuture future = clientConnector.send(outboundRequestMsg);
if (async) {
future.setResponseHandleListener(httpClientConnectorLister);
} else {
future.setHttpConnectorListener(httpClientConnectorLister);
}
try {
if (boundaryString != null) {
serializeMultiparts(dataContext.context, messageOutputStream, boundaryString);
} else {
serializeDataSource(dataContext.context, messageOutputStream);
}
} catch (IOException | EncoderException serializerException) {
// We don't have to do anything here as the client connector will notify
// the error though the listener
logger.warn("couldn't serialize the message", serializerException);
}
}
use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.
the class AbstractHTTPAction method serializeDataSource.
private void serializeDataSource(Context context, OutputStream messageOutputStream) throws IOException {
BStruct requestStruct = ((BStruct) context.getRefArgument(1));
BStruct entityStruct = MimeUtil.extractEntity(requestStruct);
if (entityStruct != null) {
MessageDataSource messageDataSource = EntityBodyHandler.getMessageDataSource(entityStruct);
if (messageDataSource != null) {
messageDataSource.serializeData(messageOutputStream);
HttpUtil.closeMessageOutputStream(messageOutputStream);
} else {
// When the entity body is a byte channel
try {
EntityBodyHandler.writeByteChannelToOutputStream(entityStruct, messageOutputStream);
} finally {
HttpUtil.closeMessageOutputStream(messageOutputStream);
}
}
}
}
use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.
the class AbstractHTTPAction method createResponseStruct.
/**
* Creates InResponse using the native {@code HTTPCarbonMessage}.
*
* @param context ballerina context
* @param httpCarbonMessage the HTTPCarbonMessage
* @return the Response struct
*/
BStruct createResponseStruct(Context context, HTTPCarbonMessage httpCarbonMessage) {
BStruct responseStruct = createStruct(context, HttpConstants.RESPONSE, HttpConstants.PROTOCOL_PACKAGE_HTTP);
BStruct entity = createStruct(context, HttpConstants.ENTITY, PROTOCOL_PACKAGE_MIME);
BStruct mediaType = createStruct(context, MEDIA_TYPE, PROTOCOL_PACKAGE_MIME);
ResponseCacheControlStruct responseCacheControl = new ResponseCacheControlStruct(context.getProgramFile().getPackageInfo(PROTOCOL_PACKAGE_HTTP).getStructInfo(RESPONSE_CACHE_CONTROL));
HttpUtil.populateInboundResponse(responseStruct, entity, mediaType, responseCacheControl, httpCarbonMessage);
return responseStruct;
}
Aggregations