Search in sources :

Example 1 with Reference

use of org.osgi.service.component.annotations.Reference in project sling by apache.

the class LegacyResourceProviderWhiteboard method bindResourceProviderFactory.

@Reference(service = ResourceProviderFactory.class, cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
protected void bindResourceProviderFactory(final ServiceReference<ResourceProviderFactory> ref) {
    final BundleContext bundleContext = ref.getBundle().getBundleContext();
    final ResourceProviderFactory factory = bundleContext.getService(ref);
    if (factory != null) {
        final String[] propertyNames = ref.getPropertyKeys();
        final boolean ownsRoot = toBoolean(ref.getProperty(OWNS_ROOTS), false);
        final List<ServiceRegistration> newServices = new ArrayList<>();
        for (final String path : PropertiesUtil.toStringArray(ref.getProperty(ROOTS), new String[0])) {
            if (path != null && !path.isEmpty()) {
                final Dictionary<String, Object> newProps = new Hashtable<>();
                if (PropertiesUtil.toBoolean(ref.getProperty(PROPERTY_REQUIRED), false)) {
                    newProps.put(PROPERTY_AUTHENTICATE, AuthType.required.toString());
                } else {
                    newProps.put(PROPERTY_AUTHENTICATE, AuthType.lazy.toString());
                }
                newProps.put(PROPERTY_MODIFIABLE, true);
                newProps.put(PROPERTY_ADAPTABLE, true);
                newProps.put(PROPERTY_ATTRIBUTABLE, true);
                newProps.put(PROPERTY_REFRESHABLE, true);
                newProps.put(PROPERTY_NAME, factory.getClass().getName());
                newProps.put(PROPERTY_ROOT, normalizePath(path));
                if (ArrayUtils.contains(propertyNames, SERVICE_PID)) {
                    newProps.put(ORIGINAL_SERVICE_PID, ref.getProperty(SERVICE_PID));
                }
                if (ArrayUtils.contains(propertyNames, USE_RESOURCE_ACCESS_SECURITY)) {
                    newProps.put(PROPERTY_USE_RESOURCE_ACCESS_SECURITY, ref.getProperty(USE_RESOURCE_ACCESS_SECURITY));
                }
                if (ArrayUtils.contains(propertyNames, SERVICE_RANKING)) {
                    newProps.put(SERVICE_RANKING, ref.getProperty(SERVICE_RANKING));
                }
                String[] languages = PropertiesUtil.toStringArray(ref.getProperty(LANGUAGES), new String[0]);
                ServiceRegistration reg = bundleContext.registerService(org.apache.sling.spi.resource.provider.ResourceProvider.class.getName(), new LegacyResourceProviderFactoryAdapter(factory, languages, ownsRoot), newProps);
                newServices.add(reg);
            }
        }
        registrations.put(factory, newServices);
    }
}
Also used : ResourceProviderFactory(org.apache.sling.api.resource.ResourceProviderFactory) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) RefreshableResourceProvider(org.apache.sling.api.resource.RefreshableResourceProvider) AttributableResourceProvider(org.apache.sling.api.resource.AttributableResourceProvider) ModifyingResourceProvider(org.apache.sling.api.resource.ModifyingResourceProvider) ResourceProvider(org.apache.sling.api.resource.ResourceProvider) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) ServiceReference(org.osgi.framework.ServiceReference) Reference(org.osgi.service.component.annotations.Reference)

Example 2 with Reference

use of org.osgi.service.component.annotations.Reference in project smarthome by eclipse.

the class BluetoothDiscoveryService method addBluetoothAdapter.

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
protected void addBluetoothAdapter(BluetoothAdapter adapter) {
    this.adapters.add(adapter);
    BluetoothDiscoveryListener listener = new BluetoothDiscoveryListener() {

        @Override
        public void deviceDiscovered(BluetoothDevice device) {
            BluetoothDiscoveryService.this.deviceDiscovered(adapter, device);
        }
    };
    adapter.addDiscoveryListener(listener);
    registeredListeners.put(adapter.getUID(), listener);
}
Also used : BluetoothDevice(org.eclipse.smarthome.binding.bluetooth.BluetoothDevice) BluetoothDiscoveryListener(org.eclipse.smarthome.binding.bluetooth.BluetoothDiscoveryListener) Reference(org.osgi.service.component.annotations.Reference)

Example 3 with Reference

use of org.osgi.service.component.annotations.Reference in project smarthome by eclipse.

