use of org.osgi.service.component.ComponentContext in project fabric8 by jboss-fuse.
the class ServiceImplTest method createMockServiceImpl.
/*
* Create a mock patch service implementation with a provided patch storage location
*/
private ServiceImpl createMockServiceImpl(File patches) throws IOException {
ComponentContext componentContext = createMock(ComponentContext.class);
BundleContext bundleContext = createMock(BundleContext.class);
Bundle sysBundle = createMock(Bundle.class);
BundleContext sysBundleContext = createMock(BundleContext.class);
Bundle bundle = createMock(Bundle.class);
GitPatchRepository repository = createMock(GitPatchRepository.class);
//
// Create a new service, download a patch
//
expect(bundle.getVersion()).andReturn(new Version(1, 2, 0)).anyTimes();
expect(componentContext.getBundleContext()).andReturn(bundleContext);
expect(bundleContext.getBundle(0)).andReturn(sysBundle).anyTimes();
expect(bundleContext.getBundle()).andReturn(bundle).anyTimes();
expect(sysBundle.getBundleContext()).andReturn(sysBundleContext).anyTimes();
expect(sysBundleContext.getProperty(Service.NEW_PATCH_LOCATION)).andReturn(patches.toString()).anyTimes();
expect(sysBundleContext.getProperty("karaf.default.repository")).andReturn("system").anyTimes();
expect(sysBundleContext.getProperty("karaf.data")).andReturn(patches.getParent() + "/data").anyTimes();
try {
expect(repository.getManagedPatch(anyString())).andReturn(null).anyTimes();
expect(repository.findOrCreateMainGitRepository()).andReturn(null).anyTimes();
expect(sysBundleContext.getProperty("karaf.home")).andReturn(karaf.getCanonicalPath()).anyTimes();
expect(sysBundleContext.getProperty("karaf.base")).andReturn(karaf.getCanonicalPath()).anyTimes();
expect(sysBundleContext.getProperty("karaf.name")).andReturn("root").anyTimes();
expect(sysBundleContext.getProperty("karaf.instances")).andReturn(karaf.getCanonicalPath() + "/instances").anyTimes();
} catch (GitAPIException | IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
replay(componentContext, sysBundleContext, sysBundle, bundleContext, bundle, repository);
PatchManagement pm = new GitPatchManagementServiceImpl(bundleContext);
((GitPatchManagementServiceImpl) pm).setGitPatchRepository(repository);
ServiceImpl service = new ServiceImpl();
setField(service, "patchManagement", pm);
service.activate(componentContext);
return service;
}
use of org.osgi.service.component.ComponentContext in project fabric8 by jboss-fuse.
the class ServiceImplTest method testPatchWithVersionRanges.
@Test
public void testPatchWithVersionRanges() throws Exception {
ComponentContext componentContext = createMock(ComponentContext.class);
BundleContext bundleContext = createMock(BundleContext.class);
Bundle sysBundle = createMock(Bundle.class);
BundleContext sysBundleContext = createMock(BundleContext.class);
Bundle bundle = createMock(Bundle.class);
Bundle bundle2 = createMock(Bundle.class);
FrameworkWiring wiring = createMock(FrameworkWiring.class);
GitPatchRepository repository = createMock(GitPatchRepository.class);
//
// Create a new service, download a patch
//
expect(componentContext.getBundleContext()).andReturn(bundleContext);
expect(bundleContext.getBundle(0)).andReturn(sysBundle).anyTimes();
expect(sysBundle.getBundleContext()).andReturn(sysBundleContext).anyTimes();
expect(sysBundleContext.getProperty(Service.NEW_PATCH_LOCATION)).andReturn(storage.toString()).anyTimes();
expect(repository.getManagedPatch(anyString())).andReturn(null).anyTimes();
expect(repository.findOrCreateMainGitRepository()).andReturn(null).anyTimes();
expect(sysBundleContext.getProperty("karaf.default.repository")).andReturn("system").anyTimes();
expect(sysBundleContext.getProperty("karaf.home")).andReturn(karaf.getCanonicalPath()).anyTimes();
expect(sysBundleContext.getProperty("karaf.base")).andReturn(karaf.getCanonicalPath()).anyTimes();
expect(sysBundleContext.getProperty("karaf.name")).andReturn("root").anyTimes();
expect(sysBundleContext.getProperty("karaf.instances")).andReturn(karaf.getCanonicalPath() + "/instances").anyTimes();
expect(sysBundleContext.getProperty("karaf.data")).andReturn(karaf.getCanonicalPath() + "/data").anyTimes();
replay(componentContext, sysBundleContext, sysBundle, bundleContext, bundle, repository);
PatchManagement pm = mockManagementService(bundleContext);
((GitPatchManagementServiceImpl) pm).setGitPatchRepository(repository);
ServiceImpl service = new ServiceImpl();
setField(service, "patchManagement", pm);
service.activate(componentContext);
Iterable<Patch> patches = service.download(patch140.toURI().toURL());
assertNotNull(patches);
Iterator<Patch> it = patches.iterator();
assertTrue(it.hasNext());
Patch patch = it.next();
assertNotNull(patch);
assertEquals("patch-1.4.0", patch.getPatchData().getId());
assertNotNull(patch.getPatchData().getBundles());
assertEquals(1, patch.getPatchData().getBundles().size());
Iterator<String> itb = patch.getPatchData().getBundles().iterator();
assertEquals("mvn:foo/my-bsn/1.4.0", itb.next());
assertNull(patch.getResult());
verify(componentContext, sysBundleContext, sysBundle, bundleContext, bundle);
//
// Simulate the patch
//
reset(componentContext, sysBundleContext, sysBundle, bundleContext, bundle);
expect(sysBundleContext.getBundles()).andReturn(new Bundle[] { bundle });
expect(sysBundleContext.getServiceReference("io.fabric8.api.FabricService")).andReturn(null);
expect(bundle.getSymbolicName()).andReturn("my-bsn").anyTimes();
expect(bundle.getVersion()).andReturn(new Version("1.3.1")).anyTimes();
expect(bundle.getLocation()).andReturn("location").anyTimes();
expect(bundle.getBundleId()).andReturn(123L);
BundleStartLevel bsl = createMock(BundleStartLevel.class);
expect(bsl.getStartLevel()).andReturn(30).anyTimes();
expect(bundle.adapt(BundleStartLevel.class)).andReturn(bsl).anyTimes();
expect(bundle.getState()).andReturn(1);
expect(sysBundleContext.getProperty("karaf.default.repository")).andReturn("system").anyTimes();
replay(componentContext, sysBundleContext, sysBundle, bundleContext, bundle, bsl);
PatchResult result = service.install(patch, true);
assertNotNull(result);
assertNull(patch.getResult());
assertEquals(1, result.getBundleUpdates().size());
assertTrue(result.isSimulation());
}
use of org.osgi.service.component.ComponentContext in project fabric8 by jboss-fuse.
the class ServiceImplTest method testLoadWithoutRanges.
@Test
public void testLoadWithoutRanges() throws IOException, GitAPIException {
BundleContext bundleContext = createMock(BundleContext.class);
ComponentContext componentContext = createMock(ComponentContext.class);
Bundle sysBundle = createMock(Bundle.class);
BundleContext sysBundleContext = createMock(BundleContext.class);
Bundle bundle = createMock(Bundle.class);
Bundle bundle2 = createMock(Bundle.class);
FrameworkWiring wiring = createMock(FrameworkWiring.class);
GitPatchRepository repository = createMock(GitPatchRepository.class);
//
// Create a new service, download a patch
//
expect(componentContext.getBundleContext()).andReturn(bundleContext);
expect(bundleContext.getBundle(0)).andReturn(sysBundle).anyTimes();
expect(sysBundle.getBundleContext()).andReturn(sysBundleContext).anyTimes();
expect(sysBundleContext.getProperty(Service.NEW_PATCH_LOCATION)).andReturn(storage.toString()).anyTimes();
expect(repository.getManagedPatch(anyString())).andReturn(null).anyTimes();
expect(repository.findOrCreateMainGitRepository()).andReturn(null).anyTimes();
expect(sysBundleContext.getProperty("karaf.default.repository")).andReturn("system").anyTimes();
expect(sysBundleContext.getProperty("karaf.home")).andReturn(karaf.getCanonicalPath()).anyTimes();
expect(sysBundleContext.getProperty("karaf.base")).andReturn(karaf.getCanonicalPath()).anyTimes();
expect(sysBundleContext.getProperty("karaf.name")).andReturn("root").anyTimes();
expect(sysBundleContext.getProperty("karaf.instances")).andReturn(karaf.getCanonicalPath() + "/instances").anyTimes();
expect(sysBundleContext.getProperty("karaf.data")).andReturn(karaf.getCanonicalPath() + "/data").anyTimes();
replay(componentContext, sysBundleContext, sysBundle, bundleContext, bundle, repository);
PatchManagement pm = new GitPatchManagementServiceImpl(bundleContext);
((GitPatchManagementServiceImpl) pm).setGitPatchRepository(repository);
ServiceImpl service = new ServiceImpl();
setField(service, "patchManagement", pm);
service.activate(componentContext);
PatchData pd = PatchData.load(getClass().getClassLoader().getResourceAsStream("test1.patch"));
assertEquals(2, pd.getBundles().size());
assertTrue(pd.getRequirements().isEmpty());
}
use of org.osgi.service.component.ComponentContext in project fabric8 by jboss-fuse.
the class ZooKeeperClusterBootstrapImpl method cleanInternal.
private BootstrapConfiguration cleanInternal(final BundleContext syscontext, final BootstrapConfiguration bootConfig, RuntimeProperties runtimeProps) throws TimeoutException {
LOGGER.debug("Begin clean fabric");
try {
Configuration zkClientCfg = null;
Configuration zkServerCfg = null;
Configuration[] configsSet = configAdmin.get().listConfigurations("(|(service.factoryPid=io.fabric8.zookeeper.server)(service.pid=io.fabric8.zookeeper))");
if (configsSet != null) {
for (Configuration cfg : configsSet) {
// let's explicitly delete client config first
if ("io.fabric8.zookeeper".equals(cfg.getPid())) {
zkClientCfg = cfg;
}
if ("io.fabric8.zookeeper.server".equals(cfg.getFactoryPid())) {
zkServerCfg = cfg;
}
}
}
File karafData = new File(data);
// Setup the listener for unregistration of {@link BootstrapConfiguration}
final CountDownLatch unregisterLatch = new CountDownLatch(1);
ServiceListener listener = new ServiceListener() {
@Override
public void serviceChanged(ServiceEvent event) {
if (event.getType() == ServiceEvent.UNREGISTERING) {
LOGGER.debug("Unregistering BootstrapConfiguration");
bootConfig.getComponentContext().getBundleContext().removeServiceListener(this);
unregisterLatch.countDown();
}
}
};
String filter = "(objectClass=" + BootstrapConfiguration.class.getName() + ")";
// FABRIC-1052: register listener using the same bundle context that is used for listeners related to SCR
bootConfig.getComponentContext().getBundleContext().addServiceListener(listener, filter);
CountDownLatch unregisterLatch2 = null;
if (syscontext.getServiceReference(CuratorComplete.class) != null) {
unregisterLatch2 = new CountDownLatch(1);
final CountDownLatch finalUnregisterLatch = unregisterLatch2;
listener = new ServiceListener() {
@Override
public void serviceChanged(ServiceEvent event) {
if (event.getType() == ServiceEvent.UNREGISTERING) {
LOGGER.debug("Unregistering CuratorComplete");
bootConfig.getComponentContext().getBundleContext().removeServiceListener(this);
finalUnregisterLatch.countDown();
}
}
};
bootConfig.getComponentContext().getBundleContext().addServiceListener(listener, "(objectClass=" + CuratorComplete.class.getName() + ")");
}
// Disable the BootstrapConfiguration component
// ENTESB-4827: disabling BootstrapConfiguration leads to deactivation of FabricService and ProfileUrlHandler
// and we have race condition if we're --cleaning after recently created fabric. previous fabric
// started FabricConfigAdminBridge which scheduled CM updates for tens of PIDs - among others,
// org.ops4j.pax.web, which leads to an attempt to reconfigure Jetty with "profile:jetty.xml"
// and if we disable ProfileUrlHandler we may loose Jetty instance
LOGGER.debug("Disable BootstrapConfiguration");
ComponentContext componentContext = bootConfig.getComponentContext();
componentContext.disableComponent(BootstrapConfiguration.COMPONENT_NAME);
if (!unregisterLatch.await(30, TimeUnit.SECONDS))
throw new TimeoutException("Timeout for unregistering BootstrapConfiguration service");
if (unregisterLatch2 != null && !unregisterLatch2.await(30, TimeUnit.SECONDS))
throw new TimeoutException("Timeout for unregistering CuratorComplete service");
// Do the cleanup
runtimeProps.clearRuntimeAttributes();
cleanConfigurations(syscontext, zkClientCfg, zkServerCfg);
cleanZookeeperDirectory(karafData);
cleanGitDirectory(karafData);
// Setup the registration listener for the new {@link BootstrapConfiguration}
final CountDownLatch registerLatch = new CountDownLatch(1);
final AtomicReference<ServiceReference<?>> sref = new AtomicReference<ServiceReference<?>>();
listener = new ServiceListener() {
@Override
public void serviceChanged(ServiceEvent event) {
if (event.getType() == ServiceEvent.REGISTERED) {
LOGGER.debug("Registered BootstrapConfiguration");
syscontext.removeServiceListener(this);
sref.set(event.getServiceReference());
registerLatch.countDown();
}
}
};
syscontext.addServiceListener(listener, "(objectClass=" + BootstrapConfiguration.class.getName() + ")");
// Enable the {@link BootstrapConfiguration} component and await the registration of the respective service
LOGGER.debug("Enable BootstrapConfiguration");
componentContext.enableComponent(BootstrapConfiguration.COMPONENT_NAME);
if (!registerLatch.await(30, TimeUnit.SECONDS))
throw new TimeoutException("Timeout for registering BootstrapConfiguration service");
return (BootstrapConfiguration) syscontext.getService(sref.get());
} catch (RuntimeException rte) {
throw rte;
} catch (TimeoutException toe) {
throw toe;
} catch (Exception ex) {
throw new FabricException("Unable to delete zookeeper configuration", ex);
} finally {
LOGGER.debug("End clean fabric");
}
}
use of org.osgi.service.component.ComponentContext in project opencast by opencast.
the class RestPublisherTest method testResourceComparatorSameNonMatchEqualEndpoint.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testResourceComparatorSameNonMatchEqualEndpoint() {
ServiceReference serviceReference = EasyMock.createNiceMock(ServiceReference.class);
EasyMock.expect(serviceReference.getProperty(SERVICE_PATH_PROPERTY)).andReturn("/events").anyTimes();
EasyMock.replay(serviceReference);
BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
EasyMock.expect(bc.getServiceReference(EasyMock.anyString())).andReturn(serviceReference).anyTimes();
EasyMock.replay(bc);
ComponentContext cc = EasyMock.createNiceMock(ComponentContext.class);
EasyMock.expect(cc.getBundleContext()).andReturn(bc).anyTimes();
EasyMock.replay(cc);
componentContext = cc;
Message message = new MessageImpl();
ExchangeImpl exchange = new ExchangeImpl();
message.setExchange(exchange);
message.put(Message.ENDPOINT_ADDRESS, "http://localhost:8080/series");
ClassResourceInfo cri1 = new ClassResourceInfo(this.getClass());
ClassResourceInfo cri2 = new ClassResourceInfo(RestPublisher.class.getClass());
OperationResourceInfo oper1 = new OperationResourceInfo(this.getClass().getMethods()[0], cri1);
OperationResourceInfo oper2 = new OperationResourceInfo(RestPublisher.class.getClass().getMethods()[0], cri2);
Assert.assertEquals(0, rc.compare(cri1, cri2, message));
Assert.assertEquals(0, rc.compare(oper1, oper2, message));
}
Aggregations