use of org.osgi.framework.ServiceReference 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);
}
}
use of org.osgi.framework.ServiceReference in project sling by apache.
the class BindingsValuesProvidersByContextImpl method activate.
@Activate
public void activate(ComponentContext ctx) {
bundleContext = ctx.getBundleContext();
synchronized (pendingRefs) {
for (ServiceReference ref : pendingRefs) {
addingService(ref);
}
pendingRefs.clear();
}
bvpTracker = new ServiceTracker(bundleContext, BindingsValuesProvider.class.getName(), this);
bvpTracker.open();
// Map services can also be registered to provide bindings
mapsTracker = new ServiceTracker(bundleContext, Map.class.getName(), this);
mapsTracker.open();
}
use of org.osgi.framework.ServiceReference in project sling by apache.
the class ScriptHelper method getServices.
/**
* @see org.apache.sling.api.scripting.SlingScriptHelper#getServices(java.lang.Class, java.lang.String)
*/
@SuppressWarnings("unchecked")
public <ServiceType> ServiceType[] getServices(Class<ServiceType> serviceType, String filter) throws InvalidServiceFilterSyntaxException {
try {
final ServiceReference[] refs = this.bundleContext.getServiceReferences(serviceType.getName(), filter);
ServiceType[] result = null;
if (refs != null) {
// sort by service ranking (lowest first) (see ServiceReference#compareTo(Object))
List<ServiceReference> references = Arrays.asList(refs);
Collections.sort(references);
// get the highest ranking first
Collections.reverse(references);
final List<ServiceType> objects = new ArrayList<ServiceType>();
for (ServiceReference reference : references) {
final ServiceType service = (ServiceType) this.bundleContext.getService(reference);
if (service != null) {
if (this.references == null) {
this.references = new ArrayList<ServiceReference>();
}
this.references.add(reference);
objects.add(service);
}
}
if (objects.size() > 0) {
ServiceType[] srv = (ServiceType[]) Array.newInstance(serviceType, objects.size());
result = objects.toArray(srv);
}
}
return result;
} catch (InvalidSyntaxException ise) {
throw new InvalidServiceFilterSyntaxException(filter, "Invalid filter syntax", ise);
}
}
use of org.osgi.framework.ServiceReference in project sling by apache.
the class ScriptHelper method cleanup.
/**
* Clean up this instance.
*/
public void cleanup() {
if (this.references != null) {
final Iterator<ServiceReference> i = this.references.iterator();
while (i.hasNext()) {
final ServiceReference ref = i.next();
this.bundleContext.ungetService(ref);
}
this.references.clear();
}
if (this.services != null) {
this.services.clear();
}
}
use of org.osgi.framework.ServiceReference in project sling by apache.
the class ServiceCache method getService.
/**
* Return a service for the given service class.
* @param <ServiceType> The service class / interface
* @param type The requested service
* @return The service or <code>null</code>
*/
@SuppressWarnings("unchecked")
public <ServiceType> ServiceType getService(Class<ServiceType> type) {
final String key = type.getName();
Reference reference = this.cache.get(key);
if (reference == null) {
// get the service
ServiceReference ref = this.bundleContext.getServiceReference(key);
if (ref != null) {
final Object service = this.bundleContext.getService(ref);
if (service != null) {
reference = new Reference();
reference.service = service;
reference.reference = ref;
} else {
ref = null;
}
}
// assume missing service
if (reference == null) {
reference = NULL_REFERENCE;
}
// check to see whether another thread has not done the same thing
synchronized (this) {
Reference existing = this.cache.get(key);
if (existing == null) {
this.cache.put(key, reference);
ref = null;
} else {
reference = existing;
}
}
// unget the service if another thread was faster
if (ref != null) {
this.bundleContext.ungetService(ref);
}
}
// return whatever we got (which may be null)
return (ServiceType) reference.service;
}
Aggregations