use of org.osgi.framework.InvalidSyntaxException in project karaf by apache.
the class EncryptablePropertyPlaceholderTest method getOsgiService.
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.InvalidSyntaxException in project karaf by apache.
the class BulkRequestContext method newContext.
public static BulkRequestContext newContext(ConfigurationAdmin configAdmin) throws IOException {
BulkRequestContext context = new BulkRequestContext();
context.configAdmin = configAdmin;
try {
// check JAAS subject here
AccessControlContext acc = AccessController.getContext();
if (acc == null) {
context.anonymous = true;
} else {
Subject subject = Subject.getSubject(acc);
if (subject == null) {
context.anonymous = true;
} else {
context.principals.addAll(subject.getPrincipals());
}
}
// list available ACL configs - valid for this instance only
for (Configuration config : configAdmin.listConfigurations("(service.pid=jmx.acl*)")) {
context.allPids.add(config.getPid());
}
// list available ACT whitelist configs
Configuration[] configs = configAdmin.listConfigurations("(service.pid=jmx.acl.whitelist)");
if (configs != null) {
for (Configuration config : configs) {
context.whiteListProperties.add(config.getProperties());
}
}
} catch (InvalidSyntaxException ise) {
throw new RuntimeException(ise);
}
return context;
}
use of org.osgi.framework.InvalidSyntaxException in project karaf by apache.
the class Wait method execute.
@Override
public Object execute() throws Exception {
ServiceTracker<?, ?> tracker = null;
try {
String filter = service;
if (!filter.startsWith("(")) {
if (!filter.contains("=")) {
filter = Constants.OBJECTCLASS + "=" + filter;
}
filter = "(" + filter + ")";
}
Filter osgiFilter = FrameworkUtil.createFilter(filter);
tracker = new ServiceTracker<>(bundleContext, osgiFilter, null);
tracker.open(true);
Object svc = tracker.getService();
if (timeout >= 0) {
svc = tracker.waitForService(timeout);
}
if (exception && svc == null) {
throw new TimeoutException("Can not find service '" + service + "' in the OSGi registry");
}
return svc != null;
} catch (InvalidSyntaxException e) {
throw new IllegalArgumentException("Invalid filter", e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
if (tracker != null) {
tracker.close();
}
}
}
use of org.osgi.framework.InvalidSyntaxException in project karaf by apache.
the class FeatureConfigInstaller method installFeatureConfigs.
public void installFeatureConfigs(Feature feature) throws IOException, InvalidSyntaxException {
for (ConfigInfo config : feature.getConfigurations()) {
TypedProperties props = new TypedProperties();
// trim lines
String val = config.getValue();
if (config.isExternal()) {
props.load(new URL(val));
} else {
props.load(new StringReader(val));
}
String[] pid = parsePid(config.getName());
Configuration cfg = findExistingConfiguration(configAdmin, pid[0], pid[1]);
if (cfg == null) {
Dictionary<String, Object> cfgProps = convertToDict(props);
cfg = createConfiguration(configAdmin, pid[0], pid[1]);
String key = createConfigurationKey(pid[0], pid[1]);
cfgProps.put(CONFIG_KEY, key);
cfg.update(cfgProps);
try {
updateStorage(pid[0], pid[1], props, false);
} catch (Exception e) {
LOGGER.warn("Can't update cfg file", e);
}
} else if (config.isAppend()) {
boolean update = false;
Dictionary<String, Object> properties = cfg.getProperties();
for (String key : props.keySet()) {
if (properties.get(key) == null) {
properties.put(key, props.get(key));
update = true;
}
}
if (update) {
cfg.update(properties);
try {
updateStorage(pid[0], pid[1], props, true);
} catch (Exception e) {
LOGGER.warn("Can't update cfg file", e);
}
}
}
}
for (ConfigFileInfo configFile : feature.getConfigurationFiles()) {
installConfigurationFile(configFile.getLocation(), configFile.getFinalname(), configFile.isOverride());
}
}
use of org.osgi.framework.InvalidSyntaxException in project sling by apache.
the class AbstractJobHandlingTest method cleanup.
public void cleanup() {
// clean job area
final ServiceReference<ResourceResolverFactory> ref = this.bc.getServiceReference(ResourceResolverFactory.class);
final ResourceResolverFactory factory = this.bc.getService(ref);
ResourceResolver resolver = null;
try {
resolver = factory.getAdministrativeResourceResolver(null);
final Resource rsrc = resolver.getResource("/var/eventing");
if (rsrc != null) {
delete(rsrc);
resolver.commit();
}
} catch (final LoginException le) {
// ignore
} catch (final PersistenceException e) {
// ignore
} catch (final Exception e) {
// sometimes an NPE is thrown from the repository, as we
// are in the cleanup, we can ignore this
} finally {
if (resolver != null) {
resolver.close();
}
}
// unregister all services
for (final ServiceRegistration<?> reg : this.registrations) {
reg.unregister();
}
this.registrations.clear();
// remove all configurations
try {
final org.osgi.service.cm.Configuration[] cfgs = this.configAdmin.listConfigurations(null);
if (cfgs != null) {
for (final org.osgi.service.cm.Configuration c : cfgs) {
try {
c.delete();
} catch (final IOException io) {
// ignore
}
}
}
} catch (final IOException io) {
// ignore
} catch (final InvalidSyntaxException e) {
// ignore
}
this.sleep(1000);
}
Aggregations