Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
[BZ 1069545] First cut at removing the predefined subcategories conce…
Browse files Browse the repository at this point in the history
…pt. This version has some functionality removed but will be added in later revisions.
  • Loading branch information
Stefan Negrea committed Apr 22, 2014
1 parent decdcfd commit 1b3f058
Show file tree
Hide file tree
Showing 22 changed files with 247 additions and 865 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import org.rhq.core.clientapi.descriptor.plugin.Bundle;
import org.rhq.core.clientapi.descriptor.plugin.BundleTargetDescriptor;
import org.rhq.core.clientapi.descriptor.plugin.BundleTargetDescriptor.DestinationBaseDir;
import org.rhq.core.clientapi.descriptor.plugin.ContentDescriptor;
import org.rhq.core.clientapi.descriptor.plugin.DiscoveryCallbacksType;
import org.rhq.core.clientapi.descriptor.plugin.DiscoveryTypeCallbackType;
Expand All @@ -53,8 +54,6 @@
import org.rhq.core.clientapi.descriptor.plugin.RunsInsideType;
import org.rhq.core.clientapi.descriptor.plugin.ServerDescriptor;
import org.rhq.core.clientapi.descriptor.plugin.ServiceDescriptor;
import org.rhq.core.clientapi.descriptor.plugin.SubCategoryDescriptor;
import org.rhq.core.clientapi.descriptor.plugin.BundleTargetDescriptor.DestinationBaseDir;
import org.rhq.core.domain.bundle.BundleType;
import org.rhq.core.domain.bundle.ResourceTypeBundleConfiguration;
import org.rhq.core.domain.configuration.Configuration;
Expand All @@ -65,7 +64,6 @@
import org.rhq.core.domain.resource.ProcessScan;
import org.rhq.core.domain.resource.ResourceCategory;
import org.rhq.core.domain.resource.ResourceCreationDataType;
import org.rhq.core.domain.resource.ResourceSubCategory;
import org.rhq.core.domain.resource.ResourceType;

/**
Expand Down Expand Up @@ -295,7 +293,7 @@ private ResourceType parseServerDescriptor(ServerDescriptor serverDescriptor, Re
// Let the plugin writer override these, or if not, parseResourceDescriptor() will pick up the source type's
// values.
serverResourceType.setDescription(serverDescriptor.getDescription());
setSubCategory(serverDescriptor, serverResourceType);
serverResourceType.setSubCategory(serverDescriptor.getSubCategory());

serverResourceType.setCreationDataType(convertCreationDataType(serverDescriptor.getCreationDataType()));
serverResourceType
Expand Down Expand Up @@ -431,7 +429,7 @@ private ResourceType parseServiceDescriptor(ServiceDescriptor serviceDescriptor,
// Let the plugin writer override these, or if not, parseResourceDescriptor() will pick up the source type's
// values.
serviceResourceType.setDescription(serviceDescriptor.getDescription());
setSubCategory(serviceDescriptor, serviceResourceType);
serviceResourceType.setSubCategory(serviceDescriptor.getSubCategory());

serviceResourceType.setCreationDataType(convertCreationDataType(serviceDescriptor.getCreationDataType()));
serviceResourceType.setCreateDeletePolicy(convertCreateDeletePolicy(serviceDescriptor
Expand Down Expand Up @@ -493,60 +491,6 @@ private ResourceType parseServiceDescriptor(ServiceDescriptor serviceDescriptor,
return serviceResourceType;
}

/**
* Try to find the subCategory of the p/s/s descriptor in one of the parents
* <subcategories><subcategory>Foo</subcategory></subcategories> elements and
* set it on the resourceType if found.
*
* It is not enough to look at the direct parents, but we need to also look at the
* <runs-inside> types if our type is "embedded" in a different type.
* @param resourceDescriptor Descriptor to get the subCategory attribute from
* @param resourceType The type to attach the ResourceSubCategory to.
* @throws InvalidPluginDescriptorException If the descriptor.subCategory can not be found in any parent.
*/
private void setSubCategory(ResourceDescriptor resourceDescriptor, ResourceType resourceType)
throws InvalidPluginDescriptorException {
String subCatName = resourceDescriptor.getSubCategory();
if (subCatName != null) {
ResourceSubCategory subCat = SubCategoriesMetadataParser.findSubCategoryOnResourceTypeAncestor(
resourceType, subCatName);

// We need to look at resourceDescriptor -> runsInside to see if one of those defines the
// subcategories that we are looking for.
if (subCat == null && resourceDescriptor.getRunsInside() != null) {
RunsInsideType rit = resourceDescriptor.getRunsInside();
List<ParentResourceType> parentResourceTypeList = rit.getParentResourceType();
for (ParentResourceType parentResourceType : parentResourceTypeList) {
ResourceType parentType = getResourceTypeFromPlugin(parentResourceType.getName(),parentResourceType.getPlugin());
// check on the parent
if (parentType.getChildSubCategories()!=null ) {
for (ResourceSubCategory parentSubcat : parentType.getChildSubCategories()) {
if (parentSubcat.getName().equals(subCatName)) {
subCat = parentSubcat;
break;
}
}
}

// Not found on runs-inside type look at the ancestor of those runs-inside types?
if (subCat==null) {
subCat = SubCategoriesMetadataParser.findSubCategoryOnResourceTypeAncestor(parentType,subCatName);
}
if (subCat!=null) {
break;
}
}
}

if (subCat == null) {
throw new InvalidPluginDescriptorException("Resource type [" + resourceType.getName()
+ "] specified a subcategory (" + subCatName
+ ") that is not defined as a child subcategory of one of its ancestor resource types.");
}
resourceType.setSubCategory(subCat);
}
}

