Search in sources :

Example 1 with Adaption

use of org.apache.sling.adapter.Adaption in project sling by apache.

the class AdapterManagerImpl method registerAdapterFactory.

/**
     * Unregisters the {@link AdapterFactory} referred to by the service
     * <code>reference</code> from the registry.
     */
private void registerAdapterFactory(final ComponentContext context, final ServiceReference<AdapterFactory> reference) {
    final String[] adaptables = PropertiesUtil.toStringArray(reference.getProperty(ADAPTABLE_CLASSES));
    final String[] adapters = PropertiesUtil.toStringArray(reference.getProperty(ADAPTER_CLASSES));
    final boolean allowedInPrivatePackage = PropertiesUtil.toBoolean(reference.getProperty(ALLOWED_IN_PRIVATE), false);
    if (adaptables == null || adaptables.length == 0 || adapters == null || adapters.length == 0) {
        return;
    }
    for (String clazz : adaptables) {
        if (!allowedInPrivatePackage && !checkPackage(packageAdmin, clazz)) {
            log.warn("Adaptable class {} in factory service {} is not in an exported package.", clazz, reference.getProperty(Constants.SERVICE_ID));
        }
    }
    for (String clazz : adapters) {
        if (!allowedInPrivatePackage && !checkPackage(packageAdmin, clazz)) {
            log.warn("Adapter class {} in factory service {} is not in an exported package.", clazz, reference.getProperty(Constants.SERVICE_ID));
        }
    }
    final AdapterFactoryDescriptor factoryDesc = new AdapterFactoryDescriptor(context, reference, adapters);
    for (final String adaptable : adaptables) {
        AdapterFactoryDescriptorMap adfMap = null;
        synchronized (this.descriptors) {
            adfMap = descriptors.get(adaptable);
            if (adfMap == null) {
                adfMap = new AdapterFactoryDescriptorMap();
                descriptors.put(adaptable, adfMap);
            }
        }
        synchronized (adfMap) {
            adfMap.put(reference, factoryDesc);
        }
    }
    // clear the factory cache to force rebuild on next access
    this.factoryCache.clear();
    // register adaption
    final Dictionary<String, Object> props = new Hashtable<>();
    props.put(SlingConstants.PROPERTY_ADAPTABLE_CLASSES, adaptables);
    props.put(SlingConstants.PROPERTY_ADAPTER_CLASSES, adapters);
    ServiceRegistration<Adaption> adaptionRegistration = this.context.getBundleContext().registerService(Adaption.class, AdaptionImpl.INSTANCE, props);
    if (log.isDebugEnabled()) {
        log.debug("Registered service {} with {} : {} and {} : {}", new Object[] { Adaption.class.getName(), SlingConstants.PROPERTY_ADAPTABLE_CLASSES, Arrays.toString(adaptables), SlingConstants.PROPERTY_ADAPTER_CLASSES, Arrays.toString(adapters) });
    }
    factoryDesc.setAdaption(adaptionRegistration);
    // send event
    final EventAdmin localEA = this.eventAdmin;
    if (localEA != null) {
        localEA.postEvent(new Event(SlingConstants.TOPIC_ADAPTER_FACTORY_ADDED, props));
    }
}
Also used : Adaption(org.apache.sling.adapter.Adaption) EventAdmin(org.osgi.service.event.EventAdmin) Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event)

Example 2 with Adaption

use of org.apache.sling.adapter.Adaption in project sling by apache.

the class MockAdapterManagerImpl method registerAdapterFactory.

/**
     * Unregisters the {@link AdapterFactory} referred to by the service
     * <code>reference</code> from the registry.
     */