the class ManagedItemProvider method addItemFactory.

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
protected void addItemFactory(ItemFactory itemFactory) {
    itemFactories.add(itemFactory);
    if (failedToCreate.size() > 0) {
        // retry failed creation attempts
        Iterator<Entry<String, PersistedItem>> iterator = failedToCreate.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<String, PersistedItem> entry = iterator.next();
            String itemName = entry.getKey();
            PersistedItem persistedItem = entry.getValue();
            @SuppressWarnings("null") ActiveItem item = itemFactory.createItem(persistedItem.itemType, itemName);
            if (item != null) {
                iterator.remove();
                configureItem(persistedItem, item);
                notifyListenersAboutAddedElement(item);
            } else {
                logger.debug("The added item factory '{}' still could not instantiate item '{}'.", itemFactory, itemName);
            }
        }
        if (failedToCreate.isEmpty()) {
            logger.info("Finished loading the items which could not have been created before.");
        }
    }
}
Also used : PersistedItem(org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem) Entry(java.util.Map.Entry) Reference(org.osgi.service.component.annotations.Reference)

Example 4 with Reference

use of org.osgi.service.component.annotations.Reference in project bndtools by bndtools.

the class VersionControlIgnoresManagerImpl method addPlugin.

@Reference(cardinality = ReferenceCardinality.AT_LEAST_ONE, policy = ReferencePolicy.DYNAMIC)
void addPlugin(VersionControlIgnoresPlugin plugin) {
    if (plugin == null) {
        return;
    }
    NamedPlugin pluginInformation = plugin.getInformation();
    String name = pluginInformation.getName();
    synchronized (plugins) {
        plugins.put(name, plugin);
        pluginsInformation.put(name, pluginInformation);
    }
}
Also used : NamedPlugin(org.bndtools.api.NamedPlugin) Reference(org.osgi.service.component.annotations.Reference)

Example 5 with Reference

use of org.osgi.service.component.annotations.Reference in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class SearchResultServlet method getResults.

/**
 * Gets the search results.
 *
 * @param request The search request.
 * @param searchComponent The search component.
 * @param pageManager A PageManager.
 * @return List of search results.
 */
