Skip to content

Commit

Permalink
SLING-9719 Utilize any available oak EditorProvider components that were
Browse files Browse the repository at this point in the history
declared
  • Loading branch information
enapps-enorman committed Sep 9, 2020
1 parent 2f5f359 commit 77c571f
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 42 deletions.
Expand Up @@ -18,27 +18,27 @@
*/
package org.apache.sling.jcr.oak.server.internal;

import static com.google.common.collect.ImmutableSet.of;
import static java.util.Collections.singleton;
import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_DEFINITIONS_NAME;
import static org.apache.jackrabbit.oak.plugins.index.IndexUtils.createIndexDefinition;

import java.util.Collections;
import java.util.Dictionary;

import javax.jcr.Repository;

import org.apache.jackrabbit.JcrConstants;
import org.apache.jackrabbit.api.JackrabbitRepository;
import org.apache.jackrabbit.oak.InitialContent;
import org.apache.jackrabbit.oak.Oak;
import org.apache.jackrabbit.oak.jcr.Jcr;
import org.apache.jackrabbit.oak.osgi.OsgiWhiteboard;
import org.apache.jackrabbit.oak.plugins.commit.ConflictValidatorProvider;
import org.apache.jackrabbit.oak.plugins.commit.JcrConflictHandler;
import org.apache.jackrabbit.oak.plugins.index.WhiteboardIndexEditorProvider;
import org.apache.jackrabbit.oak.plugins.index.aggregate.SimpleNodeAggregator;
import org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper;
import org.apache.jackrabbit.oak.plugins.name.NameValidatorProvider;
import org.apache.jackrabbit.oak.plugins.name.NamespaceEditorProvider;
import org.apache.jackrabbit.oak.plugins.nodetype.TypeEditorProvider;
import org.apache.jackrabbit.oak.plugins.observation.CommitRateLimiter;
import org.apache.jackrabbit.oak.plugins.version.VersionHook;
import org.apache.jackrabbit.oak.spi.commit.WhiteboardEditorProvider;
import org.apache.jackrabbit.oak.spi.lifecycle.RepositoryInitializer;
import org.apache.jackrabbit.oak.spi.query.QueryIndex.NodeAggregator;
import org.apache.jackrabbit.oak.spi.query.WhiteboardIndexProvider;
Expand All @@ -47,6 +47,7 @@
import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
import org.apache.jackrabbit.oak.spi.state.NodeStore;
import org.apache.jackrabbit.oak.spi.whiteboard.Tracker;
import org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard;
import org.apache.sling.jcr.base.AbstractSlingRepository2;
import org.apache.sling.jcr.base.AbstractSlingRepositoryManager;
Expand All @@ -64,11 +65,6 @@
import org.osgi.service.component.annotations.ReferencePolicyOption;
import org.osgi.service.metatype.annotations.Designate;

import static com.google.common.collect.ImmutableSet.of;
import static java.util.Collections.singleton;
import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_DEFINITIONS_NAME;
import static org.apache.jackrabbit.oak.plugins.index.IndexUtils.createIndexDefinition;

