Refine search
@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; }
static BundleContext getBundleContext(ClassLoader loader) { if (!(loader instanceof BundleReference)) { throw new IllegalStateException("Not an OSGi ClassLoader"); } return ((BundleReference) loader).getBundle().getBundleContext(); }
/** * Checks the bundle processing preference and compare with the enforced preference. * * @param callable the callable */ private void checkBundlePreference(Callable<?> callable) { if (callable instanceof BundleReference) { Bundle bundle = ((BundleReference) callable).getBundle(); Preference preference = m_strategy.select(bundle); if (!isCompatible(preference)) { // Log a warning, Bundle asked for a synchronous processing, // but we will enforce parametrised processing String message = String.format( "Enforcing %s mode for Bundle %s/%s [%d] (asking for %s)", m_enforced.name(), bundle.getSymbolicName(), bundle.getVersion(), bundle.getBundleId(), preference ); m_logger.log(Logger.WARNING, message); } } }
/** {@inheritDoc} */ @Override protected E load() { context = BundleReference.class.cast(serviceType.getClassLoader()).getBundle().getBundleContext(); ServiceReference<T> ref = context.getServiceReference(serviceType); try{ if(ref == null){ return null; } T service = context.getService(ref); return doLoad(service); }catch(Exception e){ LOGGER.error("Could not load object from service. Returning null. Service called: "+serviceType+" from bundle "+context.getBundle().getSymbolicName(), e); return null; }finally{ if(ref != null) context.ungetService(ref); } }
/** * Retrieve the BeanManager from the CdiContainer service. */ static BeanManager getBeanManager() throws Exception { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader instanceof BundleReference) { Bundle bundle = ((BundleReference) classLoader).getBundle(); ServiceReference<?>[] refs = bundle.getBundleContext().getServiceReferences( "org.ops4j.pax.cdi.spi.CdiContainer", "(bundleId=" + bundle.getBundleId() + ")"); if (refs != null && refs.length == 1) { Object cdiContainer = bundle.getBundleContext().getService(refs[0]); try { Method method = cdiContainer.getClass().getMethod("getBeanManager"); return (BeanManager) method.invoke(cdiContainer); } finally { bundle.getBundleContext().ungetService(refs[0]); } } } return null; }
private static boolean checkValidLoader(ClassLoader loader) throws ClassNotFoundException { if (loader instanceof BundleReference) { Bundle b = ((BundleReference) loader).getBundle(); if (b == null || b.getState() == Bundle.INSTALLED || b.getState() == Bundle.UNINSTALLED) { return false; } // If the bundle has dynamic imports, do not try to load from it // as it could cause a resolution and lead to deadlocks if (b.getHeaders().get(Constants.DYNAMICIMPORT_PACKAGE) != null) { return false; } } return true; }
@Override public void typeChanged(TypeEvent typeEvent) { switch (typeEvent.eventType()) { case ADDED: Type type = typeEvent.type(); Class<?> rawType = getRawType(type); ClassLoader classLoader = rawType.getClassLoader(); if (classLoader instanceof BundleReference) { Bundle bundle = ((BundleReference) classLoader).getBundle(); cache.computeIfAbsent(bundle.getBundleId(), key -> new CopyOnWriteArraySet<>()).add(type); } else { LOG.warning(type + "'s class loader is not a BundleReference"); } break; case REMOVED: default: // nothing to do } } }
/** * Return a {@code Bundle} for the specified bundle class. The returned * {@code Bundle} is the bundle associated with the bundle class loader * which defined the specified class. * * @param classFromBundle A class defined by a bundle class loader. * @return A {@code Bundle} for the specified bundle class or {@code null} * if the specified class was not defined by a bundle class loader. * @since 1.5 */ public static Bundle getBundle(final Class<?> classFromBundle) { // We use doPriv since the caller may not have permission // to call getClassLoader. Object cl = AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { return classFromBundle.getClassLoader(); } }); if (cl instanceof BundleReference) { return ((BundleReference) cl).getBundle(); } return null; }
public String getAppUserId() { Class[] clss = getClassesOnCallStack(); for (Class<?> cls : clss) { ClassLoader cl = cls.getClassLoader(); if (cl instanceof BundleReference) { BundleReference ref = (BundleReference) cl; Bundle b = ref.getBundle(); return b.getLocation(); } } return null; }
static BundleContext getBundleContext(ClassLoader loader) { if (!(loader instanceof BundleReference)) { throw new IllegalStateException("Not an OSGi ClassLoader"); } return ((BundleReference) loader).getBundle().getBundleContext(); }
/** * Checks the bundle processing preference and compare with the enforced preference. * * @param callable the callable */ private void checkBundlePreference(Callable<?> callable) { if (callable instanceof BundleReference) { Bundle bundle = ((BundleReference) callable).getBundle(); Preference preference = m_strategy.select(bundle); if (!isCompatible(preference)) { // Log a warning, Bundle asked for a synchronous processing, // but we will enforce parametrised processing String message = String.format( "Enforcing %s mode for Bundle %s/%s [%d] (asking for %s)", m_enforced.name(), bundle.getSymbolicName(), bundle.getVersion(), bundle.getBundleId(), preference ); m_logger.log(Logger.WARNING, message); } } }
/** {@inheritDoc} */ @Override protected E load() { context = BundleReference.class.cast(serviceType.getClassLoader()).getBundle().getBundleContext(); ServiceReference<T> ref = context.getServiceReference(serviceType); try{ if(ref == null){ return null; } T service = context.getService(ref); return doLoad(service); }catch(Exception e){ LOGGER.error("Could not load object from service. Returning null. Service called: "+serviceType+" from bundle "+context.getBundle().getSymbolicName(), e); return null; }finally{ if(ref != null) context.ungetService(ref); } }
private static boolean checkValidLoader(ClassLoader loader) throws ClassNotFoundException { if (loader instanceof BundleReference) { Bundle b = ((BundleReference) loader).getBundle(); if (b == null || b.getState() == Bundle.INSTALLED || b.getState() == Bundle.UNINSTALLED) { return false; } // If the bundle has dynamic imports, do not try to load from it // as it could cause a resolution and lead to deadlocks if (b.getHeaders().get(Constants.DYNAMICIMPORT_PACKAGE) != null) { return false; } } return true; }
@Override @SuppressWarnings("unchecked") public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) { final Map settings = generateSettings( properties ); // OSGi ClassLoaders must implement BundleReference settings.put( org.hibernate.cfg.AvailableSettings.SCANNER, new OsgiScanner( ( (BundleReference) info.getClassLoader() ).getBundle() ) ); osgiClassLoader.addClassLoader( info.getClassLoader() ); return Bootstrap.getEntityManagerFactoryBuilder( info, settings, new OSGiClassLoaderServiceImpl( osgiClassLoader, osgiServiceUtil ) ).build(); }
/** Obtaining BundleContext which belongs to osgi-javaee-base * @param clazz some class belongs to osgi-javaee-base * @return BundleContext which belongs to osgi-javaee-base */ private BundleContext getBundleContext(Class<?> clazz) { return BundleReference.class.cast(clazz.getClassLoader()) .getBundle().getBundleContext(); } }
@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; }
/** * Returns the bundle associated with this plug-in. * * @return the associated bundle * @since 3.0 */ public final Bundle getBundle() { if (bundle != null) return bundle; ClassLoader cl = getClass().getClassLoader(); if (cl instanceof BundleReference) return ((BundleReference) cl).getBundle(); return null; } }
private BundleContext injectBundleContext(Class<?> requestingClass, Field field) { ClassLoader classLoader = requestingClass.getClassLoader(); if (classLoader instanceof BundleReference) { BundleReference bundleReference = (BundleReference) classLoader; Bundle bundle = bundleReference.getBundle(); return bundle.getBundleContext(); } else { throw new IllegalStateException("the classloader does not provide a BundleReference"); } }
private long findSafe(Bundle bundle, String className) { try { Class<?> clazz = bundle.loadClass(className); if (clazz.getClassLoader() == null) { return (LOADED_FROM_BOOTCLASSLOADER); } else if (clazz.getClassLoader() instanceof BundleReference) { Bundle p = ((BundleReference) clazz.getClassLoader()).getBundle(); return p.getBundleId(); } else { return (LOADED_FROM_OTHER_CLASSLOADER); } } catch (ClassNotFoundException ex) { return (CLAZZ_NOT_FOUND); } } }
public Bundle getBudleByClass(Class cls) { ClassLoader cl = cls.getClassLoader(); if (cl instanceof BundleReference) { BundleReference ref = (BundleReference) cl; Bundle b = ref.getBundle(); return b; } return null; }