removing unsued apis from metadata service
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/da776459 Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/da776459 Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/da776459 Branch: refs/heads/master Commit: da776459f23c008e4f0e59d34392d923ecffe970 Parents: 54de45e Author: Udara Liyanage <[email protected]> Authored: Tue May 12 22:17:30 2015 +0530 Committer: Udara Liyanage <[email protected]> Committed: Tue May 12 22:36:49 2015 +0530 ---------------------------------------------------------------------- .../service/definition/NewProperty.java | 67 -------------- .../metadata/service/definition/Property.java | 67 ++++++++++++++ .../service/registry/CarbonRegistry.java | 81 +++++++++-------- .../metadata/service/registry/DataStore.java | 16 +--- .../service/services/MetaDataAdmin.java | 96 +++++--------------- 5 files changed, 137 insertions(+), 190 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/da776459/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/definition/NewProperty.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/definition/NewProperty.java b/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/definition/NewProperty.java deleted file mode 100644 index d6627c3..0000000 --- a/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/definition/NewProperty.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * 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.definition; - -import javax.xml.bind.annotation.XmlRootElement; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -@XmlRootElement(name = "properties") -public class NewProperty implements Serializable { - - private String key; - private List<String> values = new ArrayList<String>(); - - public NewProperty() { - } - - public NewProperty(String key, String value) { - this.key = key; - this.values.add(value); - } - - public String getKey() { - return key; - } - - public void setKey(String key) { - this.key = key; - } - - public String[] getValues() { - String[] values = new String[this.values.size()]; - values = this.values.toArray(values); - return values; - } - - public void setValues(String value) { - this.values.add(value); - } - - public void setValues(String[] values) { - this.values.addAll(Arrays.asList(values)); - } - - public void addValue(String value) { - this.values.add(value); - } -} http://git-wip-us.apache.org/repos/asf/stratos/blob/da776459/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/definition/Property.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/definition/Property.java b/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/definition/Property.java new file mode 100644 index 0000000..3ceb942 --- /dev/null +++ b/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/definition/Property.java @@ -0,0 +1,67 @@ +/** + * 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.definition; + +import javax.xml.bind.annotation.XmlRootElement; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +@XmlRootElement(name = "properties") +public class Property implements Serializable { + + private String key; + private List<String> values = new ArrayList<String>(); + + public Property() { + } + + public Property(String key, String value) { + this.key = key; + this.values.add(value); + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String[] getValues() { + String[] values = new String[this.values.size()]; + values = this.values.toArray(values); + return values; + } + + public void setValues(String value) { + this.values.add(value); + } + + public void setValues(String[] values) { + this.values.addAll(Arrays.asList(values)); + } + + public void addValue(String value) { + this.values.add(value); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/da776459/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/CarbonRegistry.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/CarbonRegistry.java b/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/CarbonRegistry.java index d384e80..d83edf2 100644 --- a/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/CarbonRegistry.java +++ b/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/CarbonRegistry.java @@ -21,7 +21,7 @@ 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.metadata.service.definition.NewProperty; +import org.apache.stratos.metadata.service.definition.Property; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.registry.core.Resource; @@ -44,11 +44,12 @@ public class CarbonRegistry implements DataStore { private static Log log = LogFactory.getLog(CarbonRegistry.class); @Context HttpServletRequest httpServletRequest; + private static int count = 0; public CarbonRegistry() { } - public List<NewProperty> getApplicationProperties(String applicationName) throws RegistryException { + public List<Property> getApplicationProperties(String applicationName) throws RegistryException { Registry tempRegistry = getRegistry(); String resourcePath = mainResource + applicationName; @@ -61,14 +62,14 @@ public class CarbonRegistry implements DataStore { ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); Resource regResource = tempRegistry.get(resourcePath); - ArrayList<NewProperty> newProperties = new ArrayList<NewProperty>(); + ArrayList<Property> newProperties = new ArrayList<Property>(); 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 property = new Property(); property.setKey(key); String[] valueArr = new String[values.size()]; property.setValues(values.toArray(valueArr)); @@ -86,7 +87,7 @@ public class CarbonRegistry implements DataStore { * @return * @throws RegistryException */ - public List<NewProperty> getClusterProperties(String applicationName, String clusterId) throws RegistryException { + public List<Property> getClusterProperties(String applicationName, String clusterId) throws RegistryException { Registry tempRegistry = getRegistry(); String resourcePath = mainResource + applicationName + "/" + clusterId; @@ -102,14 +103,14 @@ public class CarbonRegistry implements DataStore { Resource regResource = tempRegistry.get(resourcePath); - ArrayList<NewProperty> newProperties = new ArrayList<NewProperty>(); + ArrayList<Property> newProperties = new ArrayList<Property>(); 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 property = new Property(); property.setKey(key); String[] valueArr = new String[values.size()]; property.setValues(values.toArray(valueArr)); @@ -120,22 +121,18 @@ public class CarbonRegistry implements DataStore { return newProperties; } - public void addPropertiesToApplication(String applicationId, NewProperty[] properties) throws RegistryException { - for (NewProperty property : properties) { - addPropertyToApplication(applicationId, property); - } - } - - public void addPropertyToApplication(String applicationId, NewProperty property) throws RegistryException { + public void addPropertyToApplication(String applicationId, Property property) throws RegistryException { Registry registry = getRegistry(); String resourcePath = mainResource + applicationId; + log.info("**************addPropertyToApplication " + count++ + " property name " + property.getKey() + " values " + Arrays.toString(property.getValues())); + //log.info("**** property name " + property.getKey() + " values " + Arrays.toString(property.getValues())); try { PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID); ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); - registry.beginTransaction(); + //registry.beginTransaction(); Resource nodeResource = null; if (registry.resourceExists(resourcePath)) { nodeResource = registry.get(resourcePath); @@ -151,19 +148,43 @@ public class CarbonRegistry implements DataStore { nodeResource.addProperty(property.getKey(), value); } - registry.put(resourcePath, nodeResource); - registry.commitTransaction(); + boolean updated = false; + for(String value : property.getValues()){ + if(!propertyValueExist(nodeResource, property.getKey(), value)){ + updated = true; + log.info(String.format("Registry property is added: [resource-path] %s [Property Name] %s [Property Value] %s", + resourcePath, property.getKey(), value)); + nodeResource.addProperty(property.getKey(), value); + }else{ + log.info(String.format("Property value already exist property=%s value=%s", property.getKey(), value)); + } + } + + if(updated){ + registry.put(resourcePath, nodeResource); + } + //registry.commitTransaction(); + + - log.info(String.format("Registry property is persisted: [resource-path] %s [Property Name] %s [Property Values] %s", - resourcePath, property.getKey(), Arrays.asList(property.getValues()))); } catch (Exception e) { String msg = "Failed to persist properties in registry: " + resourcePath; - registry.rollbackTransaction(); + //registry.rollbackTransaction(); log.error(msg, e); throw new RegistryException(msg, e); } } + private boolean propertyValueExist(Resource nodeResource, String key, String value) { + List<String> properties = nodeResource.getPropertyValues(key); + if(properties == null){ + return false; + }else{ + return properties.contains(value); + } + + } + public boolean removePropertyFromApplication(String applicationId, String propertyName, String valueToRemove) throws RegistryException { Registry registry = getRegistry(); String resourcePath = mainResource + applicationId; @@ -197,8 +218,6 @@ public class CarbonRegistry implements DataStore { return true; } - - /** * Add property to cluster * @@ -207,7 +226,7 @@ public class CarbonRegistry implements DataStore { * @param property * @throws RegistryException */ - public void addPropertyToCluster(String applicationId, String clusterId, NewProperty property) throws RegistryException { + public void addPropertyToCluster(String applicationId, String clusterId, Property property) throws RegistryException { Registry registry = getRegistry(); String resourcePath = mainResource + applicationId + "/" + clusterId; @@ -243,7 +262,7 @@ public class CarbonRegistry implements DataStore { } } - public void addPropertyToGroup(String applicationId, String groupId, NewProperty property) throws RegistryException { + public void addPropertyToGroup(String applicationId, String groupId, Property property) throws RegistryException { Registry registry = getRegistry(); String resourcePath = mainResource + applicationId + "/groups/" + groupId; @@ -324,20 +343,6 @@ public class CarbonRegistry implements DataStore { } /** - * Add properties to cluster - * - * @param applicationName - * @param clusterId - * @param properties - * @throws RegistryException - */ - public void addPropertiesToCluster(String applicationName, String clusterId, NewProperty[] properties) throws RegistryException { - for (NewProperty property : properties) { - this.addPropertyToCluster(applicationName, clusterId, property); - } - } - - /** * Create or get resource for application * * @param tempRegistry http://git-wip-us.apache.org/repos/asf/stratos/blob/da776459/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/DataStore.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/DataStore.java b/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/DataStore.java index 80d3427..dbdd6df 100644 --- a/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/DataStore.java +++ b/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/DataStore.java @@ -19,7 +19,7 @@ package org.apache.stratos.metadata.service.registry; -import org.apache.stratos.metadata.service.definition.NewProperty; +import org.apache.stratos.metadata.service.definition.Property; import org.wso2.carbon.registry.api.RegistryException; import java.util.List; @@ -29,20 +29,14 @@ import java.util.List; */ public interface DataStore { - public void addPropertyToApplication(String applicationId, NewProperty property) throws RegistryException; + public void addPropertyToApplication(String applicationId, Property property) throws RegistryException; - public void addPropertiesToApplication(String applicationName, NewProperty[] properties) - throws RegistryException; - - public void addPropertiesToCluster(String applicationName, String clusterId, NewProperty[] properties) - throws RegistryException; - - public List<NewProperty> getApplicationProperties(String applicationName) throws RegistryException; + public List<Property> getApplicationProperties(String applicationName) throws RegistryException; - public List<NewProperty> getClusterProperties(String applicationName, String clusterId) + public List<Property> getClusterProperties(String applicationName, String clusterId) throws RegistryException; - public void addPropertyToCluster(String applicationId, String clusterId, NewProperty property) throws RegistryException; + public void addPropertyToCluster(String applicationId, String clusterId, Property property) throws RegistryException; public boolean deleteApplication(String applicationId) throws RegistryException; http://git-wip-us.apache.org/repos/asf/stratos/blob/da776459/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/services/MetaDataAdmin.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/services/MetaDataAdmin.java b/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/services/MetaDataAdmin.java index 123b52f..68b960c 100644 --- a/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/services/MetaDataAdmin.java +++ b/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/services/MetaDataAdmin.java @@ -19,11 +19,10 @@ package org.apache.stratos.metadata.service.services; import org.apache.commons.configuration.XMLConfiguration; -import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.stratos.metadata.service.annotation.AuthorizationAction; -import org.apache.stratos.metadata.service.definition.NewProperty; +import org.apache.stratos.metadata.service.definition.Property; import org.apache.stratos.metadata.service.exception.RestAPIException; import org.apache.stratos.metadata.service.registry.DataRegistryFactory; import org.apache.stratos.metadata.service.registry.DataStore; @@ -62,13 +61,13 @@ public class MetaDataAdmin { public Response getApplicationProperties(@PathParam("application_id") String applicationId) throws RestAPIException { - List<NewProperty> properties; - NewProperty[] propertiesArr = null; + List<Property> properties; + Property[] propertiesArr = null; try { properties = registry .getApplicationProperties(applicationId); if (properties != null) { - propertiesArr = new NewProperty[properties.size()]; + propertiesArr = new Property[properties.size()]; propertiesArr = properties.toArray(propertiesArr); } } catch (RegistryException e) { @@ -93,13 +92,13 @@ public class MetaDataAdmin { @AuthorizationAction("/permission/protected/manage/monitor/tenants") public Response getClusterProperties(@PathParam("application_id") String applicationId, @PathParam("cluster_id") String clusterId) throws RestAPIException { - List<NewProperty> properties; - NewProperty[] propertiesArr = null; + List<Property> properties; + Property[] propertiesArr = null; try { properties = registry .getClusterProperties(applicationId, clusterId); if (properties != null) { - propertiesArr = new NewProperty[properties.size()]; + propertiesArr = new Property[properties.size()]; propertiesArr = properties.toArray(propertiesArr); } } catch (RegistryException e) { @@ -118,23 +117,23 @@ public class MetaDataAdmin { } @GET - @Path("/application/{application_id}/property/{property_name}") + @Path("/application/{application_id}/properties/{property_name}") @Produces("application/json") @Consumes("application/json") @AuthorizationAction("/permission/protected/manage/monitor/tenants") public Response getApplicationProperty(@PathParam("application_id") String applicationId, @PathParam("property_name") String propertyName) throws RestAPIException { - List<NewProperty> properties; + List<Property> properties; - NewProperty property = null; + Property property = null; try { properties = registry.getApplicationProperties(applicationId); if (properties == null) { return Response.status(Response.Status.NOT_FOUND).build(); } - for (NewProperty p : properties) { + for (Property p : properties) { if (propertyName.equals(p.getKey())) { property = p; break; @@ -156,21 +155,20 @@ public class MetaDataAdmin { } @GET - @Path("/application/{application_id}/cluster/{cluster_id}/property/{property_name}") + @Path("/application/{application_id}/cluster/{cluster_id}/properties/{property_name}") @Produces("application/json") @Consumes("application/json") @AuthorizationAction("/permission/protected/manage/monitor/tenants") public Response getClusterProperty(@PathParam("application_id") String applicationId, @PathParam("cluster_id") String clusterId, @PathParam("property_name") String propertyName) throws RestAPIException { - List<NewProperty> properties; + List<Property> properties; - - NewProperty property = null; + Property property = null; try { properties = registry.getClusterProperties(applicationId, clusterId); if (properties == null) { return Response.status(Response.Status.NOT_FOUND).build(); } - for (NewProperty p : properties) { + for (Property p : properties) { if (propertyName.equals(p.getKey())) { property = p; break; @@ -192,66 +190,17 @@ public class MetaDataAdmin { } @POST - @Path("application/{application_id}/property") - @Produces("application/json") - @Consumes("application/json") - @AuthorizationAction("/permission/protected/manage/monitor/tenants") - public Response addPropertyToApplication(@PathParam("application_id") String applicationId, - NewProperty property) - throws RestAPIException { - - URI url = uriInfo.getAbsolutePathBuilder().path(applicationId + "/" + property.getKey()).build(); - if (StringUtils.isEmpty(property.getKey())) { - throw new RestAPIException("Property key can not be empty"); - } - - try { - registry.addPropertyToApplication(applicationId, property); - } catch (RegistryException e) { - String msg = "Error occurred while adding property"; - log.error(msg, e); - throw new RestAPIException(msg, e); - } - return Response.created(url).build(); - } - - @POST - @Path("application/{application_id}/cluster/{cluster_id}/property") - @Produces("application/json") - @Consumes("application/json") - @AuthorizationAction("/permission/protected/manage/monitor/tenants") - public Response addPropertyToCluster(@PathParam("application_id") String applicationId, - @PathParam("cluster_id") String clusterId, NewProperty property) - throws RestAPIException { - - URI url = uriInfo.getAbsolutePathBuilder().path(applicationId + "/" + clusterId + "/" + property.getKey()).build(); - if (StringUtils.isEmpty(property.getKey())) { - throw new RestAPIException("Property key can not be empty"); - } - - try { - registry.addPropertyToCluster(applicationId, clusterId, property); - } catch (RegistryException e) { - String msg = "Error occurred while adding property"; - log.error(msg, e); - throw new RestAPIException(msg, e); - } - - return Response.created(url).build(); - } - - @POST @Path("application/{application_id}/properties") @Produces("application/json") @Consumes("application/json") @AuthorizationAction("/permission/protected/manage/monitor/tenants") - public Response addPropertiesToApplication(@PathParam("application_id") String applicationId, - NewProperty[] properties) + public Response addPropertyToApplication(@PathParam("application_id") String applicationId, + Property property) throws RestAPIException { URI url = uriInfo.getAbsolutePathBuilder().path(applicationId).build(); try { - registry.addPropertiesToApplication(applicationId, properties); + registry.addPropertyToApplication(applicationId, property); } catch (RegistryException e) { String msg = "Error occurred while adding properties "; log.error(msg, e); @@ -265,13 +214,13 @@ public class MetaDataAdmin { @Produces("application/json") @Consumes("application/json") @AuthorizationAction("/permission/protected/manage/monitor/tenants") - public Response addPropertiesToCluster(@PathParam("application_id") String applicationId, - @PathParam("cluster_id") String clusterId, NewProperty[] properties) + public Response addPropertyToCluster(@PathParam("application_id") String applicationId, + @PathParam("cluster_id") String clusterId, Property property) throws RestAPIException { URI url = uriInfo.getAbsolutePathBuilder().path(applicationId + "/" + clusterId).build(); try { - registry.addPropertiesToCluster(applicationId, clusterId, properties); + registry.addPropertyToCluster(applicationId, clusterId, property); } catch (RegistryException e) { String msg = "Error occurred while adding properties "; log.error(msg, e); @@ -306,7 +255,7 @@ public class MetaDataAdmin { } @DELETE - @Path("application/{application_id}/property/{property_name}/value/{value}") + @Path("application/{application_id}/properties/{property_name}/value/{value}") @Produces("application/json") @Consumes("application/json") @AuthorizationAction("/permission/protected/manage/monitor/tenants") @@ -315,7 +264,6 @@ public class MetaDataAdmin { throws RestAPIException { try { - log.info("***************************** application/{application_id}/property/{property_name}/value/{value}"); registry.removePropertyFromApplication(applicationId, propertyName, propertyValue); } catch (RegistryException e) { String msg = "Resource value deletion failed ";
