Skip to content

Commit a064d66

Browse files
committed
--amend
1 parent d5bb435 commit a064d66

File tree

12 files changed

+324
-75
lines changed

12 files changed

+324
-75
lines changed

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIAdminImpl.java

+22-3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO;
7171
import org.wso2.carbon.apimgt.impl.dao.LabelsDAO;
7272
import org.wso2.carbon.apimgt.impl.dao.constants.SQLConstants;
73+
import org.wso2.carbon.apimgt.impl.deployer.GatewayConfigurationService;
74+
import org.wso2.carbon.apimgt.impl.deployer.GatewayConfigurationServiceImpl;
7375
import org.wso2.carbon.apimgt.impl.dto.ThrottleProperties;
7476
import org.wso2.carbon.apimgt.impl.dto.WorkflowProperties;
7577
import org.wso2.carbon.apimgt.impl.factory.PersistenceFactory;
@@ -117,8 +119,6 @@
117119
import java.util.TimeZone;
118120
import java.util.TreeSet;
119121
import java.util.UUID;
120-
import java.util.regex.Matcher;
121-
import java.util.regex.Pattern;
122122
import java.util.stream.Collectors;
123123

124124
import javax.xml.XMLConstants;
@@ -199,7 +199,14 @@ public Environment addEnvironment(String tenantDomain, Environment environment)
199199
validateForUniqueVhostNames(environment);
200200
Environment environmentToStore = new Environment(environment);
201201
encryptGatewayConfigurationValues(null, environmentToStore);
202-
return apiMgtDAO.addEnvironment(tenantDomain, environment);
202+
apiMgtDAO.addEnvironment(tenantDomain, environment);
203+
204+
if (environment.getProvider().equals(APIConstants.EXTERNAL_GATEWAY_VENDOR)) {
205+
GatewayConfigurationService gatewayConfigurationService = new GatewayConfigurationServiceImpl();
206+
gatewayConfigurationService.addGatewayConfiguration(tenantDomain, environment.getName(),
207+
environment.getGatewayType(), APIUtil.extractGatewayConfiguration(environment, tenantDomain));
208+
}
209+
return environment;
203210
}
204211

205212
@Override
@@ -213,6 +220,11 @@ public void deleteEnvironment(String tenantDomain, String uuid) throws APIManage
213220
ExceptionCodes.from(ExceptionCodes.READONLY_GATEWAY_ENVIRONMENT, String.format("UUID '%s'", uuid)));
214221
}
215222
apiMgtDAO.deleteEnvironment(uuid);
223+
224+
if (existingEnv.getProvider().equals(APIConstants.EXTERNAL_GATEWAY_VENDOR)) {
225+
GatewayConfigurationService gatewayConfigurationService = new GatewayConfigurationServiceImpl();
226+
gatewayConfigurationService.removeGatewayConfiguration(tenantDomain, existingEnv.getName());
227+
}
216228
}
217229

218230
@Override
@@ -250,6 +262,13 @@ public Environment updateEnvironment(String tenantDomain, Environment environmen
250262
// Perform a separate task of updating gateway label names
251263
updateGatewayLabelNameForGatewayPolicies(existingEnv.getDisplayName(), updatedEnvironment.getDisplayName(),
252264
tenantDomain);
265+
266+
if (environment.getProvider().equals(APIConstants.EXTERNAL_GATEWAY_VENDOR)) {
267+
GatewayConfigurationService gatewayConfigurationService = new GatewayConfigurationServiceImpl();
268+
gatewayConfigurationService.updateGatewayConfiguration(tenantDomain, environment.getName(),
269+
environment.getType(), APIUtil.extractGatewayConfiguration(environment, tenantDomain));
270+
}
271+
253272
return updatedEnvironment;
254273
}
255274

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java