private void registerAdapterFactory(final ComponentContext context, final ServiceReference reference) {
    final String[] adaptables = PropertiesUtil.toStringArray(reference.getProperty(ADAPTABLE_CLASSES));
    final String[] adapters = PropertiesUtil.toStringArray(reference.getProperty(ADAPTER_CLASSES));
    if (adaptables == null || adaptables.length == 0 || adapters == null || adapters.length == 0) {
        return;
    }
    // DISABLED IN THIS COPY OF CLASS
    /*
        for (String clazz : adaptables) {
            if (!checkPackage(packageAdmin, clazz)) {
                log.warn("Adaptable class {} in factory service {} is not in an exported package.", clazz, reference.getProperty(Constants.SERVICE_ID));
            }
        }

        for (String clazz : adapters) {
            if (!checkPackage(packageAdmin, clazz)) {
                log.warn("Adapter class {} in factory service {} is not in an exported package.", clazz, reference.getProperty(Constants.SERVICE_ID));
            }
        }
        */
    final AdapterFactoryDescriptor factoryDesc = new AdapterFactoryDescriptor(context, reference, adapters);
    for (final String adaptable : adaptables) {
        AdapterFactoryDescriptorMap adfMap = null;
        synchronized (this.descriptors) {
            adfMap = descriptors.get(adaptable);
            if (adfMap == null) {
                adfMap = new AdapterFactoryDescriptorMap();
                descriptors.put(adaptable, adfMap);
            }
        }
        synchronized (adfMap) {
            adfMap.put(reference, factoryDesc);
        }
    }
    // clear the factory cache to force rebuild on next access
    this.factoryCache.clear();
    // register adaption
    final Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(SlingConstants.PROPERTY_ADAPTABLE_CLASSES, adaptables);
    props.put(SlingConstants.PROPERTY_ADAPTER_CLASSES, adapters);
    ServiceRegistration adaptionRegistration = this.context.getBundleContext().registerService(Adaption.class.getName(), AdaptionImpl.INSTANCE, props);
    if (log.isDebugEnabled()) {
        log.debug("Registered service {} with {} : {} and {} : {}", new Object[] { Adaption.class.getName(), SlingConstants.PROPERTY_ADAPTABLE_CLASSES, Arrays.toString(adaptables), SlingConstants.PROPERTY_ADAPTER_CLASSES, Arrays.toString(adapters) });
    }
    factoryDesc.setAdaption(adaptionRegistration);
    // send event
    final EventAdmin localEA = this.eventAdmin;
    if (localEA != null) {
        localEA.postEvent(new Event(SlingConstants.TOPIC_ADAPTER_FACTORY_ADDED, props));
    }
}
Also used : AdapterFactoryDescriptorMap(org.apache.sling.adapter.internal.AdapterFactoryDescriptorMap) Adaption(org.apache.sling.adapter.Adaption) EventAdmin(org.osgi.service.event.EventAdmin) AdapterFactoryDescriptor(org.apache.sling.adapter.internal.AdapterFactoryDescriptor) Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 3 with Adaption

use of org.apache.sling.adapter.Adaption in project sling by apache.

the class AdapterManagerImpl method unregisterAdapterFactory.

/**
     * Unregisters the {@link AdapterFactory} referred to by the service
     * <code>reference</code> from the registry.
     */
private void unregisterAdapterFactory(final ServiceReference<AdapterFactory> reference) {
    synchronized (this.boundAdapterFactories) {
        boundAdapterFactories.remove(reference);
    }
    final String[] adaptables = PropertiesUtil.toStringArray(reference.getProperty(ADAPTABLE_CLASSES));
    final String[] adapters = PropertiesUtil.toStringArray(reference.getProperty(ADAPTER_CLASSES));
    if (adaptables == null || adaptables.length == 0 || adapters == null || adapters.length == 0) {
        return;
    }
    boolean factoriesModified = false;
    AdapterFactoryDescriptorMap adfMap = null;
    AdapterFactoryDescriptor removedDescriptor = null;
    for (final String adaptable : adaptables) {
        synchronized (this.descriptors) {
            adfMap = this.descriptors.get(adaptable);
        }
        if (adfMap != null) {
            synchronized (adfMap) {
                AdapterFactoryDescriptor factoryDesc = adfMap.remove(reference);
                if (factoryDesc != null) {
                    factoriesModified = true;
                    // Since the code paths above does not fully guarantee it though, let's keep this check in place
                    if (removedDescriptor != null && removedDescriptor != factoryDesc) {
                        log.error("When unregistering reference {} got duplicate service descriptors {} and {}. Unregistration of {} services may be incomplete.", new Object[] { reference, removedDescriptor, factoryDesc, Adaption.class.getName() });
                    }
                    removedDescriptor = factoryDesc;
                }
            }
        }
    }
    // removed
    if (factoriesModified) {
        this.factoryCache.clear();
    }
    // unregister adaption
    if (removedDescriptor != null) {
        removedDescriptor.getAdaption().unregister();
        if (log.isDebugEnabled()) {
            log.debug("Unregistered service {} with {} : {} and {} : {}", new Object[] { Adaption.class.getName(), SlingConstants.PROPERTY_ADAPTABLE_CLASSES, Arrays.toString(adaptables), SlingConstants.PROPERTY_ADAPTER_CLASSES, Arrays.toString(adapters) });
        }
    }
    // send event
    final EventAdmin localEA = this.eventAdmin;
    if (localEA != null) {
        final Dictionary<String, Object> props = new Hashtable<>();
        props.put(SlingConstants.PROPERTY_ADAPTABLE_CLASSES, adaptables);
        props.put(SlingConstants.PROPERTY_ADAPTER_CLASSES, adapters);
        localEA.postEvent(new Event(SlingConstants.TOPIC_ADAPTER_FACTORY_REMOVED, props));
    }
}
Also used : Adaption(org.apache.sling.adapter.Adaption) EventAdmin(org.osgi.service.event.EventAdmin) Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event)

