You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:36:33 UTC

[sling-org-apache-sling-hapi] 04/15: SLING-5055 - avoid infinite recursion - patch contributed by Andrei Dulvac, thanks!

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

rombert pushed a commit to annotated tag org.apache.sling.hapi-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-hapi.git

commit ef543d290478e514c71e8f0db52cc2db498010d2
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Wed Sep 23 12:17:20 2015 +0000

    SLING-5055 - avoid infinite recursion - patch contributed by Andrei Dulvac, thanks!
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/hapi/core@1704829 13f79535-47bb-0310-9956-ffa450edef68
---
 .../org/apache/sling/hapi/impl/HApiTypeImpl.java   | 20 ++++++-
 .../org/apache/sling/hapi/impl/HApiUtilImpl.java   | 65 ++++++++++++++--------
 2 files changed, 61 insertions(+), 24 deletions(-)

diff --git a/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java b/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java
index faa6e1e..8648179 100644
--- a/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java
+++ b/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java
@@ -31,7 +31,7 @@ public class HApiTypeImpl implements HApiType {
 
     public static final Logger LOG = LoggerFactory.getLogger(HApiTypeImpl.class);
 
-    private final HApiType parent;
+    private HApiType parent;
 
     private String name;
 
@@ -68,6 +68,7 @@ public class HApiTypeImpl implements HApiType {
     /**
      * {@inheritDoc}
      */
+    @Override
     public String getName() {
         return name;
     }
@@ -75,6 +76,7 @@ public class HApiTypeImpl implements HApiType {
     /**
      * {@inheritDoc}
      */
+    @Override
     public String getDescription() {
         return description;
     }
@@ -82,6 +84,7 @@ public class HApiTypeImpl implements HApiType {
     /**
      * {@inheritDoc}
      */
+    @Override
     public String getPath() {
         return path;
     }
@@ -89,6 +92,7 @@ public class HApiTypeImpl implements HApiType {
     /**
      * {@inheritDoc}
      */
+    @Override
     public String getUrl() {
         return getPath() + ".html";
     }
@@ -96,6 +100,7 @@ public class HApiTypeImpl implements HApiType {
     /**
      * {@inheritDoc}
      */
+    @Override
     public String getFqdn() {
         return fqdn;
     }
@@ -103,6 +108,7 @@ public class HApiTypeImpl implements HApiType {
     /**
      * {@inheritDoc}
      */
+    @Override
     public List<String> getParameters() {
         return parameters;
     }
@@ -110,6 +116,7 @@ public class HApiTypeImpl implements HApiType {
     /**
      * {@inheritDoc}
      */
+    @Override
     public Map<String, HApiProperty> getProperties() {
         return properties;
     }
@@ -117,6 +124,7 @@ public class HApiTypeImpl implements HApiType {
     /**
      * {@inheritDoc}
      */
+    @Override
     public Map<String, HApiProperty> getAllProperties() {
         Map<String, HApiProperty> allProps = new HashMap<String, HApiProperty>();
         LOG.debug("parent: {}", parent);
@@ -132,6 +140,7 @@ public class HApiTypeImpl implements HApiType {
     /**
      * {@inheritDoc}
      */
+    @Override
     public HApiType getParent() {
         return parent;
     }
@@ -139,8 +148,17 @@ public class HApiTypeImpl implements HApiType {
     /**
      * {@inheritDoc}
      */
+    @Override
     public boolean isAbstract() {
         return isAbstract;
     }
 
+
+    public void setParent(HApiType parent) {
+        this.parent = parent;
+    }
+
+    public void setProperties(Map<String, HApiProperty> properties) {
+        this.properties = properties;
+    }
 }
diff --git a/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java b/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
index a8f3d51..7b448f5 100644
--- a/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
+++ b/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
@@ -126,13 +126,6 @@ public class HApiUtilImpl implements HApiUtil {
         String path = typeNode.getPath();
         String fqdn = typeNode.getProperty("fqdn").getValue().getString();
 
-        // get parent if it exists
-        HApiType parent = null;
-        String parentPath = typeNode.hasProperty("extends") ? typeNode.getProperty("extends").getString() : null;
-        if (null != parentPath) {
-            parent = this.fromPath(resolver, parentPath);
-        }
-
         // get parameters
         Value[] parameterValues = typeNode.hasProperty("parameters") ? typeNode.getProperty("parameters").getValues() : new Value[]{};
         List<String> parameters = new ArrayList<String>(parameterValues.length);
@@ -140,27 +133,49 @@ public class HApiUtilImpl implements HApiUtil {
         for (Value p : Arrays.asList(parameterValues)) {
             parameters.add(p.getString());
         }
+        HApiTypeImpl newType = new HApiTypeImpl(name, description, path, fqdn, parameters, null, null, false);
+        TypesCache.getInstance(this).addType(newType);
+
+        try {
+            // get parent if it exists
+            HApiType parent = null;
+            String parentPath = typeNode.hasProperty("extends") ? typeNode.getProperty("extends").getString() : null;
+            if (null != parentPath) {
+                parent = TypesCache.getInstance(this).getType(resolver, parentPath);
+            }
 
-        // Get properties
-        Map<String, HApiProperty> properties = new HashMap<String, HApiProperty>();
+            // Get properties
+            Map<String, HApiProperty> properties = new HashMap<String, HApiProperty>();
 
-        // Add the properties from this node
-        Iterator<Node> it = typeNode.getNodes();
-        while (it.hasNext()) {
-            Node propNode = it.next();
-            String propName  = propNode.getName();
-            String propDescription = propNode.hasProperty("description") ? propNode.getProperty("description").getString() : "";
+            // Add the properties from this node
+            Iterator<Node> it = typeNode.getNodes();
+            while (it.hasNext()) {
+                Node propNode = it.next();
+                System.out.println("Node=" + propNode);
+                String propName = propNode.getName();
+                String propDescription = propNode.hasProperty("description") ? propNode.getProperty("description").getString() : "";
 
-            // TODO: maybe create adapter and use adaptTo()
-            // TODO: this could be slow, the types can be instantiated externally in a service activate()
-            String type = propNode.getProperty("type").getValue().getString();
-            HApiType propType = this.fromPath(resolver, type);
-            Boolean propMultiple = propNode.hasProperty("multiple") ? propNode.getProperty("multiple").getBoolean() : false;
+                String typePath = propNode.getProperty("type").getValue().getString();
+                HApiType propType = TypesCache.getInstance(this).getType(resolver, typePath);
+                Boolean propMultiple = propNode.hasProperty("multiple") ? propNode.getProperty("multiple").getBoolean() : false;
 
-            HApiProperty prop = new HApiPropertyImpl(propName, propDescription, propType, propMultiple);
-            properties.put(prop.getName(), prop);
+                HApiProperty prop = new HApiPropertyImpl(propName, propDescription, propType, propMultiple);
+                properties.put(prop.getName(), prop);
+            }
+            newType.setParent(parent);
+            newType.setProperties(properties);
+
+        } catch (RuntimeException t) {
+            // Remove type from cache if it wasn't created successfully
+            TypesCache.getInstance(this).removeType(newType.getPath());
+            throw t;
+        } catch (RepositoryException e) {
+            // Remove type from cache if it wasn't created successfully
+            TypesCache.getInstance(this).removeType(newType.getPath());
+            throw e;
         }
-        return new HApiTypeImpl(name, description, path, fqdn, parameters, properties, parent, false);
+
+        return newType;
     }
 
     /**
@@ -208,5 +223,9 @@ class TypesCache {
     public void addType(HApiType type) {
         this.types.put(type.getPath(), type);
     }
+
+    public void removeType(String path) {
+        this.types.remove(path);
+    }
 }
 

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.