@Override public URL loadResource(String uri) { return context.getBundle().getResource(uri); }
@Override public void start(final BundleContext bundleContext) throws Exception { ProviderUtil.STARTUP_LOCK.lock(); lockingProviderUtil = true; final BundleWiring self = bundleContext.getBundle().adapt(BundleWiring.class); final List<BundleWire> required = self.getRequiredWires(LoggerContextFactory.class.getName()); for (final BundleWire wire : required) { loadProvider(bundleContext, wire.getProviderWiring()); } bundleContext.addBundleListener(this); final Bundle[] bundles = bundleContext.getBundles(); for (final Bundle bundle : bundles) { loadProvider(bundle); } unlockIfReady(); }
private boolean isOurBundle(final Bundle bundle) { return bundle.getBundleId() == bundleContext.getBundle().getBundleId(); }
@Override public String getResourceURL(long bundleID, String resource) { Bundle b = bundleContext.getBundle(bundleID); if (b == null) throw new IllegalArgumentException("Not a valid bundle ID: " + bundleID); URL res = b.getResource(resource); if (res == null) return null; else return res.toString(); } }
@Override public Class<?> loadClass(String className) { try { return context.getBundle().loadClass(className); } catch (ClassNotFoundException e) { throw new MappingException(e); } }
/** * Given a parser factory class name, instantiate that class. * * @param parserFactoryClassName A {@code String} object containing the name * of the parser factory class * @return a parserFactoryClass Object * @pre parserFactoryClassName!=null */ private Object getFactory(String parserFactoryClassName) throws FactoryConfigurationError { try { return context.getBundle().loadClass(parserFactoryClassName).newInstance(); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new FactoryConfigurationError(e); } }
@Override public void start(BundleContext bundleContext) throws Exception { Bundle bundle = bundleContext.getBundle(); pluginId = bundle.getSymbolicName(); pluginHealthService = bundleContext.getService(bundleContext.getServiceReference(PluginHealthService.class)); LoggingService loggingService = bundleContext.getService(bundleContext.getServiceReference(LoggingService.class)); Logger.initialize(loggingService); getImplementersAndRegister(bundleContext, bundle); reportErrorsToHealthService(); }
/** * Set the DOM parser service properties. * * By default, the following properties are set: * <ul> * <li>{@code SERVICE_DESCRIPTION}</li> * <li>{@code SERVICE_PID}</li> * <li>{@code PARSER_VALIDATING}</li> * <li>{@code PARSER_NAMESPACEAWARE}</li> * <ul> * * @param factory The {@code DocumentBuilderFactory} object * @param props {@code Hashtable} of service properties. */ private void setDefaultDOMProperties(DocumentBuilderFactory factory, Hashtable props, int index) { props.put(Constants.SERVICE_DESCRIPTION, DOMFACTORYDESCRIPTION); props.put(Constants.SERVICE_PID, DOMFACTORYNAME + "." + context.getBundle().getBundleId() + "." + index); setDOMProperties(factory, props); }
public PluginNamespace(BundleContext bundleContext, URL xsdResource) { if (bundleContext == null) { throw new IllegalArgumentException(String.format("context for xsd-resource %s is null", xsdResource)); } Bundle bundle = bundleContext.getBundle(); if (bundle == null) { throw new IllegalArgumentException(String.format("bundle for xsd-resource %s is null", xsdResource)); } GoPluginManifest manifest = new GoPluginManifest(bundle); this.prefix = manifest.getPluginNamespacePrefix(); this.uri = manifest.getPluginNamespaceUri(); this.xsdResource = xsdResource; }
/** * <p> * Set the SAX Parser Service Properties. By default, the following * properties are set: * <ul> * <li>{@code SERVICE_DESCRIPTION}</li> * <li>{@code SERVICE_PID}</li> * <li>{@code PARSER_VALIDATING}- instantiates a parser and queries it to * find out whether it is validating or not</li> * <li>{@code PARSER_NAMESPACEAWARE}- instantiates a parser and queries it * to find out whether it is namespace aware or not</li> * <ul> * * @param factory The {@code SAXParserFactory} object * @param props {@code Hashtable} of service properties. */ private void setDefaultSAXProperties(SAXParserFactory factory, Hashtable props, int index) { props.put(Constants.SERVICE_DESCRIPTION, SAXFACTORYDESCRIPTION); props.put(Constants.SERVICE_PID, SAXFACTORYNAME + "." + context.getBundle().getBundleId() + "." + index); setSAXProperties(factory, props); }
@Override public long getLoadClassOrigin(long bundleID, String clazz) { Bundle b = bundleContext.getBundle(bundleID); if (b == null) throw new IllegalArgumentException("Not a valid bundle ID: " + bundleID); try { Class<?> cls = b.loadClass(clazz); ClassLoader classLoader = cls.getClassLoader(); if (classLoader instanceof BundleReference) return ((BundleReference) classLoader).getBundle().getBundleId(); else return 0; } catch (ClassNotFoundException e) { } return -1; }
@Override public void start(BundleContext context) { LOG.info("Starting Dozer OSGi bundle"); setContext(context); setBundle(context.getBundle()); }
@Override public void start(BundleContext context) throws Exception { BundleContext currentContext = CORE_BUNDLE.getAndUpdate(current -> current == null ? context : current); if (currentContext == null) { String greeting = "Detected OSGi Environment (core is in bundle: " + context.getBundle() + ")"; if ("false".equalsIgnoreCase(context.getProperty(OSGI_LOADING))) { SafeOsgi.disableOSGiServiceLoading(); LOGGER.info(greeting + ": OSGi Based Service Loading Disabled Via System/Framework Property - Extensions Outside This Bundle Will Not Be Detected"); LOGGER.debug("JDK Service Loading Sees:\n\t" + stream(spliterator(ClassLoading.servicesOfType(ServiceFactory.class).iterator(), Long.MAX_VALUE, 0), false) .map(sf -> sf.getServiceType().getName()).collect(joining("\n\t"))); } else { SafeOsgi.enableOSGiServiceLoading(); LOGGER.info(greeting + ": Using OSGi Based Service Loading"); } } else { throw new IllegalStateException("Multiple bundle instances running against the same core classes: existing bundle: " + currentContext.getBundle() + " new bundle: " + context.getBundle()); } }
/** * Caches the package capabilities that are needed for a set of interface classes * * @param classes interfaces we want to track */ private void cachePackageCapabilities(Class<?> ... classes) { BundleWiring ourWiring = bundleContext.getBundle().adapt(BundleWiring.class); Set<String> packageNames = new HashSet<String>(); for (Class<?> clazz: classes) { packageNames.add(clazz.getPackage().getName()); } List<BundleCapability> ourExports = ourWiring.getCapabilities(PACKAGE_NAMESPACE); for (BundleCapability ourExport : ourExports) { String ourPkgName = (String) ourExport.getAttributes().get(PACKAGE_NAMESPACE); if (packageNames.contains(ourPkgName)) { packageCapabilities.add(ourExport); } } }
public void start(BundleContext context) { setBundleContext(context); setBundle(context.getBundle()); }
public static BundleContext bundleCtxWithHeaders(Map headerMap) { BundleContext bundleCtx = mock(BundleContext.class); Bundle bundle = bundleWithHeaders(headerMap); when(bundleCtx.getBundle()).thenReturn(bundle); return bundleCtx; }
@Before public void setUp() { initMocks(this); when(context.getServiceReference(PluginHealthService.class)).thenReturn(pluginHealthServiceReference); when(context.getServiceReference(LoggingService.class)).thenReturn(loggingServiceReference); when(context.getService(pluginHealthServiceReference)).thenReturn(pluginHealthService); when(context.getService(loggingServiceReference)).thenReturn(loggingService); when(context.getBundle()).thenReturn(bundle); when(bundle.getSymbolicName()).thenReturn(PLUGIN_ID); when(bundle.findEntries("/", "*.class", true)).thenReturn(emptyListOfClassesInBundle); activator = new DefaultGoPluginActivator(); }
/** * Called when this bundle is started so the Framework can perform the * bundle-specific activities necessary to start this bundle. This method * can be used to register services or to allocate any resources that this * bundle needs. * * <p> * This method must complete and return to its caller in a timely manner. * * <p> * This method attempts to register a SAX and DOM parser with the * Framework's service registry. * * @param context The execution context of the bundle being started. * @throws java.lang.Exception If this method throws an exception, this * bundle is marked as stopped and the Framework will remove this * bundle's listeners, unregister all services registered by this * bundle, and release all services used by this bundle. */ public void start(BundleContext context) throws Exception { this.context = context; Bundle parserBundle = context.getBundle(); // check for sax parsers registerSAXParsers(getParserFactoryClassNames(parserBundle.getResource(SAXCLASSFILE))); // check for dom parsers registerDOMParsers(getParserFactoryClassNames(parserBundle.getResource(DOMCLASSFILE))); }
public boolean isRunning() { try { return initialized && context != null && context.getBundle().getState() == Bundle.ACTIVE; } catch (IllegalStateException e) { return false; } }