Search in sources :

Example 11 with Bundle

use of org.osgi.framework.Bundle in project atlas by alibaba.

the class DelegateClassLoader method loadFromInstalledBundles.

static Class<?> loadFromInstalledBundles(String className, boolean safe) throws ClassNotFoundException {
    Class<?> clazz = null;
    List<Bundle> bundles = Framework.getBundles();
    String bundleName = AtlasBundleInfoManager.instance().getBundleForComponet(className);
    BundleImpl bundle = (BundleImpl) Atlas.getInstance().getBundle(bundleName);
    if (bundle != null) {
        if (!Framework.isDeubgMode()) {
            bundle.optDexFile();
        }
        bundle.startBundle();
        ClassLoader classloader = bundle.getClassLoader();
        try {
            if (classloader != null) {
                clazz = classloader.loadClass(className);
                if (clazz != null) {
                    return clazz;
                }
            }
        } catch (ClassNotFoundException e) {
        }
        if ((Thread.currentThread().getId() == Looper.getMainLooper().getThread().getId())) {
            Throwable ex = new Throwable();
            ex.fillInStackTrace();
            Log.e("MainThreadFindClass", String.format("can not findClass %s from %s in UI thread ", className, bundle));
            ex.printStackTrace();
        }
        if (safe) {
            ComponentName component = new ComponentName(RuntimeVariables.androidApplication.getPackageName(), className);
            if (isProvider(component)) {
                return Atlas.class.getClassLoader().loadClass("android.taobao.atlas.util.FakeProvider");
            } else if (isReceiver(component)) {
                return Atlas.class.getClassLoader().loadClass("android.taobao.atlas.util.FakeReceiver");
            } else {
                throw new ClassNotFoundException("Can't find class " + className + " in BundleClassLoader: " + bundle.getLocation() + " [" + (bundles == null ? 0 : bundles.size()) + "]" + (classloader == null ? "classloader is null" : "classloader not null") + " packageversion " + getPackageVersion() + FileUtils.getAvailableDisk());
            }
        }
    }
    /*
         * The layout may uses the designated classes, in this case,
         * the class not exist in components, we need search all bundles
         * to find the class.
         */
    if (bundles != null && !bundles.isEmpty()) {
        for (Bundle b : Framework.getBundles()) {
            bundle = (BundleImpl) b;
            if (bundle.getArchive().isDexOpted()) {
                ClassLoader classloader = bundle.getClassLoader();
                try {
                    if (classloader != null) {
                        clazz = classloader.loadClass(className);
                        if (clazz != null) {
                            return clazz;
                        }
                    }
                } catch (ClassNotFoundException e) {
                }
            }
        }
    }
    return clazz;
}
Also used : BundleImpl(android.taobao.atlas.framework.BundleImpl) Atlas(android.taobao.atlas.framework.Atlas) Bundle(org.osgi.framework.Bundle) ComponentName(android.content.ComponentName)

Example 12 with Bundle

use of org.osgi.framework.Bundle in project atlas by alibaba.

the class BundlePackageManager method queryIntentService.

/**
     * 新增service中进行优先filter
     * @param intent
     * @param resolvedType
     * @param flags
     * @param userId
     * @return
     */
public static List<ResolveInfo> queryIntentService(Intent intent, String resolvedType, int flags, int userId) {
    List<Bundle> bundles = Atlas.getInstance().getBundles();
    for (Bundle bundle : bundles) {
        BundleImpl impl = (BundleImpl) bundle;
        if (impl.isUpdated() && impl.getPackageManager() != null) {
            ResolveInfo info = impl.getPackageManager().wrapperServiceIntentIfNeed(intent);
            if (info != null) {
                List<ResolveInfo> rf = new ArrayList<ResolveInfo>(1);
                rf.add(info);
                return rf;
            }
        }
    }
    return null;
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) BundleImpl(android.taobao.atlas.framework.BundleImpl) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList)

Example 13 with Bundle

use of org.osgi.framework.Bundle in project generator by mybatis.

the class MyBatisGeneratorClasspathResolver method getGeneratorPath.

