use of org.osgi.framework.ServicePermission in project felix by apache.
the class EventDispatcher method invokeServiceListenerCallback.
private static void invokeServiceListenerCallback(Bundle bundle, final EventListener l, Filter filter, Object acc, final EventObject event, final Dictionary oldProps) {
// STOPPING, and ACTIVE bundles.
if ((bundle.getState() != Bundle.STARTING) && (bundle.getState() != Bundle.STOPPING) && (bundle.getState() != Bundle.ACTIVE)) {
return;
}
// Check that the bundle has permission to get at least
// one of the service interfaces; the objectClass property
// of the service stores its service interfaces.
ServiceReference ref = ((ServiceEvent) event).getServiceReference();
boolean hasPermission = true;
Object sm = System.getSecurityManager();
if ((acc != null) && (sm != null)) {
try {
ServicePermission perm = new ServicePermission(ref, ServicePermission.GET);
((SecurityManager) sm).checkPermission(perm, acc);
} catch (Exception ex) {
hasPermission = false;
}
}
if (hasPermission) {
// Dispatch according to the filter.
boolean matched;
if (l instanceof UnfilteredServiceListener) {
// An UnfilteredServiceListener always matches, regardless of the filter.
// The filter is still passed on to the Service Registry Hooks.
matched = true;
} else {
matched = (filter == null) || filter.match(((ServiceEvent) event).getServiceReference());
}
if (matched) {
if ((l instanceof AllServiceListener) || Util.isServiceAssignable(bundle, ((ServiceEvent) event).getServiceReference())) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
((ServiceListener) l).serviceChanged((ServiceEvent) event);
return null;
}
});
} else {
((ServiceListener) l).serviceChanged((ServiceEvent) event);
}
}
} else // matched previously.
if (((ServiceEvent) event).getType() == ServiceEvent.MODIFIED) {
if (filter.match(oldProps)) {
final ServiceEvent se = new ServiceEvent(ServiceEvent.MODIFIED_ENDMATCH, ((ServiceEvent) event).getServiceReference());
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
((ServiceListener) l).serviceChanged(se);
return null;
}
});
} else {
((ServiceListener) l).serviceChanged(se);
}
}
}
}
}
use of org.osgi.framework.ServicePermission in project felix by apache.
the class BundleContextImpl method registerService.
public ServiceRegistration<?> registerService(String[] clazzes, Object svcObj, Dictionary<String, ?> dict) {
checkValidity();
// CONCURRENCY NOTE: This is a NOT a check-then-act situation,
// because internally the framework acquires the bundle state
// lock to ensure state consistency.
Object sm = System.getSecurityManager();
if (sm != null) {
if (clazzes != null) {
for (int i = 0; i < clazzes.length; i++) {
((SecurityManager) sm).checkPermission(new ServicePermission(clazzes[i], ServicePermission.REGISTER));
}
}
}
return m_felix.registerService(this, clazzes, svcObj, dict);
}
use of org.osgi.framework.ServicePermission in project felix by apache.
the class BundleContextImpl method getServiceObjects.
/**
* @see org.osgi.framework.BundleContext#getServiceObjects(org.osgi.framework.ServiceReference)
*/
public <S> ServiceObjects<S> getServiceObjects(final ServiceReference<S> ref) {
checkValidity();
Object sm = System.getSecurityManager();
if (sm != null) {
((SecurityManager) sm).checkPermission(new ServicePermission(ref, ServicePermission.GET));
}
ServiceRegistrationImpl reg = ((ServiceRegistrationImpl.ServiceReferenceImpl) ref).getRegistration();
if (reg.isValid()) {
return new ServiceObjectsImpl(ref);
}
return null;
}
use of org.osgi.framework.ServicePermission in project felix by apache.
the class BundleImpl method getRegisteredServices.
/**
* Returns an array of service references corresponding to
* the bundle's registered services.
*
* @return an array of service references or null.
*/
@Override
public ServiceReference[] getRegisteredServices() {
Object sm = System.getSecurityManager();
if (sm != null) {
ServiceReference[] refs = getFramework().getBundleRegisteredServices(this);
if (refs == null) {
return refs;
}
List result = new ArrayList();
for (int i = 0; i < refs.length; i++) {
try {
((SecurityManager) sm).checkPermission(new ServicePermission(refs[i], ServicePermission.GET));
result.add(refs[i]);
} catch (Exception ex) {
// Silently ignore.
}
}
if (result.isEmpty()) {
return null;
}
return (ServiceReference[]) result.toArray(new ServiceReference[result.size()]);
} else {
return getFramework().getBundleRegisteredServices(this);
}
}
use of org.osgi.framework.ServicePermission in project felix by apache.
the class BundleImpl method getServicesInUse.
@Override
public ServiceReference[] getServicesInUse() {
Object sm = System.getSecurityManager();
if (sm != null) {
ServiceReference[] refs = getFramework().getBundleServicesInUse(this);
if (refs == null) {
return refs;
}
List result = new ArrayList();
for (int i = 0; i < refs.length; i++) {
try {
((SecurityManager) sm).checkPermission(new ServicePermission(refs[i], ServicePermission.GET));
result.add(refs[i]);
} catch (Exception ex) {
// Silently ignore.
}
}
if (result.isEmpty()) {
return null;
}
return (ServiceReference[]) result.toArray(new ServiceReference[result.size()]);
}
return getFramework().getBundleServicesInUse(this);
}
Aggregations