Java Code Examples for org.wso2.carbon.registry.core.Registry#newCollection()

The following examples show how to use org.wso2.carbon.registry.core.Registry#newCollection() . 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: SecurityMgtServiceComponent.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
private void addKeystores() throws RegistryException {
    Registry registry = SecurityServiceHolder.getRegistryService().getGovernanceSystemRegistry();
    try {
        boolean transactionStarted = Transaction.isStarted();
        if (!transactionStarted) {
            registry.beginTransaction();
        }
        if (!registry.resourceExists(SecurityConstants.KEY_STORES)) {
            Collection kstores = registry.newCollection();
            registry.put(SecurityConstants.KEY_STORES, kstores);

            Resource primResource = registry.newResource();
            if (!registry.resourceExists(RegistryResources.SecurityManagement.PRIMARY_KEYSTORE_PHANTOM_RESOURCE)) {
                registry.put(RegistryResources.SecurityManagement.PRIMARY_KEYSTORE_PHANTOM_RESOURCE,
                        primResource);
            }
        }
        if (!transactionStarted) {
            registry.commitTransaction();
        }
    } catch (Exception e) {
        registry.rollbackTransaction();
        throw e;
    }
}
 
Example 2
Source File: SecurityDeploymentInterceptor.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
private void addKeystores() throws RegistryException {
    Registry registry = SecurityServiceHolder.getRegistryService().getGovernanceSystemRegistry();
    try {
        boolean transactionStarted = Transaction.isStarted();
        if (!transactionStarted) {
            registry.beginTransaction();
        }
        if (!registry.resourceExists(SecurityConstants.KEY_STORES)) {
            Collection kstores = registry.newCollection();
            registry.put(SecurityConstants.KEY_STORES, kstores);

            Resource primResource = registry.newResource();
            if (!registry.resourceExists(RegistryResources.SecurityManagement.PRIMARY_KEYSTORE_PHANTOM_RESOURCE)) {
                registry.put(RegistryResources.SecurityManagement.PRIMARY_KEYSTORE_PHANTOM_RESOURCE,
                        primResource);
            }
        }
        if (!transactionStarted) {
            registry.commitTransaction();
        }
    } catch (Exception e) {
        registry.rollbackTransaction();
        throw e;
    }
}
 
Example 3
Source File: IdentityMgtServiceComponent.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
private static void init() {

        Registry registry;
        IdentityMgtConfig.getInstance(realmService.getBootstrapRealmConfiguration());
        recoveryProcessor = new RecoveryProcessor();
        try {
            registry = IdentityMgtServiceComponent.getRegistryService()
                    .getConfigSystemRegistry();
            if (!registry
                    .resourceExists(IdentityMgtConstants.IDENTITY_MANAGEMENT_PATH)) {
                Collection questionCollection = registry.newCollection();
                registry.put(IdentityMgtConstants.IDENTITY_MANAGEMENT_PATH,
                        questionCollection);
                loadDefaultChallenges();
            }
        } catch (RegistryException e) {
            log.error("Error while creating registry collection for org.wso2.carbon.identity.mgt component", e);
        }

    }
 
Example 4
Source File: DefaultPolicyDataStore.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
@Override
public void setPolicyData(String policyId, PolicyStoreDTO policyDataDTO) throws EntitlementException {

    Registry registry = getGovernanceRegistry();
    try {
        String path = policyDataCollection + policyId;
        Resource resource;
        if (registry.resourceExists(path)) {
            resource = registry.get(path);
        } else {
            resource = registry.newCollection();
        }

        if (policyDataDTO.isSetActive()) {
            resource.setProperty("active", Boolean.toString(policyDataDTO.isActive()));
        }
        if (policyDataDTO.isSetOrder()) {
            int order = policyDataDTO.getPolicyOrder();
            if (order > 0) {
                resource.setProperty("order", Integer.toString(order));
            }
        }
        registry.put(path, resource);
    } catch (RegistryException e) {
        log.error("Error while updating Policy data in policy store ", e);
        throw new EntitlementException("Error while updating Policy data in policy store");
    }
}
 
Example 5
Source File: ChallengeQuestionProcessor.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * @param questionDTOs
 * @throws IdentityException
 */
