Java Code Examples for org.osgi.framework.BundleContext
The following code examples are extracted from open source projects. You can click to
vote up the examples that are useful to you.
Example 1
From project arkadiko, under directory /src/com/liferay/arkadiko/.
Source file: AKBeanPostProcessor.java

/** * Post process after initialisation. * @param bean the bean * @param beanId the bean id * @return the object * @throws BeansException the beans exception */ public Object postProcessAfterInitialization(Object bean,String beanId) throws BeansException { List<Class<?>> interfaces=AKIntrospector.getInterfacesAsList(bean); if (ignoreBean(bean,beanId,interfaces)) { return bean; } Framework framework=getFramework(); BundleContext bundleContext=framework.getBundleContext(); registerService(bundleContext,bean,beanId,interfaces); return createProxy(bundleContext,bean,beanId,interfaces); }
Example 2
From project arquillian-container-osgi, under directory /testenricher-osgi/src/main/java/org/jboss/arquillian/testenricher/osgi/.
Source file: OSGiTestEnricher.java

private void injectBundleContext(Object testCase,Field field){ try { BundleContext context=getBundleContext(); log.fine("Injecting bundle context: " + context); field.set(testCase,context); } catch ( IllegalAccessException ex) { throw new IllegalStateException("Cannot inject BundleContext",ex); } }
Example 3
From project arquillian_deprecated, under directory /testenrichers/osgi/src/main/java/org/jboss/arquillian/testenricher/osgi/.
Source file: OSGiTestEnricher.java

private BundleContext getSystemBundleContext(){ BundleContext bundleContext=bundleContextInst.get(); if (bundleContext == null) bundleContext=getBundleContextFromHolder(); bundleContext=bundleContext.getBundle(0).getBundleContext(); return bundleContext; }
Example 4
From project camel-osgi, under directory /component/src/test/java/org/apache/camel/osgi/service/.
Source file: OsgiDefaultConsumerTest.java

@Test public void testDoStart() throws Exception { BundleContext bundleContext=mock(BundleContext.class); OsgiDefaultEndpoint endpoint=mock(OsgiDefaultEndpoint.class); when(endpoint.getApplicationBundleContext()).thenReturn(bundleContext); Service processor=mock(Service.class,withSettings().extraInterfaces(Processor.class)); Map<String,Object> props=Collections.singletonMap("key",(Object)"value"); OsgiDefaultConsumer consumer=new OsgiDefaultConsumer(endpoint,(Processor)processor,props); ServiceHelper.startService(consumer); verify(processor).start(); verify(bundleContext).registerService(eq(OsgiComponent.OBJECT_CLASS),same(consumer),eq(new Hashtable<String,Object>(props))); }
Example 5
From project CIShell, under directory /clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/.
Source file: Activator.java

@Override public void serviceChanged(ServiceEvent event){ BundleContext bundleContext=event.getServiceReference().getBundle().getBundleContext(); LogReaderService logReaderService=(LogReaderService)bundleContext.getService(event.getServiceReference()); if (logReaderService != null) { if (event.getType() == ServiceEvent.REGISTERED) { Activator.this.logReaders.add(logReaderService); logReaderService.addLogListener(Activator.this.fileLogger); } else if (event.getType() == ServiceEvent.UNREGISTERING) { logReaderService.removeLogListener(Activator.this.fileLogger); Activator.this.logReaders.remove(logReaderService); } } }
Example 6
From project arquillian-showcase, under directory /osgi/src/test/java/com/acme/osgi/.
Source file: SimpleOSGiServiceTestCase.java

@Test public void testBundleInjection() throws Exception { assertNotNull("Bundle injected",bundle); assertEquals("Bundle RESOLVED",Bundle.RESOLVED,bundle.getState()); bundle.start(); assertEquals("Bundle ACTIVE",Bundle.ACTIVE,bundle.getState()); BundleContext context=bundle.getBundleContext(); assertNotNull("BundleContext available",context); ServiceReference sref=context.getServiceReference(SimpleService.class.getName()); assertNotNull("ServiceReference not null",sref); SimpleService service=(SimpleService)context.getService(sref); assertNotNull("Service not null",service); int sum=service.sum(1,2,3); assertEquals(6,sum); bundle.stop(); assertEquals("Bundle RESOLVED",Bundle.RESOLVED,bundle.getState()); }
Example 7
From project blueprint-namespaces, under directory /blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/.
Source file: BlueprintAnnotationScannerImpl.java

