From 52e1260db01d1b52d67af1cf6ea2c30095e7aa9c Mon Sep 17 00:00:00 2001 From: Chanaka Lakmal Date: Wed, 23 Dec 2020 09:16:25 +0530 Subject: [PATCH] Replace hard coded native package version --- ldap-ballerina/init.bal | 25 +++++++++++ .../stdlib/ldap/LdapConstants.java | 9 ---- .../nativeimpl/InitLdapConnectionContext.java | 2 +- .../stdlib/ldap/nativeimpl/ModuleUtils.java | 42 +++++++++++++++++++ .../stdlib/ldap/util/LdapUtils.java | 8 ++-- 5 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 ldap-ballerina/init.bal create mode 100644 ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/nativeimpl/ModuleUtils.java diff --git a/ldap-ballerina/init.bal b/ldap-ballerina/init.bal new file mode 100644 index 0000000..b97cccd --- /dev/null +++ b/ldap-ballerina/init.bal @@ -0,0 +1,25 @@ +// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import ballerina/java; + +isolated function init() { + setModule(); +} + +isolated function setModule() = @java:Method { + 'class: "org.ballerinalang.stdlib.ldap.nativeimpl.ModuleUtils" +} external; diff --git a/ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/LdapConstants.java b/ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/LdapConstants.java index 402f374..f98aaf5 100644 --- a/ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/LdapConstants.java +++ b/ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/LdapConstants.java @@ -18,21 +18,12 @@ package org.ballerinalang.stdlib.ldap; -import io.ballerina.runtime.api.Module; - -import static io.ballerina.runtime.api.constants.RuntimeConstants.BALLERINA_BUILTIN_PKG_PREFIX; - /** * Constants to be used in LDAP user stores. * * @since 0.983.0 */ public class LdapConstants { - - // Name of the Ballerina ldap module, used to create struct instances. - public static final String PACKAGE_NAME = "ldap"; - public static final Module LDAP_PACKAGE_ID = new Module(BALLERINA_BUILTIN_PKG_PREFIX, PACKAGE_NAME, "1.0.5"); - // Record used to reference to a LDAP connection. public static final String LDAP_CONNECTION = "LdapConnection"; diff --git a/ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/nativeimpl/InitLdapConnectionContext.java b/ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/nativeimpl/InitLdapConnectionContext.java index 64cdfbb..7baf9b4 100644 --- a/ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/nativeimpl/InitLdapConnectionContext.java +++ b/ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/nativeimpl/InitLdapConnectionContext.java @@ -110,7 +110,7 @@ public static Object initLdapConnectionContext(BMap authProvide DirContext dirContext = connectionSource.getContext(); BMap ldapConnectionRecord = ValueCreator. - createRecordValue(LdapConstants.LDAP_PACKAGE_ID, LdapConstants.LDAP_CONNECTION); + createRecordValue(ModuleUtils.getModule(), LdapConstants.LDAP_CONNECTION); ldapConnectionRecord.addNativeData(LdapConstants.LDAP_CONFIGURATION, commonLdapConfiguration); ldapConnectionRecord.addNativeData(LdapConstants.LDAP_CONNECTION_SOURCE, connectionSource); ldapConnectionRecord.addNativeData(LdapConstants.LDAP_CONNECTION_CONTEXT, dirContext); diff --git a/ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/nativeimpl/ModuleUtils.java b/ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/nativeimpl/ModuleUtils.java new file mode 100644 index 0000000..cfd87ab --- /dev/null +++ b/ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/nativeimpl/ModuleUtils.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.ballerinalang.stdlib.ldap.nativeimpl; + +import io.ballerina.runtime.api.Environment; +import io.ballerina.runtime.api.Module; + +/** + * Utility functions relevant to module operations. + * + * @since 2.0.0 + */ +public class ModuleUtils { + + private static Module ldapModule; + + private ModuleUtils() {} + + public static void setModule(Environment env) { + ldapModule = env.getCurrentModule(); + } + + public static Module getModule() { + return ldapModule; + } +} diff --git a/ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/util/LdapUtils.java b/ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/util/LdapUtils.java index be38eee..1f95248 100644 --- a/ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/util/LdapUtils.java +++ b/ldap-native/src/main/java/org/ballerinalang/stdlib/ldap/util/LdapUtils.java @@ -21,6 +21,8 @@ import io.ballerina.runtime.api.utils.StringUtils; import io.ballerina.runtime.api.values.BError; import org.ballerinalang.stdlib.ldap.CommonLdapConfiguration; +import org.ballerinalang.stdlib.ldap.LdapConstants; +import org.ballerinalang.stdlib.ldap.nativeimpl.ModuleUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,9 +38,6 @@ import javax.naming.directory.SearchControls; import javax.naming.directory.SearchResult; -import static org.ballerinalang.stdlib.ldap.LdapConstants.LDAP_ERROR_TYPE; -import static org.ballerinalang.stdlib.ldap.LdapConstants.LDAP_PACKAGE_ID; - /** * Utility class for LDAP related common operations. * @@ -258,6 +257,7 @@ public static void removeServiceName() { } public static BError createError(String errMsg) { - return ErrorCreator.createDistinctError(LDAP_ERROR_TYPE, LDAP_PACKAGE_ID, StringUtils.fromString(errMsg)); + return ErrorCreator.createDistinctError(LdapConstants.LDAP_ERROR_TYPE, ModuleUtils.getModule(), + StringUtils.fromString(errMsg)); } }