public void setChallengeQuestions(ChallengeQuestionDTO[] questionDTOs) throws IdentityException {
    Registry registry = null;
    try {
        int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
        registry = IdentityMgtServiceComponent.getRegistryService().getConfigSystemRegistry(tenantId);

        if (!registry.resourceExists(IdentityMgtConstants.IDENTITY_MANAGEMENT_PATH)) {
            Collection securityQuestionResource = registry.newCollection();
            registry.put(IdentityMgtConstants.IDENTITY_MANAGEMENT_PATH, securityQuestionResource);
        }
        Resource identityMgtResource = registry.get(IdentityMgtConstants.IDENTITY_MANAGEMENT_PATH);
        if (identityMgtResource != null) {
            String questionCollectionPath = IdentityMgtConstants.IDENTITY_MANAGEMENT_QUESTIONS;
            if (registry.resourceExists(questionCollectionPath)) {
                registry.delete(questionCollectionPath);
            }

            Collection questionCollection = registry.newCollection();
            registry.put(questionCollectionPath, questionCollection);

            for (int i = 0; i < questionDTOs.length; i++) {
                Resource resource = registry.newResource();
                resource.addProperty("question", questionDTOs[i].getQuestion());
                resource.addProperty("isPromoteQuestion", String.valueOf(questionDTOs[i].isPromoteQuestion()));
                resource.addProperty("questionSetId", questionDTOs[i].getQuestionSetId());
                registry.put(IdentityMgtConstants.IDENTITY_MANAGEMENT_QUESTIONS +
                        RegistryConstants.PATH_SEPARATOR + "question" + i +
                        RegistryConstants.PATH_SEPARATOR, resource);
            }
        }
    } catch (RegistryException e) {
        throw IdentityException.error("Error while setting challenge question.", e);
    }

}
 
Example 6
Source File: DefaultPolicyDataStore.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
@Override
public void setPolicyData(String policyId, PolicyStoreDTO policyDataDTO) throws EntitlementException {

    Registry registry = EntitlementServiceComponent.
            getGovernanceRegistry(CarbonContext.getThreadLocalCarbonContext().getTenantId());
    try {
        String path = policyDataCollection + policyId;
        Resource resource;
        if (registry.resourceExists(path)) {
            resource = registry.get(path);
        } else {
            resource = registry.newCollection();
        }
        resource.setMediaType(PDPConstants.REGISTRY_MEDIA_TYPE);
        if (policyDataDTO.isSetActive()) {
            resource.setProperty("active", Boolean.toString(policyDataDTO.isActive()));
        }
        if (policyDataDTO.isSetOrder()) {
            int order = policyDataDTO.getPolicyOrder();
            if (order > 0) {
                resource.setProperty("order", Integer.toString(order));
            }
        }
        registry.put(path, resource);
    } catch (RegistryException e) {
        log.error("Error while updating Policy data in policy store ", e);
        throw new EntitlementException("Error while updating Policy data in policy store");
    }
}
 
Example 7
Source File: ChallengeQuestionProcessor.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * @param questionDTOs
 * @throws IdentityException
 */
public void setChallengeQuestions(ChallengeQuestionDTO[] questionDTOs) throws IdentityException {
    Registry registry = null;
    try {
        int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
        registry = IdentityMgtServiceComponent.getRegistryService().getConfigSystemRegistry(tenantId);

        if (!registry.resourceExists(IdentityMgtConstants.IDENTITY_MANAGEMENT_PATH)) {
            Collection securityQuestionResource = registry.newCollection();
            registry.put(IdentityMgtConstants.IDENTITY_MANAGEMENT_PATH, securityQuestionResource);
        }
        Resource identityMgtResource = registry.get(IdentityMgtConstants.IDENTITY_MANAGEMENT_PATH);
        if (identityMgtResource != null) {
            String questionCollectionPath = IdentityMgtConstants.IDENTITY_MANAGEMENT_QUESTIONS;
            if (registry.resourceExists(questionCollectionPath)) {
                registry.delete(questionCollectionPath);
            }

            Collection questionCollection = registry.newCollection();
            registry.put(questionCollectionPath, questionCollection);

            for (int i = 0; i < questionDTOs.length; i++) {
                Resource resource = registry.newResource();
                resource.addProperty("question", questionDTOs[i].getQuestion());
                resource.addProperty("isPromoteQuestion", String.valueOf(questionDTOs[i].isPromoteQuestion()));
                resource.addProperty("questionSetId", questionDTOs[i].getQuestionSetId());
                registry.put(IdentityMgtConstants.IDENTITY_MANAGEMENT_QUESTIONS +
                        RegistryConstants.PATH_SEPARATOR + "question" + i +
                        RegistryConstants.PATH_SEPARATOR, resource);
            }
        }
    } catch (RegistryException e) {
        throw IdentityException.error("Error while setting challenge question.", e);
    }

}
 
