|
| 1 | +package org.wso2.carbon.apimgt.impl.factory; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.StringUtils; |
| 4 | +import org.apache.commons.logging.Log; |
| 5 | +import org.apache.commons.logging.LogFactory; |
| 6 | +import org.wso2.carbon.apimgt.api.APIManagementException; |
| 7 | +import org.wso2.carbon.apimgt.api.model.Environment; |
| 8 | +import org.wso2.carbon.apimgt.api.model.GatewayAgentConfiguration; |
| 9 | +import org.wso2.carbon.apimgt.api.model.GatewayConfiguration; |
| 10 | +import org.wso2.carbon.apimgt.api.model.GatewayDeployer; |
| 11 | +import org.wso2.carbon.apimgt.api.model.KeyManager; |
| 12 | +import org.wso2.carbon.apimgt.impl.APIConstants; |
| 13 | +import org.wso2.carbon.apimgt.impl.dto.GatewayDto; |
| 14 | +import org.wso2.carbon.apimgt.impl.dto.KeyManagerDto; |
| 15 | +import org.wso2.carbon.apimgt.impl.dto.OrganizationGatewayDto; |
| 16 | +import org.wso2.carbon.apimgt.impl.dto.OrganizationKeyManagerDto; |
| 17 | +import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder; |
| 18 | +import org.wso2.carbon.apimgt.impl.loader.KeyManagerConfigurationDataRetriever; |
| 19 | +import org.wso2.carbon.apimgt.impl.utils.APIUtil; |
| 20 | + |
| 21 | +import java.lang.reflect.InvocationTargetException; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +public class GatewayHolder { |
| 26 | + private static Log log = LogFactory.getLog(GatewayHolder.class); |
| 27 | + private static final Map<String, OrganizationGatewayDto> organizationWiseMap = new HashMap<>(); |
| 28 | + |
| 29 | + public static void addGatewayConfiguration(String organization, String name, String type, |
| 30 | + GatewayConfiguration gatewayConfiguration) throws APIManagementException { |
| 31 | + |
| 32 | + OrganizationGatewayDto organizationGatewayDto = getTenantGatewayDtoFromMap(organization); |
| 33 | + if (organizationGatewayDto == null) { |
| 34 | + organizationGatewayDto = new OrganizationGatewayDto(); |
| 35 | + } |
| 36 | + |
| 37 | + if (organizationGatewayDto.getGatewayByName(name) != null) { |
| 38 | + log.warn("Gateway " + name + " already initialized in tenant " + organization); |
| 39 | + } |
| 40 | + |
| 41 | + GatewayDeployer deployer = null; |
| 42 | + GatewayAgentConfiguration gatewayAgentConfiguration = ServiceReferenceHolder.getInstance(). |
| 43 | + getExternalGatewayConnectorConfiguration(type); |
| 44 | + if (gatewayAgentConfiguration != null) { |
| 45 | + if (StringUtils.isNotEmpty(gatewayAgentConfiguration.getImplementation())) { |
| 46 | + try { |
| 47 | + deployer = (GatewayDeployer) Class.forName(gatewayAgentConfiguration.getImplementation()) |
| 48 | + .getDeclaredConstructor().newInstance(); |
| 49 | + deployer.init(gatewayConfiguration); |
| 50 | + } catch (ClassNotFoundException | IllegalAccessException | InstantiationException |
| 51 | + | NoSuchMethodException | InvocationTargetException e) { |
| 52 | + throw new APIManagementException("Error while loading gateway configuration", e); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + GatewayDto gatewayDto = new GatewayDto(); |
| 58 | + gatewayDto.setName(name); |
| 59 | + gatewayDto.setGatewayDeployer(deployer); |
| 60 | + organizationGatewayDto.putGatewayDto(gatewayDto); |
| 61 | + organizationWiseMap.put(organization, organizationGatewayDto); |
| 62 | + } |
| 63 | + |
| 64 | + public static void updateGatewayConfiguration(String organization, String name, String type, |
| 65 | + GatewayConfiguration gatewayConfiguration) throws APIManagementException { |
| 66 | + |
| 67 | + removeGatewayConfiguration(organization, name); |
| 68 | + addGatewayConfiguration(organization, name, type, gatewayConfiguration); |
| 69 | + } |
| 70 | + |
| 71 | + public static void removeGatewayConfiguration(String tenantDomain, String name) { |
| 72 | + |
| 73 | + OrganizationGatewayDto organizationGatewayDto = getTenantGatewayDtoFromMap(tenantDomain); |
| 74 | + if (organizationGatewayDto != null) { |
| 75 | + organizationGatewayDto.removeGatewayDtoByName(name); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public static GatewayDeployer getTenantGatewayInstance(String tenantDomain, String gatewayName) { |
| 80 | + |
| 81 | + OrganizationGatewayDto organizationGatewayDto = getTenantGatewayDto(tenantDomain); |
| 82 | + if (organizationGatewayDto != null) { |
| 83 | + GatewayDto gatewayDto = organizationGatewayDto.getGatewayByName(gatewayName); |
| 84 | + if (gatewayDto == null) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + return gatewayDto.getGatewayDeployer(); |
| 88 | + } |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + private static OrganizationGatewayDto getTenantGatewayDto(String tenantDomain) { |
| 93 | + |
| 94 | + OrganizationGatewayDto organizationGatewayDto = getTenantGatewayDtoFromMap(tenantDomain); |
| 95 | + if (organizationGatewayDto == null) { |
| 96 | + try { |
| 97 | + Map<String, Environment> environmentMap = APIUtil.getEnvironments(tenantDomain); |
| 98 | + OrganizationGatewayDto newOrganizationGatewayDto = new OrganizationGatewayDto(); |
| 99 | + for (Map.Entry<String, Environment> entry : environmentMap.entrySet()) { |
| 100 | + Environment environment = entry.getValue(); |
| 101 | + if (environment.getProvider().equals(APIConstants.EXTERNAL_GATEWAY_VENDOR)) { |
| 102 | + GatewayDto gatewayDto = new GatewayDto(); |
| 103 | + gatewayDto.setName(entry.getKey()); |
| 104 | + GatewayAgentConfiguration gatewayAgentConfiguration = ServiceReferenceHolder.getInstance(). |
| 105 | + getExternalGatewayConnectorConfiguration(entry.getValue().getGatewayType()); |
| 106 | + GatewayDeployer deployer = (GatewayDeployer) Class.forName(gatewayAgentConfiguration.getImplementation()) |
| 107 | + .getDeclaredConstructor().newInstance(); |
| 108 | + deployer.init(APIUtil.extractGatewayConfiguration(entry.getValue(), tenantDomain)); |
| 109 | + gatewayDto.setGatewayDeployer(deployer); |
| 110 | + newOrganizationGatewayDto.putGatewayDto(gatewayDto); |
| 111 | + } |
| 112 | + } |
| 113 | + organizationWiseMap.put(tenantDomain, newOrganizationGatewayDto); |
| 114 | + return newOrganizationGatewayDto; |
| 115 | + } catch (APIManagementException | ClassNotFoundException | IllegalAccessException | InstantiationException |
| 116 | + | NoSuchMethodException | InvocationTargetException e) { |
| 117 | + log.error("Error while loading environments for tenant " + tenantDomain, e); |
| 118 | + return null; |
| 119 | + } |
| 120 | + } |
| 121 | + return organizationGatewayDto; |
| 122 | + } |
| 123 | + |
| 124 | + private static OrganizationGatewayDto getTenantGatewayDtoFromMap(String tenantDomain) { |
| 125 | + return organizationWiseMap.get(tenantDomain); |
| 126 | + } |
| 127 | +} |
0 commit comments