/**
* Parses the resource descriptor and registers the type and its component classes.
*
Expand Down Expand Up @@ -586,9 +530,8 @@ private void parseResourceDescriptor(ResourceDescriptor resourceDescriptor, Reso
resourceType.setDescription(resourceDescriptor.getDescription());
}

// TODO (ips): I don't think platforms can have a subcategory.
if (resourceType.getSubCategory() == null) {
setSubCategory(resourceDescriptor, resourceType);
resourceType.setSubCategory(resourceDescriptor.getSubCategory());
}

if (discoveryClass == null) {
Expand Down Expand Up @@ -655,15 +598,6 @@ private void parseResourceDescriptor(ResourceDescriptor resourceDescriptor, Reso
resourceType.addPackageType(ContentMetadataParser.parseContentDescriptor(contentDescriptor));
}

// TODO not sure we really want this wrapping <subcategories> element since no one else uses it
if (resourceDescriptor.getSubcategories() != null) {
for (SubCategoryDescriptor subCategoryDescriptor : resourceDescriptor.getSubcategories()
.getSubcategory()) {
resourceType.addChildSubCategory(SubCategoriesMetadataParser.getSubCategory(subCategoryDescriptor,
resourceType));
}
}

Bundle bundle = resourceDescriptor.getBundle();
if (bundle != null) {
String typeName = bundle.getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@
*/
package org.rhq.core.clientapi.agent.metadata;

import java.util.List;
import java.util.Set;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import org.rhq.core.clientapi.descriptor.plugin.SubCategoryDescriptor;
import org.rhq.core.domain.resource.ResourceSubCategory;
import org.rhq.core.domain.resource.ResourceType;
import org.rhq.core.domain.util.StringUtils;