public URL createBlueprintModel(Bundle bundle){ Tblueprint tblueprint=generateBlueprintModel(bundle); if (tblueprint != null) { BundleContext ctx=getBlueprintExtenderContext(); if (ctx == null) { ctx=bundle.getBundleContext(); } File dir=ctx.getDataFile(bundle.getSymbolicName() + "/" + bundle.getVersion()+ "/"); if (!dir.exists()) { dir.mkdirs(); } String blueprintPath=cachePath(bundle,"annotation-generated-blueprint.xml"); File file=ctx.getDataFile(blueprintPath); if (!file.exists()) { try { file.createNewFile(); } catch ( IOException e) { e.printStackTrace(); } } try { marshallOBRModel(tblueprint,file); } catch ( JAXBException e) { e.printStackTrace(); } try { return file.toURL(); } catch ( MalformedURLException e) { e.printStackTrace(); } } return null; }
Example 8
From project bndtools, under directory /bndtools.core/src/bndtools/builder/.
Source file: BuildListeners.java

public BuildListeners(){ IConfigurationElement[] elements=Platform.getExtensionRegistry().getConfigurationElementsFor(Plugin.PLUGIN_ID,"buildListeners"); listeners=new ArrayList<BuildListener>(elements.length); for ( IConfigurationElement elem : elements) { try { BuildListener listener=(BuildListener)elem.createExecutableExtension("class"); listeners.add(listener); } catch ( Exception e) { logger.logError("Unable to instantiate build listener: " + elem.getAttribute("name"),e); } } BundleContext context=FrameworkUtil.getBundle(BuildListeners.class).getBundleContext(); listenerTracker=new ServiceTracker(context,BuildListener.class.getName(),null); listenerTracker.open(); }
Example 9
From project cellar, under directory /core/src/main/java/net/cellar/core/control/.
Source file: ManageHandlersCommandHandler.java

/** * Returns a map containing all managed {@code EventHandler}s and their status. * @param command * @return */ @Override public ManageHandlersResult execute(ManageHandlersCommand command){ ManageHandlersResult result=new ManageHandlersResult(command.getId()); BundleContext bundleContext=((BundleReference)getClass().getClassLoader()).getBundle().getBundleContext(); ServiceReference[] references=new ServiceReference[0]; try { references=bundleContext.getServiceReferences(EventHandler.class.getName(),EventHandler.MANAGED_FILTER); if (references != null && references.length > 0) { for ( ServiceReference ref : references) { EventHandler handler=(EventHandler)bundleContext.getService(ref); if (command.getHandlesName() != null && command.getHandlesName().equals(handler.getClass().getName())) { if (command.getStatus()) { handler.getSwitch().turnOn(); } else handler.getSwitch().turnOff(); } result.getHandlers().put(handler.getClass().getName(),handler.getSwitch().getStatus().name()); } } } catch ( InvalidSyntaxException e) { logger.error("Syntax error looking up service {} using filter {}",EventHandler.class.getName(),EventHandler.MANAGED_FILTER); } finally { if (references != null) { for ( ServiceReference ref : references) { bundleContext.ungetService(ref); } } } return result; }
Example 10
From project Cilia_1, under directory /framework/runtime/src/main/java/fr/liglab/adele/cilia/runtime/impl/.
Source file: SchedulerHandler.java

private void retreiveAdminService(){ ServiceReference[] refs=null; BundleContext context=getInstanceManager().getContext(); try { refs=context.getServiceReferences(AdminData.class.getName(),"(chain.name=" + (String)m_dictionary.get(ConstModel.PROPERTY_CHAIN_ID) + ")"); } catch ( InvalidSyntaxException e) { logger.warn("Admin data service lookup unrecoverable error"); refs=null; } if (refs != null) m_refData=refs[0]; else { logger.warn("Admin data service not found"); return; } }
Example 11
From project agile, under directory /agile-core/src/main/java/org/headsupdev/agile/core/.
Source file: CoreActivator.java

/** * Called whenever the OSGi framework starts our bundle */ public void start(BundleContext bc) throws Exception { HeadsUpLoggerManager.getInstance().setConfiguration(Manager.getStorageInstance().getGlobalConfiguration()); DefaultManager manager=new DefaultManager(); manager.load(); Manager.setInstance(manager); tracker=new AppTracker(bc); tracker.open(); services=new ServTracker(bc); services.open(); }
Example 12
From project al.gov.asp.smc.osgi.vaadin.siemens, under directory /com.siemens.ct.osgi.vaadin.pm.bundleview/src/com/siemens/ct/osgi/vaadin/pm/bundleview/.
Source file: Activator.java

