incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1345010 - /sling/whiteboard/SLING-2396/jcr.resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
Date Fri, 01 Jun 2012 08:31:53 GMT
Author: cziegeler
Date: Fri Jun  1 08:31:53 2012
New Revision: 1345010

URL: http://svn.apache.org/viewvc?rev=1345010&view=rev
Log:
SLING-2396 : Add new resource provider interfaces (WiP)

Modified:
    sling/whiteboard/SLING-2396/jcr.resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java

Modified: sling/whiteboard/SLING-2396/jcr.resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/SLING-2396/jcr.resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java?rev=1345010&r1=1345009&r2=1345010&view=diff
==============================================================================
--- sling/whiteboard/SLING-2396/jcr.resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
(original)
+++ sling/whiteboard/SLING-2396/jcr.resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
Fri Jun  1 08:31:53 2012
@@ -19,6 +19,7 @@
 package org.apache.sling.jcr.resource.internal.helper.jcr;
 
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
@@ -29,16 +30,22 @@ import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.jcr.Value;
+import javax.jcr.query.Query;
+import javax.jcr.query.QueryResult;
+import javax.jcr.query.Row;
+import javax.jcr.query.RowIterator;
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.sling.api.SlingException;
 import org.apache.sling.api.resource.AttributesProvider;
 import org.apache.sling.api.resource.DynamicResourceProvider;
 import org.apache.sling.api.resource.QueriableResourceProvider;
+import org.apache.sling.api.resource.QuerySyntaxException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceProvider;
 import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.api.resource.ResourceWrapper;