/**
* Parser responsible for translating the subcategories section of the rhq-plugin.xml descriptor into domain objects.
Expand All @@ -41,99 +31,4 @@
* @author Charles Crouch
*/
public class SubCategoriesMetadataParser {
/**
* Parses the contents of the SubCategoryDescriptor and populates an instance of the domain model representation,
* ResourceSubCategory
*
* @param descriptor non-null SubCategoryDescriptor.
*
* @return domain model object populated with the descriptor's values.
*/
public static ResourceSubCategory getSubCategory(SubCategoryDescriptor descriptor, ResourceType resourceType) {
ResourceSubCategory subCat = new ResourceSubCategory();

subCat.setName(descriptor.getName());
if (descriptor.getDisplayName() != null) {
subCat.setDisplayName(descriptor.getDisplayName());
} else {
subCat.setDisplayName(StringUtils.deCamelCase(descriptor.getName()));
}

subCat.setDescription(descriptor.getDescription());

List<SubCategoryDescriptor> childDescriptors = descriptor.getSubcategory();
if (childDescriptors != null) {
for (SubCategoryDescriptor childDescriptor : childDescriptors) {
subCat.addChildSubCategory(getSubCategory(childDescriptor, resourceType));
}
}

return subCat;
}

/**
* Given a resourceType this method looks on the parent resourcetypes/grandparent resourcetypes... of the specified
* resourceType to see if a child ResourceSubCategory with the specified name has been defined on one of them.
* If the ResourceSubCategory cannot be found, null will be returned
*
* @param resourceType
* @param subCategoryName
*
* @return
*/
@Nullable
public static ResourceSubCategory findSubCategoryOnResourceTypeAncestor(@NotNull
ResourceType resourceType, @Nullable
String subCategoryName) {
ResourceSubCategory selectedSubCategory = null;

if (subCategoryName != null) {
Set<ResourceType> parents = resourceType.getParentResourceTypes();
for (ResourceType parent : parents) {
List<ResourceSubCategory> ownedSubCategories = parent.getChildSubCategories();
if (ownedSubCategories != null) {
for (ResourceSubCategory ownedSubCategory : ownedSubCategories) {
// need to check whether this subCat or any of its children match
selectedSubCategory = findSubCategoryOnSubCategoryDescendant(ownedSubCategory, subCategoryName);
if (selectedSubCategory != null) {
// found the category in the parent or one of its children
return selectedSubCategory;
}
}
}
// Recurse...
selectedSubCategory = findSubCategoryOnResourceTypeAncestor(parent, subCategoryName);
if (selectedSubCategory != null) {
break; // found the category in one of the ancestors of the parent
}
}
}

return selectedSubCategory;
}

/**
* Checks whether a ResourceSubCategory with the specified name can be found in the tree of ResourceSubCategories
* who's top level parent is specified by parentSubCategory.
*/
@Nullable
private static ResourceSubCategory findSubCategoryOnSubCategoryDescendant(@NotNull
ResourceSubCategory parentSubCategory, @NotNull
String subCategoryName) {
ResourceSubCategory selectedSubCategory = null;
if (parentSubCategory.getName().equals(subCategoryName)) {
selectedSubCategory = parentSubCategory;
return selectedSubCategory;
} else // parent doesn't match, check children
{
for (ResourceSubCategory childSubCategory : parentSubCategory.getChildSubCategories()) {
selectedSubCategory = findSubCategoryOnSubCategoryDescendant(childSubCategory, subCategoryName);
if (selectedSubCategory != null) {
break; // found the category in one of the children of the parent
}
}
}

return selectedSubCategory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ private void assertJBossASTypes() {
assert jbossServer.getCategory().equals(ResourceCategory.SERVER);
assert jbossServer.getDescription().equals("JBoss Application Server Description");
assert jbossServer.getParentResourceTypes().size() == 0;
assert jbossServer.getChildSubCategories().size() == 2;

assert jbossServer.getChildResourceTypes().size() == 1;
ResourceType embeddedTomcatServer = jbossServer.getChildResourceTypes().iterator().next();
Expand Down Expand Up @@ -264,7 +263,7 @@ private void assertHibernateTypes() {
assert hibernateService.getDescription().equals("Hibernate Service Description");
assert hibernateService.getChildResourceTypes().size() == 0;
assert hibernateService.getParentResourceTypes().size() == 3;
assert hibernateService.getSubCategory().getName().equals("Framework");
assert hibernateService.getSubCategory().equals("Framework");

ResourceType tomcatServer = metadataManager.getType("TomcatServer", "Tomcat");
ResourceType jbossServer = metadataManager.getType("JBossASServer", "JBossAS");
Expand Down Expand Up @@ -333,7 +332,7 @@ private PluginDescriptor loadPluginDescriptor(String file) throws Exception {
URL descriptorUrl = this.getClass().getClassLoader().getResource(file);
System.out.println("Loading plugin descriptor at: " + descriptorUrl);

pluginDescriptor = (PluginDescriptor) AgentPluginDescriptorUtil.parsePluginDescriptor(descriptorUrl
pluginDescriptor = AgentPluginDescriptorUtil.parsePluginDescriptor(descriptorUrl
.openStream());

this.metadataManager.loadPlugin(pluginDescriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,29 @@
package org.rhq.core.clientapi.agent.metadata.test;

import java.net.URL;
import java.util.List;
import java.util.Set;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.util.ValidationEventCollector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import org.rhq.core.clientapi.agent.metadata.PluginMetadataManager;
import org.rhq.core.clientapi.agent.metadata.SubCategoriesMetadataParser;
import org.rhq.core.clientapi.descriptor.AgentPluginDescriptorUtil;
import org.rhq.core.clientapi.descriptor.DescriptorPackages;
import org.rhq.core.clientapi.descriptor.plugin.PluginDescriptor;
import org.rhq.core.clientapi.descriptor.plugin.ResourceDescriptor;
import org.rhq.core.clientapi.descriptor.plugin.ServerDescriptor;
import org.rhq.core.clientapi.descriptor.plugin.ServiceDescriptor;
import org.rhq.core.clientapi.descriptor.plugin.SubCategoryDescriptor;
import org.rhq.core.domain.resource.ResourceCategory;
import org.rhq.core.domain.resource.ResourceSubCategory;
import org.rhq.core.domain.resource.ResourceType;
import java.util.List;
import java.util.Set;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.util.ValidationEventCollector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import org.rhq.core.clientapi.agent.metadata.PluginMetadataManager;
import org.rhq.core.clientapi.descriptor.AgentPluginDescriptorUtil;
import org.rhq.core.clientapi.descriptor.DescriptorPackages;
import org.rhq.core.clientapi.descriptor.plugin.PluginDescriptor;
import org.rhq.core.clientapi.descriptor.plugin.ResourceDescriptor;
import org.rhq.core.clientapi.descriptor.plugin.ServerDescriptor;
import org.rhq.core.clientapi.descriptor.plugin.ServiceDescriptor;
import org.rhq.core.clientapi.descriptor.plugin.SubCategoryDescriptor;
import org.rhq.core.domain.resource.ResourceCategory;
import org.rhq.core.domain.resource.ResourceSubCategory;
import org.rhq.core.domain.resource.ResourceType;

/**
* @author Charles Crouch
Expand Down Expand Up @@ -93,12 +92,13 @@ public void parseSingleSubCategory() {
ResourceSubCategory subCat;

ResourceType resType = new ResourceType("testResType", "myplugin", ResourceCategory.SERVER, null);
subCat = SubCategoriesMetadataParser.getSubCategory(subCategoryDescriptors.get(0), resType);
//TODO: Re-enable Subcategory
/*subCat = SubCategoriesMetadataParser.getSubCategory(subCategoryDescriptors.get(0), resType);
assert subCat != null : "Null subcategory received from parser";
assert subCat.getName().equals("applications") : "Name not read correctly";
assert subCat.getDisplayName().equals("Apps") : "Display name not read correctly";
assert subCat.getDescription().equals("The apps.") : "Description not read correctly";
assert subCat != null : "Null subcategory received from parser";
assert subCat.getName().equals("applications") : "Name not read correctly";
assert subCat.getDisplayName().equals("Apps") : "Display name not read correctly";
assert subCat.getDescription().equals("The apps.") : "Description not read correctly";*/
// getSubCategory is no longer responsible for setting resourcetype information, that is done in PluginMetadataParser
//assert subCat.getResourceType().equals(resType) : "ResourceType not set correctly";

Expand Down Expand Up @@ -127,7 +127,7 @@ public void testParseViaMetaDataManager() throws Exception {
URL descriptorUrl = this.getClass().getClassLoader().getResource(DESCRIPTOR_FILENAME);
System.out.println("Loading plugin descriptor at: " + descriptorUrl);

pluginDescriptor = (PluginDescriptor) AgentPluginDescriptorUtil.parsePluginDescriptor(descriptorUrl
pluginDescriptor = AgentPluginDescriptorUtil.parsePluginDescriptor(descriptorUrl
.openStream());

PluginMetadataManager metadataManager = new PluginMetadataManager();
Expand All @@ -136,15 +136,13 @@ public void testParseViaMetaDataManager() throws Exception {
assert typeSet.size()==5 : "Expected 5 types, but got " + typeSet.size();

ResourceType testService = findType(typeSet,"testService");
assert testService.getSubCategory().getName().equals("applications");
assert testService.getSubCategory().equals("applications");

ResourceType testService2 = findType(typeSet,"testService2");
assert testService2.getSubCategory().getName().equals("applications");
assert testService2.getSubCategory().equals("applications");

ResourceType testService3 = findType(typeSet,"testService3");
assert testService3.getSubCategory().getName().equals("fooBar");


assert testService3.getSubCategory().equals("fooBar");
}

private ResourceType findType(Set<ResourceType> types, String name) {
Expand Down

0 comments on commit 1b3f058

Please sign in to comment.