org.osgi.framework.BundleReference Java Examples

The following examples show how to use org.osgi.framework.BundleReference. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OSGiUtil.java    From extended-objects with Apache License 2.0 6 votes vote down vote up
public static boolean isXOLoadedAsOSGiBundle() {
    if (loadedAsBundle == null) {
        ClassLoader classLoader = OSGiUtil.class.getClassLoader();
        try {
            classLoader.loadClass("org.osgi.framework.BundleReference");
        } catch (ClassNotFoundException e) {
            return false;
        }

        if (classLoader instanceof BundleReference) {
            loadedAsBundle = true;
        } else {
            loadedAsBundle = false;
        }
    }
    return loadedAsBundle;
}
 
Example #2
Source File: BundleContextSelector.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext,
                                final URI configLocation) {
    if (currentContext) {
        final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
        if (ctx != null) {
            return ctx;
        }
        return getDefault();
    }
    // it's quite possible that the provided ClassLoader may implement BundleReference which gives us a nice shortcut
    if (loader instanceof BundleReference) {
        return locateContext(((BundleReference) loader).getBundle(), configLocation);
    }
    final Class<?> callerClass = StackLocatorUtil.getCallerClass(fqcn);
    if (callerClass != null) {
        return locateContext(FrameworkUtil.getBundle(callerClass), configLocation);
    }
    final LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
    return lc == null ? getDefault() : lc;
}
 
Example #3
Source File: TestRuntime.java    From vespa with Apache License 2.0 5 votes vote down vote up
static TestRuntime get() {
    var classloader = TestRuntime.class.getClassLoader();

    if (classloader instanceof BundleReference) {
        logger.info("Loading Test runtime from TestRuntimeProvider");
        return Optional.ofNullable(TestRuntimeProvider.getTestRuntime())
                .orElseThrow(() -> new RuntimeException("Component graph not ready, retrying"));
    } else {
        logger.info("Loading Test runtime from ServiceLoader");
        ServiceLoader<TestRuntime> serviceLoader = ServiceLoader.load(TestRuntime.class, TestRuntime.class.getClassLoader());
        return serviceLoader.findFirst().orElseThrow(() -> new RuntimeException("No TestRuntime implementation found"));
    }
}
 
Example #4
Source File: PackageAdminImpl.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see org.osgi.service.packageadmin.PackageAdmin#getBundle(java.lang.Class)
 */
public Bundle getBundle(@SuppressWarnings("rawtypes") final Class clazz) {
	final ClassLoader cl = clazz.getClassLoader();

	if (cl instanceof BundleReference) {
		return ((BundleReference) cl).getBundle();
	}

	return null;
}
 
Example #5
Source File: GrammarHelper.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets the bundle symbolic name.
 * 
 * @param classe
 *          the class
 * @return the bundle symbolic name
 */
protected String getBundleSymbolicName(final Class<?> classe) {
  ClassLoader cl = classe.getClassLoader();
  if (cl instanceof BundleReference) {
    Bundle bundle = ((BundleReference) cl).getBundle();
    if (bundle != null) {
      return bundle.getSymbolicName();
    }
  }
  return null;
}
 
Example #6
Source File: Slf4jEclipseLoggerFactory.java    From sarl with Apache License 2.0 5 votes vote down vote up
private Bundle getBundle() {
	final ClassLoader cl = getClass().getClassLoader();
	if (cl instanceof BundleReference) {
		return ((BundleReference) cl).getBundle();
	}
	return null;
}
 
Example #7
Source File: BundleContextSelector.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void shutdown(final String fqcn, final ClassLoader loader, final boolean currentContext,
                     final boolean allContexts) {
    LoggerContext ctx = null;
    Bundle bundle = null;
    if (currentContext) {
        ctx = ContextAnchor.THREAD_CONTEXT.get();
        ContextAnchor.THREAD_CONTEXT.remove();
    }
    if (ctx == null && loader instanceof BundleReference) {
        bundle = ((BundleReference) loader).getBundle();
        ctx = getLoggerContext(bundle);
        removeLoggerContext(ctx);
    }
    if (ctx == null) {
        final Class<?> callerClass = StackLocatorUtil.getCallerClass(fqcn);
        if (callerClass != null) {
            bundle = FrameworkUtil.getBundle(callerClass);
            ctx = getLoggerContext(FrameworkUtil.getBundle(callerClass));
            removeLoggerContext(ctx);
        }
    }
    if (ctx == null) {
        ctx = ContextAnchor.THREAD_CONTEXT.get();
        ContextAnchor.THREAD_CONTEXT.remove();
    }
    if (ctx != null) {
        ctx.stop(DEFAULT_STOP_TIMEOUT, TimeUnit.MILLISECONDS);
    }
    if (bundle != null && allContexts) {
        final Bundle[] bundles = bundle.getBundleContext().getBundles();
        for (final Bundle bdl : bundles) {
            ctx = getLoggerContext(bdl);
            if (ctx != null) {
                ctx.stop(DEFAULT_STOP_TIMEOUT, TimeUnit.MILLISECONDS);
            }
        }
    }
}
 
Example #8
Source File: BundleContextSelector.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean hasContext(final String fqcn, final ClassLoader loader, final boolean currentContext) {
    if (currentContext && ContextAnchor.THREAD_CONTEXT.get() != null) {
        return ContextAnchor.THREAD_CONTEXT.get().isStarted();
    }
    if (loader instanceof BundleReference) {
        return hasContext(((BundleReference) loader).getBundle());
    }
    final Class<?> callerClass = StackLocatorUtil.getCallerClass(fqcn);
    if (callerClass != null) {
        return hasContext(FrameworkUtil.getBundle(callerClass));
    }
    return ContextAnchor.THREAD_CONTEXT.get() != null && ContextAnchor.THREAD_CONTEXT.get().isStarted();
}
 
Example #9
Source File: ProjectManifestFactory.java    From bonita-studio with GNU General Public License v2.0 4 votes vote down vote up
protected String engineBundleSymbolicName() {
    final BundleReference cl = (BundleReference) BusinessArchive.class.getClassLoader();
    return cl.getBundle().getSymbolicName();
}