private synchronized ClassRealm getMavenRealm() { if (mavenRealm == null) { mavenRealm = newRealm("maven.api"); importMavenApi(mavenRealm); mavenRealm.setParentClassLoader(ClassLoader.getSystemClassLoader()); List<ClassRealmManagerDelegate> delegates = getDelegates(); if (!delegates.isEmpty()) { List<ClassRealmConstituent> constituents = new ArrayList<ClassRealmConstituent>(); ClassRealmRequest request = new DefaultClassRealmRequest( RealmType.Core, null, new ArrayList<String>(), constituents); for (ClassRealmManagerDelegate delegate : delegates) { delegate.setupRealm(mavenRealm, request); } populateRealm(mavenRealm, constituents); } } return mavenRealm; }
/** * Creates a new class realm with the specified parent and imports. * * @param baseRealmId The base id to use for the new realm, must not be {@code null}. * @param type The type of the class realm, must not be {@code null}. * @param parent The parent realm for the new realm, may be {@code null} to use the Maven core * realm. * @param imports The packages/types to import from the parent realm, may be {@code null}. * @param artifacts The artifacts to add to the realm, may be {@code null}. Unresolved artifacts * (i.e. with a missing file) will automatically be excluded from the realm. * @return The created class realm, never {@code null}. */ private ClassRealm createRealm( String baseRealmId, RealmType type, ClassLoader parent, List<String> imports, boolean importXpp3Dom, List<Artifact> artifacts) { Set<String> artifactIds = new LinkedHashSet<String>(); List<ClassRealmConstituent> constituents = new ArrayList<ClassRealmConstituent>(); if (artifacts != null) { for (Artifact artifact : artifacts) { artifactIds.add(getId(artifact)); if (artifact.getFile() != null) { constituents.add(new ArtifactClassRealmConstituent(artifact)); } } } if (imports != null) { imports = new ArrayList<String>(imports); } else { imports = new ArrayList<String>(); } ClassRealm classRealm = newRealm(baseRealmId); if (parent != null) { classRealm.setParentClassLoader(parent); } else { classRealm.setParentRealm(getMavenRealm()); } List<ClassRealmManagerDelegate> delegates = getDelegates(); if (!delegates.isEmpty()) { ClassRealmRequest request = new DefaultClassRealmRequest(type, parent, imports, constituents); for (ClassRealmManagerDelegate delegate : delegates) { delegate.setupRealm(classRealm, request); } } if (importXpp3Dom) { importXpp3Dom(classRealm); } if (!imports.isEmpty()) { ClassLoader importedRealm = classRealm.getParentClassLoader(); if (logger.isDebugEnabled()) { logger.debug("Importing packages into class realm " + classRealm.getId()); } for (String imp : imports) { if (logger.isDebugEnabled()) { logger.debug(" Imported: " + imp); } classRealm.importFrom(importedRealm, imp); } } Set<String> includedIds = populateRealm(classRealm, constituents); if (logger.isDebugEnabled()) { artifactIds.removeAll(includedIds); for (String id : artifactIds) { logger.debug(" Excluded: " + id); } } return classRealm; }