Author: bdelacretaz
Date: Wed Sep 23 12:17:20 2015
New Revision: 1704829

URL: http://svn.apache.org/viewvc?rev=1704829&view=rev
Log:
SLING-5055 - avoid infinite recursion - patch contributed by Andrei Dulvac, 
thanks!

Modified:
    
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java
    
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java

Modified: 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java?rev=1704829&r1=1704828&r2=1704829&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java
 (original)
+++ 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java
 Wed Sep 23 12:17:20 2015
@@ -31,7 +31,7 @@ public class HApiTypeImpl implements HAp
 
     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 HAp
     /**
      * {@inheritDoc}
      */
+    @Override
     public String getName() {
         return name;
     }
@@ -75,6 +76,7 @@ public class HApiTypeImpl implements HAp
     /**
      * {@inheritDoc}
      */
+    @Override
     public String getDescription() {
         return description;
     }
@@ -82,6 +84,7 @@ public class HApiTypeImpl implements HAp
     /**
      * {@inheritDoc}
      */
+    @Override
     public String getPath() {
         return path;
     }
@@ -89,6 +92,7 @@ public class HApiTypeImpl implements HAp
     /**
      * {@inheritDoc}
      */
+    @Override
     public String getUrl() {
         return getPath() + ".html";
     }
@@ -96,6 +100,7 @@ public class HApiTypeImpl implements HAp
     /**
      * {@inheritDoc}
      */
+    @Override
     public String getFqdn() {
         return fqdn;
     }
@@ -103,6 +108,7 @@ public class HApiTypeImpl implements HAp
     /**
      * {@inheritDoc}
      */
+    @Override
     public List<String> getParameters() {
         return parameters;
     }
@@ -110,6 +116,7 @@ public class HApiTypeImpl implements HAp
     /**
      * {@inheritDoc}
      */
+    @Override
     public Map<String, HApiProperty> getProperties() {
         return properties;
     }
@@ -117,6 +124,7 @@ public class HApiTypeImpl implements HAp
     /**
      * {@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 HAp
     /**
      * {@inheritDoc}
      */
+    @Override
     public HApiType getParent() {
         return parent;
     }
@@ -139,8 +148,17 @@ public class HApiTypeImpl implements HAp
     /**
      * {@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;
+    }
 }

Modified: 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java?rev=1704829&r1=1704828&r2=1704829&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
 (original)
+++ 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
 Wed Sep 23 12:17:20 2015
@@ -126,13 +126,6 @@ public class HApiUtilImpl implements HAp
         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 HAp
         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);
 
-        // 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() : "";
-
-            // 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;
-
-            HApiProperty prop = new HApiPropertyImpl(propName, 
propDescription, propType, propMultiple);
-            properties.put(prop.getName(), prop);
+        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>();
+
+            // 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() : "";
+
+                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);
+            }
+            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);
+    }
 }
 


Reply via email to