+21-23
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
import org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO;
104104
import org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO;
105105
import org.wso2.carbon.apimgt.impl.dto.WorkflowDTO;
106+
import org.wso2.carbon.apimgt.impl.factory.GatewayHolder;
106107
import org.wso2.carbon.apimgt.impl.factory.KeyManagerHolder;
107108
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
108109
import org.wso2.carbon.apimgt.impl.monetization.DefaultMonetizationImpl;
@@ -3627,30 +3628,27 @@ private Map<String, String> getHostWithSchemeMappingForEnvironment(API api, Stri
36273628
Map<String, GatewayAgentConfiguration> gatewayConfigurations =
36283629
ServiceReferenceHolder.getInstance().getExternalGatewayConnectorConfigurations();
36293630
GatewayAgentConfiguration gatewayConfiguration = gatewayConfigurations.get(environment.getGatewayType());
3630-
try {
3631-
boolean isExternalGateway = false;
3632-
GatewayDeployer gatewayDeployer = null;
3633-
if (gatewayConfiguration != null && StringUtils.isNotEmpty(gatewayConfiguration.getImplementation())) {
3634-
gatewayDeployer = (GatewayDeployer) Class.forName(
3635-
gatewayConfiguration.getImplementation()).getDeclaredConstructor().newInstance();
3636-
isExternalGateway = true;
3637-
}
36383631

3639-
if (StringUtils.containsIgnoreCase(api.getTransports(), APIConstants.HTTP_PROTOCOL)
3640-
&& vhost.getHttpPort() != -1) {
3641-
String httpUrl = isExternalGateway ? gatewayDeployer.getAPIExecutionURL(api.getUuid()) :
3642-
vhost.getHttpUrl();
3643-
hostsWithSchemes.put(APIConstants.HTTP_PROTOCOL, httpUrl);
3644-
}
3645-
if (StringUtils.containsIgnoreCase(api.getTransports(), APIConstants.HTTPS_PROTOCOL)
3646-
&& vhost.getHttpsPort() != -1) {
3647-
String httpsUrl = isExternalGateway ? gatewayDeployer.getAPIExecutionURL(api.getUuid()) :
3648-
vhost.getHttpsUrl();
3649-
hostsWithSchemes.put(APIConstants.HTTPS_PROTOCOL, httpsUrl);
3650-
}
3651-
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException
3652-
| InvocationTargetException e) {
3653-
throw new APIManagementException(e.getMessage());
3632+
boolean isExternalGateway = false;
3633+
GatewayDeployer gatewayDeployer = null;
3634+
if (gatewayConfiguration != null && StringUtils.isNotEmpty(gatewayConfiguration.getImplementation())) {
3635+
gatewayDeployer = GatewayHolder.getTenantGatewayInstance(tenantDomain, environmentName);
3636+
isExternalGateway = true;
3637+
}
3638+
3639+
String externalReference = APIUtil.getApiExternalApiMappingReferenceByApiId(api.getUuid(),
3640+
environment.getUuid());
3641+
if (StringUtils.containsIgnoreCase(api.getTransports(), APIConstants.HTTP_PROTOCOL)
3642+
&& vhost.getHttpPort() != -1) {
3643+
String httpUrl = isExternalGateway ? gatewayDeployer.getAPIExecutionURL(externalReference) :
3644+
vhost.getHttpUrl();
3645+
hostsWithSchemes.put(APIConstants.HTTP_PROTOCOL, httpUrl);
3646+
}
3647+
if (StringUtils.containsIgnoreCase(api.getTransports(), APIConstants.HTTPS_PROTOCOL)
3648+
&& vhost.getHttpsPort() != -1) {
3649+
String httpsUrl = isExternalGateway ? gatewayDeployer.getAPIExecutionURL(externalReference) :
3650+
vhost.getHttpsUrl();
3651+
hostsWithSchemes.put(APIConstants.HTTPS_PROTOCOL, httpsUrl);
36543652
}
36553653
}
36563654
return hostsWithSchemes;

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
import org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO;
138138
import org.wso2.carbon.apimgt.impl.dto.WorkflowDTO;
139139
import org.wso2.carbon.apimgt.impl.dto.WorkflowProperties;
140+
import org.wso2.carbon.apimgt.impl.factory.GatewayHolder;
140141
import org.wso2.carbon.apimgt.impl.factory.KeyManagerHolder;
141142
import org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.ArtifactSaver;
142143
import org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.wso2.carbon.apimgt.impl.deployer;
2+
3+
import org.wso2.carbon.apimgt.api.APIManagementException;
4+
import org.wso2.carbon.apimgt.api.model.GatewayConfiguration;
5+
6+
public interface GatewayConfigurationService {
7+
void addGatewayConfiguration(String organization, String name, String type,
8+
GatewayConfiguration gatewayConfiguration) throws APIManagementException;
9+
10+
void updateGatewayConfiguration(String organization, String name, String type,
11+
GatewayConfiguration gatewayConfiguration) throws APIManagementException;
12+
13+
void removeGatewayConfiguration(String tenantDomain, String name) throws APIManagementException;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.wso2.carbon.apimgt.impl.deployer;
2+
3+
import org.wso2.carbon.apimgt.api.APIManagementException;
4+
import org.wso2.carbon.apimgt.api.model.GatewayConfiguration;
5+
import org.wso2.carbon.apimgt.impl.factory.GatewayHolder;
6+
7+
public class GatewayConfigurationServiceImpl implements GatewayConfigurationService {
8+
@Override
9+
public void addGatewayConfiguration(String organization, String name, String type,
10+
GatewayConfiguration gatewayConfiguration) throws APIManagementException {
11+
String internKey = this.getClass().getName().concat(organization).concat(name);
12+
synchronized (internKey.intern()) {
13+
GatewayHolder.addGatewayConfiguration(organization, name, type, gatewayConfiguration);
14+
}
15+
}
16+
17+
@Override
18+
public void updateGatewayConfiguration(String organization, String name, String type,
19+
GatewayConfiguration gatewayConfiguration) throws APIManagementException {
20+
String internKey = this.getClass().getName().concat(organization).concat(name);
21+
synchronized (internKey.intern()) {
22+
GatewayHolder.updateGatewayConfiguration(organization, name, type, gatewayConfiguration);
23+
}
24+
}
25+
26+
@Override
27+
public void removeGatewayConfiguration(String tenantDomain, String name) throws APIManagementException {
28+
String internKey = this.getClass().getName().concat(tenantDomain).concat(name);
29+
synchronized (internKey.intern()) {
30+
GatewayHolder.removeGatewayConfiguration(tenantDomain, name);
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.wso2.carbon.apimgt.impl.dto;
2+
3+
import org.wso2.carbon.apimgt.api.model.GatewayDeployer;
4+
5+
public class GatewayDto {
6+
private String name;
7+
private GatewayDeployer gatewayDeployer;
8+
9+
public String getName() {
10+
return name;
11+
}
12+
13+
public void setName(String name) {
14+
this.name = name;
15+
}
16+
17+
public GatewayDeployer getGatewayDeployer() {
18+
return gatewayDeployer;
19+
}
20+
21+
public void setGatewayDeployer(GatewayDeployer gatewayDeployer) {
22+
this.gatewayDeployer = gatewayDeployer;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.wso2.carbon.apimgt.impl.dto;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
public class OrganizationGatewayDto {
7+
private Map<String, GatewayDto> gatewayMap = new LinkedHashMap<>();
8+
9+
public Map<String, GatewayDto> getGatewayMap() {
10+
return gatewayMap;
11+
}
12+
13+
public void setGatewayMap(Map<String, GatewayDto> gatewayMap) {
14+
this.gatewayMap = gatewayMap;
15+
}
16+
17+
public void putGatewayDto(GatewayDto gatewayDto) {
18+
gatewayMap.put(gatewayDto.getName(), gatewayDto);
19+
}
20+
21+
public GatewayDto getGatewayByName(String name) {
22+
return gatewayMap.get(name);
23+
}
24+
25+
public void removeGatewayDtoByName(String name) {
26+
27+
GatewayDto gatewayDto = gatewayMap.get(name);
28+
if (gatewayDto != null) {
29+
gatewayMap.remove(name);
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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

Comments
 (0)