Author: chetanm
Date: Fri Oct 27 11:46:16 2017
New Revision: 1813511

URL: http://svn.apache.org/viewvc?rev=1813511&view=rev
Log:
OAK-6873 - UserInitializer should not use hard coded QueryIndexProvider

Added:
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/CustomQueryIndexProviderTest.java
   (with props)
    
jackrabbit/oak/trunk/oak-query-spi/src/main/java/org/apache/jackrabbit/oak/spi/query/QueryIndexProviderAware.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserInitializer.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java?rev=1813511&r1=1813510&r2=1813511&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java 
(original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java 
Fri Oct 27 11:46:16 2017
@@ -100,6 +100,7 @@ import org.apache.jackrabbit.oak.spi.lif
 import org.apache.jackrabbit.oak.spi.lifecycle.WorkspaceInitializer;
 import org.apache.jackrabbit.oak.spi.query.CompositeQueryIndexProvider;
 import org.apache.jackrabbit.oak.spi.query.QueryIndexProvider;
+import org.apache.jackrabbit.oak.spi.query.QueryIndexProviderAware;
 import org.apache.jackrabbit.oak.spi.query.QueryLimits;
 import org.apache.jackrabbit.oak.spi.security.SecurityConfiguration;
 import org.apache.jackrabbit.oak.spi.security.SecurityProvider;
@@ -716,7 +717,11 @@ public class Oak {
                         new Function<SecurityConfiguration, 
WorkspaceInitializer>() {
                             @Override
                             public WorkspaceInitializer 
apply(SecurityConfiguration sc) {
-                                return sc.getWorkspaceInitializer();
+                                WorkspaceInitializer wi = 
sc.getWorkspaceInitializer();
+                                if (wi instanceof QueryIndexProviderAware){
+                                    ((QueryIndexProviderAware) 
wi).setQueryIndexProvider(indexProvider);
+                                }
+                                return wi;
                             }
                         });
         OakInitializer.initialize(

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserInitializer.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserInitializer.java?rev=1813511&r1=1813510&r2=1813511&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserInitializer.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserInitializer.java
 Fri Oct 27 11:46:16 2017
@@ -28,14 +28,13 @@ import org.apache.jackrabbit.oak.commons
 import org.apache.jackrabbit.oak.namepath.NamePathMapper;
 import org.apache.jackrabbit.oak.plugins.index.IndexConstants;
 import org.apache.jackrabbit.oak.plugins.index.IndexUtils;
-import org.apache.jackrabbit.oak.plugins.index.nodetype.NodeTypeIndexProvider;
-import org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexProvider;
 import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore;
 import org.apache.jackrabbit.oak.plugins.tree.factories.RootFactory;
 import org.apache.jackrabbit.oak.plugins.tree.TreeUtil;
 import org.apache.jackrabbit.oak.spi.commit.EmptyHook;
 import org.apache.jackrabbit.oak.spi.lifecycle.WorkspaceInitializer;
-import org.apache.jackrabbit.oak.spi.query.CompositeQueryIndexProvider;
+import org.apache.jackrabbit.oak.spi.query.QueryIndexProvider;
+import org.apache.jackrabbit.oak.spi.query.QueryIndexProviderAware;
 import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
 import org.apache.jackrabbit.oak.spi.security.SecurityProvider;
 import org.apache.jackrabbit.oak.spi.security.user.UserConfiguration;
@@ -46,6 +45,7 @@ import org.apache.jackrabbit.oak.spi.sta
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 import static 
org.apache.jackrabbit.oak.plugins.memory.ModifiedNodeState.squeeze;
 
@@ -73,7 +73,7 @@ import static org.apache.jackrabbit.oak.
  * <li>{@link UserConstants#REP_MEMBERS}</li>
  * </ul>
  */
-class UserInitializer implements WorkspaceInitializer, UserConstants {
+class UserInitializer implements WorkspaceInitializer, UserConstants, 
QueryIndexProviderAware {
 
     /**
      * logger instance
@@ -82,6 +82,8 @@ class UserInitializer implements Workspa
 
     private final SecurityProvider securityProvider;
 
+    private QueryIndexProvider queryIndexProvider;
+
     UserInitializer(SecurityProvider securityProvider) {
         this.securityProvider = securityProvider;
     }
@@ -95,8 +97,7 @@ class UserInitializer implements Workspa
         MemoryNodeStore store = new MemoryNodeStore(base);
 
         Root root = RootFactory.createSystemRoot(store, EmptyHook.INSTANCE, 
workspaceName,
-                securityProvider,
-                new CompositeQueryIndexProvider(new PropertyIndexProvider(), 
new NodeTypeIndexProvider()));
+                securityProvider,  getIndexProvider());
 
         UserConfiguration userConfiguration = 
securityProvider.getConfiguration(UserConfiguration.class);
         UserManager userManager = userConfiguration.getUserManager(root, 
NamePathMapper.DEFAULT);
@@ -153,4 +154,13 @@ class UserInitializer implements Workspa
         NodeState target = store.getRoot();
         target.compareAgainstBaseState(base, new ApplyDiff(builder));
     }
+
+    private QueryIndexProvider getIndexProvider() {
+        return checkNotNull(queryIndexProvider, "QueryIndexProvider yet not 
initialized");
+    }
+
+    @Override
+    public void setQueryIndexProvider(QueryIndexProvider provider) {
+        this.queryIndexProvider = provider;
+    }
 }

Added: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/CustomQueryIndexProviderTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/CustomQueryIndexProviderTest.java?rev=1813511&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/CustomQueryIndexProviderTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/CustomQueryIndexProviderTest.java
 Fri Oct 27 11:46:16 2017
@@ -0,0 +1,138 @@
+/*
+ * 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.oak.security;
+
+import java.util.Collections;
+import java.util.List;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+
+import org.apache.jackrabbit.oak.AbstractSecurityTest;
+import org.apache.jackrabbit.oak.Oak;
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.plugins.index.Cursors;
+import org.apache.jackrabbit.oak.plugins.index.IndexEditorProvider;
+import org.apache.jackrabbit.oak.plugins.index.IndexUpdateCallback;
+import 
org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider;
+import org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexLookup;
+import org.apache.jackrabbit.oak.query.QueryEngineSettings;
+import org.apache.jackrabbit.oak.spi.commit.Editor;
+import org.apache.jackrabbit.oak.spi.lifecycle.RepositoryInitializer;
+import org.apache.jackrabbit.oak.spi.query.Cursor;
+import org.apache.jackrabbit.oak.spi.query.Filter;
+import org.apache.jackrabbit.oak.spi.query.QueryIndex;
+import org.apache.jackrabbit.oak.spi.query.QueryIndexProvider;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * This test validates if Oak initialization works fine with custom 
QueryIndexProvider
+ * and none of the initializers rely on some hard coded index types
+ */
+public class CustomQueryIndexProviderTest extends AbstractSecurityTest {
+
+    public static final String TEST_INDEX = "test-index";
+
+    @Override
+    protected Oak withEditors(Oak oak) {
+        oak.with(new UUIDIndexReplacementInitializer());
+        oak.with(new TestIndexEditor());
+        oak.with(new TestQueryProvider());
+        return oak;
+    }
+
+    @Test
+    public void initWentFine() throws Exception{
+        assertNotNull(root);
+    }
+
+    private class UUIDIndexReplacementInitializer implements 
RepositoryInitializer {
+        @Override
+        public void initialize(@Nonnull NodeBuilder builder) {
+            builder.child("oak:index").child("uuid").setProperty("type", 
TEST_INDEX);
+        }
+    }
+
+    private static class TestIndexEditor implements IndexEditorProvider {
+        @CheckForNull
+        @Override
+        public Editor getIndexEditor(@Nonnull String type, @Nonnull 
NodeBuilder definition,
+                                     @Nonnull NodeState root, @Nonnull 
IndexUpdateCallback callback)
+                throws CommitFailedException {
+            if (TEST_INDEX.equals(type)) {
+                PropertyIndexEditorProvider piep = new 
PropertyIndexEditorProvider();
+                return piep.getIndexEditor(PropertyIndexEditorProvider.TYPE, 
definition, root, callback);
+            }
+            return null;
+        }
+    }
+
+    private static class TestQueryProvider implements QueryIndexProvider {
+        @Nonnull
+        @Override
+        public List<? extends QueryIndex> getQueryIndexes(NodeState nodeState) 
{
+            return Collections.singletonList(new TestQueryIndex());
+        }
+    }
+
+    private static class TestQueryIndex implements QueryIndex {
+
+        @Override
+        public double getMinimumCost() {
+            return 1;
+        }
+
+        @Override
+        public double getCost(Filter filter, NodeState rootState) {
+            if (filter.getPropertyRestriction("jcr:uuid") != null) {
+                return 1;
+            }
+            return Double.MAX_VALUE;
+        }
+
+        @Override
+        public Cursor query(Filter filter, NodeState rootState) {
+            Filter.PropertyRestriction pr = 
filter.getPropertyRestriction("jcr:uuid");
+            if (pr != null) {
+                NodeBuilder nb = rootState.builder();
+                //Fake the index type by reverting to "property" for final 
evaluation
+                nb.child("oak:index").child("uuid").setProperty("type", 
PropertyIndexEditorProvider.TYPE);
+                rootState = nb.getNodeState();
+                PropertyIndexLookup pil = new PropertyIndexLookup(rootState);
+                return Cursors.newPathCursor(pil.query(filter, "jcr:uuid", 
pr.first), new QueryEngineSettings());
+            }
+            return null;
+        }
+
+        @Override
+        public String getPlan(Filter filter, NodeState rootState) {
+            return "Test";
+        }
+
+        @Override
+        public String getIndexName() {
+            return "TestIndex";
+        }
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/CustomQueryIndexProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-query-spi/src/main/java/org/apache/jackrabbit/oak/spi/query/QueryIndexProviderAware.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-query-spi/src/main/java/org/apache/jackrabbit/oak/spi/query/QueryIndexProviderAware.java?rev=1813511&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-query-spi/src/main/java/org/apache/jackrabbit/oak/spi/query/QueryIndexProviderAware.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-query-spi/src/main/java/org/apache/jackrabbit/oak/spi/query/QueryIndexProviderAware.java
 Fri Oct 27 11:46:16 2017
@@ -0,0 +1,28 @@
+/*
+ * 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.oak.spi.query;
+
+/**
+ * Marker interface for services that need a QueryIndexProvider
+ */
+public interface QueryIndexProviderAware {
+
+    void setQueryIndexProvider(QueryIndexProvider provider);
+}

Propchange: 
jackrabbit/oak/trunk/oak-query-spi/src/main/java/org/apache/jackrabbit/oak/spi/query/QueryIndexProviderAware.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to