@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(); }
static Xnio doGetOsgiService() { Bundle bundle = FrameworkUtil.getBundle(Xnio.class); BundleContext context = bundle.getBundleContext(); if (context == null) { throw new IllegalStateException("Bundle not started"); } ServiceReference<Xnio> sr = context.getServiceReference(Xnio.class); if (sr == null) { return null; } return context.getService(sr); }
private void register(final Bundle bundle) { if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.log(Level.FINEST, "checking bundle {0}", bundle.getBundleId()); } Map<String, Callable<List<Class<?>>>> map; lock.writeLock().lock(); try { map = factories.get(bundle.getBundleId()); if (map == null) { map = new ConcurrentHashMap<String, Callable<List<Class<?>>>>(); factories.put(bundle.getBundleId(), map); } } finally { lock.writeLock().unlock(); } final Enumeration<URL> e = findEntries(bundle, "META-INF/services/", "*", false); if (e != null) { while (e.hasMoreElements()) { final URL u = e.nextElement(); final String url = u.toString(); if (url.endsWith("/")) { continue; } final String factoryId = url.substring(url.lastIndexOf("/") + 1); map.put(factoryId, new BundleSpiProvidersLoader(factoryId, u, bundle)); } } }
public Void run() { map.put("id", new Long(bundle.getBundleId())); map.put("location", bundle.getLocation()); String name = bundle.getSymbolicName(); if (name != null) { map.put("name", name); } SignerProperty signer = new SignerProperty(bundle); if (signer.isBundleSigned()) { map.put("signer", signer); } return null; } });
Class<?> cls = null; Bundle[] bundles = bundle.getBundleContext().getBundles(); if (b.getState() <= Bundle.RESOLVED || b.getHeaders().get(Constants.FRAGMENT_HOST) != null) continue; cls = b.loadClass(name); break;
public static Bundle bundleWithHeaders(Map headerMap, String symbolicName) { Bundle bundle = mock(Bundle.class); Hashtable<String, String> headers = new Hashtable<>(); headers.putAll(headerMap); when(bundle.getHeaders()).thenReturn(headers); when(bundle.getSymbolicName()).thenReturn(symbolicName); return bundle; }
final List<URL> result = new LinkedList<URL>(); for (final Bundle bundle : bundleContext.getBundles()) { while (enumeration.hasMoreElements()) { final URL url = enumeration.nextElement(); final String path = url.getPath(); while (jars.hasMoreElements()) { final URL jar = jars.nextElement(); final InputStream inputStream = classLoader.getResourceAsStream(jar.getPath()); if (inputStream == null) { LOGGER.config(LocalizationMessages.OSGI_REGISTRY_ERROR_OPENING_RESOURCE_STREAM(jar)); LOGGER.log(Level.CONFIG, LocalizationMessages.OSGI_REGISTRY_ERROR_PROCESSING_RESOURCE_STREAM(jar), ex); try { inputStream.close(); } catch (final IOException e) { result.add(bundle.getResource(jarEntryName));
private void copyJarInJarLoader(File targetFile) throws IOException { InputStream is= JavaPlugin.getDefault().getBundle().getEntry(FatJarRsrcUrlBuilder.JAR_RSRC_LOADER_ZIP).openStream(); OutputStream os= new FileOutputStream(targetFile); byte[] buf= new byte[1024]; while (true) { int cnt= is.read(buf); if (cnt <= 0) break; os.write(buf, 0, cnt); } os.close(); is.close(); }
@GET @Path("/version") public Map<String,String> getVersion() { Map<String,String> versionInfo = new HashMap<>(); Dictionary<String,String> bundleHeaders = bundleContext.getBundle().getHeaders(); Enumeration<String> bundleHeaderKeyEnum = bundleHeaders.keys(); while (bundleHeaderKeyEnum.hasMoreElements()) { String bundleHeaderKey = bundleHeaderKeyEnum.nextElement(); versionInfo.put(bundleHeaderKey, bundleHeaders.get((bundleHeaderKey))); } return versionInfo; }
public Manifest getManifest() throws IOException { URL url = b.getEntry(JarFile.MANIFEST_NAME); if (url != null) { InputStream is = url.openStream(); try { return new Manifest(is); } finally { is.close(); } } return null; }
@Override public URL getResource(String name) { Enumeration<URL> resourceFiles = this.bundle.findEntries(this.path, this.filePattern, true); while (resourceFiles.hasMoreElements()) { URL resourceURL = resourceFiles.nextElement(); String resourcePath = resourceURL.getFile(); File resourceFile = new File(resourcePath); String resourceFileName = resourceFile.getName(); boolean isHostResource = bundle.getEntry(url.getPath()) != null && bundle.getEntry(url.getPath()).equals(url); if (isHostResource) { continue;
private void index() { LOGGER.debug("Indexing files for WebJar library {}-{} contained in bundle {} [{}]", name, version, bundle.getSymbolicName(), bundle.getBundleId()); Enumeration<URL> urls = bundle.findEntries(WebJarController.WEBJAR_LOCATION + name + "/" + version, "*", true); String root = "/" + WebJarController.WEBJAR_LOCATION + name + "/" + version; while (urls.hasMoreElements()) { URL url = urls.nextElement(); if (url.getPath().startsWith(root) && url.getPath().length() > root.length()) { String path = url.getPath().substring(root.length() + 1); index.put(path, url); } } }
@SuppressWarnings("unchecked") public BundleAnalysingComponentInstantiationListener(BundleContext bundleContext, String defaultInjectionSource) { this.bundleContext = bundleContext; this.defaultInjectionSource = defaultInjectionSource; Enumeration<URL> entries = bundleContext.getBundle().findEntries("/", "*.class", true); if (entries == null) { // bundle with no .class files (see PAXWICKET-305) return; } while (entries.hasMoreElements()) { String urlRepresentation = entries.nextElement().toExternalForm().replace("bundle://.+?/", "").replace('/', '.'); LOGGER.trace("Found entry {} in bundle {}", urlRepresentation, bundleContext.getBundle().getSymbolicName()); bundleResources += urlRepresentation; } }
public static Map<String,Boolean> getResources( Pattern pattern ) { Bundle bundle = FrameworkUtil.getBundle(ResourceMap.class); String base = bundle.getResource("META-INF/MANIFEST.MF").toExternalForm(); int prefix = base.indexOf("META-INF/MANIFEST.MF"); Map<String, Boolean> map = new HashMap<String, Boolean>(); for (Enumeration e = bundle.findEntries("", "*", true); e.hasMoreElements();) { String url = ((URL) e.nextElement()).toExternalForm(); url = url.substring(prefix); if (pattern.matcher(url).matches()) { map.put(url, true); } } return map; }
@Override public ClassLoader load(Integer key) throws Exception { Bundle bundle = context.getBundle(key); String bundleName = bundle.getHeaders().get("Bundle-Localisation"); if(bundleName==null) bundleName = "OSGI-INF/l10n/bundle"; String prefix = "/"+bundleName.substring(0, bundleName.lastIndexOf('/')); Enumeration<URL> entries = bundle.findEntries(prefix, "*.properties", false); List<URL> urls = Collections.emptyList(); if(entries!=null) { urls = new ArrayList<URL>(); while(entries.hasMoreElements()) { URL url = entries.nextElement(); urls.add(url); } } return new BundleClassloader(prefix,urls); }
public XBeanNamespaceHandler(String namespace, String schemaLocation, Bundle bundle, String propertiesLocation) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { URL propertiesUrl = bundle.getResource(propertiesLocation); InputStream in = propertiesUrl.openStream(); Properties properties = new Properties(); try { properties.load(in); } finally { in.close(); } this.namespace = namespace; this.schemaLocation = bundle.getEntry(schemaLocation); this.managedClasses = managedClassesFromProperties(bundle, properties); managedClassesByName = mapClasses(managedClasses); propertyEditors = propertyEditorsFromProperties(bundle, properties); this.mappingMetaData = new MappingMetaData(properties); }
public Properties getResourceAsProperties(Bundle loader, String path) throws IOException { URL resourceUrl = loader.getResource(path); if (null == resourceUrl) { return null; } InputStream in = resourceUrl.openStream(); try { Properties rv = new Properties(); rv.load(in); return rv; } finally { if (null != in) { in.close(); } } }
private void loadPredefinedPropertyMergeStrategies(BundleContext bundleContext) { Enumeration<URL> predefinedPropertyMergeStrategyEntries = bundleContext.getBundle().findEntries("META-INF/cxs/mergers", "*.json", true); if (predefinedPropertyMergeStrategyEntries == null) { return; } ArrayList<PluginType> pluginTypeArrayList = (ArrayList<PluginType>) pluginTypes.get(bundleContext.getBundle().getBundleId()); while (predefinedPropertyMergeStrategyEntries.hasMoreElements()) { URL predefinedPropertyMergeStrategyURL = predefinedPropertyMergeStrategyEntries.nextElement(); logger.debug("Found predefined property merge strategy type at " + predefinedPropertyMergeStrategyURL + ", loading... "); try { PropertyMergeStrategyType propertyMergeStrategyType = CustomObjectMapper.getObjectMapper().readValue(predefinedPropertyMergeStrategyURL, PropertyMergeStrategyType.class); propertyMergeStrategyType.setPluginId(bundleContext.getBundle().getBundleId()); propertyMergeStrategyTypeById.put(propertyMergeStrategyType.getId(), propertyMergeStrategyType); pluginTypeArrayList.add(propertyMergeStrategyType); } catch (Exception e) { logger.error("Error while loading property type definition " + predefinedPropertyMergeStrategyURL, e); } } }
protected InputStream requireDataFile(String uri) throws IOException { URL url = bundleContext.getBundle().getResource(uri); if (url == null) { throw new IllegalArgumentException("Could not find file '" + uri + "' on the bundle classpath"); } return url.openStream(); }
@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(); } }