You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by "jsedding (via GitHub)" <gi...@apache.org> on 2023/09/06 10:01:08 UTC
[GitHub] [sling-org-apache-sling-resourceresolver] jsedding opened a new pull request, #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
jsedding opened a new pull request, #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100
(no comment)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] sonarcloud[bot] commented on pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#issuecomment-1717542331
Kudos, SonarCloud Quality Gate passed! [](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY)
[](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT) [](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL) [6 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL)
[](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_coverage&view=list) [81.9% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_coverage&view=list)
[](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_duplicated_lines_density&view=list)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] jsedding merged pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "jsedding (via GitHub)" <gi...@apache.org>.
jsedding merged PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] sonarcloud[bot] commented on pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#issuecomment-1715977003
Kudos, SonarCloud Quality Gate passed! [](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY)
[](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT) [](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL) [6 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL)
[](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_coverage&view=list) [80.0% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_coverage&view=list)
[](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_duplicated_lines_density&view=list)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] jsedding commented on a diff in pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "jsedding (via GitHub)" <gi...@apache.org>.
jsedding commented on code in PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#discussion_r1324434631
##########
src/main/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandler.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.resourceresolver.impl;
+
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.runtime.RuntimeService;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+public class FactoryRegistrationHandler implements AutoCloseable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(FactoryRegistrationHandler.class);
+
+ private final ResourceResolverFactoryActivator activator;
+
+ private final FactoryPreconditions factoryPreconditions;
+
+ private final ExecutorService factoryRegistrationWorker;
+
+ @SuppressWarnings("java:S3077") // The field is only ever set to null or to a new FactoryRegistration instance, which is safe.
+ private volatile FactoryRegistration factoryRegistration;
+
+ public FactoryRegistrationHandler(
+ ResourceResolverFactoryActivator activator, FactoryPreconditions factoryPreconditions) {
+ this.factoryPreconditions = factoryPreconditions;
+ this.activator = activator;
+ this.factoryRegistrationWorker = Executors.newSingleThreadExecutor(
+ r -> new Thread(r, "ResourceResolverFactory registration/deregistration"));
+ }
+
+ @Override
+ public void close() {
+ unregisterFactory();
+ factoryRegistrationWorker.shutdown();
+ try {
+ if (!factoryRegistrationWorker.awaitTermination(5, TimeUnit.SECONDS)) {
+ final List<Runnable> runnables = factoryRegistrationWorker.shutdownNow();
+ if (runnables.size() >= 2) {
Review Comment:
Fixed with https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100/commits/64a00364721b44d9319219ce9ac0e102fa8032fa
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] jsedding commented on a diff in pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "jsedding (via GitHub)" <gi...@apache.org>.
jsedding commented on code in PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#discussion_r1317054545
##########
src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java:
##########
@@ -490,69 +555,26 @@ protected void deactivate() {
private void unregisterFactory() {
FactoryRegistration local = null;
synchronized ( this ) {
Review Comment:
We could probably get rid of the synchronization. `factoryRegistration` should only ever get set via the `factoryRegistrationWorker` thread and is this guaranteed not to be concurrent.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] rombert commented on a diff in pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "rombert (via GitHub)" <gi...@apache.org>.
rombert commented on code in PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#discussion_r1324414773
##########
src/main/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandler.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.resourceresolver.impl;
+
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.runtime.RuntimeService;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+public class FactoryRegistrationHandler implements AutoCloseable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(FactoryRegistrationHandler.class);
+
+ private final ResourceResolverFactoryActivator activator;
+
+ private final FactoryPreconditions factoryPreconditions;
+
+ private final ExecutorService factoryRegistrationWorker;
+
+ @SuppressWarnings("java:S3077") // The field is only ever set to null or to a new FactoryRegistration instance, which is safe.
+ private volatile FactoryRegistration factoryRegistration;
+
+ public FactoryRegistrationHandler(
+ ResourceResolverFactoryActivator activator, FactoryPreconditions factoryPreconditions) {
+ this.factoryPreconditions = factoryPreconditions;
+ this.activator = activator;
+ this.factoryRegistrationWorker = Executors.newSingleThreadExecutor(
+ r -> new Thread(r, "ResourceResolverFactory registration/deregistration"));
+ }
+
+ @Override
+ public void close() {
+ unregisterFactory();
+ factoryRegistrationWorker.shutdown();
+ try {
+ if (!factoryRegistrationWorker.awaitTermination(5, TimeUnit.SECONDS)) {
+ final List<Runnable> runnables = factoryRegistrationWorker.shutdownNow();
+ if (runnables.size() >= 2) {
Review Comment:
This deserves a comment/method, I can't tell why we check for more than two runnables or why we ignore the first two.
##########
src/main/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandler.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.resourceresolver.impl;
+
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.runtime.RuntimeService;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+public class FactoryRegistrationHandler implements AutoCloseable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(FactoryRegistrationHandler.class);
+
+ private final ResourceResolverFactoryActivator activator;
+
+ private final FactoryPreconditions factoryPreconditions;
+
+ private final ExecutorService factoryRegistrationWorker;
+
+ @SuppressWarnings("java:S3077") // The field is only ever set to null or to a new FactoryRegistration instance, which is safe.
+ private volatile FactoryRegistration factoryRegistration;
+
+ public FactoryRegistrationHandler(
+ ResourceResolverFactoryActivator activator, FactoryPreconditions factoryPreconditions) {
+ this.factoryPreconditions = factoryPreconditions;
+ this.activator = activator;
+ this.factoryRegistrationWorker = Executors.newSingleThreadExecutor(
+ r -> new Thread(r, "ResourceResolverFactory registration/deregistration"));
+ }
+
+ @Override
+ public void close() {
+ unregisterFactory();
+ factoryRegistrationWorker.shutdown();
+ try {
+ if (!factoryRegistrationWorker.awaitTermination(5, TimeUnit.SECONDS)) {
+ final List<Runnable> runnables = factoryRegistrationWorker.shutdownNow();
+ if (runnables.size() >= 2) {
+ final Runnable unregisterTask = runnables.get(runnables.size() - 2);
+ unregisterTask.run();
+ }
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ /**
+ * Check the preconditions and if it changed, either register factory or unregister
+ */
+ void maybeRegisterFactory(final String unavailableName, final String unavailableServicePid) {
+ if (!factoryRegistrationWorker.isShutdown()) {
+ LOG.debug("submitting maybeRegisterFactory");
+ factoryRegistrationWorker.submit(() -> {
+ final boolean preconditionsOk = factoryPreconditions.checkPreconditions(unavailableName, unavailableServicePid);
+ if ( preconditionsOk && this.factoryRegistration == null ) {
+ // check system bundle state - if stopping, don't register new factory
+ final Bundle systemBundle = activator.getBundleContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION);
+ if ( systemBundle != null && systemBundle.getState() != Bundle.STOPPING ) {
+ withThreadName("ResourceResolverFactory registration", this::doRegisterFactory);
+ }
+ } else if ( !preconditionsOk && this.factoryRegistration != null ) {
+ LOG.debug("performing unregisterFactory via maybeRegisterFactory");
+ withThreadName("ResourceResolverFactory deregistration", this::doUnregisterFactory);
+ }
+ });
+ }
+ }
+
+ void unregisterFactory() {
+ LOG.debug("submitting unregisterFactory");
+ factoryRegistrationWorker.submit(() -> withThreadName("ResourceResolverFactory deregistration",
+ this::doUnregisterFactory));
+ }
+
+ /**
+ * Register the factory.
+ */
+ private void doRegisterFactory() {
+ LOG.debug("performing registerFactory, factoryRegistration == {}", factoryRegistration);
+ if (this.factoryRegistration == null) {
+ this.factoryRegistration = new FactoryRegistration(activator.getBundleContext(), activator);
+ }
+ LOG.debug("finished performing registerFactory, factoryRegistration == {}", factoryRegistration);
+ }
+
+ /**
+ * Unregister the factory (if registered).
+ */
+ private void doUnregisterFactory() {
+ LOG.debug("performing unregisterFactory, factoryRegistration == {}", factoryRegistration);
+ if (this.factoryRegistration != null) {
+ this.factoryRegistration.unregister();
+ this.factoryRegistration = null;
+ }
+ LOG.debug("finished performing unregisterFactory, factoryRegistration == {}", factoryRegistration);
+ }
+
+ private static void withThreadName(String threadName, Runnable task) {
+ final String name = Thread.currentThread().getName();
Review Comment:
Are you trying to expose what is going on via the thread name? It might be better for observability to keep a stable thread name prefix and reflect the current task in the second part, e.g. `getClass().getSimpleName() + " - " threadName`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] jsedding commented on pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "jsedding (via GitHub)" <gi...@apache.org>.
jsedding commented on PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#issuecomment-1708596490
The build failure is related to Java 17. I will take a look to see if I can get it running.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] jsedding commented on a diff in pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "jsedding (via GitHub)" <gi...@apache.org>.
jsedding commented on code in PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#discussion_r1321431492
##########
pom.xml:
##########
@@ -54,6 +54,7 @@
<plugin>
<groupId>org.apache.sling</groupId>
<artifactId>sling-maven-plugin</artifactId>
+ <version>3.0.2</version>
Review Comment:
Thanks @kwin ! It looks like Jörg already integrated the updates for java 17 into master, so now I merged them in instead of this commit.
##########
pom.xml:
##########
@@ -54,6 +54,7 @@
<plugin>
<groupId>org.apache.sling</groupId>
<artifactId>sling-maven-plugin</artifactId>
+ <version>3.0.2</version>
Review Comment:
Thanks @kwin ! It looks like Jörg already integrated the updates for java 17 into master, so now I merged them in instead of this commit.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] rombert commented on a diff in pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "rombert (via GitHub)" <gi...@apache.org>.
rombert commented on code in PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#discussion_r1324447258
##########
src/main/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandler.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.resourceresolver.impl;
+
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.runtime.RuntimeService;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+public class FactoryRegistrationHandler implements AutoCloseable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(FactoryRegistrationHandler.class);
+
+ private final ResourceResolverFactoryActivator activator;
+
+ private final FactoryPreconditions factoryPreconditions;
+
+ private final ExecutorService factoryRegistrationWorker;
+
+ @SuppressWarnings("java:S3077") // The field is only ever set to null or to a new FactoryRegistration instance, which is safe.
+ private volatile FactoryRegistration factoryRegistration;
+
+ public FactoryRegistrationHandler(
+ ResourceResolverFactoryActivator activator, FactoryPreconditions factoryPreconditions) {
+ this.factoryPreconditions = factoryPreconditions;
+ this.activator = activator;
+ this.factoryRegistrationWorker = Executors.newSingleThreadExecutor(
+ r -> new Thread(r, "ResourceResolverFactory registration/deregistration"));
+ }
+
+ @Override
+ public void close() {
+ unregisterFactory();
+ factoryRegistrationWorker.shutdown();
+ try {
+ if (!factoryRegistrationWorker.awaitTermination(5, TimeUnit.SECONDS)) {
+ final List<Runnable> runnables = factoryRegistrationWorker.shutdownNow();
+ if (runnables.size() >= 2) {
Review Comment:
Thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] rombert commented on a diff in pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "rombert (via GitHub)" <gi...@apache.org>.
rombert commented on code in PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#discussion_r1324433044
##########
src/main/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandler.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.resourceresolver.impl;
+
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.runtime.RuntimeService;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+public class FactoryRegistrationHandler implements AutoCloseable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(FactoryRegistrationHandler.class);
+
+ private final ResourceResolverFactoryActivator activator;
+
+ private final FactoryPreconditions factoryPreconditions;
+
+ private final ExecutorService factoryRegistrationWorker;
+
+ @SuppressWarnings("java:S3077") // The field is only ever set to null or to a new FactoryRegistration instance, which is safe.
+ private volatile FactoryRegistration factoryRegistration;
+
+ public FactoryRegistrationHandler(
+ ResourceResolverFactoryActivator activator, FactoryPreconditions factoryPreconditions) {
+ this.factoryPreconditions = factoryPreconditions;
+ this.activator = activator;
+ this.factoryRegistrationWorker = Executors.newSingleThreadExecutor(
+ r -> new Thread(r, "ResourceResolverFactory registration/deregistration"));
+ }
+
+ @Override
+ public void close() {
+ unregisterFactory();
+ factoryRegistrationWorker.shutdown();
+ try {
+ if (!factoryRegistrationWorker.awaitTermination(5, TimeUnit.SECONDS)) {
+ final List<Runnable> runnables = factoryRegistrationWorker.shutdownNow();
+ if (runnables.size() >= 2) {
+ final Runnable unregisterTask = runnables.get(runnables.size() - 2);
+ unregisterTask.run();
+ }
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ /**
+ * Check the preconditions and if it changed, either register factory or unregister
+ */
+ void maybeRegisterFactory(final String unavailableName, final String unavailableServicePid) {
+ if (!factoryRegistrationWorker.isShutdown()) {
+ LOG.debug("submitting maybeRegisterFactory");
+ factoryRegistrationWorker.submit(() -> {
+ final boolean preconditionsOk = factoryPreconditions.checkPreconditions(unavailableName, unavailableServicePid);
+ if ( preconditionsOk && this.factoryRegistration == null ) {
+ // check system bundle state - if stopping, don't register new factory
+ final Bundle systemBundle = activator.getBundleContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION);
+ if ( systemBundle != null && systemBundle.getState() != Bundle.STOPPING ) {
+ withThreadName("ResourceResolverFactory registration", this::doRegisterFactory);
+ }
+ } else if ( !preconditionsOk && this.factoryRegistration != null ) {
+ LOG.debug("performing unregisterFactory via maybeRegisterFactory");
+ withThreadName("ResourceResolverFactory deregistration", this::doUnregisterFactory);
+ }
+ });
+ }
+ }
+
+ void unregisterFactory() {
+ LOG.debug("submitting unregisterFactory");
+ factoryRegistrationWorker.submit(() -> withThreadName("ResourceResolverFactory deregistration",
+ this::doUnregisterFactory));
+ }
+
+ /**
+ * Register the factory.
+ */
+ private void doRegisterFactory() {
+ LOG.debug("performing registerFactory, factoryRegistration == {}", factoryRegistration);
+ if (this.factoryRegistration == null) {
+ this.factoryRegistration = new FactoryRegistration(activator.getBundleContext(), activator);
+ }
+ LOG.debug("finished performing registerFactory, factoryRegistration == {}", factoryRegistration);
+ }
+
+ /**
+ * Unregister the factory (if registered).
+ */
+ private void doUnregisterFactory() {
+ LOG.debug("performing unregisterFactory, factoryRegistration == {}", factoryRegistration);
+ if (this.factoryRegistration != null) {
+ this.factoryRegistration.unregister();
+ this.factoryRegistration = null;
+ }
+ LOG.debug("finished performing unregisterFactory, factoryRegistration == {}", factoryRegistration);
+ }
+
+ private static void withThreadName(String threadName, Runnable task) {
+ final String name = Thread.currentThread().getName();
Review Comment:
Ah, that works for me. Maybe extract the prefix as a constant value somewhere, just to make it clear?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] sonarcloud[bot] commented on pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#issuecomment-1717534348
Kudos, SonarCloud Quality Gate passed! [](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY)
[](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT) [](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL) [7 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL)
[](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_coverage&view=list) [81.9% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_coverage&view=list)
[](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_duplicated_lines_density&view=list)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] sonarcloud[bot] commented on pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#issuecomment-1713740970
SonarCloud Quality Gate failed. [](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG) [2 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY)
[](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT) [](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL) [6 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL)
[](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_coverage&view=list) [66.9% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_coverage&view=list)
[](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_duplicated_lines_density&view=list)
 Catch issues before they fail your Quality Gate with our IDE extension  [SonarLint](https://www.sonarsource.com/products/sonarlint/features/connected-mode/?referrer=sonarcloud-welcome)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] jsedding commented on a diff in pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "jsedding (via GitHub)" <gi...@apache.org>.
jsedding commented on code in PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#discussion_r1317051206
##########
src/main/java/org/apache/sling/resourceresolver/impl/providers/stateful/AuthenticatedResourceProvider.java:
##########
@@ -52,7 +52,7 @@ public class AuthenticatedResourceProvider {
private static final Logger logger = LoggerFactory.getLogger(ResourceResolverImpl.class);
- public static final AuthenticatedResourceProvider UNAUTHENTICATED_PROVIDER = new AuthenticatedResourceProvider(null, false, null, null);
Review Comment:
My IDE complained and failed the tests because `null` is being passed into `@NotNull` constructor arguments. That's why I fixed this. It is otherwise unrelated.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] jsedding commented on a diff in pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "jsedding (via GitHub)" <gi...@apache.org>.
jsedding commented on code in PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#discussion_r1324438406
##########
src/main/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandler.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.resourceresolver.impl;
+
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.runtime.RuntimeService;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+public class FactoryRegistrationHandler implements AutoCloseable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(FactoryRegistrationHandler.class);
+
+ private final ResourceResolverFactoryActivator activator;
+
+ private final FactoryPreconditions factoryPreconditions;
+
+ private final ExecutorService factoryRegistrationWorker;
+
+ @SuppressWarnings("java:S3077") // The field is only ever set to null or to a new FactoryRegistration instance, which is safe.
+ private volatile FactoryRegistration factoryRegistration;
+
+ public FactoryRegistrationHandler(
+ ResourceResolverFactoryActivator activator, FactoryPreconditions factoryPreconditions) {
+ this.factoryPreconditions = factoryPreconditions;
+ this.activator = activator;
+ this.factoryRegistrationWorker = Executors.newSingleThreadExecutor(
+ r -> new Thread(r, "ResourceResolverFactory registration/deregistration"));
+ }
+
+ @Override
+ public void close() {
+ unregisterFactory();
+ factoryRegistrationWorker.shutdown();
+ try {
+ if (!factoryRegistrationWorker.awaitTermination(5, TimeUnit.SECONDS)) {
+ final List<Runnable> runnables = factoryRegistrationWorker.shutdownNow();
+ if (runnables.size() >= 2) {
+ final Runnable unregisterTask = runnables.get(runnables.size() - 2);
+ unregisterTask.run();
+ }
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ /**
+ * Check the preconditions and if it changed, either register factory or unregister
+ */
+ void maybeRegisterFactory(final String unavailableName, final String unavailableServicePid) {
+ if (!factoryRegistrationWorker.isShutdown()) {
+ LOG.debug("submitting maybeRegisterFactory");
+ factoryRegistrationWorker.submit(() -> {
+ final boolean preconditionsOk = factoryPreconditions.checkPreconditions(unavailableName, unavailableServicePid);
+ if ( preconditionsOk && this.factoryRegistration == null ) {
+ // check system bundle state - if stopping, don't register new factory
+ final Bundle systemBundle = activator.getBundleContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION);
+ if ( systemBundle != null && systemBundle.getState() != Bundle.STOPPING ) {
+ withThreadName("ResourceResolverFactory registration", this::doRegisterFactory);
+ }
+ } else if ( !preconditionsOk && this.factoryRegistration != null ) {
+ LOG.debug("performing unregisterFactory via maybeRegisterFactory");
+ withThreadName("ResourceResolverFactory deregistration", this::doUnregisterFactory);
+ }
+ });
+ }
+ }
+
+ void unregisterFactory() {
+ LOG.debug("submitting unregisterFactory");
+ factoryRegistrationWorker.submit(() -> withThreadName("ResourceResolverFactory deregistration",
+ this::doUnregisterFactory));
+ }
+
+ /**
+ * Register the factory.
+ */
+ private void doRegisterFactory() {
+ LOG.debug("performing registerFactory, factoryRegistration == {}", factoryRegistration);
+ if (this.factoryRegistration == null) {
+ this.factoryRegistration = new FactoryRegistration(activator.getBundleContext(), activator);
+ }
+ LOG.debug("finished performing registerFactory, factoryRegistration == {}", factoryRegistration);
+ }
+
+ /**
+ * Unregister the factory (if registered).
+ */
+ private void doUnregisterFactory() {
+ LOG.debug("performing unregisterFactory, factoryRegistration == {}", factoryRegistration);
+ if (this.factoryRegistration != null) {
+ this.factoryRegistration.unregister();
+ this.factoryRegistration = null;
+ }
+ LOG.debug("finished performing unregisterFactory, factoryRegistration == {}", factoryRegistration);
+ }
+
+ private static void withThreadName(String threadName, Runnable task) {
+ final String name = Thread.currentThread().getName();
Review Comment:
Fixed in https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100/commits/b28d4d0b208ea29579566f0b9bd823a1a3933af3
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] sonarcloud[bot] commented on pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#issuecomment-1716007770
Kudos, SonarCloud Quality Gate passed! [](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY)
[](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT) [](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL) [6 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL)
[](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_coverage&view=list) [80.0% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_coverage&view=list)
[](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_duplicated_lines_density&view=list)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] jsedding commented on a diff in pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "jsedding (via GitHub)" <gi...@apache.org>.
jsedding commented on code in PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#discussion_r1324431204
##########
src/main/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandler.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.resourceresolver.impl;
+
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.runtime.RuntimeService;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+public class FactoryRegistrationHandler implements AutoCloseable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(FactoryRegistrationHandler.class);
+
+ private final ResourceResolverFactoryActivator activator;
+
+ private final FactoryPreconditions factoryPreconditions;
+
+ private final ExecutorService factoryRegistrationWorker;
+
+ @SuppressWarnings("java:S3077") // The field is only ever set to null or to a new FactoryRegistration instance, which is safe.
+ private volatile FactoryRegistration factoryRegistration;
+
+ public FactoryRegistrationHandler(
+ ResourceResolverFactoryActivator activator, FactoryPreconditions factoryPreconditions) {
+ this.factoryPreconditions = factoryPreconditions;
+ this.activator = activator;
+ this.factoryRegistrationWorker = Executors.newSingleThreadExecutor(
+ r -> new Thread(r, "ResourceResolverFactory registration/deregistration"));
+ }
+
+ @Override
+ public void close() {
+ unregisterFactory();
+ factoryRegistrationWorker.shutdown();
+ try {
+ if (!factoryRegistrationWorker.awaitTermination(5, TimeUnit.SECONDS)) {
+ final List<Runnable> runnables = factoryRegistrationWorker.shutdownNow();
+ if (runnables.size() >= 2) {
Review Comment:
I think you found an artifact from a previous iteration of the patch. The intention was to make sure that the RRF is unregistered before we exit the `close()` method. I'll fix the code accordingly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] jsedding commented on pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "jsedding (via GitHub)" <gi...@apache.org>.
jsedding commented on PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#issuecomment-1708042813
@cziegeler here's a proposal for a fix. I would appreciate your thoughts.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] jsedding commented on a diff in pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "jsedding (via GitHub)" <gi...@apache.org>.
jsedding commented on code in PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#discussion_r1317054089
##########
src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java:
##########
@@ -490,69 +555,26 @@ protected void deactivate() {
private void unregisterFactory() {
FactoryRegistration local = null;
synchronized ( this ) {
- if ( this.factoryRegistration != null ) {
+ if (this.factoryRegistration != null) {
local = this.factoryRegistration;
this.factoryRegistration = null;
}
}
- this.unregisterFactory(local);
- }
-
- /**
- * Unregister the provided factory
- */
- private void unregisterFactory(final FactoryRegistration local) {
- if ( local != null ) {
- if ( local.factoryRegistration != null ) {
- local.factoryRegistration.unregister();
- }
- if ( local.runtimeRegistration != null ) {
- local.runtimeRegistration.unregister();
- }
- if ( local.commonFactory != null ) {
- local.commonFactory.deactivate();
- }
+ if (local != null) {
+ local.unregister();
}
}
/**
* Try to register the factory.
*/
private void registerFactory(final BundleContext localContext) {
- final FactoryRegistration local = new FactoryRegistration();
-
- if ( localContext != null ) {
- // activate and register factory
- final Dictionary<String, Object> serviceProps = new Hashtable<>();
- serviceProps.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
- serviceProps.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Resource Resolver Factory");
-
- local.commonFactory = new CommonResourceResolverFactoryImpl(this);
- local.commonFactory.activate(localContext);
- local.factoryRegistration = localContext.registerService(
- ResourceResolverFactory.class, new ServiceFactory<ResourceResolverFactory>() {
-
- @Override
- public ResourceResolverFactory getService(final Bundle bundle, final ServiceRegistration<ResourceResolverFactory> registration) {
- if ( ResourceResolverFactoryActivator.this.bundleContext == null ) {
- return null;
- }
- final ResourceResolverFactoryImpl r = new ResourceResolverFactoryImpl(
- local.commonFactory, bundle,
- ResourceResolverFactoryActivator.this.getServiceUserMapper());
- return r;
- }
-
- @Override
- public void ungetService(final Bundle bundle, final ServiceRegistration<ResourceResolverFactory> registration, final ResourceResolverFactory service) {
- // nothing to do
- }
- }, serviceProps);
-
- local.runtimeRegistration = localContext.registerService(RuntimeService.class,
- this.getRuntimeService(), null);
-
- this.factoryRegistration = local;
+ if (!this.isDeactivating) {
+ synchronized (this) {
Review Comment:
We could probably get rid of the synchronization. `factoryRegistration` should only ever get set via the `factoryRegistrationWorker` thread and is this guaranteed not to be concurrent.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] jsedding commented on a diff in pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "jsedding (via GitHub)" <gi...@apache.org>.
jsedding commented on code in PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#discussion_r1324428679
##########
src/main/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandler.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.resourceresolver.impl;
+
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.runtime.RuntimeService;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+public class FactoryRegistrationHandler implements AutoCloseable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(FactoryRegistrationHandler.class);
+
+ private final ResourceResolverFactoryActivator activator;
+
+ private final FactoryPreconditions factoryPreconditions;
+
+ private final ExecutorService factoryRegistrationWorker;
+
+ @SuppressWarnings("java:S3077") // The field is only ever set to null or to a new FactoryRegistration instance, which is safe.
+ private volatile FactoryRegistration factoryRegistration;
+
+ public FactoryRegistrationHandler(
+ ResourceResolverFactoryActivator activator, FactoryPreconditions factoryPreconditions) {
+ this.factoryPreconditions = factoryPreconditions;
+ this.activator = activator;
+ this.factoryRegistrationWorker = Executors.newSingleThreadExecutor(
+ r -> new Thread(r, "ResourceResolverFactory registration/deregistration"));
+ }
+
+ @Override
+ public void close() {
+ unregisterFactory();
+ factoryRegistrationWorker.shutdown();
+ try {
+ if (!factoryRegistrationWorker.awaitTermination(5, TimeUnit.SECONDS)) {
+ final List<Runnable> runnables = factoryRegistrationWorker.shutdownNow();
+ if (runnables.size() >= 2) {
+ final Runnable unregisterTask = runnables.get(runnables.size() - 2);
+ unregisterTask.run();
+ }
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ /**
+ * Check the preconditions and if it changed, either register factory or unregister
+ */
+ void maybeRegisterFactory(final String unavailableName, final String unavailableServicePid) {
+ if (!factoryRegistrationWorker.isShutdown()) {
+ LOG.debug("submitting maybeRegisterFactory");
+ factoryRegistrationWorker.submit(() -> {
+ final boolean preconditionsOk = factoryPreconditions.checkPreconditions(unavailableName, unavailableServicePid);
+ if ( preconditionsOk && this.factoryRegistration == null ) {
+ // check system bundle state - if stopping, don't register new factory
+ final Bundle systemBundle = activator.getBundleContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION);
+ if ( systemBundle != null && systemBundle.getState() != Bundle.STOPPING ) {
+ withThreadName("ResourceResolverFactory registration", this::doRegisterFactory);
+ }
+ } else if ( !preconditionsOk && this.factoryRegistration != null ) {
+ LOG.debug("performing unregisterFactory via maybeRegisterFactory");
+ withThreadName("ResourceResolverFactory deregistration", this::doUnregisterFactory);
+ }
+ });
+ }
+ }
+
+ void unregisterFactory() {
+ LOG.debug("submitting unregisterFactory");
+ factoryRegistrationWorker.submit(() -> withThreadName("ResourceResolverFactory deregistration",
+ this::doUnregisterFactory));
+ }
+
+ /**
+ * Register the factory.
+ */
+ private void doRegisterFactory() {
+ LOG.debug("performing registerFactory, factoryRegistration == {}", factoryRegistration);
+ if (this.factoryRegistration == null) {
+ this.factoryRegistration = new FactoryRegistration(activator.getBundleContext(), activator);
+ }
+ LOG.debug("finished performing registerFactory, factoryRegistration == {}", factoryRegistration);
+ }
+
+ /**
+ * Unregister the factory (if registered).
+ */
+ private void doUnregisterFactory() {
+ LOG.debug("performing unregisterFactory, factoryRegistration == {}", factoryRegistration);
+ if (this.factoryRegistration != null) {
+ this.factoryRegistration.unregister();
+ this.factoryRegistration = null;
+ }
+ LOG.debug("finished performing unregisterFactory, factoryRegistration == {}", factoryRegistration);
+ }
+
+ private static void withThreadName(String threadName, Runnable task) {
+ final String name = Thread.currentThread().getName();
Review Comment:
I found it helpful to have the registration/deregistration reflected in the thread-name, because service registrations trigger further service registrations etc. So it helps understand, which changes in the system happened due to the registration/deregistration.
Regarding the naming of the thread, yes the class-name as a prefix would work. De-facto, I am using the prefix "ResourceResolverFactory". I can incorporate that into the method, if you think it would be valuable.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] kwin commented on a diff in pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "kwin (via GitHub)" <gi...@apache.org>.
kwin commented on code in PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#discussion_r1321301818
##########
pom.xml:
##########
@@ -54,6 +54,7 @@
<plugin>
<groupId>org.apache.sling</groupId>
<artifactId>sling-maven-plugin</artifactId>
+ <version>3.0.2</version>
Review Comment:
this is part of latest parent, rather update parent version.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] sonarcloud[bot] commented on pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#issuecomment-1717471488
Kudos, SonarCloud Quality Gate passed! [](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY)
[](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT) [](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL) [6 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL)
[](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_coverage&view=list) [80.0% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_coverage&view=list)
[](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_duplicated_lines_density&view=list)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] sonarcloud[bot] commented on pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#issuecomment-1717676464
Kudos, SonarCloud Quality Gate passed! [](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=BUG)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=VULNERABILITY)
[](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT) [](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=SECURITY_HOTSPOT)
[](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL) [](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL) [6 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&resolved=false&types=CODE_SMELL)
[](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_coverage&view=list) [81.9% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_coverage&view=list)
[](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-resourceresolver&pullRequest=100&metric=new_duplicated_lines_density&view=list)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] jsedding commented on a diff in pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "jsedding (via GitHub)" <gi...@apache.org>.
jsedding commented on code in PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#discussion_r1324435077
##########
src/main/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandler.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.resourceresolver.impl;
+
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.runtime.RuntimeService;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+public class FactoryRegistrationHandler implements AutoCloseable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(FactoryRegistrationHandler.class);
+
+ private final ResourceResolverFactoryActivator activator;
+
+ private final FactoryPreconditions factoryPreconditions;
+
+ private final ExecutorService factoryRegistrationWorker;
+
+ @SuppressWarnings("java:S3077") // The field is only ever set to null or to a new FactoryRegistration instance, which is safe.
+ private volatile FactoryRegistration factoryRegistration;
+
+ public FactoryRegistrationHandler(
+ ResourceResolverFactoryActivator activator, FactoryPreconditions factoryPreconditions) {
+ this.factoryPreconditions = factoryPreconditions;
+ this.activator = activator;
+ this.factoryRegistrationWorker = Executors.newSingleThreadExecutor(
+ r -> new Thread(r, "ResourceResolverFactory registration/deregistration"));
+ }
+
+ @Override
+ public void close() {
+ unregisterFactory();
+ factoryRegistrationWorker.shutdown();
+ try {
+ if (!factoryRegistrationWorker.awaitTermination(5, TimeUnit.SECONDS)) {
+ final List<Runnable> runnables = factoryRegistrationWorker.shutdownNow();
+ if (runnables.size() >= 2) {
+ final Runnable unregisterTask = runnables.get(runnables.size() - 2);
+ unregisterTask.run();
+ }
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ /**
+ * Check the preconditions and if it changed, either register factory or unregister
+ */
+ void maybeRegisterFactory(final String unavailableName, final String unavailableServicePid) {
+ if (!factoryRegistrationWorker.isShutdown()) {
+ LOG.debug("submitting maybeRegisterFactory");
+ factoryRegistrationWorker.submit(() -> {
+ final boolean preconditionsOk = factoryPreconditions.checkPreconditions(unavailableName, unavailableServicePid);
+ if ( preconditionsOk && this.factoryRegistration == null ) {
+ // check system bundle state - if stopping, don't register new factory
+ final Bundle systemBundle = activator.getBundleContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION);
+ if ( systemBundle != null && systemBundle.getState() != Bundle.STOPPING ) {
+ withThreadName("ResourceResolverFactory registration", this::doRegisterFactory);
+ }
+ } else if ( !preconditionsOk && this.factoryRegistration != null ) {
+ LOG.debug("performing unregisterFactory via maybeRegisterFactory");
+ withThreadName("ResourceResolverFactory deregistration", this::doUnregisterFactory);
+ }
+ });
+ }
+ }
+
+ void unregisterFactory() {
+ LOG.debug("submitting unregisterFactory");
+ factoryRegistrationWorker.submit(() -> withThreadName("ResourceResolverFactory deregistration",
+ this::doUnregisterFactory));
+ }
+
+ /**
+ * Register the factory.
+ */
+ private void doRegisterFactory() {
+ LOG.debug("performing registerFactory, factoryRegistration == {}", factoryRegistration);
+ if (this.factoryRegistration == null) {
+ this.factoryRegistration = new FactoryRegistration(activator.getBundleContext(), activator);
+ }
+ LOG.debug("finished performing registerFactory, factoryRegistration == {}", factoryRegistration);
+ }
+
+ /**
+ * Unregister the factory (if registered).
+ */
+ private void doUnregisterFactory() {
+ LOG.debug("performing unregisterFactory, factoryRegistration == {}", factoryRegistration);
+ if (this.factoryRegistration != null) {
+ this.factoryRegistration.unregister();
+ this.factoryRegistration = null;
+ }
+ LOG.debug("finished performing unregisterFactory, factoryRegistration == {}", factoryRegistration);
+ }
+
+ private static void withThreadName(String threadName, Runnable task) {
+ final String name = Thread.currentThread().getName();
Review Comment:
Sure 👍
##########
src/main/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandler.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.resourceresolver.impl;
+
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.runtime.RuntimeService;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+public class FactoryRegistrationHandler implements AutoCloseable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(FactoryRegistrationHandler.class);
+
+ private final ResourceResolverFactoryActivator activator;
+
+ private final FactoryPreconditions factoryPreconditions;
+
+ private final ExecutorService factoryRegistrationWorker;
+
+ @SuppressWarnings("java:S3077") // The field is only ever set to null or to a new FactoryRegistration instance, which is safe.
+ private volatile FactoryRegistration factoryRegistration;
+
+ public FactoryRegistrationHandler(
+ ResourceResolverFactoryActivator activator, FactoryPreconditions factoryPreconditions) {
+ this.factoryPreconditions = factoryPreconditions;
+ this.activator = activator;
+ this.factoryRegistrationWorker = Executors.newSingleThreadExecutor(
+ r -> new Thread(r, "ResourceResolverFactory registration/deregistration"));
+ }
+
+ @Override
+ public void close() {
+ unregisterFactory();
+ factoryRegistrationWorker.shutdown();
+ try {
+ if (!factoryRegistrationWorker.awaitTermination(5, TimeUnit.SECONDS)) {
+ final List<Runnable> runnables = factoryRegistrationWorker.shutdownNow();
+ if (runnables.size() >= 2) {
+ final Runnable unregisterTask = runnables.get(runnables.size() - 2);
+ unregisterTask.run();
+ }
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ /**
+ * Check the preconditions and if it changed, either register factory or unregister
+ */
+ void maybeRegisterFactory(final String unavailableName, final String unavailableServicePid) {
+ if (!factoryRegistrationWorker.isShutdown()) {
+ LOG.debug("submitting maybeRegisterFactory");
+ factoryRegistrationWorker.submit(() -> {
+ final boolean preconditionsOk = factoryPreconditions.checkPreconditions(unavailableName, unavailableServicePid);
+ if ( preconditionsOk && this.factoryRegistration == null ) {
+ // check system bundle state - if stopping, don't register new factory
+ final Bundle systemBundle = activator.getBundleContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION);
+ if ( systemBundle != null && systemBundle.getState() != Bundle.STOPPING ) {
+ withThreadName("ResourceResolverFactory registration", this::doRegisterFactory);
+ }
+ } else if ( !preconditionsOk && this.factoryRegistration != null ) {
+ LOG.debug("performing unregisterFactory via maybeRegisterFactory");
+ withThreadName("ResourceResolverFactory deregistration", this::doUnregisterFactory);
+ }
+ });
+ }
+ }
+
+ void unregisterFactory() {
+ LOG.debug("submitting unregisterFactory");
+ factoryRegistrationWorker.submit(() -> withThreadName("ResourceResolverFactory deregistration",
+ this::doUnregisterFactory));
+ }
+
+ /**
+ * Register the factory.
+ */
+ private void doRegisterFactory() {
+ LOG.debug("performing registerFactory, factoryRegistration == {}", factoryRegistration);
+ if (this.factoryRegistration == null) {
+ this.factoryRegistration = new FactoryRegistration(activator.getBundleContext(), activator);
+ }
+ LOG.debug("finished performing registerFactory, factoryRegistration == {}", factoryRegistration);
+ }
+
+ /**
+ * Unregister the factory (if registered).
+ */
+ private void doUnregisterFactory() {
+ LOG.debug("performing unregisterFactory, factoryRegistration == {}", factoryRegistration);
+ if (this.factoryRegistration != null) {
+ this.factoryRegistration.unregister();
+ this.factoryRegistration = null;
+ }
+ LOG.debug("finished performing unregisterFactory, factoryRegistration == {}", factoryRegistration);
+ }
+
+ private static void withThreadName(String threadName, Runnable task) {
+ final String name = Thread.currentThread().getName();
Review Comment:
Sure 👍
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-resourceresolver] jsedding commented on a diff in pull request #100: SLING-12019 - Avoid duplicate ResourceResolverFactory registrations
Posted by "jsedding (via GitHub)" <gi...@apache.org>.
jsedding commented on code in PR #100:
URL: https://github.com/apache/sling-org-apache-sling-resourceresolver/pull/100#discussion_r1317051206
##########
src/main/java/org/apache/sling/resourceresolver/impl/providers/stateful/AuthenticatedResourceProvider.java:
##########
@@ -52,7 +52,7 @@ public class AuthenticatedResourceProvider {
private static final Logger logger = LoggerFactory.getLogger(ResourceResolverImpl.class);
- public static final AuthenticatedResourceProvider UNAUTHENTICATED_PROVIDER = new AuthenticatedResourceProvider(null, false, null, null);
Review Comment:
My IDE complained and failed the tests because `null` is being passed into `@NotNull` constructor arguments. That's why I fixed this.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org