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