@NotNull
private List<ListItem> getResults(@NotNull final SlingHttpServletRequest request, @NotNull final Search searchComponent, @NotNull final PageManager pageManager) {
    List<ListItem> results = new ArrayList<>();
    String fulltext = request.getParameter(PARAM_FULLTEXT);
    if (fulltext == null || fulltext.length() < searchComponent.getSearchTermMinimumLength()) {
        return results;
    }
    long resultsOffset = Optional.ofNullable(request.getParameter(PARAM_RESULTS_OFFSET)).map(Long::parseLong).orElse(0L);
    Map<String, String> predicatesMap = new HashMap<>();
    predicatesMap.put(FulltextPredicateEvaluator.FULLTEXT, fulltext);
    predicatesMap.put(PathPredicateEvaluator.PATH, searchComponent.getSearchRootPagePath());
    predicatesMap.put(TypePredicateEvaluator.TYPE, NameConstants.NT_PAGE);
    PredicateGroup predicates = PredicateConverter.createPredicates(predicatesMap);
    ResourceResolver resourceResolver = request.getResource().getResourceResolver();
    Query query = queryBuilder.createQuery(predicates, resourceResolver.adaptTo(Session.class));
    if (searchComponent.getResultsSize() != 0) {
        query.setHitsPerPage(searchComponent.getResultsSize());
    }
    if (resultsOffset != 0) {
        query.setStart(resultsOffset);
    }
    SearchResult searchResult = query.getResult();
    LinkHandler linkHandler = request.adaptTo(LinkHandler.class);
    // Query builder has a leaking resource resolver, so the following work around is required.
    ResourceResolver leakingResourceResolver = null;
    try {
        Iterator<Resource> resourceIterator = searchResult.getResources();
        while (resourceIterator.hasNext()) {
            Resource resource = resourceIterator.next();
            // Get a reference to QB's leaking resource resolver
            if (leakingResourceResolver == null) {
                leakingResourceResolver = resource.getResourceResolver();
            }
            Optional.of(resource).map(res -> resourceResolver.getResource(res.getPath())).map(pageManager::getContainingPage).map(page -> new PageListItemImpl(linkHandler, page, searchComponent.getId(), null)).ifPresent(results::add);
        }
    } finally {
        if (leakingResourceResolver != null) {
            leakingResourceResolver.close();
        }
    }
    return results;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ScriptHelper(org.apache.sling.scripting.core.ScriptHelper) TypePredicateEvaluator(com.day.cq.search.eval.TypePredicateEvaluator) StringUtils(org.apache.commons.lang3.StringUtils) Page(com.day.cq.wcm.api.Page) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) SearchResult(com.day.cq.search.result.SearchResult) PredicateConverter(com.day.cq.search.PredicateConverter) Map(java.util.Map) PageListItemImpl(com.adobe.cq.wcm.core.components.internal.models.v1.PageListItemImpl) Search(com.adobe.cq.wcm.core.components.models.Search) PN_FRAGMENT_VARIATION_PATH(com.adobe.cq.wcm.core.components.models.ExperienceFragment.PN_FRAGMENT_VARIATION_PATH) PathPredicateEvaluator(com.day.cq.search.eval.PathPredicateEvaluator) Session(javax.jcr.Session) Servlet(javax.servlet.Servlet) SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) BundleContext(org.osgi.framework.BundleContext) Objects(java.util.Objects) PageManager(com.day.cq.wcm.api.PageManager) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Query(com.day.cq.search.Query) Optional(java.util.Optional) SlingSafeMethodsServlet(org.apache.sling.api.servlets.SlingSafeMethodsServlet) NotNull(org.jetbrains.annotations.NotNull) FulltextPredicateEvaluator(com.day.cq.search.eval.FulltextPredicateEvaluator) ModelFactory(org.apache.sling.models.factory.ModelFactory) LinkHandler(com.adobe.cq.wcm.core.components.internal.link.LinkHandler) HashMap(java.util.HashMap) LanguageManager(com.day.cq.wcm.api.LanguageManager) ArrayList(java.util.ArrayList) Component(org.osgi.service.component.annotations.Component) StreamSupport(java.util.stream.StreamSupport) Activate(org.osgi.service.component.annotations.Activate) SearchImpl(com.adobe.cq.wcm.core.components.internal.models.v1.SearchImpl) QueryBuilder(com.day.cq.search.QueryBuilder) Iterator(java.util.Iterator) ListItem(com.adobe.cq.wcm.core.components.models.ListItem) HttpServletResponse(javax.servlet.http.HttpServletResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Resource(org.apache.sling.api.resource.Resource) IOException(java.io.IOException) LiveRelationshipManager(com.day.cq.wcm.msm.api.LiveRelationshipManager) LocalizationUtils(com.adobe.cq.wcm.core.components.internal.LocalizationUtils) PredicateGroup(com.day.cq.search.PredicateGroup) Template(com.day.cq.wcm.api.Template) NameConstants(com.day.cq.wcm.api.NameConstants) Reference(org.osgi.service.component.annotations.Reference) Query(com.day.cq.search.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) SearchResult(com.day.cq.search.result.SearchResult) PredicateGroup(com.day.cq.search.PredicateGroup) PageListItemImpl(com.adobe.cq.wcm.core.components.internal.models.v1.PageListItemImpl) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ListItem(com.adobe.cq.wcm.core.components.models.ListItem) LinkHandler(com.adobe.cq.wcm.core.components.internal.link.LinkHandler) Session(javax.jcr.Session) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Reference (org.osgi.service.component.annotations.Reference)12 ArrayList (java.util.ArrayList)4 BundleContext (org.osgi.framework.BundleContext)4 Hashtable (java.util.Hashtable)2 List (java.util.List)2 ServiceReference (org.osgi.framework.ServiceReference)2 DeclarativeServicesAnnotationError (aQute.bnd.component.error.DeclarativeServicesAnnotationError)1 Annotation (aQute.bnd.osgi.Annotation)1 Clazz (aQute.bnd.osgi.Clazz)1 TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)1 LocalizationUtils (com.adobe.cq.wcm.core.components.internal.LocalizationUtils)1 LinkHandler (com.adobe.cq.wcm.core.components.internal.link.LinkHandler)1 PageListItemImpl (com.adobe.cq.wcm.core.components.internal.models.v1.PageListItemImpl)1 SearchImpl (com.adobe.cq.wcm.core.components.internal.models.v1.SearchImpl)1 PN_FRAGMENT_VARIATION_PATH (com.adobe.cq.wcm.core.components.models.ExperienceFragment.PN_FRAGMENT_VARIATION_PATH)1 ListItem (com.adobe.cq.wcm.core.components.models.ListItem)1 Search (com.adobe.cq.wcm.core.components.models.Search)1 PredicateConverter (com.day.cq.search.PredicateConverter)1 PredicateGroup (com.day.cq.search.PredicateGroup)1 Query (com.day.cq.search.Query)1