+import org.apache.sling.jcr.resource.JcrResourceUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -54,9 +61,21 @@ public class JcrResourceProvider
                AttributesProvider,
                QueriableResourceProvider {
 
+    /** column name for node path */
+    private static final String QUERY_COLUMN_PATH = "jcr:path";
+
+    /** column name for score value */
+    private static final String QUERY_COLUMN_SCORE = "jcr:score";
+
+    @SuppressWarnings("deprecation")
+    private static final String DEFAULT_QUERY_LANGUAGE = Query.XPATH;
+
     /** Default logger */
     private final Logger log = LoggerFactory.getLogger(getClass());
 
+    /** Flag for closing. */
+    private boolean closed = false;
+
     private final Session session;
     private final ClassLoader dynamicClassLoader;
     private final boolean closeSession;
@@ -74,6 +93,7 @@ public class JcrResourceProvider
     /**
      * @see org.apache.sling.api.resource.ResourceProvider#getResource(org.apache.sling.api.resource.ResourceResolver,
javax.servlet.http.HttpServletRequest, java.lang.String)
      */
+    @SuppressWarnings("javadoc")
     public Resource getResource(ResourceResolver resourceResolver,
             HttpServletRequest request, String path) throws SlingException {
         return getResource(resourceResolver, path);
@@ -83,8 +103,8 @@ public class JcrResourceProvider
      * @see org.apache.sling.api.resource.ResourceProvider#getResource(org.apache.sling.api.resource.ResourceResolver,
java.lang.String)
      */
     public Resource getResource(ResourceResolver resourceResolver, String path)
-            throws SlingException {
-
+    throws SlingException {
+        this.checkClosed();
         try {
             return createResource(resourceResolver, path);
         } catch (RepositoryException re) {
@@ -97,7 +117,8 @@ public class JcrResourceProvider
     /**
      * @see org.apache.sling.api.resource.ResourceProvider#listChildren(org.apache.sling.api.resource.Resource)
      */
-    public Iterator<Resource> listChildren(Resource parent) {
+    public Iterator<Resource> listChildren(final Resource parent) {
+        this.checkClosed();
 
         JcrItemResource parentItemResource;
 
@@ -106,10 +127,6 @@ public class JcrResourceProvider
 
             parentItemResource = (JcrItemResource) parent;
 
-        } else if (parent instanceof ResourceWrapper) {
-
-            return listChildren(((ResourceWrapper) parent).getResource());
-
         } else {
 
             // try to get the JcrItemResource for the parent path to list
@@ -189,38 +206,134 @@ public class JcrResourceProvider
      * @see org.apache.sling.api.resource.DynamicResourceProvider#isLive()
      */
     public boolean isLive() {
-        return session.isLive();
+        return !closed && session.isLive();
     }
 
     /**
      * @see org.apache.sling.api.resource.DynamicResourceProvider#close()
      */
     public void close() {
-        if ( this.closeSession ) {
+        if ( this.closeSession && !closed) {
             session.logout();
         }
+        this.closed = true;
     }
 
     /**
-     * @see org.apache.sling.api.resource.QueriableResourceProvider#findResources(java.lang.String,
java.lang.String)
+     * Check if the resource resolver is already closed.
+     *
+     * @throws IllegalStateException If the resolver is already closed
      */
-    public Iterator<Resource> findResources(String query, String language) {
-        // TODO Auto-generated method stub
-        return null;
+    private void checkClosed() {
+        if ( this.closed ) {
+            throw new IllegalStateException("Resource resolver is already closed.");
+        }
+    }
+
+    /**
+     * @see org.apache.sling.api.resource.QueriableResourceProvider#findResources(ResourceResolver,
java.lang.String, java.lang.String)
+     */
+    public Iterator<Resource> findResources(final ResourceResolver resolver,
+                    final String query, final String language) {
+        checkClosed();
+
+        try {
+            final QueryResult res = JcrResourceUtil.query(session, query, language);
+            return new JcrNodeResourceIterator(resolver, res.getNodes(), this.dynamicClassLoader);
+        } catch (final javax.jcr.query.InvalidQueryException iqe) {
+            throw new QuerySyntaxException(iqe.getMessage(), query, language, iqe);
+        } catch (final RepositoryException re) {
+            throw new SlingException(re.getMessage(), re);
+        }
     }
 
     /**
      * @see org.apache.sling.api.resource.QueriableResourceProvider#queryResources(java.lang.String,
java.lang.String)
      */
-    public Iterator<Map<String, Object>> queryResources(String query, String
language) {
-        // TODO Auto-generated method stub
-        return null;
+    public Iterator<Map<String, Object>> queryResources(final String query, final
String language) {
+        checkClosed();
+
+        final String queryLanguage = isSupportedQueryLanguage(language) ? language : DEFAULT_QUERY_LANGUAGE;
+
+        try {
+            QueryResult result = JcrResourceUtil.query(session, query,
+                queryLanguage);
+            final String[] colNames = result.getColumnNames();
+            final RowIterator rows = result.getRows();
+            return new Iterator<Map<String, Object>>() {
+                public boolean hasNext() {
+                    return rows.hasNext();
+                };
+
+                public Map<String, Object> next() {
+                    Map<String, Object> row = new HashMap<String, Object>();
+                    try {
+                        Row jcrRow = rows.nextRow();
+                        boolean didPath = false;
+                        boolean didScore = false;
+                        Value[] values = jcrRow.getValues();
+                        for (int i = 0; i < values.length; i++) {
+                            Value v = values[i];
+                            if (v != null) {
+                                String colName = colNames[i];
+                                row.put(colName,
+                                    JcrResourceUtil.toJavaObject(values[i]));
+                                if (colName.equals(QUERY_COLUMN_PATH)) {
+                                    didPath = true;
+                                }
+                                if (colName.equals(QUERY_COLUMN_SCORE)) {
+                                    didScore = true;
+                                }
+                            }
+                        }
+                        if (!didPath) {
+                            row.put(QUERY_COLUMN_PATH, jcrRow.getPath());
+                        }
+                        if (!didScore) {
+                            row.put(QUERY_COLUMN_SCORE, jcrRow.getScore());
+                        }
+
+                    } catch (RepositoryException re) {
+                        log.error(
+                            "queryResources$next: Problem accessing row values",
+                            re);
+                    }
+                    return row;
+                }
+
+                public void remove() {
+                    throw new UnsupportedOperationException("remove");
+                }
+            };
+        } catch (final javax.jcr.query.InvalidQueryException iqe) {
+            throw new QuerySyntaxException(iqe.getMessage(), query, language,
+                iqe);
+        } catch (final RepositoryException re) {
+            throw new SlingException(re.getMessage(), re);
+        }
+    }
+
+    private boolean isSupportedQueryLanguage(final String language) {
+        try {
+            String[] supportedLanguages = session.getWorkspace().
+                getQueryManager().getSupportedQueryLanguages();
+            for (String lang : supportedLanguages) {
+                if (lang.equals(language)) {
+                    return true;
+                }
+            }
+        } catch (final RepositoryException e) {
+            log.error("Unable to discover supported query languages", e);
+        }
+        return false;
     }
 
     /**
      * @see org.apache.sling.api.resource.AttributesProvider#getAttributeNames()
      */
     public Collection<String> getAttributeNames() {
+        this.checkClosed();
+
         final Set<String> names = new HashSet<String>();
         final String[] sessionNames = session.getAttributeNames();
         for(final String name : sessionNames) {
@@ -235,6 +348,8 @@ public class JcrResourceProvider
      * @see org.apache.sling.api.resource.AttributesProvider#getAttribute(java.lang.String)
      */
     public Object getAttribute(final String name) {
+        this.checkClosed();
+
         if ( JcrResourceProviderFactory.isAttributeVisible(name) ) {
             return session.getAttribute(name);
         }



Mime
View raw message