This is an automated email from the ASF dual-hosted git repository. dklco pushed a commit to branch feature/file-metadata-loaded in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-app-cms.git
The following commit(s) were added to refs/heads/feature/file-metadata-loaded by this push: new 5562827 Cleaning up the service references and instead using OSGi Reference annotations 5562827 is described below commit 55628277ea5f4e044224aaf471c6490f537d0a3c Author: Dan Klco <dk...@apache.org> AuthorDate: Fri Jan 11 23:46:00 2019 -0500 Cleaning up the service references and instead using OSGi Reference annotations --- .../sling/cms/readability/ReadabilityService.java | 9 +++ .../apache/sling/cms/readability/package-info.java | 2 +- .../cms/core/insights/impl/InsightFactoryImpl.java | 80 ++++------------------ .../core/internal/models/CMSJobManagerImpl.java | 33 ++------- .../internal/models/InternalCMSJobManager.java | 35 ++++++++++ .../impl/ReadabilityServiceFactoryImpl.java | 44 +++--------- .../readability/impl/ReadabilityServiceImpl.java | 5 ++ ui/src/main/frontend/package.json | 2 +- 8 files changed, 80 insertions(+), 130 deletions(-) diff --git a/api/src/main/java/org/apache/sling/cms/readability/ReadabilityService.java b/api/src/main/java/org/apache/sling/cms/readability/ReadabilityService.java index 3dfd100..08aa2e3 100644 --- a/api/src/main/java/org/apache/sling/cms/readability/ReadabilityService.java +++ b/api/src/main/java/org/apache/sling/cms/readability/ReadabilityService.java @@ -16,6 +16,8 @@ */ package org.apache.sling.cms.readability; +import java.util.Locale; + /** * Service for calculating the readability of text */ @@ -140,4 +142,11 @@ public interface ReadabilityService { * @return the analyzied text */ Text extractSentences(String text); + + /** + * Gets the locale for this configuration. + * + * @return the locale + */ + Locale getLocale(); } diff --git a/api/src/main/java/org/apache/sling/cms/readability/package-info.java b/api/src/main/java/org/apache/sling/cms/readability/package-info.java index 75c534a..d3f397d 100644 --- a/api/src/main/java/org/apache/sling/cms/readability/package-info.java +++ b/api/src/main/java/org/apache/sling/cms/readability/package-info.java @@ -22,7 +22,7 @@ * * @version 0.10.2 */ -@Version("0.10.2") +@Version("1.0.0") package org.apache.sling.cms.readability; import org.osgi.annotation.versioning.Version; diff --git a/core/src/main/java/org/apache/sling/cms/core/insights/impl/InsightFactoryImpl.java b/core/src/main/java/org/apache/sling/cms/core/insights/impl/InsightFactoryImpl.java index 798b332..b6ac9d8 100644 --- a/core/src/main/java/org/apache/sling/cms/core/insights/impl/InsightFactoryImpl.java +++ b/core/src/main/java/org/apache/sling/cms/core/insights/impl/InsightFactoryImpl.java @@ -18,14 +18,9 @@ */ package org.apache.sling.cms.core.insights.impl; -import java.util.Collection; import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; -import java.util.stream.Stream; import org.apache.sling.cms.File; import org.apache.sling.cms.Page; @@ -34,86 +29,41 @@ import org.apache.sling.cms.insights.InsightFactory; import org.apache.sling.cms.insights.InsightProvider; import org.apache.sling.cms.insights.InsightRequest; import org.apache.sling.engine.SlingRequestProcessor; -import org.osgi.framework.Constants; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceEvent; -import org.osgi.framework.ServiceListener; -import org.osgi.framework.ServiceReference; -import org.osgi.service.component.ComponentContext; -import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.osgi.service.component.annotations.ReferenceCardinality; /** * Implementation of the InsightFactory service interface */ -@Component(immediate = true, service = { InsightFactory.class, ServiceListener.class }) -public class InsightFactoryImpl implements InsightFactory, ServiceListener { +@Component(immediate = true, service = { InsightFactory.class }) +public class InsightFactoryImpl implements InsightFactory { - private static final Logger log = LoggerFactory.getLogger(InsightFactoryImpl.class); - private static final Map<String, InsightProvider> insightProviders = new HashMap<>(); + @Reference(cardinality = ReferenceCardinality.MULTIPLE) + private volatile List<InsightProvider> providers; @Reference private SlingRequestProcessor requestProcessor; - - @Activate - public void activate(ComponentContext context) throws InvalidSyntaxException { - log.info("activate"); - insightProviders.clear(); - String filter = "(" + Constants.OBJECTCLASS + "=" + InsightProvider.class.getName() + ")"; - context.getBundleContext().addServiceListener(this, filter); - @SuppressWarnings("unchecked") - ServiceReference<InsightProvider>[] serviceReferences = (ServiceReference<InsightProvider>[]) context - .getBundleContext().getServiceReferences(InsightProvider.class.getName(), null); - if (serviceReferences != null) { - Stream.of(serviceReferences).map(sr -> context.getBundleContext().getService(sr)) - .forEach(provider -> insightProviders.put(provider.getId(), provider)); - } - log.info("Check Service Listener registered successfully!"); - } - - @Override - public void serviceChanged(ServiceEvent event) { - @SuppressWarnings("unchecked") - ServiceReference<InsightProvider> reference = (ServiceReference<InsightProvider>) event.getServiceReference(); - InsightProvider provider = (InsightProvider) event.getServiceReference().getBundle().getBundleContext() - .getService(reference); - synchronized (this) { - log.info("Unregistering {}", provider.getId()); - insightProviders.remove(provider.getId()); - if (event.getType() == ServiceEvent.MODIFIED || event.getType() == ServiceEvent.REGISTERED) { - log.info("Registering {}", provider.getId()); - insightProviders.put(provider.getId(), provider); - } - } - } - + @Override public List<Insight> getInsights(File file) { return getInsights(new FileInsightRequestImpl(file)); } + private List<Insight> getInsights(InsightRequest request) { + List<Insight> insights = providers.stream().filter(ip -> ip.isEnabled(request)) + .map(ip -> ip.evaluateRequest(request)).collect(Collectors.toList()); + Collections.sort(insights, (o1, o2) -> o1.getProvider().getTitle().compareTo(o2.getProvider().getTitle())); + return insights; + } + @Override public List<Insight> getInsights(Page page) { return getInsights(new PageInsightRequestImpl(page, requestProcessor)); } - public Collection<InsightProvider> getProviders() { - return insightProviders.values(); - } - - private List<Insight> getInsights(InsightRequest request) { - List<Insight> insights = insightProviders.values().stream().filter(ip -> ip.isEnabled(request)) - .map(ip -> ip.evaluateRequest(request)).collect(Collectors.toList()); - Collections.sort(insights, new Comparator<Insight>() { - @Override - public int compare(Insight o1, Insight o2) { - return o1.getProvider().getTitle().compareTo(o2.getProvider().getTitle()); - } - }); - return insights; + public List<InsightProvider> getProviders() { + return providers; } } diff --git a/core/src/main/java/org/apache/sling/cms/core/internal/models/CMSJobManagerImpl.java b/core/src/main/java/org/apache/sling/cms/core/internal/models/CMSJobManagerImpl.java index b707e65..0b36c01 100644 --- a/core/src/main/java/org/apache/sling/cms/core/internal/models/CMSJobManagerImpl.java +++ b/core/src/main/java/org/apache/sling/cms/core/internal/models/CMSJobManagerImpl.java @@ -16,7 +16,6 @@ */ package org.apache.sling.cms.core.internal.models; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; @@ -35,11 +34,6 @@ import org.apache.sling.event.jobs.JobManager.QueryType; import org.apache.sling.event.jobs.consumer.JobConsumer; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.OSGiService; -import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.InvalidSyntaxException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Default implementation of the CMS Job Manager. @@ -47,14 +41,15 @@ import org.slf4j.LoggerFactory; @Model(adaptables = SlingHttpServletRequest.class, adapters = CMSJobManager.class) public class CMSJobManagerImpl implements CMSJobManager { - private static final Logger log = LoggerFactory.getLogger(CMSJobManagerImpl.class); - private static final String PN_INITIATOR = "_initiator"; private static final String PN_JOB_TITLE_KEY = "_titleKey"; private static final String PN_USER_ID = "_userId"; private static final String VALUE_SLING_CMS = "SlingCMS"; @OSGiService + private InternalCMSJobManager cmsJobManager; + + @OSGiService private JobManager jobManager; private SlingHttpServletRequest request; @@ -65,14 +60,7 @@ public class CMSJobManagerImpl implements CMSJobManager { @Override public Collection<ConfigurableJobExecutor> getAvailableJobs() { - BundleContext bundleContext = FrameworkUtil.getBundle(CMSJobManager.class).getBundleContext(); - try { - return bundleContext.getServiceReferences(ConfigurableJobExecutor.class, null).stream() - .map(bundleContext::getService).collect(Collectors.toList()); - } catch (InvalidSyntaxException e) { - log.warn("Failed to get available jobs", e); - return new ArrayList<>(); - } + return cmsJobManager.getJobs(); } @SuppressWarnings("unchecked") @@ -86,17 +74,8 @@ public class CMSJobManagerImpl implements CMSJobManager { } private String getJobTitleKey(String jobTopic) { - BundleContext bundleContext = FrameworkUtil.getBundle(CMSJobManager.class).getBundleContext(); - try { - Optional<String> op = bundleContext - .getServiceReferences(ConfigurableJobExecutor.class, - "(" + JobConsumer.PROPERTY_TOPICS + "=" + jobTopic + ")") - .stream().map(bundleContext::getService).map(ConfigurableJobExecutor::getTitleKey).findFirst(); - return op.orElse(null); - } catch (InvalidSyntaxException e) { - log.warn("Failed to get available jobs", e); - } - return null; + return cmsJobManager.getJobs().stream().filter(j -> jobTopic.equals(j.getTopic())) + .map(ConfigurableJobExecutor::getTitleKey).findFirst().orElse(null); } @Override diff --git a/core/src/main/java/org/apache/sling/cms/core/internal/models/InternalCMSJobManager.java b/core/src/main/java/org/apache/sling/cms/core/internal/models/InternalCMSJobManager.java new file mode 100644 index 0000000..7a6e1fc --- /dev/null +++ b/core/src/main/java/org/apache/sling/cms/core/internal/models/InternalCMSJobManager.java @@ -0,0 +1,35 @@ +/* + * 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.cms.core.internal.models; + +import java.util.List; + +import org.apache.sling.cms.ConfigurableJobExecutor; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.component.annotations.ReferenceCardinality; + +@Component(service = InternalCMSJobManager.class) +public class InternalCMSJobManager { + + @Reference(cardinality = ReferenceCardinality.MULTIPLE) + private volatile List<ConfigurableJobExecutor> jobs; + + public List<ConfigurableJobExecutor> getJobs() { + return jobs; + } +} diff --git a/core/src/main/java/org/apache/sling/cms/core/readability/impl/ReadabilityServiceFactoryImpl.java b/core/src/main/java/org/apache/sling/cms/core/readability/impl/ReadabilityServiceFactoryImpl.java index 80ca370..e5354ab 100644 --- a/core/src/main/java/org/apache/sling/cms/core/readability/impl/ReadabilityServiceFactoryImpl.java +++ b/core/src/main/java/org/apache/sling/cms/core/readability/impl/ReadabilityServiceFactoryImpl.java @@ -16,16 +16,14 @@ */ package org.apache.sling.cms.core.readability.impl; -import java.util.Collection; +import java.util.List; import java.util.Locale; import org.apache.sling.cms.readability.ReadabilityService; import org.apache.sling.cms.readability.ReadabilityServiceFactory; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceReference; -import org.osgi.service.component.ComponentContext; -import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.component.annotations.ReferenceCardinality; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,39 +35,13 @@ public class ReadabilityServiceFactoryImpl implements ReadabilityServiceFactory private static final Logger log = LoggerFactory.getLogger(ReadabilityServiceFactoryImpl.class); - private ComponentContext context; - - @Activate - public void activate(ComponentContext context) { - this.context = context; - } + @Reference(cardinality = ReferenceCardinality.MULTIPLE) + private volatile List<ReadabilityService> services; public ReadabilityService getReadabilityService(Locale locale) { - - try { - log.debug("Locating readability service for {}", locale); - Collection<ServiceReference<ReadabilityService>> references = context.getBundleContext() - .getServiceReferences(ReadabilityService.class, "(locale=" + locale.toString() + ")"); - if (references == null || references.isEmpty()) { - log.debug("Trying language fallback {}", locale.getLanguage()); - references = context.getBundleContext().getServiceReferences(ReadabilityService.class, - "(locale=" + locale.getLanguage() + ")"); - } - if (references != null && !references.isEmpty()) { - for (ServiceReference<ReadabilityService> ref : references) { - ReadabilityService service = context.getBundleContext().getService(ref); - if (service != null) { - log.debug("Found readability service for {}", locale); - return service; - } - } - } else { - log.debug("No readibility services found for {}", locale); - } - } catch (InvalidSyntaxException e) { - log.error("Exception creating expression to find service", e); - } - return null; + log.debug("Locating readability service for {}", locale); + return services.stream().filter(svc -> locale.equals(svc.getLocale())).findFirst().orElse(services.stream() + .filter(svc -> locale.getLanguage().equals(svc.getLocale().getLanguage())).findFirst().orElse(null)); } } diff --git a/core/src/main/java/org/apache/sling/cms/core/readability/impl/ReadabilityServiceImpl.java b/core/src/main/java/org/apache/sling/cms/core/readability/impl/ReadabilityServiceImpl.java index f6833df..1a2dddb 100644 --- a/core/src/main/java/org/apache/sling/cms/core/readability/impl/ReadabilityServiceImpl.java +++ b/core/src/main/java/org/apache/sling/cms/core/readability/impl/ReadabilityServiceImpl.java @@ -200,6 +200,11 @@ public class ReadabilityServiceImpl implements ReadabilityService { return words; } + @Override + public Locale getLocale() { + return locale; + } + private boolean isVowel(char ch) { return vowelExpression.matcher(String.valueOf(ch)).matches(); } diff --git a/ui/src/main/frontend/package.json b/ui/src/main/frontend/package.json index 0d053f3..4fe26a9 100644 --- a/ui/src/main/frontend/package.json +++ b/ui/src/main/frontend/package.json @@ -14,7 +14,7 @@ "jam-icons": "^2.0.0", "js-autocomplete": "^1.0.4", "datatables": "^1.10.18", - "bulma": "^0.7.1", + "bulma": "^0.7.2", "datatables-bulma": "^1.0.1" }, "devDependencies": {