Search in sources :

Example 1 with BLangRuntimeException

use of org.ballerinalang.util.exceptions.BLangRuntimeException in project carbon-apimgt by wso2.

the class DeployService method execute.

@Override
public BValue[] execute(Context context) {
    String fileName = getStringArgument(context, 0);
    String serviceName = getStringArgument(context, 1);
    String config = getStringArgument(context, 2);
    String packageName = getStringArgument(context, 3);
    Path path = Paths.get(packageName);
    String filePath = path.toAbsolutePath() + File.separator + fileName;
    if (Util.saveFile(filePath, config)) {
        ProgramFile programFile = new BLangProgramLoader().loadServiceProgramFile(programDirPath, path);
        String[] servicePackageNameList = programFile.getServicePackageNameList();
        if (servicePackageNameList.length == 0) {
            throw new BallerinaException("no service found in '" + programFile.getProgramFilePath() + "'");
        }
        // This is required to invoke package/service init functions;
        Context bContext = new Context(programFile);
        // bContext.initFunction = true;
        PackageInfo packageInfo = programFile.getPackageInfo(packageName.replace("/", "."));
        // Invoke package init function
        BLangFunctions.invokeFunction(programFile, packageInfo, packageInfo.getInitFunctionInfo(), bContext);
        if (bContext.getError() != null) {
            String stackTraceStr = BLangVMErrors.getPrintableStackTrace(bContext.getError());
            throw new BLangRuntimeException("error: " + stackTraceStr);
        }
        for (ServiceInfo serviceInfo : packageInfo.getServiceInfoList()) {
            // Invoke service init function
            if (serviceName.equals(serviceInfo.getName())) {
                BLangFunctions.invokeFunction(programFile, packageInfo, serviceInfo.getInitFunctionInfo(), bContext);
                if (bContext.getError() != null) {
                    String stackTraceStr = BLangVMErrors.getPrintableStackTrace(bContext.getError());
                    throw new BLangRuntimeException("error: " + stackTraceStr);
                }
                // Deploy service
                DispatcherRegistry.getInstance().getServiceDispatchers().forEach((protocol, dispatcher) -> dispatcher.serviceRegistered(serviceInfo));
            }
        }
    }
    return new BValue[0];
}
Also used : Path(java.nio.file.Path) Context(org.ballerinalang.bre.Context) ServiceInfo(org.ballerinalang.util.codegen.ServiceInfo) BLangRuntimeException(org.ballerinalang.util.exceptions.BLangRuntimeException) BLangProgramLoader(org.ballerinalang.BLangProgramLoader) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) BValue(org.ballerinalang.model.values.BValue) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Example 2 with BLangRuntimeException

use of org.ballerinalang.util.exceptions.BLangRuntimeException in project ballerina by ballerina-lang.

the class BXMLItem method setAttribute.

/**
 * {@inheritDoc}
 */
@Override
public void setAttribute(String localName, String namespaceUri, String prefix, String value) {
    if (nodeType != XMLNodeType.ELEMENT) {
        return;
    }
    if (localName == null || localName.isEmpty()) {
        throw new BLangRuntimeException("localname of the attribute cannot be empty");
    }
    // Validate whether the attribute name is an XML supported qualified name, according to the XML recommendation.
    XMLValidationUtils.validateXMLName(localName);
    XMLValidationUtils.validateXMLName(prefix);
    // If the attribute already exists, update the value.
    OMElement node = (OMElement) omNode;
    QName qname = getQName(localName, namespaceUri, prefix);
    OMAttribute attr = node.getAttribute(qname);
    if (attr != null) {
        attr.setAttributeValue(value);
        return;
    }
    // If the prefix is 'xmlns' then this is a namespace addition
    if (prefix != null && prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
        node.declareNamespace(value, localName);
        return;
    }
    // If the namespace is null/empty, only the local part exists. Therefore add a simple attribute.
    if (namespaceUri == null || namespaceUri.isEmpty()) {
        attr = new OMAttributeImpl();
        attr.setAttributeValue(value);
        attr.setLocalName(localName);
        node.addAttribute(attr);
        return;
    }
    // treat this attribute-add operation as a namespace addition.
    if ((node.getDefaultNamespace() != null && namespaceUri.equals(node.getDefaultNamespace().getNamespaceURI())) || namespaceUri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
        node.declareNamespace(value, localName);
        return;
    }
    OMNamespace ns = null;
    if (prefix != null && !prefix.isEmpty()) {
        OMNamespace existingNs = node.findNamespaceURI(prefix);
        // If a namespace exists with the same prefix but a different uri, then do not add the new attribute.
        if (existingNs != null && !namespaceUri.equals(existingNs.getNamespaceURI())) {
            throw new BLangRuntimeException("failed to add attribute '" + prefix + ":" + localName + "'. prefix '" + prefix + "' is already bound to namespace '" + existingNs.getNamespaceURI() + "'");
        }
        ns = new OMNamespaceImpl(namespaceUri, prefix);
        node.addAttribute(localName, value, ns);
        return;
    }
    // We reach here if the namespace prefix is null/empty, and a namespace uri exists
    if (namespaceUri != null && !namespaceUri.isEmpty()) {
        prefix = null;
        // Find a prefix that has the same namespaceUri, out of the defined namespaces
        Iterator<String> prefixes = node.getNamespaceContext(false).getPrefixes(namespaceUri);
        while (prefixes.hasNext()) {
            String definedPrefix = prefixes.next();
            if (definedPrefix.isEmpty()) {
                continue;
            }
            prefix = definedPrefix;
            break;
        }
        if (prefix != null && prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
            // If found, and if its the default namespace, add a namespace decl
            node.declareNamespace(value, localName);
            return;
        }
        // else use the prefix. If the prefix is null, it will generate a random prefix.
        ns = new OMNamespaceImpl(namespaceUri, prefix);
    }
    node.addAttribute(localName, value, ns);
}
Also used : BLangRuntimeException(org.ballerinalang.util.exceptions.BLangRuntimeException) OMNamespace(org.apache.axiom.om.OMNamespace) QName(javax.xml.namespace.QName) OMAttributeImpl(org.apache.axiom.om.impl.llom.OMAttributeImpl) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute) OMNamespaceImpl(org.apache.axiom.om.impl.common.OMNamespaceImpl)

