Load old-style catalog items when referenced with explicit version
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/2bd6480c Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/2bd6480c Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/2bd6480c Branch: refs/heads/master Commit: 2bd6480c890698d37b07a34622ebaa3708c9f577 Parents: 9090aab Author: Svetoslav Neykov <[email protected]> Authored: Wed Feb 25 16:08:30 2015 +0200 Committer: Svetoslav Neykov <[email protected]> Committed: Wed Feb 25 16:08:30 2015 +0200 ---------------------------------------------------------------------- .../BrooklynAssemblyTemplateInstantiator.java | 11 ++++ .../BrooklynComponentTemplateResolver.java | 15 ++++- .../brooklyn/catalog/CatalogXmlVersionTest.java | 59 ++++++++++++++++++++ .../camp/src/test/resources/simple-catalog.xml | 37 ++++++++++++ 4 files changed, 119 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2bd6480c/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java index 17949d8..379cd43 100644 --- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java +++ b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java @@ -195,13 +195,24 @@ public class BrooklynAssemblyTemplateInstantiator implements AssemblyTemplateSpe } if (spec == null) { + // - Load a java class from current loader (item == null || entityResolver.isJavaTypePrefix()) + // - Load a java class specified in an old-style catalog item (item != null && item.getJavaType() != null) + // Old-style catalog items (can be defined in catalog.xml only) don't have structure, only a single type, so + // they are loaded as a simple java type, only taking the class name from the catalog item instead of the + // type value in the YAML. Classpath entries in the item are also used (through the catalog root classloader). if (item == null || item.getJavaType() != null || entityResolver.isJavaTypePrefix()) { spec = entityResolver.resolveSpec(); + + // Same as above case, but this time force java type loading (either as plain class or through an old-style + // catalog item, since we have already loaded a class item with the same name as the type value. } else if (recursiveButTryJava) { if (entityResolver.tryLoadEntityClass().isAbsent()) { throw new IllegalStateException("Recursive reference to " + brooklynType + " (and cannot be resolved as a Java type)"); } spec = entityResolver.resolveSpec(); + + // Only case that's left is a catalog item with YAML content - try to parse it recursively + // including it's OSGi bundles in the loader classpath. } else { //TODO migrate to catalog.createSpec spec = resolveCatalogYamlReferenceSpec(mgmt, item, encounteredCatalogTypes); http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2bd6480c/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java index 9584e78..416f2e8 100644 --- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java +++ b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java @@ -200,9 +200,9 @@ public class BrooklynComponentTemplateResolver { } public boolean canResolve() { - if (getCatalogItem()!=null) + if (getCatalogItem()!=null) return true; - if (loader.tryLoadClass(getBrooklynType(), Entity.class).isPresent()) + if (loader.tryLoadClass(getJavaType(), Entity.class).isPresent()) return true; return false; } @@ -217,7 +217,16 @@ public class BrooklynComponentTemplateResolver { /** tries to load the Java entity class */ public Maybe<Class<? extends Entity>> tryLoadEntityClass() { - return loader.tryLoadClass(getBrooklynType(), Entity.class); + return loader.tryLoadClass(getJavaType(), Entity.class); + } + + private String getJavaType() { + CatalogItem<Entity, EntitySpec<?>> item = getCatalogItem(); + if (item != null && item.getJavaType() != null) { + return item.getJavaType(); + } else { + return getBrooklynType(); + } } /** resolves the spec, updating the loader if a catalog item is loaded */ http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2bd6480c/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogXmlVersionTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogXmlVersionTest.java b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogXmlVersionTest.java new file mode 100644 index 0000000..5ae5493 --- /dev/null +++ b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogXmlVersionTest.java @@ -0,0 +1,59 @@ +/* + * 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 io.brooklyn.camp.brooklyn.catalog; + +import io.brooklyn.camp.brooklyn.AbstractYamlTest; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import brooklyn.config.BrooklynProperties; +import brooklyn.config.BrooklynServerConfig; +import brooklyn.management.internal.LocalManagementContext; +import brooklyn.test.entity.LocalManagementContextForTests; + +public class CatalogXmlVersionTest extends AbstractYamlTest { + @Override + protected LocalManagementContext newTestManagementContext() { + BrooklynProperties properties = BrooklynProperties.Factory.newEmpty(); + properties.put(BrooklynServerConfig.BROOKLYN_CATALOG_URL, "classpath://simple-catalog.xml"); + return LocalManagementContextForTests.newInstance(properties); + } + + @DataProvider(name = "types") + public Object[][] createTypes() { + return new Object[][] { + {"brooklyn.entity.basic.BasicApplication"}, + {"brooklyn.entity.basic.BasicApplication:0.0.0.SNAPSHOT"}, + {"brooklyn.entity.basic.BasicApplication:2.0"}, + {"BasicApp"}, + {"BasicApp:0.0.0.SNAPSHOT"}, + {"BasicApp:2.0"} + }; + } + + @Test(dataProvider = "types") + public void testXmlCatalogItem(String type) throws Exception { + String yaml = "name: simple-app-yaml\n" + + "location: localhost\n" + + "services: \n" + + " - type: " + type; + createAndStartApplication(yaml); + } +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2bd6480c/usage/camp/src/test/resources/simple-catalog.xml ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/resources/simple-catalog.xml b/usage/camp/src/test/resources/simple-catalog.xml new file mode 100644 index 0000000..b0d88cc --- /dev/null +++ b/usage/camp/src/test/resources/simple-catalog.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> + +<catalog> + <name>Simple catalogue</name> + <template name="Basic App" type="brooklyn.entity.basic.BasicApplication"> + <description>An example application</description> + </template> + <template name="Basic App" type="brooklyn.entity.basic.BasicApplication" version="2.0"> + <description>An example application</description> + </template> + <template name="Basic App" type="brooklyn.entity.basic.BasicApplication"> + <symbolicName>BasicApp</symbolicName> + <description>An example application</description> + </template> + <template name="Basic App" type="brooklyn.entity.basic.BasicApplication" version="2.0"> + <symbolicName>BasicApp</symbolicName> + <description>An example application</description> + </template> +</catalog>
