private static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName) { if (Util.isOSGiContext()) { Bundle thisBundle = FrameworkUtil.getBundle(EntityManagerFactoryRegistry.class); BundleContext context = thisBundle.getBundleContext(); ServiceReference<?> serviceReference = context.getServiceReference(PersistenceProvider.class.getName()); PersistenceProvider persistenceProvider = (PersistenceProvider) context.getService(serviceReference); return persistenceProvider.createEntityManagerFactory(persistenceUnitName, null); } else { return Persistence.createEntityManagerFactory(persistenceUnitName); } } }
private OsgiClassLoader() { // DO NOT use ClassLoader#parent, which is typically the SystemClassLoader for most containers. Instead, // allow the ClassNotFoundException to be thrown. ClassLoaderServiceImpl will check the SystemClassLoader // later on. This is especially important for embedded OSGi containers, etc. super(null); if (Util.isOSGiContext()) { final BundleContext bundleContext = FrameworkUtil.getBundle(OsgiClassLoader.class).getBundleContext(); Bundle[] foundBundles = bundleContext.getBundles(); bundles = new ArrayList<>(foundBundles.length); for (Bundle foundBundle : foundBundles) { bundles.add(new WeakReference<>(foundBundle)); } } else { bundles = Collections.emptyList(); } }
public static ClassLoader[] getClassLoaders(ClassLoader appClassLoader) { if (isOSGiContext()) { return SecurityActions.getOSGIContextClassLoaders(appClassLoader); } else { return SecurityActions.getClassLoaders(appClassLoader); } }
private static <T> void addOsgiServices(Class<T> contract, Map<String, T> services) { if (!Util.isOSGiContext()) { return; } final BundleContext bundleContext = FrameworkUtil.getBundle(ServiceFinder.class).getBundleContext(); final ServiceTracker<T, T> serviceTracker = new ServiceTracker<T, T>(bundleContext, contract.getName(), null); serviceTracker.open(); try { final Object[] osgiServices = serviceTracker.getServices(); if (osgiServices != null) { for (Object osgiService : osgiServices) { if (services.putIfAbsent(osgiService.getClass().getName(), (T) osgiService) == null) { LOG.debugf("Loading service impl: %s", osgiService.getClass().getSimpleName()); } else { LOG.debugf("Ignoring already loaded service: %s", osgiService.getClass().getSimpleName()); } } } } catch (Exception e) { // ignore } } }
private static void markAsTransactional(boolean transactional, ConfigurationBuilder builder) { if (!transactional) { builder.transaction().transactionMode(TransactionMode.NON_TRANSACTIONAL); } else { builder.transaction() .transactionMode(TransactionMode.TRANSACTIONAL); if (!Util.isOSGiContext()) { //automatically change default TM lookup to the desired one but only outside OSGi. In OSGi we need to use GenericTransactionManagerLookup builder.transaction().transactionManagerLookup(Util.getInstance(TransactionSetup.getManagerLookup(), TestCacheManagerFactory.class.getClassLoader())); } } }