<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.stratos.metadata.service.registry;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.stratos.manager.internal.ServiceReferenceHolder;
import org.apache.stratos.metadata.service.definition.NewProperty;
import org.wso2.carbon.registry.api.Registry;
import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.api.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import java.util.*;


/**
 * Carbon registry implementation
 *
 */

public class CarbonRegistry implements DataStore {

    private static Log log = LogFactory.getLog(CarbonRegistry.class);
    @Context
    HttpServletRequest httpServletRequest;

    private static final String mainResource = "/stratos/";

    public CarbonRegistry() {
    }


    /**
     * Get Properties of clustor
     * @param applicationName
     * @param clusterId
     * @return
     * @throws RegistryException
     */
    public List<NewProperty> getPropertiesOfCluster(String applicationName, String clusterId) throws RegistryException {
        Registry tempRegistry = ServiceReferenceHolder.getRegistryService().getRegistry();
        String resourcePath = mainResource + applicationName + "/" + clusterId;
        if (!tempRegistry.resourceExists(resourcePath)) {
            return null;
            //throw new RegistryException("Cluster does not exist at " + resourcePath);
        }
        Resource regResource = tempRegistry.get(resourcePath);

        ArrayList<NewProperty> newProperties = new ArrayList<NewProperty>();

        Properties props = regResource.getProperties();
        Enumeration<?> x = props.propertyNames();
        while (x.hasMoreElements()) {
            String key = (String) x.nextElement();
            List<String> values = regResource.getPropertyValues(key);
            NewProperty property = new NewProperty();
            property.setKey(key);
            String[] valueArr = new String[values.size()];
            property.setValues(values.toArray(valueArr));

            newProperties.add(property);
        }

        return newProperties;
    }

    /**
     * Add property to cluster
     * @param applicationId
     * @param clusterId
     * @param property
     * @throws RegistryException
     */
    public void addPropertyToCluster(String applicationId, String clusterId, NewProperty property) throws RegistryException {
        Registry tempRegistry = ServiceReferenceHolder.getRegistryService().getRegistry();
        String resourcePath = mainResource + applicationId + "/" + clusterId;
        Resource regResource = createOrGetResourceforCluster(tempRegistry, resourcePath);

        regResource.setProperty(property.getKey(), Arrays.asList(property.getValues()));
        tempRegistry.put(resourcePath, regResource);
        log.info(String.format("Property %s is added to cluster %s of application %s", property.getKey(), clusterId, applicationId));

    }

    /**
     * Delete the resource identified by the applicationId, if exist.
     * @param applicationId ID of the application.
     * @return True if resource exist and able to delete, else false.
     * @throws RegistryException
     */
    public boolean deleteApplication(String applicationId) throws RegistryException {
        if(StringUtils.isBlank(applicationId)){
            throw new IllegalArgumentException("Application ID can not be null");
        }
        Registry tempRegistry = ServiceReferenceHolder.getRegistryService().getRegistry();
        String resourcePath = mainResource + applicationId;

        if(tempRegistry.resourceExists(resourcePath)){
            tempRegistry.delete(resourcePath);
            log.info(String.format("Application removed from registry %s", applicationId));
            return true;
        }

        return false;
    }

    /**
     * Add properties to cluster
     * @param applicationName
     * @param clusterId
     * @param properties
     * @throws RegistryException
     */
    public void addPropertiesToCluster(String applicationName, String clusterId, NewProperty[] properties) throws RegistryException {
        Registry tempRegistry = ServiceReferenceHolder.getRegistryService().getRegistry();
        String resourcePath = mainResource + applicationName + "/" + clusterId;<commit_msg>Fixing the issues related to the governance registry meta data update<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.stratos.metadata.service.registry;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.stratos.manager.internal.ServiceReferenceHolder;
import org.apache.stratos.metadata.service.definition.NewProperty;
import org.wso2.carbon.registry.api.Registry;
import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.api.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import java.util.*;


/**
 * Carbon registry implementation
 *
 */

public class CarbonRegistry implements DataStore {

    private static Log log = LogFactory.getLog(CarbonRegistry.class);
    @Context
    HttpServletRequest httpServletRequest;

    private static final String mainResource = "/stratos/";

    public CarbonRegistry() {
    }


    /**
     * Get Properties of clustor
     * @param applicationName
     * @param clusterId
     * @return
     * @throws RegistryException
     */
    public List<NewProperty> getPropertiesOfCluster(String applicationName, String clusterId) throws RegistryException {
        Registry tempRegistry = ServiceReferenceHolder.getInstance().getRegistryService().getRegistry();
        String resourcePath = mainResource + applicationName + "/" + clusterId;
        if (!tempRegistry.resourceExists(resourcePath)) {
            return null;
            //throw new RegistryException("Cluster does not exist at " + resourcePath);
        }
        Resource regResource = tempRegistry.get(resourcePath);

        ArrayList<NewProperty> newProperties = new ArrayList<NewProperty>();

        Properties props = regResource.getProperties();
        Enumeration<?> x = props.propertyNames();
        while (x.hasMoreElements()) {
            String key = (String) x.nextElement();
            List<String> values = regResource.getPropertyValues(key);
            NewProperty property = new NewProperty();
            property.setKey(key);
            String[] valueArr = new String[values.size()];
            property.setValues(values.toArray(valueArr));

            newProperties.add(property);
        }

        return newProperties;
    }

    /**
     * Add property to cluster
     * @param applicationId
     * @param clusterId
     * @param property
     * @throws RegistryException
     */
    public void addPropertyToCluster(String applicationId, String clusterId, NewProperty property) throws RegistryException {
        Registry tempRegistry = ServiceReferenceHolder.getInstance().getRegistryService().getRegistry();
        String resourcePath = mainResource + applicationId + "/" + clusterId;
        Resource regResource = createOrGetResourceforCluster(tempRegistry, resourcePath);

        regResource.setProperty(property.getKey(), Arrays.asList(property.getValues()));
        tempRegistry.put(resourcePath, regResource);
        log.info(String.format("Property %s is added to cluster %s of application %s", property.getKey(), clusterId, applicationId));

    }

    /**
     * Delete the resource identified by the applicationId, if exist.
     * @param applicationId ID of the application.
     * @return True if resource exist and able to delete, else false.
     * @throws RegistryException
     */
    public boolean deleteApplication(String applicationId) throws RegistryException {
        if(StringUtils.isBlank(applicationId)){
            throw new IllegalArgumentException("Application ID can not be null");
        }
        Registry tempRegistry = ServiceReferenceHolder.getInstance().getRegistryService().getRegistry();
        String resourcePath = mainResource + applicationId;

        if(tempRegistry.resourceExists(resourcePath)){
            tempRegistry.delete(resourcePath);
            log.info(String.format("Application removed from registry %s", applicationId));
            return true;
        }

        return false;
    }

    /**
     * Add properties to cluster
     * @param applicationName
     * @param clusterId
     * @param properties
     * @throws RegistryException
     */
    public void addPropertiesToCluster(String applicationName, String clusterId, NewProperty[] properties) throws RegistryException {
        Registry tempRegistry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry();
        String resourcePath = mainResource + applicationName + "/" + clusterId;
{'fraction_non_alphanumeric': 0.08732714529266906, 'fraction_numerical': 0.0013260087137715476, 'mean_word_length': 3.416143872856545, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5716952', 'n_tokens_mistral': 2551, 'n_tokens_neox': 2475, 'n_words': 765}