Example 4 with Adaption

use of org.apache.sling.adapter.Adaption in project sling by apache.

the class MockAdapterManagerImpl method unregisterAdapterFactory.

/**
     * Check that the package containing the class is exported or is a java.*
     * class.
     * 
     * @param packageAdmin the PackageAdmin service
     * @param clazz the class name
     * @return true if the package is exported
     */
// DISABLED IN THIS COPY OF CLASS
/*
    static boolean checkPackage(PackageAdmin packageAdmin, String clazz) {
        final String packageName = getPackageName(clazz); 
        if (packageName.startsWith("java.")) {
            return true;
        }
        return packageAdmin.getExportedPackage(packageName) != null;
    }
    */
/**
     * Unregisters the {@link AdapterFactory} referred to by the service
     * <code>reference</code> from the registry.
     */
private void unregisterAdapterFactory(final ServiceReference reference) {
    synchronized (this.boundAdapterFactories) {
        boundAdapterFactories.remove(reference);
    }
    final String[] adaptables = PropertiesUtil.toStringArray(reference.getProperty(ADAPTABLE_CLASSES));
    final String[] adapters = PropertiesUtil.toStringArray(reference.getProperty(ADAPTER_CLASSES));
    if (adaptables == null || adaptables.length == 0 || adapters == null || adapters.length == 0) {
        return;
    }
    boolean factoriesModified = false;
    AdapterFactoryDescriptorMap adfMap = null;
    AdapterFactoryDescriptor removedDescriptor = null;
    for (final String adaptable : adaptables) {
        synchronized (this.descriptors) {
            adfMap = this.descriptors.get(adaptable);
        }
        if (adfMap != null) {
            synchronized (adfMap) {
                AdapterFactoryDescriptor factoryDesc = adfMap.remove(reference);
                if (factoryDesc != null) {
                    factoriesModified = true;
                    // Since the code paths above does not fully guarantee it though, let's keep this check in place
                    if (removedDescriptor != null && removedDescriptor != factoryDesc) {
                        log.error("When unregistering reference {} got duplicate service descriptors {} and {}. Unregistration of {} services may be incomplete.", new Object[] { reference, removedDescriptor, factoryDesc, Adaption.class.getName() });
                    }
                    removedDescriptor = factoryDesc;
                }
            }
        }
    }
    // removed
    if (factoriesModified) {
        this.factoryCache.clear();
    }
    // unregister adaption
    if (removedDescriptor != null) {
        removedDescriptor.getAdaption().unregister();
        if (log.isDebugEnabled()) {
            log.debug("Unregistered service {} with {} : {} and {} : {}", new Object[] { Adaption.class.getName(), SlingConstants.PROPERTY_ADAPTABLE_CLASSES, Arrays.toString(adaptables), SlingConstants.PROPERTY_ADAPTER_CLASSES, Arrays.toString(adapters) });
        }
    }
    // send event
    final EventAdmin localEA = this.eventAdmin;
    if (localEA != null) {
        final Dictionary<String, Object> props = new Hashtable<String, Object>();
        props.put(SlingConstants.PROPERTY_ADAPTABLE_CLASSES, adaptables);
        props.put(SlingConstants.PROPERTY_ADAPTER_CLASSES, adapters);
        localEA.postEvent(new Event(SlingConstants.TOPIC_ADAPTER_FACTORY_REMOVED, props));
    }
}
Also used : AdapterFactoryDescriptorMap(org.apache.sling.adapter.internal.AdapterFactoryDescriptorMap) Adaption(org.apache.sling.adapter.Adaption) EventAdmin(org.osgi.service.event.EventAdmin) AdapterFactoryDescriptor(org.apache.sling.adapter.internal.AdapterFactoryDescriptor) Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event)

Aggregations

Hashtable (java.util.Hashtable)4 Adaption (org.apache.sling.adapter.Adaption)4 Event (org.osgi.service.event.Event)4 EventAdmin (org.osgi.service.event.EventAdmin)4 AdapterFactoryDescriptor (org.apache.sling.adapter.internal.AdapterFactoryDescriptor)2 AdapterFactoryDescriptorMap (org.apache.sling.adapter.internal.AdapterFactoryDescriptorMap)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)1