private static ClassLoader internalGetFelixBundleClassLoader(Bundle bundle) { // assume felix: try { // now get the current module from the bundle. // and return the private field m_classLoader of ModuleImpl if (Felix_BundleImpl_m_modules_field == null) { Felix_BundleImpl_m_modules_field = bundle .getClass() .getClassLoader() .loadClass("org.apache.felix.framework.BundleImpl") .getDeclaredField("m_modules"); Felix_BundleImpl_m_modules_field.setAccessible(true); } Object[] moduleArray = (Object[]) Felix_BundleImpl_m_modules_field.get(bundle); Object currentModuleImpl = moduleArray[moduleArray.length - 1]; if (Felix_ModuleImpl_m_classLoader_field == null && currentModuleImpl != null) { Felix_ModuleImpl_m_classLoader_field = bundle .getClass() .getClassLoader() .loadClass("org.apache.felix.framework.ModuleImpl") .getDeclaredField("m_classLoader"); Felix_ModuleImpl_m_classLoader_field.setAccessible(true); } // first make sure that the classloader is ready: // the m_classLoader field must be initialized by the // ModuleImpl.getClassLoader() private method. ClassLoader cl = (ClassLoader) Felix_ModuleImpl_m_classLoader_field.get(currentModuleImpl); if (cl == null) { // looks like it was not ready: // the m_classLoader field must be initialized by the // ModuleImpl.getClassLoader() private method. // this call will do that. bundle.loadClass("java.lang.Object"); cl = (ClassLoader) Felix_ModuleImpl_m_classLoader_field.get(currentModuleImpl); // System.err.println("Got the bundle class loader of felix_: " // + cl); return cl; } else { // System.err.println("Got the bundle class loader of felix: " + // cl); return cl; } } catch (Throwable t) { t.printStackTrace(); } return null; }
private static ClassLoader internalGetEquinoxBundleClassLoader(Bundle bundle) { // assume equinox: try { if (Equinox_BundleHost_getBundleLoader_method == null) { Equinox_BundleHost_getBundleLoader_method = bundle .getClass() .getClassLoader() .loadClass("org.eclipse.osgi.framework.internal.core.BundleHost") .getDeclaredMethod("getBundleLoader", new Class[] {}); Equinox_BundleHost_getBundleLoader_method.setAccessible(true); } Object bundleLoader = Equinox_BundleHost_getBundleLoader_method.invoke(bundle, new Object[] {}); if (Equinox_BundleLoader_createClassLoader_method == null && bundleLoader != null) { Equinox_BundleLoader_createClassLoader_method = bundleLoader .getClass() .getClassLoader() .loadClass("org.eclipse.osgi.internal.loader.BundleLoader") .getDeclaredMethod("createClassLoader", new Class[] {}); Equinox_BundleLoader_createClassLoader_method.setAccessible(true); } return (ClassLoader) Equinox_BundleLoader_createClassLoader_method.invoke(bundleLoader, new Object[] {}); } catch (Throwable t) { t.printStackTrace(); } return null; }
/** * Returns the underlying BundleContext for the given Bundle. This uses reflection and highly * dependent of the OSGi implementation. Should not be used if OSGi 4.1 is being used. * * @param bundle OSGi bundle * @return the bundle context for this bundle */ public static BundleContext getBundleContext(final Bundle bundle) { if (bundle == null) return null; // try Equinox getContext Method meth = ReflectionUtils.findMethod(bundle.getClass(), "getContext", new Class[0]); // fallback to getBundleContext (OSGi 4.1) if (meth == null) meth = ReflectionUtils.findMethod(bundle.getClass(), "getBundleContext", new Class[0]); final Method m = meth; if (meth != null) { ReflectionUtils.makeAccessible(meth); return (BundleContext) ReflectionUtils.invokeMethod(m, bundle); } // fallback to field inspection (KF and Prosyst) final BundleContext[] ctx = new BundleContext[1]; ReflectionUtils.doWithFields( bundle.getClass(), new FieldCallback() { public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException { ReflectionUtils.makeAccessible(field); ctx[0] = (BundleContext) field.get(bundle); } }, new FieldFilter() { public boolean matches(Field field) { return BundleContext.class.isAssignableFrom(field.getType()); } }); return ctx[0]; }
private static void init(Bundle bundle) { identifiedOsgiImpl = true; try { isEquinox = bundle .getClass() .getClassLoader() .loadClass("org.eclipse.osgi.framework.internal.core.BundleHost") != null; } catch (Throwable t) { isEquinox = false; } if (!isEquinox) { try { isFelix = bundle.getClass().getClassLoader().loadClass("org.apache.felix.framework.BundleImpl") != null; } catch (Throwable t2) { isFelix = false; } } // System.err.println("isEquinox=" + isEquinox); // System.err.println("isFelix=" + isFelix); }
protected File getEclipseBundleFileUsingReflection(Bundle bundle) { try { Object proxy = bundle.getClass().getMethod("getLoaderProxy").invoke(bundle); Object loader = proxy.getClass().getMethod("getBundleLoader").invoke(proxy); URL root = (URL) loader.getClass().getMethod("findResource", String.class).invoke(loader, "/"); Field field = root.getClass().getDeclaredField("handler"); field.setAccessible(true); Object handler = field.get(root); Field entryField = handler.getClass().getSuperclass().getDeclaredField("bundleEntry"); entryField.setAccessible(true); Object entry = entryField.get(handler); Field fileField = entry.getClass().getDeclaredField("file"); fileField.setAccessible(true); return (File) fileField.get(entry); } catch (Throwable e) { log.error("Cannot access to eclipse bundle system files of " + bundle.getSymbolicName()); return null; } }
/** @see WebXmlParser#parse(InputStream) */ public WebApp parse(final Bundle bundle, final InputStream inputStream) { final WebApp webApp = new WebApp(); // changed to final because of inner // class. try { final Element rootElement = getRootElement(inputStream); if (rootElement != null) { // webApp = new WebApp(); // web-app attributes String version = getAttribute(rootElement, "version"); Integer majorVersion = null; if (version != null && !version.isEmpty() && version.length() > 2) { LOG.debug("version found in web.xml - " + version); try { majorVersion = Integer.parseInt(version.split("\\.")[0]); } catch (NumberFormatException nfe) { // munch do nothing here stay with null therefore // annotation scanning is disabled. } } else if (version != null && !version.isEmpty() && version.length() > 0) { try { majorVersion = Integer.parseInt(version); } catch (NumberFormatException e) { // munch do nothing here stay with null.... } } Boolean metaDataComplete = Boolean.parseBoolean(getAttribute(rootElement, "metadata-complete", "false")); webApp.setMetaDataComplete(metaDataComplete); LOG.debug("metadata-complete is: " + metaDataComplete); // web-app elements webApp.setDisplayName(getTextContent(getChild(rootElement, "display-name"))); parseContextParams(rootElement, webApp); parseSessionConfig(rootElement, webApp); parseServlets(rootElement, webApp); parseFilters(rootElement, webApp); parseListeners(rootElement, webApp); parseErrorPages(rootElement, webApp); parseWelcomeFiles(rootElement, webApp); parseMimeMappings(rootElement, webApp); parseSecurity(rootElement, webApp); LOG.debug("scanninf for ServletContainerInitializers"); ServiceLoader<ServletContainerInitializer> serviceLoader = ServiceLoader.load( ServletContainerInitializer.class, bundle.getClass().getClassLoader()); if (serviceLoader != null) { LOG.debug("ServletContainerInitializers found"); while (serviceLoader.iterator().hasNext()) { // for (ServletContainerInitializer service : // serviceLoader) { ServletContainerInitializer service = null; try { bundle.loadClass(ServletContainerInitializer.class.getName()); Object obj = serviceLoader.iterator().next(); if (obj instanceof ServletContainerInitializer) service = (ServletContainerInitializer) obj; else continue; } catch (ServiceConfigurationError e) { LOG.error("ServiceConfigurationError loading ServletContainerInitializer", e); continue; } catch (ClassNotFoundException e) { LOG.error("ServiceConfigurationError loading ServletContainerInitializer", e); continue; } WebAppServletContainerInitializer webAppServletContainerInitializer = new WebAppServletContainerInitializer(); webAppServletContainerInitializer.setServletContainerInitializer(service); if (!webApp.getMetaDataComplete() && majorVersion != null && majorVersion >= 3) { HandlesTypes annotation = service.getClass().getAnnotation(HandlesTypes.class); Class[] classes; if (annotation != null) { // add annotated classes to service classes = annotation.value(); webAppServletContainerInitializer.setClasses(classes); } } webApp.addServletContainerInitializer(webAppServletContainerInitializer); } } if (!webApp.getMetaDataComplete() && majorVersion != null && majorVersion >= 3) { LOG.debug("metadata-complete is either false or not set"); LOG.debug("scanning for annotated classes"); Enumeration<?> clazzes = bundle.findEntries("/", "*.class", true); for (; clazzes.hasMoreElements(); ) { URL clazzUrl = (URL) clazzes.nextElement(); Class<?> clazz; String clazzFile = clazzUrl.getFile(); LOG.debug("Class file found at :" + clazzFile); if (clazzFile.startsWith("/WEB-INF/classes")) clazzFile = clazzFile.replaceFirst("/WEB-INF/classes", ""); else if (clazzFile.startsWith("/WEB-INF/lib")) clazzFile = clazzFile.replaceFirst("/WEB-INF/lib", ""); String clazzName = clazzFile.replaceAll("/", ".").replaceAll(".class", "").replaceFirst(".", ""); try { clazz = bundle.loadClass(clazzName); } catch (ClassNotFoundException e) { LOG.debug("Class {} not found", clazzName); continue; } if (clazz.isAnnotationPresent(WebServlet.class)) { LOG.debug("found WebServlet annotation on class: " + clazz); WebServletAnnotationScanner annonScanner = new WebServletAnnotationScanner(bundle, clazz.getCanonicalName()); annonScanner.scan(webApp); } else if (clazz.isAnnotationPresent(WebFilter.class)) { LOG.debug("found WebFilter annotation on class: " + clazz); WebFilterAnnotationScanner filterScanner = new WebFilterAnnotationScanner(bundle, clazz.getCanonicalName()); filterScanner.scan(webApp); } else if (clazz.isAnnotationPresent(WebListener.class)) { LOG.debug("found WebListener annotation on class: " + clazz); addWebListener(webApp, clazz.getSimpleName()); } } LOG.debug("class scanning done"); } // special handling for finding JSF Context listeners wrapped in // *.tld files Enumeration tldEntries = bundle.getResources("*.tld"); // Enumeration tldEntries = bundle.findEntries("/", "*.tld", // true); while (tldEntries != null && tldEntries.hasMoreElements()) { URL url = (URL) tldEntries.nextElement(); Element rootTld = getRootElement(url.openStream()); if (rootTld != null) { parseListeners(rootTld, webApp); } } } else { LOG.warn("The parsed web.xml does not have a root element"); return null; } } catch (ParserConfigurationException ignore) { LOG.error("Cannot parse web.xml", ignore); } catch (IOException ignore) { LOG.error("Cannot parse web.xml", ignore); } catch (SAXException ignore) { LOG.error("Cannot parse web.xml", ignore); } return webApp; }