You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by iu...@apache.org on 2021/10/28 11:00:47 UTC

[brooklyn-server] branch master updated: Seatching in the added policy in app registry

This is an automated email from the ASF dual-hosted git repository.

iuliana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git


The following commit(s) were added to refs/heads/master by this push:
     new 28e7c59  Seatching in the added policy in app registry
     new 6c0da04  Merge pull request #1268 from jcabrerizo/feature/policy/search-policy-as-type-registry
28e7c59 is described below

commit 28e7c590c0873368d39fd22503e6cd0eecbfa2b2
Author: Juan Cabrerizo <ju...@cloudsoft.io>
AuthorDate: Thu Oct 28 10:43:35 2021 +0100

    Seatching in the added policy in app registry
---
 .../brooklyn/rest/resources/PolicyResource.java    | 27 +++++++++++++++++-----
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/PolicyResource.java b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/PolicyResource.java
index 777a2d1..bacdd4d 100644
--- a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/PolicyResource.java
+++ b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/PolicyResource.java
@@ -26,8 +26,12 @@ import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 
 import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.entity.EntityInitializer;
+import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.api.policy.Policy;
 import org.apache.brooklyn.api.policy.PolicySpec;
+import org.apache.brooklyn.api.typereg.RegisteredType;
+import org.apache.brooklyn.core.mgmt.classloading.JavaBrooklynClassLoadingContext;
 import org.apache.brooklyn.core.mgmt.entitlement.Entitlements;
 import org.apache.brooklyn.core.policy.Policies;
 import org.apache.brooklyn.rest.api.PolicyApi;
@@ -40,6 +44,7 @@ import org.apache.brooklyn.rest.transform.PolicyTransformer;
 import org.apache.brooklyn.rest.util.WebResourceUtils;
 import org.apache.brooklyn.util.core.ClassLoaderUtils;
 import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.guava.Maybe;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -92,18 +97,28 @@ public class PolicyResource extends AbstractBrooklynRestResource implements Poli
                     Entitlements.getEntitlementContext().user());
         }
 
-        Class<? extends Policy> policyType;
+        PolicySpec policySpec;
         try {
-            policyType = (Class<? extends Policy>) new ClassLoaderUtils(this, mgmt()).loadClass(policyTypeName);
-        } catch (ClassNotFoundException e) {
-            throw WebResourceUtils.badRequest("No policy with type %s found", policyTypeName);
+            ManagementContext managementContext = mgmt();
+            Maybe<RegisteredType> maybePolicy = managementContext.getTypeRegistry()
+                    .getMaybe(policyTypeName, null)
+                    .map(registeredType -> managementContext.getTypeRegistry().get(policyTypeName));
+
+            if(maybePolicy.isPresent()){
+                RegisteredType registeredType = maybePolicy.get();
+                policySpec = managementContext.getTypeRegistry().createSpec(registeredType, null, PolicySpec.class);
+            }else{
+                Class<? extends Policy> policyType;
+                policyType = (Class<? extends Policy>) new ClassLoaderUtils(this, mgmt()).loadClass(policyTypeName);
+                policySpec = PolicySpec.create(policyType);
+            }
         } catch (ClassCastException e) {
             throw WebResourceUtils.badRequest("No policy with type %s found", policyTypeName);
         } catch (Exception e) {
             throw Exceptions.propagate(e);
         }
-
-        Policy policy = entity.policies().add(PolicySpec.create(policyType).configure(config));
+        policySpec.configure(config);
+        Policy policy = entity.policies().add(policySpec);
         log.debug("REST API added policy " + policy + " to " + entity);
 
         return PolicyTransformer.policySummary(entity, policy, ui.getBaseUriBuilder());