/**
* A Sling repository implementation that wraps the Oak repository
* implementation from the Jackrabbit Oak project.
Expand All @@ -92,10 +88,14 @@ public class OakSlingRepositoryManager extends AbstractSlingRepositoryManager {

private ComponentContext componentContext;

private final WhiteboardEditorProvider editorProvider = new WhiteboardEditorProvider();

private final WhiteboardIndexProvider indexProvider = new WhiteboardIndexProvider();

private final WhiteboardIndexEditorProvider indexEditorProvider = new WhiteboardIndexEditorProvider();

private Tracker<RepositoryInitializer> initializers;

private CommitRateLimiter commitRateLimiter;

private OakSlingRepositoryManagerConfiguration configuration;
Expand All @@ -106,7 +106,7 @@ public class OakSlingRepositoryManager extends AbstractSlingRepositoryManager {
)
private SecurityProvider securityProvider;

private ServiceRegistration nodeAggregatorRegistration;
private ServiceRegistration<NodeAggregator> nodeAggregatorRegistration;

@Override
protected ServiceUserMapper getServiceUserMapper() {
Expand All @@ -117,34 +117,31 @@ protected ServiceUserMapper getServiceUserMapper() {
protected Repository acquireRepository() {
final BundleContext bundleContext = componentContext.getBundleContext();
final Whiteboard whiteboard = new OsgiWhiteboard(bundleContext);
this.initializers = whiteboard.track(RepositoryInitializer.class);
this.editorProvider.start(whiteboard);
this.indexProvider.start(whiteboard);
this.indexEditorProvider.start(whiteboard);

final Oak oak = new Oak(nodeStore)
.withAsyncIndexing("async", 5);

final Jcr jcr = new Jcr(oak, false)
.with(new InitialContent())
final Jcr jcr = new Jcr(oak)
.with(new ExtraSlingContent())

.with(JcrConflictHandler.createJcrConflictHandler())
.with(new VersionHook())

.with(whiteboard)
.with(securityProvider)

.with(new NameValidatorProvider())
.with(new NamespaceEditorProvider())
.with(new TypeEditorProvider())
.with(new ConflictValidatorProvider())

.with(editorProvider)
// index stuff
.with(indexProvider)
.with(indexEditorProvider)
.with(getDefaultWorkspace())
.with(whiteboard)
.withFastQueryResultSize(true)
.withFastQueryResultSize(configuration.oak_query_fastResultSize())
.withObservationQueueLength(configuration.oak_observation_queue_length());


for (RepositoryInitializer initializer : initializers.getServices()){
jcr.with(initializer);
}

if (commitRateLimiter != null) {
jcr.with(commitRateLimiter);
}
Expand Down Expand Up @@ -172,8 +169,10 @@ protected void destroy(AbstractSlingRepository2 repositoryServiceInstance) {

@Override
protected void disposeRepository(Repository repository) {
this.initializers.stop();
this.indexProvider.stop();
this.indexEditorProvider.stop();
this.editorProvider.stop();
((JackrabbitRepository) repository).shutdown();
}

Expand All @@ -189,7 +188,7 @@ private void activate(final OakSlingRepositoryManagerConfiguration configuration
if (configuration.oak_observation_limitCommitRate()) {
commitRateLimiter = new CommitRateLimiter();
}
this.nodeAggregatorRegistration = bundleContext.registerService(NodeAggregator.class.getName(), getNodeAggregator(), null);
this.nodeAggregatorRegistration = bundleContext.registerService(NodeAggregator.class, getNodeAggregator(), null);

super.start(bundleContext, new Config(defaultWorkspace, disableLoginAdministrative));
}
Expand Down
Expand Up @@ -66,4 +66,11 @@
)
boolean oak_observation_limitCommitRate() default false;


@AttributeDefinition(
name = "Fast query result size",
description = "Whether the query result size should return an estimation (or -1 if disabled) for large queries"
)
boolean oak_query_fastResultSize() default true;

}
Expand Up @@ -42,6 +42,7 @@
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerClass;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -218,7 +219,7 @@ public void testMultiValueInputStream() throws RepositoryException {
@Test
public void testOsgiResourceEvents() throws RepositoryException {
final ResourceEventListener listener = new ResourceEventListener();
final ServiceRegistration reg = listener.register(bundleContext, SlingConstants.TOPIC_RESOURCE_ADDED);
final ServiceRegistration<EventHandler> reg = listener.register(bundleContext, SlingConstants.TOPIC_RESOURCE_ADDED);
final Session s = repository.loginAdministrative(null);
final int nPaths = 2500 * TEST_SCALE;
final int timeoutMsec = 2 * nPaths;
Expand Down
Expand Up @@ -37,10 +37,10 @@ public class ResourceEventListener implements EventHandler {
private final Logger log = LoggerFactory.getLogger(getClass());
private final Set<String> paths = new HashSet<String>();

ServiceRegistration register(BundleContext ctx, String osgiEventTopic) {
ServiceRegistration<EventHandler> register(BundleContext ctx, String osgiEventTopic) {
final Hashtable<String, Object> props = new Hashtable<String, Object>();
props.put(EventConstants.EVENT_TOPIC, osgiEventTopic);
return ctx.registerService(EventHandler.class.getName(), this, props);
return ctx.registerService(EventHandler.class, this, props);
}

@Override
Expand Down
Expand Up @@ -18,8 +18,15 @@
*/
package org.apache.sling.jcr.oak.server.it;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;

import javax.jcr.Node;

import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils;
import org.apache.sling.api.resource.Resource;
Expand All @@ -28,22 +35,11 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.junit.PaxExam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.Node;
import java.util.HashMap;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;


@RunWith(PaxExam.class)
public class ResourceTypeResolutionIT extends OakServerTestSupport {

private static final Logger LOG = LoggerFactory.getLogger(ResourceTypeResolutionIT.class);

@Test
public void checkResourceType() throws Exception {
JackrabbitSession adminSession = (JackrabbitSession)slingRepository.loginAdministrative(null);
Expand Down
83 changes: 83 additions & 0 deletions src/test/java/org/apache/sling/jcr/oak/server/it/Sling9719IT.java
@@ -0,0 +1,83 @@
/*
* 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.sling.jcr.oak.server.it;

import static org.junit.Assert.assertEquals;

import javax.jcr.Node;

import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.commons.JcrUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.junit.PaxExam;


@RunWith(PaxExam.class)
public class Sling9719IT extends OakServerTestSupport {

/**
* SLING-9719 - test that the dynamic Atomic Counter EditorProvider was discovered and is
* active and functioning
*/
@Test
public void checkAtomicCounter() throws Exception {
JackrabbitSession adminSession = (JackrabbitSession)slingRepository.loginAdministrative(null);

Node counter = JcrUtils.getOrCreateByPath("/content/sling9719/counter", "nt:unstructured", adminSession);
if (!counter.isNodeType("mix:atomicCounter")) {
counter.addMixin("mix:atomicCounter");
}
if (!counter.isNew()) {
// reset the property back to the default value
counter.setProperty("oak:counter", 0L);
}

adminSession.save();

// counter initial value is 0. the default value
assertEquals(0, counter.getProperty("oak:counter").getLong());

// incrementing by 5 the counter
counter.setProperty("oak:increment", 5);
adminSession.save();

// counter value is now 5
new Retry(5000) {
@Override
protected void exec() throws Exception {
assertEquals(5, counter.getProperty("oak:counter").getLong());
}
};

// decreasing by 1
counter.setProperty("oak:increment", -1);
adminSession.save();

// counter value is now 4
new Retry(5000) {
@Override
protected void exec() throws Exception {
assertEquals(4, counter.getProperty("oak:counter").getLong());
}
};

adminSession.logout();
}
}

0 comments on commit 77c571f

Please sign in to comment.