@Override public void start(BundleContext context) throws Exception { Bundle[] allBundles=context.getBundles(); ArrayList<Bundle> bundleList=new ArrayList<Bundle>(); for ( Bundle bundle : allBundles) { String symbolicName=bundle.getSymbolicName(); if ((symbolicName.startsWith("com.siemens.ct.osgi.vaadin.pm") || symbolicName.startsWith("al.gov.asp.smc") || symbolicName.startsWith("com.siemens.ct.pm.model.")) && !((symbolicName.contains("main") || symbolicName.contains("theme") || symbolicName.contains("bundleview")|| symbolicName.contains("logback")))) { bundleList.add(bundle); } } bundles=bundleList.toArray(new Bundle[]{}); }
Example 13
From project ant4eclipse, under directory /org.ant4eclipse.integrationtest.pde/workspace/example-bundle/src/example_bundle/.
Source file: Activator.java

/** * {@inheritDoc} */ public void start(BundleContext context) throws Exception { tracker=new ServiceTracker(context,PreferencesService.class.getName(),null); tracker.open(); service=(PreferencesService)tracker.getService(); Preferences preferences=service.getSystemPreferences(); preferences.put(COLOR,"lavender"); System.out.println("My favourite color is: " + preferences.get(COLOR,"")); }
Example 14
From project aws-toolkit-for-eclipse, under directory /com.amazonaws.eclipse.core/src/com/amazonaws/eclipse/core/.
Source file: AwsToolkitCore.java

@Override public void start(BundleContext context) throws Exception { super.start(context); plugin=this; proxyServiceTracker=new ServiceTracker(context,IProxyService.class.getName(),null); proxyServiceTracker.open(); accountInfoMonitor=new AccountInfoMonitor(); bootstrapAccountPreferences(); getPreferenceStore().addPropertyChangeListener(accountInfoMonitor); }
Example 15
From project bel-editor, under directory /org.openbel.editor.ui/src/org/openbel/editor/ui/.
Source file: Activator.java

/** * {@inheritDoc} */ @Override public void start(BundleContext ctxt) throws Exception { super.start(ctxt); this.listener=new Listener(); plugin=this; runAsync(new Runnable(){ @Override public void run(){ while (workbench == null) { yield(); workbench=getWorkbench(); } IWorkbenchWindow win=workbench.getActiveWorkbenchWindow(); win=workbench.getActiveWorkbenchWindow(); ISelectionService selsvc=win.getSelectionService(); selsvc.addSelectionListener(listener); IWorkspace ws=getWorkspace(); ws.addResourceChangeListener(listener); } } ); IPreferenceStore ps=getPreferenceStore(); String bfhome=ps.getString(BF_PREF_KEY); if (noLength(bfhome)) { bfhome=ENV_BELFRAMEWORK_HOME; } if (validateFramework(bfhome) != FileState.OK) { } else { ps.setValue(BF_PREF_KEY,bfhome); reloadResources(); } }
Example 16
From project BHT-FPA, under directory /mailer-common/de.bht.fpa.mail.common/src/de/bht/fpa/mail/s000000/common/osgi/.
Source file: OSGiServiceHelper.java

/** * This method returns one service instance, or <code>null</code> if no service is currently registered. * @param bundleContext the {@link BundleContext} of your plugin. You can obtain aninstance of the {@link BundleContext} through your Activator. * @param clazz The {@link Class} of the service. Typically an Java Interface * @return service instance of the given {@link Class} type, or<code>null</code> */ @SuppressWarnings({"rawtypes","unchecked"}) public static <T>T getService(BundleContext bundleContext,Class<T> clazz){ ServiceReference serviceReference=bundleContext.getServiceReference(clazz.getName()); if (serviceReference == null) { return null; } Object service=bundleContext.getService(serviceReference); if (service == null) { return null; } return (T)service; }
Example 17
From project Bio-PEPA, under directory /uk.ac.ed.inf.biopepa.ui/src/uk/ac/ed/inf/biopepa/ui/.
Source file: BioPEPAPlugin.java

public void start(BundleContext context) throws Exception { super.start(context); plugin=this; manager=new BioPEPAManager(); colourManager=new ColourManager(); }
Example 18
From project Bioclipse.clustering, under directory /src/net/bioclipse/chem/clustering/.
Source file: Activator.java

public void start(BundleContext context) throws Exception { super.start(context); plugin=this; javaFinderTracker=new ServiceTracker(context,IJavaClusteringManager.class.getName(),null); javaFinderTracker.open(); jsFinderTracker=new ServiceTracker(context,IJavaScriptClusteringManager.class.getName(),null); jsFinderTracker.open(); }
Example 19
From project bioclipse.opentox, under directory /plugins/net.bioclipse.opentox/src/net/bioclipse/opentox/.
Source file: Activator.java

