org.osgi.framework.Bundle.getBundleContext()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.9k)|赞(0)|评价(0)|浏览(193)

本文整理了Java中org.osgi.framework.Bundle.getBundleContext()方法的一些代码示例,展示了Bundle.getBundleContext()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bundle.getBundleContext()方法的具体详情如下:
包路径:org.osgi.framework.Bundle
类名称:Bundle
方法名:getBundleContext

Bundle.getBundleContext介绍

[英]Returns this bundle's BundleContext. The returned BundleContext can be used by the caller to act on behalf of this bundle.

If this bundle is not in the #STARTING, #ACTIVE, or #STOPPING states or this bundle is a fragment bundle, then this bundle has no valid BundleContext. This method will return null if this bundle has no valid BundleContext.
[中]返回此捆绑包的BundleContext。调用者可以使用返回的BundleContext来代表该捆绑包。
如果此捆绑包未处于#开始#活动或#停止状态,或者此捆绑包是片段捆绑包,则此捆绑包没有有效的BundleContext。如果此捆绑包没有有效的BundleContext,则此方法将返回null。

代码示例

代码示例来源:origin: wildfly/wildfly

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);
}

代码示例来源:origin: jersey/jersey

/**
 * Returns an {@code OsgiRegistry} instance. Call this method only if sure that the application is running in OSGi
 * environment, otherwise a call to this method can lead to an {@link ClassNotFoundException}.
 *
 * @return an {@code OsgiRegistry} instance.
 */
public static synchronized OsgiRegistry getInstance() {
  if (instance == null) {
    final ClassLoader classLoader = AccessController
        .doPrivileged(ReflectionHelper.getClassLoaderPA(ReflectionHelper.class));
    if (classLoader instanceof BundleReference) {
      final BundleContext context = FrameworkUtil.getBundle(OsgiRegistry.class).getBundleContext();
      if (context != null) { // context could be still null if the current bundle has not been started
        instance = new OsgiRegistry(context);
      }
    }
  }
  return instance;
}

代码示例来源:origin: jersey/jersey

/**
 * Returns an {@code OsgiRegistry} instance. Call this method only if sure that the application is running in OSGi
 * environment, otherwise a call to this method can lead to an {@link ClassNotFoundException}.
 *
 * @return an {@code OsgiRegistry} instance.
 */
public static synchronized OsgiRegistry getInstance() {
  if (instance == null) {
    final ClassLoader classLoader = AccessController
        .doPrivileged(ReflectionHelper.getClassLoaderPA(ReflectionHelper.class));
    if (classLoader instanceof BundleReference) {
      final BundleContext context = FrameworkUtil.getBundle(OsgiRegistry.class).getBundleContext();
      if (context != null) { // context could be still null if the current bundle has not been started
        instance = new OsgiRegistry(context);
      }
    }
  }
  return instance;
}

代码示例来源:origin: hibernate/hibernate-orm

private SessionFactory getSessionFactory() {
    if ( sf == null ) {
      Bundle thisBundle = FrameworkUtil.getBundle(
        HibernateUtil.class
      );
      BundleContext context = thisBundle.getBundleContext();

      ServiceReference sr = context.getServiceReference(
        SessionFactory.class.getName()
      );
      sf = ( SessionFactory ) context.getService( sr );
    }
    return sf;
  }
}

代码示例来源:origin: hibernate/hibernate-orm

private EntityManagerFactory getEntityManagerFactory() {
    if ( emf == null ) {
      Bundle thisBundle = FrameworkUtil.getBundle(
        HibernateUtil.class
      );
      BundleContext context = thisBundle.getBundleContext();

      ServiceReference serviceReference = context.getServiceReference(
        PersistenceProvider.class.getName()
      );
      PersistenceProvider persistenceProvider = ( PersistenceProvider ) context
      .getService(
        serviceReference
      );

      emf = persistenceProvider.createEntityManagerFactory(
        "YourPersistenceUnitName",
        null
      );
    }
    return emf;
  }
}

代码示例来源:origin: rhuss/jolokia

/**
 * Log error to a logging service (if available), otherwise log to std error
 *
 * @param pMessage message to log
 * @param pThrowable an exception to log
 */
public static void logError(String pMessage, Throwable pThrowable) {
  final BundleContext bundleContext = FrameworkUtil
    .getBundle(ServiceAuthenticationHttpContext.class)
    .getBundleContext();
  logError(bundleContext, pMessage, pThrowable);
}

代码示例来源:origin: org.camunda.bpm.extension.osgi/camunda-bpm-osgi

