Java Code Examples for org.wso2.carbon.registry.core.session.UserRegistry#put()

The following examples show how to use org.wso2.carbon.registry.core.session.UserRegistry#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GatewayUtils.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Delete the given registry property from the given tenant registry path
 *
 * @param propertyName property name
 * @param path         resource path
 * @param tenantDomain
 * @throws AxisFault
 */
public static void deleteRegistryProperty(String propertyName, String path, String tenantDomain)
        throws AxisFault {

    try {
        UserRegistry registry = getRegistry(tenantDomain);
        PrivilegedCarbonContext.startTenantFlow();
        if (tenantDomain != null && StringUtils.isNotEmpty(tenantDomain)) {
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        } else {
            PrivilegedCarbonContext.getThreadLocalCarbonContext()
                    .setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
        }
        Resource resource = registry.get(path);
        if (resource != null && resource.getProperty(propertyName) != null) {
            resource.removeProperty(propertyName);
            registry.put(resource.getPath(), resource);
            resource.discard();
        }
    } catch (RegistryException | APIManagementException e) {
        String msg = "Failed to delete secure endpoint password alias " + e.getMessage();
        throw new AxisFault(msg, e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
 
Example 2
Source File: RegistryTopicManager.java    From carbon-commons with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public TopicNode getTopicTree() throws EventBrokerException {
    try {
        UserRegistry userRegistry =
                this.registryService.getGovernanceSystemRegistry(EventBrokerHolder.getInstance().getTenantId());
        if (!userRegistry.resourceExists(topicStoragePath)) {
            userRegistry.put(topicStoragePath, userRegistry.newCollection());
        }
        Resource root = userRegistry.get(this.topicStoragePath);
        TopicNode rootTopic = new TopicNode("/", "/");
        buildTopicTree(rootTopic, (Collection) root, userRegistry);
        return rootTopic;
    } catch (RegistryException e) {
        throw new EventBrokerException(e.getMessage(), e);
    }
}
 
Example 3
Source File: RegistryConfigWriter.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
@Override
public void write(int tenantId, Properties props, String resourcePath)
        throws IdentityMgtConfigException {

    if(log.isDebugEnabled()) {
        log.debug("Writing data to registry path : " + resourcePath);
    }
    
    RegistryService registry = IdentityMgtServiceComponent.getRegistryService();
    try {
        UserRegistry userReg = registry.getConfigSystemRegistry(tenantId);
        Resource resource = userReg.newResource();
        Set<String> names = props.stringPropertyNames();
        // Only key value pairs exists and no multiple values exists a key.
        for (String keyName : names) {
            List<String> value = new ArrayList<String>();
            String valueStr = props.getProperty(keyName);
            
            if(log.isDebugEnabled()) {
                log.debug("Write key : " + keyName + " value : " + value);
            }
            
            // This is done due to casting to List in JDBCRegistryDao
            value.add(valueStr);
            resource.setProperty(keyName, value);
        }
        userReg.put(resourcePath, resource);

    } catch (RegistryException e) {
        throw new IdentityMgtConfigException(
                "Error occurred while writing data to registry path : " + resourcePath, e);
    }

}
 
Example 4
Source File: RegistryConfigWriter.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
@Override
public void write(int tenantId, Properties props, String resourcePath)
        throws IdentityMgtConfigException {

    if(log.isDebugEnabled()) {
        log.debug("Writing data to registry path : " + resourcePath);
    }
    
    RegistryService registry = IdentityMgtServiceComponent.getRegistryService();
    try {
        UserRegistry userReg = registry.getConfigSystemRegistry(tenantId);
        Resource resource = userReg.newResource();
        Set<String> names = props.stringPropertyNames();
        // Only key value pairs exists and no multiple values exists a key.
        for (String keyName : names) {
            List<String> value = new ArrayList<String>();
            String valueStr = props.getProperty(keyName);
            
            if(log.isDebugEnabled()) {
                log.debug("Write key : " + keyName + " value : " + value);
            }
            
            // This is done due to casting to List in JDBCRegistryDao
            value.add(valueStr);
            resource.setProperty(keyName, value);
        }
        userReg.put(resourcePath, resource);

    } catch (RegistryException e) {
        throw new IdentityMgtConfigException(
                "Error occurred while writing data to registry path : " + resourcePath, e);
    }

}
 
Example 5
Source File: GatewayUtils.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Add/Update the given registry property from the given tenant registry
 * path
 *
 * @param propertyName  property name
 * @param propertyValue property value
 * @param path          resource path
 * @param tenantDomain
 * @throws APIManagementException
 */
public static void setRegistryProperty(String propertyName, String propertyValue, String path, String tenantDomain)
        throws APIManagementException {

    UserRegistry registry = getRegistry(tenantDomain);
    PrivilegedCarbonContext.startTenantFlow();
    if (tenantDomain != null && StringUtils.isNotEmpty(tenantDomain)) {
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
    } else {
        PrivilegedCarbonContext.getThreadLocalCarbonContext()
                .setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
    }
    try {
        Resource resource = registry.get(path);
        // add or update property
        if (resource.getProperty(propertyName) != null) {
            resource.setProperty(propertyName, propertyValue);
        } else {
            resource.addProperty(propertyName, propertyValue);
        }
        registry.put(resource.getPath(), resource);
        resource.discard();
    } catch (RegistryException e) {
        throw new APIManagementException("Error while reading registry resource " + path + " for tenant " +
                tenantDomain);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
 
Example 6
Source File: CarbonRepositoryUtils.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Save the given DeploymentSynchronizerConfiguration to the registry. The target
 * configuration registry space will be selected using the specified tenant ID. As a result
 * the configuration will be stored in the configuration registry of the specified
 * tenant.
 *
 * @param config The configuration to be saved
 * @param tenantId Tenant ID to select the configuration registry
 * @throws org.wso2.carbon.registry.core.exceptions.RegistryException if an error occurs while accessing the registry
 */
public static void persistConfiguration(DeploymentSynchronizerConfiguration config,
                                        int tenantId) throws RegistryException {

    Resource resource;
    UserRegistry localRepository = getLocalRepository(tenantId);
    if (!localRepository.resourceExists(DeploymentSynchronizerConstants.CARBON_REPOSITORY)) {
        resource = localRepository.newResource();
    } else {
        resource = localRepository.get(DeploymentSynchronizerConstants.CARBON_REPOSITORY);
    }

    resource.setProperty(DeploymentSynchronizerConstants.AUTO_COMMIT_MODE,
            String.valueOf(config.isAutoCommit()));
    resource.setProperty(DeploymentSynchronizerConstants.AUTO_CHECKOUT_MODE,
            String.valueOf(config.isAutoCheckout()));
    resource.setProperty(DeploymentSynchronizerConstants.AUTO_SYNC_PERIOD,
            String.valueOf(config.getPeriod()));
    resource.setProperty(DeploymentSynchronizerConstants.USE_EVENTING,
            String.valueOf(config.isUseEventing()));
    resource.setProperty(DeploymentSynchronizerConstants.REPOSITORY_TYPE,
            config.getRepositoryType());
    resource.setContent(config.isEnabled() ? "enabled" : "disabled");

    //Get Repository specific configuration parameters from config object.
    RepositoryConfigParameter[] parameters = config.getRepositoryConfigParameters();

    if (parameters != null && parameters.length != 0) {
        //Save each Repository specific configuration parameter in registry.
        for (int i = 0; i < parameters.length; i++) {
            resource.setProperty(parameters[i].getName(), parameters[i].getValue());
        }
    }

    localRepository.put(DeploymentSynchronizerConstants.CARBON_REPOSITORY, resource);
    resource.discard();
}
 
Example 7
Source File: RegistrySubscriptionManager.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Adds topic storage path and index storage path to user registry.
 *
 * @param topicStoragePath topic storage path. the path where topics will be stored.
 * @param indexStoragePath index storage path.
 * @throws EventBrokerConfigurationException
 */
public RegistrySubscriptionManager(String topicStoragePath, String indexStoragePath)
        throws EventBrokerConfigurationException {

    this.registryService = EventBrokerHolder.getInstance().getRegistryService();
    this.topicStoragePath = topicStoragePath;
    this.indexStoragePath = indexStoragePath;

    // creates the the subscription index
    // when creating subscriptions we going to add entries to this this resource
    try {
        UserRegistry userRegistry =
                this.registryService.getGovernanceSystemRegistry(EventBrokerHolder.getInstance()
                                                                         .getTenantId());

        //create the topic storage path if it does not exists
        if (!userRegistry.resourceExists(this.topicStoragePath)) {
            userRegistry.put(this.topicStoragePath, userRegistry.newCollection());
        }

        // we need to create the index here only it is not exists.
        if (!userRegistry.resourceExists(this.indexStoragePath)) {
            userRegistry.put(this.indexStoragePath, userRegistry.newResource());
        }

    } catch (RegistryException e) {
        throw new EventBrokerConfigurationException("Cannot access the registry ", e);
    }
}
 
Example 8
Source File: RegistrySubscriptionManager.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void unSubscribe(String subscriptionID) throws EventBrokerException {
    try {
        UserRegistry userRegistry =
                this.registryService.getGovernanceSystemRegistry(EventBrokerHolder.getInstance()
                                                                         .getTenantId());
        String fullPath = this.indexStoragePath;
        if (userRegistry.resourceExists(fullPath)) {
            Resource topicIndexResource = userRegistry.get(fullPath);

            String topicName = topicIndexResource.getProperty(subscriptionID);
            // delete the subscriptions resource
            // if the registry is read only there can be situations where the the subscriptions
            // is not saved to registry and hence the topic name
            if (topicName != null) {
                String resourcePath =  getResourcePath(subscriptionID, topicName);
                if (userRegistry.resourceExists(resourcePath)) {
                    userRegistry.delete(resourcePath);
                }
                String jMSResourcePath =  getJMSSubResourcePath(subscriptionID, topicName);
                if (userRegistry.resourceExists(jMSResourcePath)) {
                    userRegistry.delete(jMSResourcePath);
                }
            }

            topicIndexResource.removeProperty(subscriptionID);

            userRegistry.put(fullPath, topicIndexResource);
        }

    } catch (RegistryException e) {
        throw new EventBrokerException("Cannot access the registry ", e);
    }
}
 
Example 9
Source File: APIManagerComponent.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
private void updateRegistryResourceContent(Resource resource, UserRegistry systemRegistry, String rxtDir, String rxtPath, String resourcePath) throws RegistryException, IOException {
    String rxt = FileUtil.readFileToString(rxtDir + File.separator + rxtPath);
    resource.setContent(rxt.getBytes(Charset.defaultCharset()));
    resource.setMediaType(APIConstants.RXT_MEDIA_TYPE);
    systemRegistry.put(resourcePath, resource);
}
 
Example 10
Source File: CloudServicesUtil.java    From carbon-commons with Apache License 2.0 4 votes vote down vote up
public static void setCloudServiceActive(boolean active,
                                         String cloudServiceName,
                                         int tenantId, CloudServiceConfig cloudServiceConfig)
                                                                                             throws Exception {
    if (cloudServiceConfig.getLabel() == null) {
        // for the non-labled services, we are not setting/unsetting the
        // service active
        return;
    }

    UserRegistry govRegistry =
            CloudCommonServiceComponent.getGovernanceSystemRegistry(
                    MultitenantConstants.SUPER_TENANT_ID);
    UserRegistry configRegistry = CloudCommonServiceComponent.getConfigSystemRegistry(tenantId);
    String cloudServiceInfoPath = StratosConstants.CLOUD_SERVICE_INFO_STORE_PATH +
                                  RegistryConstants.PATH_SEPARATOR + tenantId +
                                  RegistryConstants.PATH_SEPARATOR + cloudServiceName;
    
    Resource cloudServiceInfoResource;
    if (govRegistry.resourceExists(cloudServiceInfoPath)) {
        cloudServiceInfoResource = govRegistry.get(cloudServiceInfoPath);
    } else {
        cloudServiceInfoResource = govRegistry.newCollection();
    }
    cloudServiceInfoResource.setProperty(StratosConstants.CLOUD_SERVICE_IS_ACTIVE_PROP_KEY,
                                         active ? "true" : "false");
    govRegistry.put(cloudServiceInfoPath, cloudServiceInfoResource);

    // then we will copy the permissions
    List<PermissionConfig> permissionConfigs = cloudServiceConfig.getPermissionConfigs();
    for (PermissionConfig permissionConfig : permissionConfigs) {
        String path = permissionConfig.getPath();
        String name = permissionConfig.getName();
        if (active) {
            if (!configRegistry.resourceExists(path)) {
                Collection collection = configRegistry.newCollection();
                collection.setProperty(StratosConstants.DISPLAY_NAME, name);
                configRegistry.put(path, collection);
            }
        } else {
            if (configRegistry.resourceExists(path)) {
                configRegistry.delete(path);
            }
        }
    }
}
 
Example 11
Source File: RegistrySubscriptionManager.java    From carbon-commons with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void addSubscription(Subscription subscription) throws EventBrokerException {
    try {
        UserRegistry userRegistry =
                this.registryService.getGovernanceSystemRegistry(EventBrokerHolder.getInstance()
                                                                         .getTenantId());
        String resourcePath =
                getResourcePath(subscription.getId(), subscription.getTopicName());

        Resource resource = userRegistry.newResource();
        resource.setProperty(EventBrokerConstants.EB_RES_SUBSCRIPTION_URL, subscription
                .getEventSinkURL());
        resource.setProperty(EventBrokerConstants.EB_RES_EVENT_DISPATCHER_NAME, subscription
                .getEventDispatcherName());

        if (subscription.getExpires() != null) {
            resource.setProperty(EventBrokerConstants.EB_RES_EXPIRS, ConverterUtil
                    .convertToString(subscription
                                             .getExpires()));
        }
        resource.setProperty(EventBrokerConstants.EB_RES_OWNER, subscription.getOwner());
        resource.setProperty(EventBrokerConstants.EB_RES_TOPIC_NAME, subscription
                .getTopicName());
        resource.setProperty(EventBrokerConstants.EB_RES_CREATED_TIME,
                             System.currentTimeMillis() + "");
        resource.setProperty(EventBrokerConstants.EB_RES_MODE, JavaUtil
                .getSubscriptionMode(subscription
                                             .getTopicName()));

        //set the other properties of the subscription.
        Map<String, String> properties = subscription.getProperties();
        for (String key : properties.keySet()) {
            resource.setProperty(key, properties.get(key));
        }

        userRegistry.put(resourcePath, resource);

        // add the subscription index
        String fullPath = this.indexStoragePath;
        Resource topicIndexResource;
        if (userRegistry.resourceExists(fullPath)) {
            topicIndexResource = userRegistry.get(fullPath);
            topicIndexResource.addProperty(subscription.getId(), subscription.getTopicName());
        } else {
            topicIndexResource = userRegistry.newResource();
            topicIndexResource.addProperty(subscription.getId(), subscription.getTopicName());
        }
        userRegistry.put(fullPath, topicIndexResource);

    } catch (RegistryException e) {
        throw new EventBrokerException("Cannot save to registry ", e);
    }

}
 
Example 12
Source File: CloudServicesUtil.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public static void setCloudServiceActive(boolean active,
                                         String cloudServiceName,
                                         int tenantId, CloudServiceConfig cloudServiceConfig)
        throws Exception {
    if (cloudServiceConfig.getLabel() == null) {
        // for the non-labled services, we are not setting/unsetting the
        // service active
        return;
    }

    UserRegistry govRegistry =
            ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(
                    MultitenantConstants.SUPER_TENANT_ID);
    UserRegistry configRegistry = ServiceReferenceHolder.getInstance().getRegistryService().getConfigSystemRegistry(tenantId);
    String cloudServiceInfoPath = StratosConstants.CLOUD_SERVICE_INFO_STORE_PATH +
            RegistryConstants.PATH_SEPARATOR + tenantId +
            RegistryConstants.PATH_SEPARATOR + cloudServiceName;

    Resource cloudServiceInfoResource;
    if (govRegistry.resourceExists(cloudServiceInfoPath)) {
        cloudServiceInfoResource = govRegistry.get(cloudServiceInfoPath);
    } else {
        cloudServiceInfoResource = govRegistry.newCollection();
    }
    cloudServiceInfoResource.setProperty(StratosConstants.CLOUD_SERVICE_IS_ACTIVE_PROP_KEY,
            active ? "true" : "false");
    govRegistry.put(cloudServiceInfoPath, cloudServiceInfoResource);

    // then we will copy the permissions
    List<PermissionConfig> permissionConfigs = cloudServiceConfig.getPermissionConfigs();
    for (PermissionConfig permissionConfig : permissionConfigs) {
        String path = permissionConfig.getPath();
        String name = permissionConfig.getName();
        if (active) {
            if (!configRegistry.resourceExists(path)) {
                Collection collection = configRegistry.newCollection();
                collection.setProperty(UserMgtConstants.DISPLAY_NAME, name);
                configRegistry.put(path, collection);
            }
        } else {
            if (configRegistry.resourceExists(path)) {
                configRegistry.delete(path);
            }
        }
    }
}