private ModuleClassLoader getModuleClassLoader(boolean logResolveError) { ResolutionReport report = resolve(); if (logResolveError && !Module.RESOLVED_SET.contains(module.getState())) { String reportMessage = report.getResolutionReportMessage(module.getCurrentRevision()); equinoxContainer .getEventPublisher() .publishFrameworkEvent( FrameworkEvent.ERROR, this, new BundleException(reportMessage, BundleException.RESOLVE_ERROR)); } return AccessController.doPrivileged( new PrivilegedAction<ModuleClassLoader>() { @Override public ModuleClassLoader run() { ModuleWiring wiring = getModule().getCurrentRevision().getWiring(); if (wiring != null) { ModuleLoader moduleLoader = wiring.getModuleLoader(); if (moduleLoader instanceof BundleLoader) { return ((BundleLoader) moduleLoader).getModuleClassLoader(); } } return null; } }); }
@Override public boolean hasPermission(Object permission) { Generation current = (Generation) module.getCurrentRevision().getRevisionInfo(); ProtectionDomain domain = current.getDomain(); if (domain != null) { if (permission instanceof Permission) { SecurityManager sm = System.getSecurityManager(); if (sm instanceof EquinoxSecurityManager) { /* * If the FrameworkSecurityManager is active, we need to do checks the "right" way. * We can exploit our knowledge that the security context of FrameworkSecurityManager * is an AccessControlContext to invoke it properly with the ProtectionDomain. */ AccessControlContext acc = new AccessControlContext(new ProtectionDomain[] {domain}); try { sm.checkPermission((Permission) permission, acc); return true; } catch (Exception e) { return false; } } return domain.implies((Permission) permission); } return false; } return true; }
@Override public void stop(int options) throws BundleException { if (options == 0 && equinoxContainer.getConfiguration().getDebug().MONITOR_ACTIVATION) { Debug.printStackTrace( new Exception("A persistent stop has been called on bundle: " + this)); // $NON-NLS-1$ } module.stop(getStopOptions(options)); }
@Override public int getState() { switch (module.getState()) { case INSTALLED: return Bundle.INSTALLED; case RESOLVED: return Bundle.RESOLVED; case STARTING: case LAZY_STARTING: return Bundle.STARTING; case ACTIVE: return Bundle.ACTIVE; case STOPPING: return Bundle.STOPPING; case UNINSTALLED: return Bundle.UNINSTALLED; default: throw new IllegalStateException( "No valid bundle state for module state: " + module.getState()); // $NON-NLS-1$ } }
@Override public Class<?> loadClass(String name) throws ClassNotFoundException { try { equinoxContainer.checkAdminPermission(this, AdminPermission.CLASS); } catch (SecurityException e) { throw new ClassNotFoundException(name, e); } checkValid(); if (isFragment()) { throw new ClassNotFoundException( "Can not load a class from a fragment bundle: " + this); // $NON-NLS-1$ } try { ModuleClassLoader classLoader = getModuleClassLoader(true); if (classLoader != null) { if (name.length() > 0 && name.charAt(0) == '[') return Class.forName(name, false, classLoader); return classLoader.loadClass(name); } } catch (ClassNotFoundException e) { // This is an equinox-ism. Not sure it is worth it to offer an option to disable ... // On failure attempt to activate lazy activating bundles. if (State.LAZY_STARTING.equals(module.getState())) { try { module.start(StartOptions.LAZY_TRIGGER); } catch (BundleException e1) { equinoxContainer .getLogServices() .log(EquinoxContainer.NAME, FrameworkLogEntry.WARNING, e.getMessage(), e); } } throw e; } throw new ClassNotFoundException( "No class loader available for the bundle: " + this); // $NON-NLS-1$ }
BundleContextImpl createBundleContext(boolean checkPermission) { if (isFragment()) { // fragments cannot have contexts return null; } synchronized (this.monitor) { if (context == null) { // only create the context if we are starting, active or stopping // this is so that SCR can get the context for lazy-start bundles if (Module.ACTIVE_SET.contains(module.getState())) { context = new BundleContextImpl(this, equinoxContainer); } } return context; } }
@Override public URL getResource(String name) { try { equinoxContainer.checkAdminPermission(this, AdminPermission.RESOURCE); } catch (SecurityException e) { return null; } checkValid(); if (isFragment()) { return null; } ModuleClassLoader classLoader = getModuleClassLoader(false); if (classLoader != null) { return classLoader.getResource(name); } return new ClasspathManager((Generation) module.getCurrentRevision().getRevisionInfo(), null) .findLocalResource(name); }
@Override public Enumeration<URL> getResources(String name) throws IOException { try { equinoxContainer.checkAdminPermission(this, AdminPermission.RESOURCE); } catch (SecurityException e) { return null; } checkValid(); if (isFragment()) { return null; } ModuleClassLoader classLoader = getModuleClassLoader(false); Enumeration<URL> result = null; if (classLoader != null) { result = classLoader.getResources(name); } else { result = new ClasspathManager((Generation) module.getCurrentRevision().getRevisionInfo(), null) .findLocalResources(name); } return result != null && result.hasMoreElements() ? result : null; }
@SuppressWarnings("unchecked") private <A> A adapt0(Class<A> adapterType) { if (AccessControlContext.class.equals(adapterType)) { Generation current = (Generation) module.getCurrentRevision().getRevisionInfo(); ProtectionDomain domain = current.getDomain(); return (A) (domain == null ? null : new AccessControlContext(new ProtectionDomain[] {domain})); } if (BundleContext.class.equals(adapterType)) { try { return (A) getBundleContext(); } catch (SecurityException e) { return null; } } if (BundleRevision.class.equals(adapterType)) { if (module.getState().equals(State.UNINSTALLED)) { return null; } return (A) module.getCurrentRevision(); } if (BundleRevisions.class.equals(adapterType)) { return (A) module.getRevisions(); } if (BundleStartLevel.class.equals(adapterType)) { return (A) module; } if (BundleWiring.class.equals(adapterType)) { if (module.getState().equals(State.UNINSTALLED)) { return null; } ModuleRevision revision = module.getCurrentRevision(); if (revision == null) { return null; } return (A) revision.getWiring(); } if (BundleDTO.class.equals(adapterType)) { // Unfortunately we need to lock here to make sure the BSN and version // are consistent in case of updates readLock(); try { return (A) DTOBuilder.newBundleDTO(this); } finally { readUnlock(); } } if (BundleStartLevelDTO.class.equals(adapterType)) { if (module.getState().equals(State.UNINSTALLED)) { return null; } return (A) DTOBuilder.newBundleStartLevelDTO(this, module); } if (BundleRevisionDTO.class.equals(adapterType)) { if (module.getState().equals(State.UNINSTALLED)) { return null; } return (A) DTOBuilder.newBundleRevisionDTO(module.getCurrentRevision()); } if (BundleRevisionDTO[].class.equals(adapterType)) { if (module.getState().equals(State.UNINSTALLED)) { return null; } // No need to lock the database here since the ModuleRevisions object does the // proper locking for us. return (A) DTOBuilder.newArrayBundleRevisionDTO(module.getRevisions()); } if (BundleWiringDTO.class.equals(adapterType)) { if (module.getState().equals(State.UNINSTALLED)) { return null; } readLock(); try { return (A) DTOBuilder.newBundleWiringDTO(module.getCurrentRevision()); } finally { readUnlock(); } } if (BundleWiringDTO[].class.equals(adapterType)) { if (module.getState().equals(State.UNINSTALLED)) { return null; } readLock(); try { return (A) DTOBuilder.newArrayBundleWiringDTO(module.getRevisions()); } finally { readUnlock(); } } if (ServiceReferenceDTO[].class.equals(adapterType)) { if (module.getState().equals(State.UNINSTALLED)) { return null; } BundleContextImpl current = getBundleContextImpl(); ServiceReference<?>[] references = (current == null) ? null : equinoxContainer.getServiceRegistry().getRegisteredServices(current); return (A) DTOBuilder.newArrayServiceReferenceDTO(references); } if (getBundleId() == 0) { if (Framework.class.equals(adapterType)) { return (A) this; } if (FrameworkStartLevel.class.equals(adapterType)) { return (A) equinoxContainer.getStorage().getModuleContainer().getFrameworkStartLevel(); } if (FrameworkWiring.class.equals(adapterType)) { return (A) equinoxContainer.getStorage().getModuleContainer().getFrameworkWiring(); } if (FrameworkDTO.class.equals(adapterType)) { BundleContextImpl current = getBundleContextImpl(); Map<String, String> configuration = equinoxContainer.getConfiguration().getConfiguration(); readLock(); try { return (A) DTOBuilder.newFrameworkDTO(current, configuration); } finally { readUnlock(); } } if (FrameworkStartLevelDTO.class.equals(adapterType)) { return (A) DTOBuilder.newFrameworkStartLevelDTO( equinoxContainer.getStorage().getModuleContainer().getFrameworkStartLevel()); } } // Equinox extras if (Module.class.equals(adapterType)) { return (A) module; } if (ProtectionDomain.class.equals(adapterType)) { Generation current = (Generation) module.getCurrentRevision().getRevisionInfo(); return (A) current.getDomain(); } return null; }
@Override public long getLastModified() { return module.getLastModified(); }
@Override public String getSymbolicName() { return module.getCurrentRevision().getSymbolicName(); }
@Override public String getLocation() { equinoxContainer.checkAdminPermission(getBundle(), AdminPermission.METADATA); return module.getLocation(); }
@Override public long getBundleId() { return module.getId(); }
private Dictionary<String, String> privGetHeaders(String locale) { Generation current = (Generation) module.getCurrentRevision().getRevisionInfo(); return current.getHeaders(locale); }
@Override public File getDataFile(String filename) { checkValid(); Generation current = (Generation) module.getCurrentRevision().getRevisionInfo(); return current.getBundleInfo().getDataFile(filename); }
@Override public Version getVersion() { return module.getCurrentRevision().getVersion(); }
private final void checkValid() { if (module.getState().equals(State.UNINSTALLED)) throw new IllegalStateException("Module has been uninstalled."); // $NON-NLS-1$ }
ResolutionReport resolve() { if (!Module.RESOLVED_SET.contains(module.getState())) { return module.getContainer().resolve(Arrays.asList(module), true); } return null; }