Example 3 with BLangRuntimeException

use of org.ballerinalang.util.exceptions.BLangRuntimeException in project ballerina by ballerina-lang.

the class ProgramFileReader method readFunctionInfo.

private void readFunctionInfo(DataInputStream dataInStream, PackageInfo packageInfo) throws IOException {
    int funcNameCPIndex = dataInStream.readInt();
    UTF8CPEntry funcNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(funcNameCPIndex);
    String funcName = funcNameUTF8Entry.getValue();
    FunctionInfo functionInfo = new FunctionInfo(packageInfo.getPkgNameCPIndex(), packageInfo.getPkgPath(), funcNameCPIndex, funcName);
    functionInfo.setPackageInfo(packageInfo);
    int funcSigCPIndex = dataInStream.readInt();
    UTF8CPEntry funcSigUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(funcSigCPIndex);
    setCallableUnitSignature(functionInfo, funcSigUTF8Entry.getValue(), packageInfo);
    int flags = dataInStream.readInt();
    boolean nativeFunc = Flags.isFlagOn(flags, Flags.NATIVE);
    functionInfo.setNative(nativeFunc);
    String uniqueFuncName;
    boolean attached = Flags.isFlagOn(flags, Flags.ATTACHED);
    if (attached) {
        int attachedToTypeCPIndex = dataInStream.readInt();
        functionInfo.attachedToTypeCPIndex = attachedToTypeCPIndex;
        TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) packageInfo.getCPEntry(attachedToTypeCPIndex);
        functionInfo.attachedToType = typeRefCPEntry.getType();
        uniqueFuncName = AttachedFunctionInfo.getUniqueFuncName(typeRefCPEntry.getType().getName(), funcName);
        packageInfo.addFunctionInfo(uniqueFuncName, functionInfo);
        // Update the attachedFunctionInfo
        if (typeRefCPEntry.getType().getTag() == TypeTags.STRUCT_TAG) {
            BStructType structType = (BStructType) typeRefCPEntry.getType();
            AttachedFunctionInfo attachedFuncInfo = structType.structInfo.funcInfoEntries.get(funcName);
            attachedFuncInfo.functionInfo = functionInfo;
        }
    } else {
        uniqueFuncName = funcName;
        packageInfo.addFunctionInfo(uniqueFuncName, functionInfo);
    }
    int workerDataChannelsLength = dataInStream.readShort();
    for (int i = 0; i < workerDataChannelsLength; i++) {
        readWorkerDataChannelEntries(dataInStream, packageInfo, functionInfo);
    }
    // Read worker info entries
    readWorkerInfoEntries(dataInStream, packageInfo, functionInfo);
    if (nativeFunc) {
        NativeCallableUnit nativeFunction = NativeUnitLoader.getInstance().loadNativeFunction(functionInfo.getPkgPath(), uniqueFuncName);
        if (nativeFunction == null) {
            throw new BLangRuntimeException("native function not available " + functionInfo.getPkgPath() + ":" + uniqueFuncName);
        }
        functionInfo.setNativeCallableUnit(nativeFunction);
    }
    // Read attributes
    readAttributeInfoEntries(dataInStream, packageInfo, functionInfo);
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) BLangRuntimeException(org.ballerinalang.util.exceptions.BLangRuntimeException) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit)

Example 4 with BLangRuntimeException

use of org.ballerinalang.util.exceptions.BLangRuntimeException in project ballerina by ballerina-lang.

the class ProgramFileReader method readConnectorActionInfoEntries.