public void start(BundleContext context) throws Exception { super.start(context); plugin=this; javaFinderTracker=new ServiceTracker(context,IJavaOpentoxManager.class.getName(),null); javaFinderTracker.open(); jsFinderTracker=new ServiceTracker(context,IJavaScriptOpentoxManager.class.getName(),null); jsFinderTracker.open(); openToxServices=new ArrayList<OpenToxService>(); logger.debug("Initializing OpenTox services"); List<OpenToxService> prefss=ServiceReader.readServicesFromPreferences(); openToxServices.addAll(prefss); logger.debug("Read " + prefss.size() + " services from prefs"); List<OpenToxService> epservices=ServiceReader.readServicesFromExtensionPoints(); for ( OpenToxService eps : epservices) { if (!openToxServices.contains(eps)) { openToxServices.add(eps); logger.debug("Added new service from EP: " + eps); } } Preferences preferences=ConfigurationScope.INSTANCE.getNode(OpenToxConstants.PLUGIN_ID); List<String[]> toPrefs=ServicesPreferencePage.convertPreferenceStringToArraylist(preferences.get(OpenToxConstants.SERVICES,"n/a")); for ( OpenToxService eps : epservices) { String[] entry=new String[3]; entry[0]=eps.getName(); entry[1]=eps.getService(); entry[2]=eps.getServiceSPARQL(); if (!listContains(toPrefs,entry)) toPrefs.add(entry); } String toPrefsString=ServicesPreferencePage.convertToPreferenceString(toPrefs); preferences.put(OpenToxConstants.SERVICES,toPrefsString); try { preferences.flush(); } catch ( BackingStoreException e) { logger.error(e.getMessage()); e.printStackTrace(); } logger.debug("Saved the serialized services prefs string: " + toPrefsString); logger.debug("OpenTox initialization ended"); }
Example 20
From project bioclipse.seneca, under directory /plugins/net.bioclipse.seneca/src/net/bioclipse/seneca/.
Source file: Activator.java

public void start(BundleContext context) throws Exception { super.start(context); plugin=this; finderTracker=new ServiceTracker(context,IJavaSenecaManager.class.getName(),null); finderTracker.open(); jsFinderTracker=new ServiceTracker(context,IJavaScriptSenecaManager.class.getName(),null); jsFinderTracker.open(); getJudgeExtensions(); }
Example 21
From project bioclipse.speclipse, under directory /plugins/net.bioclipse.bibtex/src/net/bioclipse/bibtex/.
Source file: Activator.java

public void start(BundleContext context) throws Exception { super.start(context); plugin=this; finderTracker=new ServiceTracker(context,IJavaBibtexManager.class.getName(),null); finderTracker.open(); jsFinderTracker=new ServiceTracker(context,IJavaScriptBibtexManager.class.getName(),null); jsFinderTracker.open(); }
Example 22
From project bioclipse.vscreen, under directory /net.bioclipse.vscreen/src/net/bioclipse/vscreen/.
Source file: Activator.java

public void start(BundleContext context) throws Exception { super.start(context); plugin=this; javaFinderTracker=new ServiceTracker(context,IJavaVScreenManager.class.getName(),null); javaFinderTracker.open(); jsFinderTracker=new ServiceTracker(context,IJavaScriptVScreenManager.class.getName(),null); jsFinderTracker.open(); }
Example 23
From project bundlemaker, under directory /jedit-example/bm.demo.jedit.launcher/src/bm/demo/jedit/launcher/.
Source file: Activator.java

public void start(BundleContext bundleContext) throws Exception { System.out.println("Register jeditresource URL handler..."); Dictionary<String,String> properties=new Hashtable<String,String>(); properties.put(URLConstants.URL_HANDLER_PROTOCOL,"jeditresource"); bundleContext.registerService(URLStreamHandlerService.class.getName(),new JEditResourceHandlerService(),properties); System.out.println("Starting JEdit..."); jEdit.main(new String[]{"-nobackground","-noserver"}); }
Example 24
public void start(BundleContext context) throws Exception { super.start(context); plugin=this; startClojureCode(context); if (System.getProperty("ccw.autostartnrepl") != null) { startREPLServer(); } this.natureAdapter.start(); }
Example 25
From project ceylon-ide-eclipse, under directory /plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/ui/.
Source file: CeylonPlugin.java

@Override public void start(BundleContext context) throws Exception { String ceylonRepositoryProperty=System.getProperty("ceylon.repo",""); ceylonRepository=getCeylonPluginRepository(ceylonRepositoryProperty); super.start(context); this.bundleContext=context; addResourceFilterPreference(); registerProjectOpenCloseListener(); }
Example 26
From project cilia-workbench, under directory /cilia-workbench-designer/src/fr/liglab/adele/cilia/workbench/designer/.
Source file: Activator.java

public void start(BundleContext context) throws Exception { super.start(context); plugin=this; SpecRepoService.getInstance(); JarRepoService.getInstance(); AbstractCompositionsRepoService.getInstance(); DSCiliaRepoService.getInstance(); }