use of org.osgi.framework.Filter in project karaf by apache.
the class GuardProxyCatalog method getServiceInvocationRoles.
// Returns what roles can possibly ever invoke this service. Note that not every invocation may be successful
// as there can be different roles for different methods and also roles based on arguments passed in.
Set<String> getServiceInvocationRoles(ServiceReference<?> serviceReference) throws Exception {
boolean definitionFound = false;
Set<String> allRoles = new HashSet<>();
// walking through all of the ones that have 'service.guard'.
for (Configuration config : getServiceGuardConfigs()) {
Dictionary<String, Object> properties = config.getProperties();
Object guardFilter = properties.get(SERVICE_GUARD_KEY);
if (guardFilter instanceof String) {
Filter filter = getFilter((String) guardFilter);
if (filter.match(serviceReference)) {
definitionFound = true;
for (Enumeration<String> e = properties.keys(); e.hasMoreElements(); ) {
String key = e.nextElement();
String bareKey = key;
int idx = bareKey.indexOf('(');
if (idx >= 0) {
bareKey = bareKey.substring(0, idx);
}
int idx1 = bareKey.indexOf('[');
if (idx1 >= 0) {
bareKey = bareKey.substring(0, idx1);
}
int idx2 = bareKey.indexOf('*');
if (idx2 >= 0) {
bareKey = bareKey.substring(0, idx2);
}
if (!isValidMethodName(bareKey)) {
continue;
}
Object value = properties.get(key);
if (value instanceof String) {
allRoles.addAll(ACLConfigurationParser.parseRoles((String) value));
}
}
}
}
}
return definitionFound ? allRoles : null;
}
use of org.osgi.framework.Filter in project karaf by apache.
the class KarafTestSupport method getOsgiService.
@SuppressWarnings({ "rawtypes", "unchecked" })
protected <T> T getOsgiService(Class<T> type, String filter, long timeout) {
ServiceTracker tracker = null;
try {
String flt;
if (filter != null) {
if (filter.startsWith("(")) {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
} else {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
}
} else {
flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
}
Filter osgiFilter = FrameworkUtil.createFilter(flt);
tracker = new ServiceTracker(bundleContext, osgiFilter, null);
tracker.open(true);
// Note that the tracker is not closed to keep the reference
// This is buggy, as the service reference may change i think
Object svc = type.cast(tracker.waitForService(timeout));
if (svc == null) {
Dictionary dic = bundleContext.getBundle().getHeaders();
System.err.println("Test bundle headers: " + explode(dic));
for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, null))) {
System.err.println("ServiceReference: " + ref);
}
for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
System.err.println("Filtered ServiceReference: " + ref);
}
throw new RuntimeException("Gave up waiting for service " + flt);
}
return type.cast(svc);
} catch (InvalidSyntaxException e) {
throw new IllegalArgumentException("Invalid filter", e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
use of org.osgi.framework.Filter in project karaf by apache.
the class SecuredCommandProcessorImpl method trackCommands.
private ServiceTracker<Object, Object> trackCommands(final BundleContext context, String roleClause) throws InvalidSyntaxException {
Filter filter = context.createFilter(String.format("(&(%s=*)(%s=*)%s)", CommandProcessor.COMMAND_SCOPE, CommandProcessor.COMMAND_FUNCTION, roleClause));
return new ServiceTracker<Object, Object>(context, filter, null) {
@Override
public Object addingService(ServiceReference<Object> reference) {
Object scope = reference.getProperty(CommandProcessor.COMMAND_SCOPE);
Object function = reference.getProperty(CommandProcessor.COMMAND_FUNCTION);
List<Object> commands = new ArrayList<>();
if (scope != null && function != null) {
if (function.getClass().isArray()) {
for (Object f : ((Object[]) function)) {
Function target = new CommandProxy(context, reference, f.toString());
addCommand(scope.toString(), target, f.toString());
commands.add(target);
}
} else {
Function target = new CommandProxy(context, reference, function.toString());
addCommand(scope.toString(), target, function.toString());
commands.add(target);
}
return commands;
}
return null;
}
@Override
public void removedService(ServiceReference<Object> reference, Object service) {
Object scope = reference.getProperty(CommandProcessor.COMMAND_SCOPE);
Object function = reference.getProperty(CommandProcessor.COMMAND_FUNCTION);
if (scope != null && function != null) {
if (!function.getClass().isArray()) {
removeCommand(scope.toString(), function.toString());
} else {
for (Object func : (Object[]) function) {
removeCommand(scope.toString(), func.toString());
}
}
}
super.removedService(reference, service);
}
};
}
use of org.osgi.framework.Filter in project karaf by apache.
the class CommandTracker method init.
public void init() throws Exception {
Filter filter = context.createFilter(String.format("(&(%s=*)(%s=*)(|(%s=%s)(%s=%s)))", CommandProcessor.COMMAND_SCOPE, CommandProcessor.COMMAND_FUNCTION, Constants.OBJECTCLASS, CommandWithAction.class.getName(), Constants.OBJECTCLASS, org.apache.felix.gogo.commands.CommandWithAction.class.getName()));
this.tracker = new ServiceTracker<>(context, filter, this);
this.tracker.open();
}
use of org.osgi.framework.Filter in project aries by apache.
the class DeploymentManifestManagerImpl method createFakeBundle.
// create a 'mock' bundle that does nothing but export services required by
// Application-ImportService
private ModelledResource createFakeBundle(Collection<ServiceDeclaration> appImportServices) throws InvalidAttributeException {
_logger.debug(LOG_ENTRY, "createFakeBundle", new Object[] { appImportServices });
Attributes attrs = new Attributes();
attrs.putValue(Constants.BUNDLE_SYMBOLICNAME, FAKE_BUNDLE_NAME);
attrs.putValue(Constants.BUNDLE_VERSION_ATTRIBUTE, "1.0");
attrs.putValue(Constants.BUNDLE_MANIFESTVERSION, "2");
// Build an ExportedService for every Application-ImportService entry
Collection<ExportedService> exportedServices = new ArrayList<ExportedService>();
for (ServiceDeclaration sDec : appImportServices) {
Collection<String> ifaces = Arrays.asList(sDec.getInterfaceName());
Filter filter = sDec.getFilter();
Map<String, String> serviceProperties;
if (filter != null) {
serviceProperties = ManifestHeaderProcessor.parseFilter(filter.toString());
} else {
serviceProperties = new HashMap<String, String>();
}
serviceProperties.put("service.imported", "");
exportedServices.add(modellingManager.getExportedService("", 0, ifaces, new HashMap<String, Object>(serviceProperties)));
}
ModelledResource fakeBundle = modellingManager.getModelledResource(null, attrs, null, exportedServices);
_logger.debug(LOG_EXIT, "createFakeBundle", new Object[] { fakeBundle });
return fakeBundle;
}
Aggregations