protected BundleContext getBundleContext() {
  if (context == null) {
    context = FrameworkUtil.getBundle(getClass()).getBundleContext();
  }
  return context;
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@Override
public Framework getFramework(String felixCacheDir, boolean clean) {
  final Bundle bundle = FrameworkUtil.getBundle(Osgis.class);
  return (Framework) bundle.getBundleContext().getBundle(0);
}

代码示例来源:origin: org.opendaylight.netvirt/openstack.net-virt

public NetvirtProvider(final DataBroker dataBroker,
            final EntityOwnershipService eos,
            final boolean conntrackEnabled,
            final boolean intBridgeGenMac) {
  LOG.info("NetvirtProvider started");
  this.dataBroker = dataBroker;
  NetvirtProvider.entityOwnershipService = eos;
  this.conntrackEnabled = conntrackEnabled;
  this.intBridgeGenMac = intBridgeGenMac;
  this.bundleContext = FrameworkUtil.getBundle(NetvirtProvider.class).getBundleContext();
}

代码示例来源:origin: org.opendaylight.netvirt/neutronvpn-impl

public NeutronBgpvpnChangeListener(final DataBroker dataBroker, final NeutronvpnManager nVpnMgr,
                  final IdManagerService idManager) {
  super(Bgpvpn.class);
  this.dataBroker = dataBroker;
  nvpnManager = nVpnMgr;
  this.idManager = idManager;
  BundleContext bundleContext=FrameworkUtil.getBundle(NeutronBgpvpnChangeListener.class).getBundleContext();
  adminRDValue = bundleContext.getProperty(NeutronConstants.RD_PROPERTY_KEY);
}

代码示例来源:origin: net.roboconf/roboconf-messaging-api

/**
   * @return the bundle context, if we run in an OSGi environment, null otherwise
   */
  public BundleContext findBundleContext() {

    final Bundle bundle = FrameworkUtil.getBundle( getClass());
    return bundle != null ? bundle.getBundleContext() : null;
  }
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

private Compat() {
  Bundle bundle = FrameworkUtil.getBundle(Compat.class);
  if (bundle != null) {
    BundleContext bundleContext = bundle.getBundleContext();
    managementContextTracker = new ServiceTracker(bundleContext, ManagementContext.class, null);
    managementContextTracker.open();
  } else {
    managementContextTracker = null;
  }
}

代码示例来源:origin: de.mhus.osgi/mhu-osgi-mailosgi

public static SendQueueManager getSendQueueManager() {
  BundleContext context = FrameworkUtil.getBundle(MailUtil.class).getBundleContext();
  ServiceReference<SendQueueManager> ref = context.getServiceReference(SendQueueManager.class);
  if (ref == null) return null;
  return context.getService(ref);
}

代码示例来源:origin: org.eclipse.platform/org.eclipse.e4.core.di

private static ServiceTracker<FrameworkLog, FrameworkLog> openLogTracker() {
  try {
    ServiceTracker<FrameworkLog, FrameworkLog> st = new ServiceTracker<>(
        FrameworkUtil.getBundle(LogHelper.class).getBundleContext(), FrameworkLog.class, null);
    st.open();
    return st;
  } catch (Throwable t) {
    return null;
  }
}

代码示例来源:origin: apache/karaf

public FeaturesProcessingSerializer() {
  Bundle bundle = FrameworkUtil.getBundle(this.getClass());
  this.bundleContext = bundle == null ? null : bundle.getBundleContext();
  try {
    FEATURES_PROCESSING_CONTEXT = JAXBContext.newInstance(ObjectFactory.class);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: org.opendaylight.controller/sal.implementation

private void registerWithOSGIConsole() {
  BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
      .getBundleContext();
  bundleContext.registerService(CommandProvider.class.getName(), this,
      null);
}

代码示例来源:origin: org.opendaylight.controller/switchmanager.implementation

private void registerWithOSGIConsole() {
  BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
      .getBundleContext();
  bundleContext.registerService(CommandProvider.class.getName(), this,
      null);
}

代码示例来源:origin: org.opendaylight.snbi/southplugin

private void registerWithOSGIConsole() {
  BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
      .getBundleContext();
  bundleContext.registerService(CommandProvider.class.getName(), this,
      null);
}

代码示例来源:origin: org.opendaylight.snmp4sdn/snmp4sdn

private void registerWithOSGIConsole() {
  BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
      .getBundleContext();
  bundleContext.registerService(CommandProvider.class.getName(), this,
      null);
}

代码示例来源:origin: apache/karaf

@Override
  public String call() throws Exception {
    BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
    Bundle sysBundle = context.getBundle(0);
    return sysBundle.getSymbolicName() + "-" + sysBundle.getVersion();
  }
};

相关文章