Example 8
Source File: ManagementPermissionsAdder.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
public void addUIPermissionFromBundle(Bundle bundle) throws Exception {
    BundleContext bundleContext = bundle.getBundleContext();
    if (bundleContext == null) { // If the bundle got uninstalled, the bundleContext will be null
        return;
    }

    URL url = bundleContext.getBundle().getEntry("META-INF/component.xml");
    if (url == null) {
        return;
    }

    InputStream xmlStream = url.openStream();
    if (xmlStream == null) {
        return;
    }

    if (log.isDebugEnabled()) {
        log.debug("Adding permissions in bundle" + 
                bundle.getSymbolicName());
    }

    Component component = ComponentConfigFactory.build(xmlStream);
    ManagementPermission[] uiPermissions = null;
    if (component != null) {
        uiPermissions = (ManagementPermission[]) component
                .getComponentConfig(ManagementPermissionsBuilder.LOCALNAME_MGT_PERMISSIONS);
    }

    if (uiPermissions != null) {
        // at the starup we are only adding permission only to tenant 0
        Registry registry = UserMgtDSComponent.getRegistryService().getGovernanceSystemRegistry();
        for (ManagementPermission uiPermission : uiPermissions) {
            if (registry.resourceExists(uiPermission.getResourceId())) {
                Resource existingResource = registry.get(uiPermission.getResourceId());
                if (existingResource.getProperty(UserMgtConstants.DISPLAY_NAME) == null) {
                    existingResource.setProperty(UserMgtConstants.DISPLAY_NAME, uiPermission.getDisplayName());
                    registry.put(uiPermission.getResourceId(), existingResource);
                }
                continue;
            }
            Collection resource = registry.newCollection();
            resource.setProperty(UserMgtConstants.DISPLAY_NAME, uiPermission.getDisplayName());
            registry.put(uiPermission.getResourceId(), resource);
        }
    }
}
 
Example 9
Source File: ManagementPermissionsAdder.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
public void addUIPermissionFromBundle(Bundle bundle) throws Exception {
    BundleContext bundleContext = bundle.getBundleContext();
    if (bundleContext == null) { // If the bundle got uninstalled, the bundleContext will be null
        return;
    }

    URL url = bundleContext.getBundle().getEntry("META-INF/component.xml");
    if (url == null) {
        return;
    }

    InputStream xmlStream = url.openStream();
    if (xmlStream == null) {
        return;
    }

    if (log.isDebugEnabled()) {
        log.debug("Adding permissions in bundle" + 
                bundle.getSymbolicName());
    }

    Component component = ComponentConfigFactory.build(xmlStream);
    ManagementPermission[] uiPermissions = null;
    if (component != null) {
        uiPermissions = (ManagementPermission[]) component
                .getComponentConfig(ManagementPermissionsBuilder.LOCALNAME_MGT_PERMISSIONS);
    }

    if (uiPermissions != null) {
        // at the starup we are only adding permission only to tenant 0
        Registry registry = UserMgtDSComponent.getRegistryService().getGovernanceSystemRegistry();
        for (ManagementPermission uiPermission : uiPermissions) {
            if (registry.resourceExists(uiPermission.getResourceId())) {
                continue;
            }
            Collection resource = registry.newCollection();
            resource.setProperty(UserMgtConstants.DISPLAY_NAME, uiPermission.getDisplayName());
            registry.put(uiPermission.getResourceId(), resource);
        }
    }
}
 
Example 10
Source File: MetadataApiRegistry.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public void addPropertyToApplication(String applicationId, Property property)
        throws RegistryException, MetadataException {
    Registry registry = getRegistry();
    String resourcePath = mainResource + applicationId;

    try {
        acquireWriteLock(applicationId);
        // We are using only super tenant registry to persist
        PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        Resource nodeResource;
        if (registry.resourceExists(resourcePath)) {
            nodeResource = registry.get(resourcePath);
        } else {
            nodeResource = registry.newCollection();
            if (log.isDebugEnabled()) {
                log.debug(String.format("Registry resource created: [resource-path] %s", resourcePath));
            }
        }

        boolean updated = false;
        for (String value : property.getValues()) {
            if (!propertyValueExist(nodeResource, property.getKey(), value)) {
                updated = true;
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Registry property updated: [resource-path] %s, [key] %s [value] %s",
                            resourcePath, property.getKey(), value));
                }
                nodeResource.addProperty(property.getKey(), value);
            } else {
                if (log.isDebugEnabled()) {
                    log.debug(
                            String.format("Registry value already exists: [resource-path] %s, [key] %s, [value] %s",
                                    resourcePath, property.getKey(), value));
                }
            }
        }
        if (updated) {
            registry.put(resourcePath, nodeResource);
            if (log.isDebugEnabled()) {
                log.debug(String.format("Registry property is persisted: [resource-path] %s, [key] %s, [values] %s",
                        resourcePath, property.getKey(), Arrays.asList(property.getValues())));
            }
        }
    } catch (Exception e) {
        String msg = String
                .format("Failed to persist properties in registry: [resource-path] %s, [key] %s, [values] %s",
                        resourcePath, property.getKey(), Arrays.asList(property.getValues()));
        log.error(msg, e);
        throw new MetadataException(msg, e);
    } finally {
        try {
            releaseWriteLock(applicationId);
        } catch (MetadataException ignored) {
        }
    }
}