private void readConnectorActionInfoEntries(DataInputStream dataInStream, PackageInfo packageInfo) throws IOException {
    for (ConnectorInfo connectorInfo : packageInfo.getConnectorInfoEntries()) {
        // Read action info entries
        int actionCount = dataInStream.readShort();
        for (int j = 0; j < actionCount; j++) {
            // Read action name;
            int actionNameCPIndex = dataInStream.readInt();
            UTF8CPEntry actionNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(actionNameCPIndex);
            String actionName = actionNameUTF8Entry.getValue();
            ActionInfo actionInfo = new ActionInfo(packageInfo.getPkgNameCPIndex(), packageInfo.getPkgPath(), actionNameCPIndex, actionName, connectorInfo);
            actionInfo.setPackageInfo(packageInfo);
            connectorInfo.addActionInfo(actionName, actionInfo);
            // Read action signature
            int actionSigCPIndex = dataInStream.readInt();
            UTF8CPEntry actionSigUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(actionSigCPIndex);
            actionInfo.setSignatureCPIndex(actionSigCPIndex);
            actionInfo.setSignature(actionSigUTF8Entry.getValue());
            int flags = dataInStream.readInt();
            // actionInfo.setflags(flags);
            setCallableUnitSignature(actionInfo, actionSigUTF8Entry.getValue(), packageInfo);
            // TODO Temp solution.
            boolean nativeAction = dataInStream.readByte() == 1;
            actionInfo.setNative(nativeAction);
            int workerDataChannelsLength = dataInStream.readShort();
            for (int k = 0; k < workerDataChannelsLength; k++) {
                readWorkerDataChannelEntries(dataInStream, packageInfo, actionInfo);
            }
            // Read worker info entries
            readWorkerInfoEntries(dataInStream, packageInfo, actionInfo);
            if (nativeAction) {
                NativeCallableUnit nativeActionObj = NativeUnitLoader.getInstance().loadNativeAction(actionInfo.getPkgPath(), actionInfo.getConnectorInfo().getName(), actionInfo.getName());
                if (nativeActionObj == null && !actionInfo.name.equals("<init>")) {
                    throw new BLangRuntimeException("native action not available " + actionInfo.getPkgPath() + ":" + actionInfo.getConnectorInfo().getName() + "." + actionName);
                }
                actionInfo.setNativeCallableUnit(nativeActionObj);
            }
            // Read attributes of the struct info
            readAttributeInfoEntries(dataInStream, packageInfo, actionInfo);
        }
        // Read attributes of the struct info
        readAttributeInfoEntries(dataInStream, packageInfo, connectorInfo);
    }
}
Also used : UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) BLangRuntimeException(org.ballerinalang.util.exceptions.BLangRuntimeException) NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit)

Example 5 with BLangRuntimeException

use of org.ballerinalang.util.exceptions.BLangRuntimeException in project ballerina by ballerina-lang.

the class BLangFunctions method invokeServiceInitFunction.

public static void invokeServiceInitFunction(FunctionInfo initFuncInfo) {
    WorkerExecutionContext context = new WorkerExecutionContext(initFuncInfo.getPackageInfo().getProgramFile());
    invokeCallable(initFuncInfo, context, new int[0], new int[0], true);
    if (context.getError() != null) {
        String stackTraceStr = BLangVMErrors.getPrintableStackTrace(context.getError());
        throw new BLangRuntimeException("error: " + stackTraceStr);
    }
}
Also used : WorkerExecutionContext(org.ballerinalang.bre.bvm.WorkerExecutionContext) BLangRuntimeException(org.ballerinalang.util.exceptions.BLangRuntimeException)

Aggregations

BLangRuntimeException (org.ballerinalang.util.exceptions.BLangRuntimeException)6 NativeCallableUnit (org.ballerinalang.model.NativeCallableUnit)2 BValue (org.ballerinalang.model.values.BValue)2 UTF8CPEntry (org.ballerinalang.util.codegen.cpentries.UTF8CPEntry)2 Path (java.nio.file.Path)1 QName (javax.xml.namespace.QName)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 OMElement (org.apache.axiom.om.OMElement)1 OMNamespace (org.apache.axiom.om.OMNamespace)1 OMNamespaceImpl (org.apache.axiom.om.impl.common.OMNamespaceImpl)1 OMAttributeImpl (org.apache.axiom.om.impl.llom.OMAttributeImpl)1 BLangProgramLoader (org.ballerinalang.BLangProgramLoader)1 Context (org.ballerinalang.bre.Context)1 WorkerExecutionContext (org.ballerinalang.bre.bvm.WorkerExecutionContext)1 CompileResult (org.ballerinalang.launcher.util.CompileResult)1 BStructType (org.ballerinalang.model.types.BStructType)1 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)1 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)1 ServiceInfo (org.ballerinalang.util.codegen.ServiceInfo)1 TypeRefCPEntry (org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry)1