As you all know in API Manager we have stored tiers and lot of other data in registry. In some scenarios we may need to modify and update before tenant user use it. In such cases we can write tenant service creator listener and do what we need. In this article we will see how we can change tiers.xml file before tenant load to system. Please note that with this change we cannot change tiers values from UI as this code replace it for each tenant load.
Java code.
CustomTenantServiceCreator.java
CustomObserverRegistryComponent.java
Complete source code for project
https://drive.google.com/file/d/0B3OmQJfm2Ft8b3cxU3QwU0MwdWM/view?usp=sharing
Once tenant loaded you will see updated values as follows.
Java code.
CustomTenantServiceCreator.java
package org.wso2.custom.observer.registry;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.utils.AbstractAxis2ConfigurationContextObserver;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.APIConstants;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
public class CustomTenantServiceCreator extends AbstractAxis2ConfigurationContextObserver {
private static final Log log = LogFactory.getLog(CustomTenantServiceCreator.class);
@Override
public void createdConfigurationContext(ConfigurationContext configurationContext) {
UserRegistry registry = null;
try {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
Resource resource = null;
resource = registry.newResource();
resource.setContent("");
InputStream inputStream =
CustomTenantServiceCreator.class.getResourceAsStream("/tiers.xml");
byte[] data = IOUtils.toByteArray(inputStream);
resource = registry.newResource();
resource.setContent(data);
registry.put(APIConstants.API_TIER_LOCATION, resource);
} catch (RegistryException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
CustomObserverRegistryComponent.java
package org.wso2.custom.observer.registry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService;
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
/**
* @scr.component name="org.wso2.custom.observer.services" immediate="true"
* @scr.reference name="api.manager.config.service"
* interface=
* "org.wso2.carbon.apimgt.impl.APIManagerConfigurationService"
* cardinality="1..1"
* policy="dynamic" bind="setAPIManagerConfigurationService"
* unbind="unsetAPIManagerConfigurationService"
*/
public class CustomObserverRegistryComponent {
private static final Log log = LogFactory.getLog(CustomObserverRegistryComponent.class);
public static final String TOPICS_ROOT = "forumtopics";
private static APIManagerConfiguration configuration = null;
protected void activate(ComponentContext componentContext) throws Exception {
if (log.isDebugEnabled()) {
log.debug("Forum Registry Component Activated");
}
try{
CustomTenantServiceCreator tenantServiceCreator = new CustomTenantServiceCreator();
BundleContext bundleContext = componentContext.getBundleContext();
bundleContext.registerService(Axis2ConfigurationContextObserver.class.getName(), tenantServiceCreator, null);
}catch(Exception e){
log.error("Could not activate Forum Registry Component " + e.getMessage());
throw e;
}
}
protected void setAPIManagerConfigurationService(APIManagerConfigurationService amcService) {
log.debug("API manager configuration service bound to the API host objects");
configuration = amcService.getAPIManagerConfiguration();
}
protected void unsetAPIManagerConfigurationService(APIManagerConfigurationService amcService) {
log.debug("API manager configuration service unbound from the API host objects");
configuration = null;
}
}
}
Complete source code for project
https://drive.google.com/file/d/0B3OmQJfm2Ft8b3cxU3QwU0MwdWM/view?usp=sharing
Once tenant loaded you will see updated values as follows.
No comments:
Post a Comment