use of org.osgi.framework.InvalidSyntaxException in project sling by apache.
the class HealthCheckExecutorImpl method activate.
@Activate
protected final void activate(final Map<String, Object> properties, final BundleContext bundleContext) {
this.bundleContext = bundleContext;
final ModifiableThreadPoolConfig hcThreadPoolConfig = new ModifiableThreadPoolConfig();
hcThreadPoolConfig.setMaxPoolSize(25);
hcThreadPool = threadPoolManager.create(hcThreadPoolConfig, "Health Check Thread Pool");
this.modified(properties);
try {
this.bundleContext.addServiceListener(this, "(" + Constants.OBJECTCLASS + "=" + HealthCheck.class.getName() + ")");
} catch (final InvalidSyntaxException ise) {
// this should really never happen as the expression above is constant
throw new RuntimeException("Unexpected exception occured.", ise);
}
}
use of org.osgi.framework.InvalidSyntaxException in project sling by apache.
the class OSGiServiceInjector method getService.
@SuppressWarnings("unchecked")
private <T> Object getService(Object adaptable, Class<T> type, String filter, DisposalCallbackRegistry callbackRegistry) {
// cannot use SlingScriptHelper since it does not support ordering by service ranking due to https://issues.apache.org/jira/browse/SLING-5665
try {
ServiceReference[] refs = bundleContext.getServiceReferences(type.getName(), filter);
if (refs == null || refs.length == 0) {
return null;
} else {
// sort by service ranking (lowest first) (see ServiceReference.compareTo)
List<ServiceReference> references = Arrays.asList(refs);
Collections.sort(references);
callbackRegistry.addDisposalCallback(new Callback(refs, bundleContext));
return bundleContext.getService(references.get(references.size() - 1));
}
} catch (InvalidSyntaxException e) {
log.error("invalid filter expression", e);
return null;
}
}
use of org.osgi.framework.InvalidSyntaxException in project aries by apache.
the class AbstractServiceReferenceRecipe method start.
public void start(SatisfactionListener listener) {
if (listener == null)
throw new NullPointerException("satisfactionListener is null");
if (started.compareAndSet(false, true)) {
try {
satisfactionListener = listener;
satisfied.set(optional);
// though this may not be sufficient because we don't control ordering of those events
synchronized (tracked) {
getBundleContextForServiceLookup().addServiceListener(this, getOsgiFilter());
ServiceReference[] references = getBundleContextForServiceLookup().getServiceReferences((String) null, getOsgiFilter());
tracked.setInitial(references != null ? references : new ServiceReference[0]);
}
tracked.trackInitial();
satisfied.set(optional || !tracked.isEmpty());
retrack();
LOGGER.debug("Found initial references {} for OSGi service {}", getServiceReferences(), getOsgiFilter());
} catch (InvalidSyntaxException e) {
throw new ComponentDefinitionException(e);
}
}
}
use of org.osgi.framework.InvalidSyntaxException in project aries by apache.
the class DSLTest method testConfigurationsAndRegistrations.
@Test
public void testConfigurationsAndRegistrations() throws InvalidSyntaxException, IOException, InterruptedException {
ServiceReference<ConfigurationAdmin> serviceReference = bundleContext.getServiceReference(ConfigurationAdmin.class);
ConfigurationAdmin configurationAdmin = bundleContext.getService(serviceReference);
/* For each factory configuration register a service with the property
key set to the value of the property key that comes with the
configuration */
OSGi<ServiceRegistration<Service>> program = configurations("test.configuration").map(d -> d.get("key")).flatMap(key -> register(Service.class, new Service(), new HashMap<String, Object>() {
{
put("key", key);
}
}));
OSGiResult<ServiceRegistration<Service>> result = program.run(bundleContext);
assertEquals(0, bundleContext.getServiceReferences(Service.class, "(test.configuration=*)").size());
CountDownLatch addedLatch = new CountDownLatch(3);
ServiceRegistration<?> addedServiceRegistration = bundleContext.registerService(ManagedServiceFactory.class, new ManagedServiceFactory() {
@Override
public String getName() {
return "";
}
@Override
public void updated(String s, Dictionary<String, ?> dictionary) throws ConfigurationException {
addedLatch.countDown();
}
@Override
public void deleted(String s) {
}
}, new Hashtable<String, Object>() {
{
put("service.pid", "test.configuration");
}
});
CountDownLatch deletedLatch = new CountDownLatch(3);
ServiceRegistration<?> deletedServiceRegistration = bundleContext.registerService(ManagedServiceFactory.class, new ManagedServiceFactory() {
@Override
public String getName() {
return "";
}
@Override
public void updated(String s, Dictionary<String, ?> dictionary) throws ConfigurationException {
}
@Override
public void deleted(String s) {
deletedLatch.countDown();
}
}, new Hashtable<String, Object>() {
{
put("service.pid", "test.configuration");
}
});
Configuration configuration = configurationAdmin.createFactoryConfiguration("test.configuration");
configuration.update(new Hashtable<String, Object>() {
{
put("key", "service one");
}
});
Configuration configuration2 = configurationAdmin.createFactoryConfiguration("test.configuration");
configuration2.update(new Hashtable<String, Object>() {
{
put("key", "service two");
}
});
Configuration configuration3 = configurationAdmin.createFactoryConfiguration("test.configuration");
configuration3.update(new Hashtable<String, Object>() {
{
put("key", "service three");
}
});
assertTrue(addedLatch.await(10, TimeUnit.SECONDS));
assertEquals(1, bundleContext.getServiceReferences(Service.class, "(key=service one)").size());
assertEquals(1, bundleContext.getServiceReferences(Service.class, "(key=service two)").size());
assertEquals(1, bundleContext.getServiceReferences(Service.class, "(key=service three)").size());
configuration3.delete();
configuration2.delete();
configuration.delete();
assertTrue(deletedLatch.await(10, TimeUnit.SECONDS));
assertEquals(0, bundleContext.getServiceReferences(Service.class, "(test.configuration=*)").size());
addedServiceRegistration.unregister();
deletedServiceRegistration.unregister();
result.close();
bundleContext.ungetService(serviceReference);
}
use of org.osgi.framework.InvalidSyntaxException in project ddf by codice.
the class CatalogBundle method waitForSource.
private <T extends Source> T waitForSource(String id, Class<T> type) throws InterruptedException, InvalidSyntaxException {
T source = null;
long timeoutLimit = System.currentTimeMillis() + CATALOG_PROVIDER_TIMEOUT;
boolean available = false;
while (!available) {
ServiceReference<CatalogFramework> frameworkRef = serviceManager.getServiceReference(CatalogFramework.class);
CatalogFramework framework = null;
if (frameworkRef != null) {
framework = serviceManager.getService(frameworkRef);
}
if (source == null) {
source = serviceManager.getServiceReferences(type, null).stream().map(serviceManager::getService).filter(src -> id.equals(src.getId())).findFirst().orElse(null);
}
if (source != null && framework != null) {
SourceInfoRequestEnterprise sourceInfoRequestEnterprise = new SourceInfoRequestEnterprise(true);
try {
SourceInfoResponse sources = framework.getSourceInfo(sourceInfoRequestEnterprise);
Set<SourceDescriptor> sourceInfo = sources.getSourceInfo();
for (SourceDescriptor sourceDescriptor : sourceInfo) {
if (sourceDescriptor.getSourceId().equals(source.getId())) {
available = sourceDescriptor.isAvailable() && source.isAvailable();
LOGGER.info("Source.isAvailable = {} Framework.isAvailable = {}", source.isAvailable(), sourceDescriptor.isAvailable());
}
}
} catch (SourceUnavailableException e) {
available = false;
}
} else {
LOGGER.info("Currently no source of type {} and name {} could be found", type.getName(), id);
}
if (!available) {
if (System.currentTimeMillis() > timeoutLimit) {
fail("Source (" + id + ") was not created in a timely manner.");
}
Thread.sleep(1000);
}
}
LOGGER.info("Source {} is available.", id);
return source;
}
Aggregations