Improve policy support for catalog items

  * policies found through scanning the classpath will be added with the 
correct yaml markup
  * support for nested catalog item loading for policies, plus infinite 
recursion avoidance check
  * fix catalog item type detection for non-entities


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/524f7023
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/524f7023
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/524f7023

Branch: refs/heads/master
Commit: 524f702359f1d3a7f776bcd457641b55d5446940
Parents: 47c6f4e
Author: Svetoslav Neykov <[email protected]>
Authored: Tue Jun 2 13:30:39 2015 +0300
Committer: Svetoslav Neykov <[email protected]>
Committed: Tue Jun 2 19:50:05 2015 +0300

----------------------------------------------------------------------
 .../catalog/internal/BasicBrooklynCatalog.java  |  60 ++++++++++++++---
 .../brooklyn/osgi/tests/more/MoreLocation.java  |  25 +++++++
 .../brooklyn/osgi/tests/more/MorePolicy.java    |  27 ++++++++
 .../brooklyn/osgi/tests/more/MoreTemplate.java  |  30 +++++++++
 .../brooklyn/osgi/tests/more/MoreLocation.java  |  27 ++++++++
 .../brooklyn/osgi/tests/more/MorePolicy.java    |  30 +++++++++
 .../brooklyn/osgi/tests/more/MoreTemplate.java  |  32 +++++++++
 .../osgi/OsgiVersionMoreEntityTest.java         |   5 +-
 .../brooklyn-test-osgi-more-entities_0.1.0.jar  | Bin 12428 -> 14164 bytes
 .../brooklyn-test-osgi-more-entities_0.2.0.jar  | Bin 13078 -> 14799 bytes
 .../CatalogOsgiVersionMoreEntityTest.java       |  67 +++++++++++++++++++
 .../more-policies-osgi-catalog-scan.yaml        |  32 +++++++++
 12 files changed, 322 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/524f7023/core/src/main/java/brooklyn/catalog/internal/BasicBrooklynCatalog.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/brooklyn/catalog/internal/BasicBrooklynCatalog.java 
