You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by md...@apache.org on 2011/07/26 23:13:29 UTC

svn commit: r1151253 - in /jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel: RepositoryServiceImpl.java util/Predicate1.java util/Predicates.java

Author: mduerig
Date: Tue Jul 26 21:13:28 2011
New Revision: 1151253

URL: http://svn.apache.org/viewvc?rev=1151253&view=rev
Log:
spi2microkernel (WIP)
- improved supertype calculation 

Added:
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Predicate1.java
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Predicates.java
Modified:
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java

Modified: jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java?rev=1151253&r1=1151252&r2=1151253&view=diff
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java (original)
+++ jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java Tue Jul 26 21:13:28 2011
@@ -47,9 +47,11 @@ import org.apache.jackrabbit.spi.commons
 import org.apache.jackrabbit.spi.commons.QNodeDefinitionImpl;
 import org.apache.jackrabbit.spi.commons.SessionInfoImpl;
 import org.apache.jackrabbit.spi.commons.batch.ConsolidatingChangeLog;
+import org.apache.jackrabbit.spi.commons.iterator.Iterators;
 import org.apache.jackrabbit.spi2microkernel.util.Function1;
 import org.apache.jackrabbit.spi2microkernel.util.JsonHandlerBase;
 import org.apache.jackrabbit.spi2microkernel.util.Paths;
+import org.apache.jackrabbit.spi2microkernel.util.Predicate1;
 import org.apache.jackrabbit.spi2microkernel.util.Values;
 import org.json.simple.parser.JSONParser;
 import org.json.simple.parser.ParseException;
@@ -83,6 +85,8 @@ import static javax.jcr.Repository.*;
 import static org.apache.jackrabbit.spi.commons.name.NameConstants.*;
 import static org.apache.jackrabbit.spi2microkernel.util.Arrays.contains;
 import static org.apache.jackrabbit.spi2microkernel.util.ItemInfos.*;
+import static org.apache.jackrabbit.spi2microkernel.util.Predicates.containsElement;
+import static org.apache.jackrabbit.spi2microkernel.util.Predicates.exists;
 import static org.apache.jackrabbit.spi2microkernel.util.Values.encodeValue;
 
 public class RepositoryServiceImpl extends AbstractRepositoryService {
@@ -625,15 +629,35 @@ public class RepositoryServiceImpl exten
             });
         }
 
-        // todo implement more general sub type relation
-        private boolean isSubType(Name name1, Name name2) throws RepositoryException {
-            if (MIX_CREATED.equals(name2)) {
-              return NT_HIERARCHYNODE.equals(name1) ||
-                     NT_FILE.equals(name1) ||
-                     NT_FOLDER.equals(name1);
+        private boolean isSubType(Name name1, final Name name2) throws RepositoryException {
+            try {
+                return isDirectSubType(name1, name2) || exists(new Predicate1<Name>() {
+                    public Boolean apply(Name name) throws Exception {
+                        return isSubType(name, name2);
+                    }
+                }, superTypes(name1));
+            }
+            catch (RepositoryException e) {
+                throw e;
             }
+            catch (Exception e) {
+                throw new RepositoryException(e); 
+            }
+        }
 
-            throw new RepositoryException("Cannot determine subtype relation: " + name1 + " > " + name2);
+        private boolean isDirectSubType(Name name1, Name name2) throws Exception {
+            return containsElement(name2, superTypes(name1));
+        }
+
+        private Iterator<Name> superTypes(Name name) throws RepositoryException {
+            Iterator<QNodeTypeDefinition> ntDefinitions = nodeTypeDefs.getDefinitions(new Name[]{name});
+            if (ntDefinitions.hasNext()) {
+                Name[] superTypes = ntDefinitions.next().getSupertypes();
+                return Iterators.arrayIterator(superTypes, 0, superTypes.length);
+            }
+            else {
+                return Iterators.empty();
+            }
         }
 
         private String target(NodeId nodeId) throws RepositoryException {

Added: jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Predicate1.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Predicate1.java?rev=1151253&view=auto
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Predicate1.java (added)
+++ jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Predicate1.java Tue Jul 26 21:13:28 2011
@@ -0,0 +1,23 @@
+/*
+ * 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 org.apache.jackrabbit.spi2microkernel.util;
+
+public interface Predicate1<T> extends Function1<T, Boolean> {
+}

Added: jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Predicates.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Predicates.java?rev=1151253&view=auto
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Predicates.java (added)
+++ jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Predicates.java Tue Jul 26 21:13:28 2011
@@ -0,0 +1,45 @@
+/*
+ * 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 org.apache.jackrabbit.spi2microkernel.util;
+
+import java.util.Iterator;
+
+public class Predicates {
+    private Predicates() {}
+
+    public static <T> boolean exists(Function1<T, Boolean> predicate, Iterator<T> iterator) throws Exception {
+        while (iterator.hasNext()) {
+            if (predicate.apply(iterator.next())) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    public static <T> boolean containsElement(final T element, Iterator<T> iterator) throws Exception {
+        return exists(new Predicate1<T>(){
+            public Boolean apply(T arg) throws Exception {
+                return element.equals(arg);
+            }
+        }, iterator);
+    }
+
+}