public static IPath getGeneratorPath() {
    //$NON-NLS-1$
    Bundle bundle = Platform.getBundle("org.mybatis.generator.core");
    if (bundle == null) {
        return null;
    }
    try {
        //$NON-NLS-1$
        URL devPath = bundle.getEntry("bin/");
        File fullPath;
        if (devPath != null) {
            devPath = FileLocator.toFileURL(devPath);
            fullPath = new File(devPath.toURI());
        } else {
            fullPath = FileLocator.getBundleFile(bundle);
        }
        return new Path(fullPath.getAbsolutePath());
    } catch (IOException e) {
        return null;
    } catch (URISyntaxException e) {
        return null;
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) Bundle(org.osgi.framework.Bundle) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URL(java.net.URL)

Example 14 with Bundle

use of org.osgi.framework.Bundle in project generator by mybatis.

the class MyBatisContainer method getGeneratorEntry.

private IClasspathEntry getGeneratorEntry() {
    //$NON-NLS-1$
    Bundle bundle = Platform.getBundle("org.mybatis.generator.core");
    if (bundle == null) {
        return null;
    }
    try {
        URL devPath = bundle.getEntry("bin/");
        File fullPath;
        IClasspathEntry answer;
        fullPath = FileLocator.getBundleFile(bundle);
        if (devPath != null) {
            // we are in development mode
            devPath = FileLocator.toFileURL(devPath);
            fullPath = new File(devPath.toURI());
            answer = JavaCore.newLibraryEntry(new Path(fullPath.getAbsolutePath()), null, new Path("/"));
        } else {
            answer = JavaCore.newLibraryEntry(new Path(FileLocator.getBundleFile(bundle).getAbsolutePath()), null, new Path("/"));
            fullPath = FileLocator.getBundleFile(bundle);
        }
        return answer;
    } catch (IOException e) {
        return null;
    } catch (URISyntaxException e) {
        return null;
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Bundle(org.osgi.framework.Bundle) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URL(java.net.URL)

Example 15 with Bundle

use of org.osgi.framework.Bundle in project jersey by jersey.

the class ReflectionHelper method getResourceAsStream.

/**
     * Lookup resource by given name. If OSGi runtime is detected and the originClass parameter is not null,
     * an attempt will be made to get the resource input stream via OSGi API from the bundle where originClass is included.
     * Otherwise (non OSGi environment) or if OSGi fails to provide the input stream, the return value
     * will be taken from the provided loader getResourceAsStream method.
     *
     * @param loader      class loader where to lookup the resource in non-OSGi environment or if OSGi means fail.
     * @param originClass if not null, and OSGi environment is detected, the resource will be taken from the bundle including
     *                    the originClass type.
     * @param name        filename of the desired resource.
     * @return an input stream corresponding to the required resource or null if the resource could not be found.
     */
public static InputStream getResourceAsStream(final ClassLoader loader, final Class<?> originClass, final String name) {
    try {
        if (bundleReferenceClass != null && originClass != null && bundleReferenceClass.isInstance(ReflectionHelper.class.getClassLoader())) {
            final Bundle bundle = FrameworkUtil.getBundle(originClass);
            final URL resourceUrl = (bundle != null) ? bundle.getEntry(name) : null;
            if (resourceUrl != null) {
                return resourceUrl.openStream();
            }
        }
    } catch (final IOException ex) {
    // ignore
    }
    return loader.getResourceAsStream(name);
}
Also used : Bundle(org.osgi.framework.Bundle) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

Bundle (org.osgi.framework.Bundle)2527 Test (org.junit.Test)674 URL (java.net.URL)389 BundleContext (org.osgi.framework.BundleContext)319 ArrayList (java.util.ArrayList)299 File (java.io.File)298 IOException (java.io.IOException)281 BundleException (org.osgi.framework.BundleException)273 HashMap (java.util.HashMap)155 ServiceReference (org.osgi.framework.ServiceReference)152 Hashtable (java.util.Hashtable)131 Path (org.eclipse.core.runtime.Path)126 HashSet (java.util.HashSet)98 InputStream (java.io.InputStream)94 List (java.util.List)87 IStatus (org.eclipse.core.runtime.IStatus)86 Status (org.eclipse.core.runtime.Status)82 Map (java.util.Map)74 BundleWiring (org.osgi.framework.wiring.BundleWiring)74 Version (org.osgi.framework.Version)73