b/core/src/main/java/brooklyn/catalog/internal/BasicBrooklynCatalog.java
index b3cdda7..421794c 100644
--- a/core/src/main/java/brooklyn/catalog/internal/BasicBrooklynCatalog.java
+++ b/core/src/main/java/brooklyn/catalog/internal/BasicBrooklynCatalog.java
@@ -347,7 +347,7 @@ public class BasicBrooklynCatalog implements 
BrooklynCatalog {
                     spec = createEntitySpec(loadedItem.getSymbolicName(), 
plan, loader);
                     break;
                 case POLICY:
-                    spec = createPolicySpec(plan, loader);
+                    spec = createPolicySpec(loadedItem.getSymbolicName(), 
plan, loader);
                     break;
                 case LOCATION:
                     spec = createLocationSpec(plan, loader);
@@ -390,7 +390,7 @@ public class BasicBrooklynCatalog implements 
BrooklynCatalog {
         case ENTITY: 
             return createEntitySpec(optionalId, plan, loader);
         case LOCATION: return createLocationSpec(plan, loader);
-        case POLICY: return createPolicySpec(plan, loader);
+        case POLICY: return createPolicySpec(optionalId, plan, loader);
         }
         throw new IllegalStateException("Unknown CI Type "+ciType+" for 
"+plan);
     }
@@ -412,7 +412,7 @@ public class BasicBrooklynCatalog implements 
BrooklynCatalog {
             AssemblyTemplateInstantiator instantiator = 
at.getInstantiator().newInstance();
             if (instantiator instanceof AssemblyTemplateSpecInstantiator) {
                 return (SpecT) 
((AssemblyTemplateSpecInstantiator)instantiator).createNestedSpec(at, camp, 
loader, 
-                    symbolicName==null ? MutableSet.<String>of() : 
MutableSet.of(symbolicName));
+                    getInitialEncounteredSymbol(symbolicName));
             }
             throw new IllegalStateException("Unable to instantiate YAML; 
incompatible instantiator "+instantiator+" for "+at);
         } catch (Exception e) {
@@ -420,7 +420,15 @@ public class BasicBrooklynCatalog implements 
BrooklynCatalog {
         }
     }
 
-    private <T, SpecT> SpecT createPolicySpec(DeploymentPlan plan, 
BrooklynClassLoadingContext loader) {
+    private MutableSet<String> getInitialEncounteredSymbol(String 
symbolicName) {
+        return symbolicName==null ? MutableSet.<String>of() : 
MutableSet.of(symbolicName);
+    }
+
+    private <T, SpecT> SpecT createPolicySpec(String symbolicName, 
DeploymentPlan plan, BrooklynClassLoadingContext loader) {
+        return createPolicySpec(plan, loader, 
getInitialEncounteredSymbol(symbolicName));
+    }
+
+    private <T, SpecT> SpecT createPolicySpec(DeploymentPlan plan, 
BrooklynClassLoadingContext loader, Set<String> encounteredCatalogTypes) {
         //Would ideally re-use 
io.brooklyn.camp.brooklyn.spi.creation.BrooklynEntityDecorationResolver.PolicySpecResolver
         //but it is CAMP specific and there is no easy way to get hold of it.
         Object policies = 
checkNotNull(plan.getCustomAttributes().get(POLICIES_KEY), "policy config");
@@ -430,12 +438,11 @@ public class BasicBrooklynCatalog implements 
BrooklynCatalog {
 
         Object policy = Iterables.getOnlyElement((Iterable<?>)policies);
 
-        return createPolicySpec(loader, policy);
+        return createPolicySpec(loader, policy, encounteredCatalogTypes);
     }
 
     @SuppressWarnings("unchecked")
-    private <T, SpecT> SpecT createPolicySpec(BrooklynClassLoadingContext 
loader, Object policy) {
-        // TODO this (and LocationSpec) lack the loop-prevention which 
createEntitySpec has (hence different signature)
+    private <T, SpecT> SpecT createPolicySpec(BrooklynClassLoadingContext 
loader, Object policy, Set<String> encounteredCatalogTypes) {
         Map<String, Object> itemMap;
         if (policy instanceof String) {
             itemMap = ImmutableMap.<String, Object>of("type", policy);
@@ -445,9 +452,28 @@ public class BasicBrooklynCatalog implements 
BrooklynCatalog {
             throw new IllegalStateException("Policy expected to be string or 
map. Unsupported object type " + policy.getClass().getName() + " (" + 
policy.toString() + ")");
         }
 
-        String type = (String) 
checkNotNull(Yamls.getMultinameAttribute(itemMap, "policy_type", "policyType", 
"type"), "policy type");
+        String versionedId = (String) 
checkNotNull(Yamls.getMultinameAttribute(itemMap, "policy_type", "policyType", 
"type"), "policy type");
+        PolicySpec<? extends Policy> spec;
+        CatalogItem<?, ?> policyItem = 
CatalogUtils.getCatalogItemOptionalVersion(mgmt, versionedId);
+        if (policyItem != null && 
!encounteredCatalogTypes.contains(policyItem.getSymbolicName())) {
+            if (policyItem.getCatalogItemType() != CatalogItemType.POLICY) {
+                throw new IllegalStateException("Non-policy catalog item in 
policy context: " + policyItem);
+            }
+            //TODO re-use createSpec
+            BrooklynClassLoadingContext itemLoader = 
CatalogUtils.newClassLoadingContext(mgmt, policyItem);
+            if (policyItem.getPlanYaml() != null) {
+                DeploymentPlan plan = 
makePlanFromYaml(policyItem.getPlanYaml());
+                encounteredCatalogTypes.add(policyItem.getSymbolicName());
+                return createPolicySpec(plan, itemLoader, 
encounteredCatalogTypes);
+            } else if (policyItem.getJavaType() != null) {
+                spec = 
PolicySpec.create((Class<Policy>)itemLoader.loadClass(policyItem.getJavaType()));
+            } else {
+                throw new IllegalStateException("Invalid policy item - neither 
yaml nor javaType: " + policyItem);
+            }
+        } else {
+            spec = PolicySpec.create(loader.loadClass(versionedId, 
Policy.class));
+        }
         Map<String, Object> brooklynConfig = (Map<String, Object>) 
itemMap.get("brooklyn.config");
-        PolicySpec<? extends Policy> spec = 
PolicySpec.create(loader.loadClass(type, Policy.class));
         if (brooklynConfig != null) {
             spec.configure(brooklynConfig);
         }
@@ -919,7 +945,8 @@ public class BasicBrooklynCatalog implements 
BrooklynCatalog {
             }
             if (type!=null && key!=null) {
                 for (CatalogItemDtoAbstract<?,?> candidate: itemsDefinedSoFar) 
{
-                    if (type.equals(candidate.getSymbolicName()) || 
type.equals(candidate.getId())) {
+                    if (candidateCiType == candidate.getCatalogItemType() &&
+                            (type.equals(candidate.getSymbolicName()) || 
type.equals(candidate.getId()))) {
                         if (version==null || 
version.equals(candidate.getVersion())) {
                             // matched - exit
                             catalogItemType = candidateCiType;
@@ -1203,7 +1230,18 @@ public class BasicBrooklynCatalog implements 
BrooklynCatalog {
                 // replace java type with plan yaml -- needed for libraries / 
catalog item to be picked up,
                 // but probably useful to transition away from javaType 
altogether
                 dto.setSymbolicName(dto.getJavaType());
-                dto.setPlanYaml("services: [{ type: "+dto.getJavaType()+" }]");
+                switch (dto.getCatalogItemType()) {
+                    case TEMPLATE:
+                    case ENTITY:
+                        dto.setPlanYaml("services: [{ type: 
"+dto.getJavaType()+" }]");
+                        break;
+                    case POLICY:
+                        dto.setPlanYaml(POLICIES_KEY + ": [{ type: 
"+dto.getJavaType()+" }]");
+                        break;
+                    case LOCATION:
+                        dto.setPlanYaml(LOCATIONS_KEY + ": [{ type: 
"+dto.getJavaType()+" }]");
+                        break;
+                }
                 dto.setJavaType(null);
 
                 return dto;

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/524f7023/core/src/test/dependencies/osgi/more-entities-v1/src/main/java/brooklyn/osgi/tests/more/MoreLocation.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/dependencies/osgi/more-entities-v1/src/main/java/brooklyn/osgi/tests/more/MoreLocation.java
 
b/core/src/test/dependencies/osgi/more-entities-v1/src/main/java/brooklyn/osgi/tests/more/MoreLocation.java
new file mode 100644
index 0000000..75a9ff4
--- /dev/null
+++ 
b/core/src/test/dependencies/osgi/more-entities-v1/src/main/java/brooklyn/osgi/tests/more/MoreLocation.java
@@ -0,0 +1,25 @@
+/*
+ * 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 brooklyn.osgi.tests.more;
+
+
+import brooklyn.location.basic.AbstractLocation;
+
+public class MoreLocation extends AbstractLocation {
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/524f7023/core/src/test/dependencies/osgi/more-entities-v1/src/main/java/brooklyn/osgi/tests/more/MorePolicy.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/dependencies/osgi/more-entities-v1/src/main/java/brooklyn/osgi/tests/more/MorePolicy.java
 
b/core/src/test/dependencies/osgi/more-entities-v1/src/main/java/brooklyn/osgi/tests/more/MorePolicy.java
new file mode 100644
index 0000000..082c234
--- /dev/null
+++ 
b/core/src/test/dependencies/osgi/more-entities-v1/src/main/java/brooklyn/osgi/tests/more/MorePolicy.java
@@ -0,0 +1,27 @@
+/*
+ * 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 brooklyn.osgi.tests.more;
+
+
+import brooklyn.catalog.Catalog;
+import brooklyn.policy.basic.AbstractPolicy;
+
+public class MorePolicy extends AbstractPolicy {
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/524f7023/core/src/test/dependencies/osgi/more-entities-v1/src/main/java/brooklyn/osgi/tests/more/MoreTemplate.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/dependencies/osgi/more-entities-v1/src/main/java/brooklyn/osgi/tests/more/MoreTemplate.java
 
b/core/src/test/dependencies/osgi/more-entities-v1/src/main/java/brooklyn/osgi/tests/more/MoreTemplate.java
new file mode 100644
index 0000000..283e5da
--- /dev/null
+++ 
b/core/src/test/dependencies/osgi/more-entities-v1/src/main/java/brooklyn/osgi/tests/more/MoreTemplate.java
@@ -0,0 +1,30 @@
+/*
+ * 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 brooklyn.osgi.tests.more;
+
+
+import brooklyn.entity.Application;
+import brooklyn.entity.Effector;
+import brooklyn.entity.Entity;
+import brooklyn.entity.basic.AbstractApplication;
+import brooklyn.entity.effector.Effectors;
+import brooklyn.entity.proxying.ImplementedBy;
+
+public class MoreTemplate extends AbstractApplication {
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/524f7023/core/src/test/dependencies/osgi/more-entities-v2/src/main/java/brooklyn/osgi/tests/more/MoreLocation.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/dependencies/osgi/more-entities-v2/src/main/java/brooklyn/osgi/tests/more/MoreLocation.java
 
b/core/src/test/dependencies/osgi/more-entities-v2/src/main/java/brooklyn/osgi/tests/more/MoreLocation.java
new file mode 100644
index 0000000..9f2b9b5
--- /dev/null
+++ 
b/core/src/test/dependencies/osgi/more-entities-v2/src/main/java/brooklyn/osgi/tests/more/MoreLocation.java
@@ -0,0 +1,27 @@
+/*
+ * 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 brooklyn.osgi.tests.more;
+
+
+import brooklyn.catalog.Catalog;
+import brooklyn.location.basic.AbstractLocation;
+
+@Catalog(name="More Location", description="Cataliog item OSGi test location")
+public class MoreLocation extends AbstractLocation {
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/524f7023/core/src/test/dependencies/osgi/more-entities-v2/src/main/java/brooklyn/osgi/tests/more/MorePolicy.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/dependencies/osgi/more-entities-v2/src/main/java/brooklyn/osgi/tests/more/MorePolicy.java
 
b/core/src/test/dependencies/osgi/more-entities-v2/src/main/java/brooklyn/osgi/tests/more/MorePolicy.java
new file mode 100644
index 0000000..06d7fb3
--- /dev/null
+++ 
b/core/src/test/dependencies/osgi/more-entities-v2/src/main/java/brooklyn/osgi/tests/more/MorePolicy.java
@@ -0,0 +1,30 @@
+/*
+ * 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 brooklyn.osgi.tests.more;
+
+
+import brooklyn.catalog.Catalog;
+import brooklyn.policy.basic.AbstractPolicy;
+
+@Catalog(name="More Policy", description="Cataliog item OSGi test policy")
+public class MorePolicy extends AbstractPolicy {
+    public String sayHI(String name) {
+        return "HI "+name.toUpperCase()+" FROM V2";
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/524f7023/core/src/test/dependencies/osgi/more-entities-v2/src/main/java/brooklyn/osgi/tests/more/MoreTemplate.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/dependencies/osgi/more-entities-v2/src/main/java/brooklyn/osgi/tests/more/MoreTemplate.java
 
b/core/src/test/dependencies/osgi/more-entities-v2/src/main/java/brooklyn/osgi/tests/more/MoreTemplate.java
new file mode 100644
index 0000000..3fa86f9
--- /dev/null
+++ 
b/core/src/test/dependencies/osgi/more-entities-v2/src/main/java/brooklyn/osgi/tests/more/MoreTemplate.java
@@ -0,0 +1,32 @@
+/*
+ * 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 brooklyn.osgi.tests.more;
+
+
+import brooklyn.catalog.Catalog;
+import brooklyn.entity.Application;
+import brooklyn.entity.Effector;
+import brooklyn.entity.Entity;
+import brooklyn.entity.basic.AbstractApplication;
+import brooklyn.entity.effector.Effectors;
+import brooklyn.entity.proxying.ImplementedBy;
+
+@Catalog(name="More Template", description="Cataliog item OSGi test template")
+public class MoreTemplate extends AbstractApplication {
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/524f7023/core/src/test/java/brooklyn/management/osgi/OsgiVersionMoreEntityTest.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/java/brooklyn/management/osgi/OsgiVersionMoreEntityTest.java 
b/core/src/test/java/brooklyn/management/osgi/OsgiVersionMoreEntityTest.java
index 7f4009d..78cebd2 100644
--- a/core/src/test/java/brooklyn/management/osgi/OsgiVersionMoreEntityTest.java
+++ b/core/src/test/java/brooklyn/management/osgi/OsgiVersionMoreEntityTest.java
@@ -32,6 +32,7 @@ import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import brooklyn.basic.BrooklynObject;
 import brooklyn.catalog.CatalogItem;
 import brooklyn.catalog.internal.CatalogEntityItemDto;
 import brooklyn.catalog.internal.CatalogItemBuilder;
@@ -171,14 +172,14 @@ public class OsgiVersionMoreEntityTest {
     public static void assertV1MethodCall(Entity me) throws 
IllegalAccessException, InvocationTargetException, NoSuchMethodException {
         Assert.assertEquals(doMethodCallBrooklyn(me), 
EXPECTED_SAY_HI_BROOKLYN_RESPONSE_FROM_V1);
     }
-    public static void assertV2MethodCall(Entity me) throws 
IllegalAccessException, InvocationTargetException, NoSuchMethodException {
+    public static void assertV2MethodCall(BrooklynObject me) throws 
IllegalAccessException, InvocationTargetException, NoSuchMethodException {
         Assert.assertEquals(doMethodCallBrooklyn(me), 
EXPECTED_SAY_HI_BROOKLYN_RESPONSE_FROM_V2);
     }
     public static void assertV2EvilTwinMethodCall(Entity me) throws 
IllegalAccessException, InvocationTargetException, NoSuchMethodException {
         Assert.assertEquals(doMethodCallBrooklyn(me), 
EXPECTED_SAY_HI_BROOKLYN_RESPONSE_FROM_V2_EVIL_TWIN);
     }
 
-    public static Object doMethodCallBrooklyn(Entity me) throws 
IllegalAccessException, InvocationTargetException, NoSuchMethodException {
+    public static Object doMethodCallBrooklyn(BrooklynObject me) throws 
IllegalAccessException, InvocationTargetException, NoSuchMethodException {
         return me.getClass().getMethod("sayHI", String.class).invoke(me, 
"Brooklyn");
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/524f7023/core/src/test/resources/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.1.0.jar
----------------------------------------------------------------------
diff --git 
a/core/src/test/resources/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.1.0.jar
 
b/core/src/test/resources/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.1.0.jar
index 9958cf1..8d52a7f 100644
Binary files 
a/core/src/test/resources/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.1.0.jar
 and 
b/core/src/test/resources/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.1.0.jar
 differ

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/524f7023/core/src/test/resources/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.2.0.jar
----------------------------------------------------------------------
diff --git 
a/core/src/test/resources/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.2.0.jar
 
b/core/src/test/resources/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.2.0.jar
index 91cc1e8..8d7ee62 100644
Binary files 
a/core/src/test/resources/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.2.0.jar
 and 
b/core/src/test/resources/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.2.0.jar
 differ

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/524f7023/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogOsgiVersionMoreEntityTest.java
----------------------------------------------------------------------
diff --git 
a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogOsgiVersionMoreEntityTest.java
 
b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogOsgiVersionMoreEntityTest.java
index 5c52c39..412de82 100644
--- 
a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogOsgiVersionMoreEntityTest.java
+++ 
b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogOsgiVersionMoreEntityTest.java
@@ -18,6 +18,7 @@
  */
 package io.brooklyn.camp.brooklyn.catalog;
 
+import static org.testng.Assert.assertTrue;
 import io.brooklyn.camp.brooklyn.AbstractYamlTest;
 import io.brooklyn.camp.brooklyn.spi.creation.BrooklynEntityMatcher;
 
@@ -26,12 +27,17 @@ import org.slf4j.LoggerFactory;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
+import brooklyn.basic.BrooklynTypes;
+import brooklyn.catalog.BrooklynCatalog;
 import brooklyn.catalog.CatalogItem;
 import brooklyn.catalog.CatalogItem.CatalogItemType;
 import brooklyn.catalog.internal.CatalogUtils;
 import brooklyn.entity.Entity;
+import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.location.LocationSpec;
 import brooklyn.management.osgi.OsgiVersionMoreEntityTest;
 import brooklyn.policy.Policy;
+import brooklyn.policy.PolicySpec;
 import brooklyn.test.TestResourceUnavailableException;
 import brooklyn.util.ResourceUtils;
 import brooklyn.util.text.Strings;
@@ -190,5 +196,66 @@ public class CatalogOsgiVersionMoreEntityTest extends 
AbstractYamlTest {
         OsgiVersionMoreEntityTest.assertV2MethodCall(moreEntity);
     }
 
+    @Test
+    public void testMorePolicyV2AutoscanWithClasspath() throws Exception {
+        
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), 
"/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.2.0.jar");
+        
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), 
"/brooklyn/osgi/brooklyn-test-osgi-entities.jar");
+        
+        
addCatalogItems(getLocalResource("more-policies-osgi-catalog-scan.yaml"));
+        
+        log.info("autoscan for osgi found catalog items: 
"+Strings.join(mgmt().getCatalog().getCatalogItems(), ", "));
+
+        CatalogItem<?, ?> item = 
CatalogUtils.getCatalogItemOptionalVersion(mgmt(), "more-policy");
+        Assert.assertNotNull(item);
+        Assert.assertEquals(item.getVersion(), "2.0.test");
+        Assert.assertEquals(item.getCatalogItemType(), CatalogItemType.POLICY);
+        
+        // this refers to the java item, where the libraries are defined
+        item = CatalogUtils.getCatalogItemOptionalVersion(mgmt(), 
"brooklyn.osgi.tests.more.MorePolicy");
+        Assert.assertEquals(item.getVersion(), "2.0.test_java");
+        Assert.assertEquals(item.getLibraries().size(), 2);
+        
+        Entity app = createAndStartApplication(
+                "services: ",
+                "- type: brooklyn.entity.basic.BasicEntity",
+                "  brooklyn.policies:",
+                "  - type: more-policy:2.0.test");
+        Entity basicEntity = Iterables.getOnlyElement(app.getChildren());
+        Policy morePolicy = 
Iterables.getOnlyElement(basicEntity.getPolicies());
+        
+        Assert.assertEquals(morePolicy.getCatalogItemId(), 
"more-policy:2.0.test");
+        OsgiVersionMoreEntityTest.assertV2MethodCall(morePolicy);
+    }
+
+    @Test
+    public void testAutoscanWithClasspathCanCreateSpecs() throws Exception {
+        
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), 
"/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.2.0.jar");
+        
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), 
"/brooklyn/osgi/brooklyn-test-osgi-entities.jar");
+
+        
addCatalogItems(getLocalResource("more-entities-osgi-catalog-scan.yaml"));
+
+        log.info("autoscan for osgi found catalog items: 
"+Strings.join(mgmt().getCatalog().getCatalogItems(), ", "));
+
+        BrooklynCatalog catalog = mgmt().getCatalog();
+        Iterable<CatalogItem<Object, Object>> items = 
catalog.getCatalogItems();
+        for (CatalogItem<Object, Object> item: items) {
+            Object spec = catalog.createSpec(item);
+            switch (item.getCatalogItemType()) {
+                case TEMPLATE:
+                case ENTITY:
+                    assertTrue(spec instanceof EntitySpec, "Not an EntitySpec: 
" + spec);
+                    
BrooklynTypes.getDefinedEntityType(((EntitySpec<?>)spec).getType());
+                    break;
+                case POLICY:
+                    assertTrue(spec instanceof PolicySpec, "Not a PolicySpec: 
" + spec);
+                    
BrooklynTypes.getDefinedBrooklynType(((PolicySpec<?>)spec).getType());
+                    break;
+                case LOCATION:
+                    assertTrue(spec instanceof LocationSpec, "Not a 
LocationSpec: " + spec);
+                    
BrooklynTypes.getDefinedBrooklynType(((LocationSpec<?>)spec).getType());
+                    break;
+            }
+        }
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/524f7023/usage/camp/src/test/resources/io/brooklyn/camp/brooklyn/catalog/more-policies-osgi-catalog-scan.yaml
----------------------------------------------------------------------
diff --git 
a/usage/camp/src/test/resources/io/brooklyn/camp/brooklyn/catalog/more-policies-osgi-catalog-scan.yaml
 
b/usage/camp/src/test/resources/io/brooklyn/camp/brooklyn/catalog/more-policies-osgi-catalog-scan.yaml
new file mode 100644
index 0000000..47f344d
--- /dev/null
+++ 
b/usage/camp/src/test/resources/io/brooklyn/camp/brooklyn/catalog/more-policies-osgi-catalog-scan.yaml
@@ -0,0 +1,32 @@
+#
+# 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.
+#
+
+# test case which demonstrates osgi bundles can be scanned, *if* expand 
classpath is true
+
+brooklyn.catalog:
+  items:
+  - scanJavaAnnotations: true
+    version: 2.0.test_java
+    libraries:
+    - classpath:/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.2.0.jar
+    - classpath:/brooklyn/osgi/brooklyn-test-osgi-entities.jar
+  - item:
+      id: more-policy
+      type: brooklyn.osgi.tests.more.MorePolicy
+      version: 2.0.test

Reply via email to