modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/metadata/PluginMetadataParser.java | 44 ++ modules/core/client-api/src/test/java/org/rhq/core/clientapi/agent/metadata/test/ExtensionModelTest.java | 2 modules/core/client-api/src/test/java/org/rhq/core/clientapi/agent/metadata/test/NestedSubCategoriesMetadataParserTest.java | 159 ++++++++++ modules/core/client-api/src/test/resources/test-hibernate.xml | 8 modules/core/client-api/src/test/resources/test-jbossas.xml | 10 modules/core/client-api/src/test/resources/test-subcategories-nested.xml | 60 +++ pom.xml | 4 7 files changed, 279 insertions(+), 8 deletions(-)
New commits: commit e85abcf495d6939d876b09bd1de1e71a29af17ec Author: Heiko W. Rupp hwr@redhat.com Date: Tue Jul 23 10:42:17 2013 +0200
BZ 796480 (and others) add support for subCategory in embedded types (aka runs-inside)
diff --git a/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/metadata/PluginMetadataParser.java b/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/metadata/PluginMetadataParser.java index 88dd865..d5ff2ef 100644 --- a/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/metadata/PluginMetadataParser.java +++ b/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/metadata/PluginMetadataParser.java @@ -417,16 +417,56 @@ public class PluginMetadataParser { return serviceResourceType; }
- private static void setSubCategory(ResourceDescriptor resourceDescriptor, ResourceType resourceType) + /** + * 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); - if (subCat == null) + + // 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); } } diff --git a/modules/core/client-api/src/test/java/org/rhq/core/clientapi/agent/metadata/test/ExtensionModelTest.java b/modules/core/client-api/src/test/java/org/rhq/core/clientapi/agent/metadata/test/ExtensionModelTest.java index 20e1aa9..2a9595f 100644 --- a/modules/core/client-api/src/test/java/org/rhq/core/clientapi/agent/metadata/test/ExtensionModelTest.java +++ b/modules/core/client-api/src/test/java/org/rhq/core/clientapi/agent/metadata/test/ExtensionModelTest.java @@ -226,6 +226,7 @@ public class ExtensionModelTest { 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(); @@ -263,6 +264,7 @@ public class ExtensionModelTest { assert hibernateService.getDescription().equals("Hibernate Service Description"); assert hibernateService.getChildResourceTypes().size() == 0; assert hibernateService.getParentResourceTypes().size() == 3; + assert hibernateService.getSubCategory().getName().equals("Framework");
ResourceType tomcatServer = metadataManager.getType("TomcatServer", "Tomcat"); ResourceType jbossServer = metadataManager.getType("JBossASServer", "JBossAS"); diff --git a/modules/core/client-api/src/test/java/org/rhq/core/clientapi/agent/metadata/test/NestedSubCategoriesMetadataParserTest.java b/modules/core/client-api/src/test/java/org/rhq/core/clientapi/agent/metadata/test/NestedSubCategoriesMetadataParserTest.java new file mode 100644 index 0000000..1cba523 --- /dev/null +++ b/modules/core/client-api/src/test/java/org/rhq/core/clientapi/agent/metadata/test/NestedSubCategoriesMetadataParserTest.java @@ -0,0 +1,159 @@ + /* + * RHQ Management Platform + * Copyright (C) 2005-2008 Red Hat, Inc. + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation, and/or the GNU Lesser + * General Public License, version 2.1, also as published by the Free + * Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License and the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * and the GNU Lesser General Public License along with this program; + * if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +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; + + /** + * @author Charles Crouch + * @author Heiko W. Rupp + */ +public class NestedSubCategoriesMetadataParserTest { + private static final String DESCRIPTOR_FILENAME = "test-subcategories-nested.xml"; + private final Log LOG = LogFactory.getLog(NestedSubCategoriesMetadataParserTest.class); + + private PluginDescriptor pluginDescriptor; + + @BeforeSuite + public void loadPluginDescriptor() throws Exception { + try { + URL descriptorUrl = this.getClass().getClassLoader().getResource(DESCRIPTOR_FILENAME); + LOG.info("Loading plugin descriptor at: " + descriptorUrl); + + JAXBContext jaxbContext = JAXBContext.newInstance(DescriptorPackages.PC_PLUGIN); + + Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); + ValidationEventCollector vec = new ValidationEventCollector(); + unmarshaller.setEventHandler(vec); + pluginDescriptor = (PluginDescriptor) unmarshaller.unmarshal(descriptorUrl.openStream()); + } catch (Throwable t) { + // Catch RuntimeExceptions and Errors and dump their stack trace, because Surefire will completely swallow them + // and throw a cryptic NPE (see http://jira.codehaus.org/browse/SUREFIRE-157)! + t.printStackTrace(); + throw new RuntimeException(t); + } + } + + @Test + public void parseSingleSubCategory() { + List<ServerDescriptor> servers = pluginDescriptor.getServers(); + ServerDescriptor server0 = servers.get(0); + ResourceDescriptor.Subcategories subCategoriesDescriptor = server0.getSubcategories(); + assert subCategoriesDescriptor != null : "No subcategories element: " + server0.getName(); + + List<SubCategoryDescriptor> subCategoryDescriptors = subCategoriesDescriptor.getSubcategory(); + + assert subCategoryDescriptors != null : "No subcategory elements: " + server0.getName(); + assert !subCategoryDescriptors.isEmpty() : "No subcategory elements: " + server0.getName(); + + ResourceSubCategory subCat; + + ResourceType resType = new ResourceType("testResType", "myplugin", ResourceCategory.SERVER, null); + 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"; + // getSubCategory is no longer responsible for setting resourcetype information, that is done in PluginMetadataParser + //assert subCat.getResourceType().equals(resType) : "ResourceType not set correctly"; + + } + + @Test + public void parseNestedSubCategories() { + List<ServerDescriptor> servers = pluginDescriptor.getServers(); + ServerDescriptor server2 = servers.get(1); + assert server2.getName().equals("testServer2"); + ResourceDescriptor.Subcategories subCategoriesDescriptor = server2.getSubcategories(); + assert subCategoriesDescriptor == null : "Unexpected subcategories element: " + server2.getName(); + assert server2.getSubCategory().equals("applications"); + + List<ServiceDescriptor> services = pluginDescriptor.getServices(); + ServiceDescriptor service1 = services.get(0); + assert service1.getName().equals("testService"); + assert service1.getSubCategory().equals("applications"); + } + + @Test + public void testParseViaMetaDataManager() throws Exception { + + PluginDescriptor pluginDescriptor; + + URL descriptorUrl = this.getClass().getClassLoader().getResource(DESCRIPTOR_FILENAME); + System.out.println("Loading plugin descriptor at: " + descriptorUrl); + + pluginDescriptor = (PluginDescriptor) AgentPluginDescriptorUtil.parsePluginDescriptor(descriptorUrl + .openStream()); + + PluginMetadataManager metadataManager = new PluginMetadataManager(); + Set<ResourceType> typeSet = metadataManager.loadPlugin(pluginDescriptor); + assert typeSet != null : "Got no types!!"; + assert typeSet.size()==5 : "Expected 5 types, but got " + typeSet.size(); + + ResourceType testService = findType(typeSet,"testService"); + assert testService.getSubCategory().getName().equals("applications"); + + ResourceType testService2 = findType(typeSet,"testService2"); + assert testService2.getSubCategory().getName().equals("applications"); + + ResourceType testService3 = findType(typeSet,"testService3"); + assert testService3.getSubCategory().getName().equals("fooBar"); + + + } + + private ResourceType findType(Set<ResourceType> types, String name) { + for (ResourceType type : types ) { + if (type.getName().equals(name)) { + return type; + } + } + assert false : "Type with name " + name + " not found"; + return null; + } + } \ No newline at end of file diff --git a/modules/core/client-api/src/test/resources/test-hibernate.xml b/modules/core/client-api/src/test/resources/test-hibernate.xml index 37a2e03..9051ca2 100644 --- a/modules/core/client-api/src/test/resources/test-hibernate.xml +++ b/modules/core/client-api/src/test/resources/test-hibernate.xml @@ -3,7 +3,7 @@ package="org.rhq.plugins.test2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:xmlns:rhq-plugin"> - + <depends plugin="JMX" /> <depends plugin="Tomcat" /> <depends plugin="JBossAS" /> @@ -11,11 +11,13 @@ <service name="HibernateService" discovery="HibernateDiscoveryComponent" class="HibernateServiceComponent" - description="Hibernate Service Description"> + description="Hibernate Service Description" + subCategory="Framework" + > <runs-inside> <parent-resource-type name="TomcatServer" plugin="Tomcat"/> <parent-resource-type name="JBossASServer" plugin="JBossAS"/> <parent-resource-type name="EmbeddedTomcatServer" plugin="JBossAS"/> </runs-inside> - </service> + </service> </plugin> \ No newline at end of file diff --git a/modules/core/client-api/src/test/resources/test-jbossas.xml b/modules/core/client-api/src/test/resources/test-jbossas.xml index 4d2602b..8269343 100644 --- a/modules/core/client-api/src/test/resources/test-jbossas.xml +++ b/modules/core/client-api/src/test/resources/test-jbossas.xml @@ -12,7 +12,11 @@ discovery="JBossASDiscoveryComponent" class="JBossASServerComponent" description="JBoss Application Server Description"> - + <subcategories> + <subcategory name="Applications" /> + <subcategory name="Framework" /> + </subcategories> + <operation name="stop" displayName="Stop JBossAS Server" description="Kills the server" timeout="30"> <parameters> <c:simple-property name="force" @@ -29,7 +33,7 @@ required="true" description="If true, the server is definitely down; otherwise, the shutdown was issued but it is unclear if it really died"/> </results> - </operation> + </operation> <server name="EmbeddedTomcatServer" description="Embedded Tomcat Web Server Description" sourcePlugin="Tomcat" @@ -37,6 +41,6 @@ discovery="JBossASTomcatDiscoveryComponent" class="JBossASTomcatServerComponent"> </server> - + </server> </plugin> \ No newline at end of file diff --git a/modules/core/client-api/src/test/resources/test-subcategories-nested.xml b/modules/core/client-api/src/test/resources/test-subcategories-nested.xml new file mode 100644 index 0000000..eb43af7 --- /dev/null +++ b/modules/core/client-api/src/test/resources/test-subcategories-nested.xml @@ -0,0 +1,60 @@ +<!-- + ~ RHQ Management Platform + ~ Copyright (C) 2005-2013 Red Hat, Inc. + ~ All rights reserved. + ~ + ~ This program is free software; you can redistribute it and/or modify + ~ it under the terms of the GNU General Public License as published by + ~ the Free Software Foundation version 2 of the License. + ~ + ~ This program is distributed in the hope that it will be useful, + ~ but WITHOUT ANY WARRANTY; without even the implied warranty of + ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ~ GNU General Public License for more details. + ~ + ~ You should have received a copy of the GNU General Public License + ~ along with this program; if not, write to the Free Software Foundation, Inc., + ~ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + --> + +<plugin name="TestPlugin" displayName="Mock JBoss AS" package="org.rhq.plugins.mock.jboss" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="urn:xmlns:rhq-plugin"> + + <server name="testServer1"> + <subcategories> + <subcategory name="applications" displayName="Apps" description="The apps."/> + <subcategory name="fooBar"/> + </subcategories> + </server> + + <!-- subCategory="applications" means that resources of this type go in the 'applications folder' of testServer1--> + <server name="testServer2" subCategory="applications"> + <runs-inside> + <parent-resource-type name="testServer1" plugin="TestPlugin"/> + </runs-inside> + <metric property="testMetric"/> + </server> + + <service name="testService" subCategory="applications"> + <runs-inside> + <parent-resource-type name="testServer1" plugin="TestPlugin"/> + </runs-inside> + + </service> + + <service name="testService2" subCategory="applications"> + <runs-inside> + <parent-resource-type name="testServer1" plugin="TestPlugin"/> + </runs-inside> + + </service> + + <service name="testService3" subCategory="fooBar"> + <runs-inside> + <parent-resource-type name="testServer1" plugin="TestPlugin"/> + </runs-inside> + + </service> + +</plugin>
commit 60329fcbda5a1961e0f9285c70eb56ea12fe2eb0 Author: Heiko W. Rupp hwr@redhat.com Date: Mon Jul 22 15:00:35 2013 +0200
Add Michael Burman as contributor
diff --git a/pom.xml b/pom.xml index c5cf5a7..3662bc7 100644 --- a/pom.xml +++ b/pom.xml @@ -2353,6 +2353,10 @@ <timezone>+1</timezone> </contributor> <contributor> + <name>Michael Burman</name> + <timezone>+2</timezone> + </contributor> + <contributor> <name>Torben Jäger</name> <timezone>+1</timezone> </contributor>
rhq-commits@lists.fedorahosted.org