Tuesday, January 10, 2017

Update Registry Properties Using Class Mediator

The current implementation of updateResource(String key, Object value) method supports only for update the registry resources and not support for update the registry properties. Also that the synapse registry API has not provided direct method to update the registry properties.
However following logic inside the class mediator can be use to update the registry properties in terms of carbon mediation registry implementation.


 package Mypackage;  
 import org.apache.synapse.MessageContext;  
 import org.apache.synapse.mediators.AbstractMediator;  
 import org.wso2.carbon.registry.core.*;  
 import org.wso2.carbon.registry.core.service.RegistryService;  
 import org.wso2.carbon.registry.core.exceptions.RegistryException;  
 import org.wso2.carbon.context.CarbonContext;  
 import org.wso2.carbon.mediation.registry.RegistryServiceHolder;  
 public class RegistryPropertyUpdate extends AbstractMediator {  
      public static final String CONFIG_REGISTRY_PREFIX = "conf:";  
      public static final String GOVERNANCE_REGISTRY_PREFIX = "gov:";  
      public static final String LOCAL_REGISTRY_PREFIX = "local:";  
      private Registry configRegistry;  
      private Registry localRegistry;  
      private Registry governanceRegistry;  
      public RegistryPropertyUpdate() throws RegistryException {  
           RegistryService registryService = RegistryServiceHolder.getInstance()  
                     .getRegistryService();  
           configRegistry = registryService.getConfigSystemRegistry(CarbonContext  
                     .getThreadLocalCarbonContext().getTenantId());  
           governanceRegistry = registryService  
                     .getGovernanceSystemRegistry(CarbonContext  
                               .getThreadLocalCarbonContext().getTenantId());  
           localRegistry = registryService.getLocalRepository(CarbonContext  
                     .getThreadLocalCarbonContext().getTenantId());  
      }  
      public boolean mediate(MessageContext mc) {  
           try {  
                RegistryPropertyUpdate prop = new RegistryPropertyUpdate();  
                prop.updateRegistryProperty("gov:/tasks/EventAlertScheduledTask",  
                          "registry property modified", "lastAccessTime");  
           } catch (Exception e) {  
                e.printStackTrace();  
           }  
           return true;  
      }  
      public void updateRegistryProperty(String key, String content,  
                String propertyName) throws RegistryException {  
           Registry registry = getRegistry(key);  
           String resolvedKey = resolvePath(key);  
           try {  
                Resource resource;  
                resource = registry.newCollection();  
                resource.setProperty(propertyName, content);  
                registry.put(resolvedKey, resource);  
           } catch (RegistryException e) {  
                System.out.println("Error while saving a resource at " + e);  
           }  
      }  
      private Registry getRegistry(String path) {  
           if (path.startsWith(GOVERNANCE_REGISTRY_PREFIX)) {  
                return governanceRegistry;  
           } else if (path.startsWith(CONFIG_REGISTRY_PREFIX)) {  
                return configRegistry;  
           } else if (path.startsWith(LOCAL_REGISTRY_PREFIX)) {  
                return localRegistry;  
           } else {  
                return governanceRegistry;  
           }  
      }  
      private String resolvePath(String path) {  
           if (path.startsWith(GOVERNANCE_REGISTRY_PREFIX)) {  
                path = path.substring(GOVERNANCE_REGISTRY_PREFIX.length());  
           } else if (path.startsWith(CONFIG_REGISTRY_PREFIX)) {  
                path = path.substring(CONFIG_REGISTRY_PREFIX.length());  
           } else if (path.startsWith(LOCAL_REGISTRY_PREFIX)) {  
                path = path.substring(LOCAL_REGISTRY_PREFIX.length());  
           }  
           return path;  
      }  
 }  

we can pass the following parameters to the "updateRegistryProperty()" method.

 updateRegistryProperty(registry path, content need to update, property name);  

Example:

 updateRegistryProperty("gov:/tasks/EventAlertScheduledTask",  
                          "registry property modified", "lastAccessTime");  

No comments:

Post a Comment