use of org.apache.karaf.features.internal.model.Bundle in project karaf by apache.
the class GenerateDescriptorMojo method writeFeatures.
/*
* Write all project dependencies as feature
*/
private void writeFeatures(PrintStream out) throws ArtifactResolutionException, ArtifactNotFoundException, IOException, JAXBException, SAXException, ParserConfigurationException, XMLStreamException, MojoExecutionException {
getLog().info("Generating feature descriptor file " + outputFile.getAbsolutePath());
//read in an existing feature.xml
ObjectFactory objectFactory = new ObjectFactory();
Features features;
if (inputFile.exists()) {
filter(inputFile, filteredInputFile);
features = readFeaturesFile(filteredInputFile);
} else {
features = objectFactory.createFeaturesRoot();
}
if (features.getName() == null) {
features.setName(project.getArtifactId());
}
Feature feature = null;
for (Feature test : features.getFeature()) {
if (test.getName().equals(primaryFeatureName)) {
feature = test;
}
}
if (feature == null) {
feature = objectFactory.createFeature();
feature.setName(primaryFeatureName);
}
if (!feature.hasVersion()) {
feature.setVersion(project.getArtifact().getBaseVersion());
}
if (feature.getDescription() == null) {
feature.setDescription(project.getName());
}
if (installMode != null) {
feature.setInstall(installMode);
}
if (project.getDescription() != null && feature.getDetails() == null) {
feature.setDetails(project.getDescription());
}
if (includeProjectArtifact) {
Bundle bundle = objectFactory.createBundle();
bundle.setLocation(this.dependencyHelper.artifactToMvn(project.getArtifact(), project.getVersion()));
if (startLevel != null) {
bundle.setStartLevel(startLevel);
}
feature.getBundle().add(bundle);
}
boolean needWrap = false;
// First pass to look for features
// Track other features we depend on and their repositories (we track repositories instead of building them from
// the feature's Maven artifact to allow for multi-feature repositories)
// TODO Initialise the repositories from the existing feature file if any
Map<Dependency, Feature> otherFeatures = new HashMap<>();
Map<Feature, String> featureRepositories = new HashMap<>();
for (final LocalDependency entry : localDependencies) {
Object artifact = entry.getArtifact();
if (excludedArtifactIds.contains(this.dependencyHelper.getArtifactId(artifact))) {
continue;
}
processFeatureArtifact(features, feature, otherFeatures, featureRepositories, artifact, entry.getParent(), true);
}
// Second pass to look for bundles
if (addBundlesToPrimaryFeature) {
localDependency: for (final LocalDependency entry : localDependencies) {
Object artifact = entry.getArtifact();
if (excludedArtifactIds.contains(this.dependencyHelper.getArtifactId(artifact))) {
continue;
}
if (!this.dependencyHelper.isArtifactAFeature(artifact)) {
String bundleName = this.dependencyHelper.artifactToMvn(artifact, getVersionOrRange(entry.getParent(), artifact));
File bundleFile = this.dependencyHelper.resolve(artifact, getLog());
Manifest manifest = getManifest(bundleFile);
for (ConfigFile cf : feature.getConfigfile()) {
if (bundleName.equals(cf.getLocation().replace('\n', ' ').trim())) {
// The bundle matches a configfile, ignore it
continue localDependency;
}
}
if (manifest == null || !ManifestUtils.isBundle(getManifest(bundleFile))) {
bundleName = "wrap:" + bundleName;
needWrap = true;
}
Bundle bundle = null;
for (Bundle b : feature.getBundle()) {
if (bundleName.equals(b.getLocation())) {
bundle = b;
break;
}
}
if (bundle == null) {
bundle = objectFactory.createBundle();
bundle.setLocation(bundleName);
// Check the features this feature depends on don't already contain the dependency
// TODO Perhaps only for transitive dependencies?
boolean includedTransitively = simplifyBundleDependencies && isBundleIncludedTransitively(feature, otherFeatures, bundle);
if (!includedTransitively && (!"provided".equals(entry.getScope()) || !ignoreScopeProvided)) {
feature.getBundle().add(bundle);
}
}
if ("runtime".equals(entry.getScope())) {
bundle.setDependency(true);
}
if (startLevel != null && bundle.getStartLevel() == 0) {
bundle.setStartLevel(startLevel);
}
}
}
}
if (needWrap) {
Dependency wrapDependency = new Dependency();
wrapDependency.setName("wrap");
wrapDependency.setDependency(false);
wrapDependency.setPrerequisite(true);
feature.getFeature().add(wrapDependency);
}
if ((!feature.getBundle().isEmpty() || !feature.getFeature().isEmpty()) && !features.getFeature().contains(feature)) {
features.getFeature().add(feature);
}
// Add any missing repositories for the included features
for (Feature includedFeature : features.getFeature()) {
for (Dependency dependency : includedFeature.getFeature()) {
Feature dependedFeature = otherFeatures.get(dependency);
if (dependedFeature != null && !features.getFeature().contains(dependedFeature)) {
String repository = featureRepositories.get(dependedFeature);
if (repository != null && !features.getRepository().contains(repository)) {
features.getRepository().add(repository);
}
}
}
}
JaxbUtil.marshal(features, out);
try {
checkChanges(features, objectFactory);
} catch (Exception e) {
throw new MojoExecutionException("Features contents have changed", e);
}
getLog().info("...done!");
}
use of org.apache.karaf.features.internal.model.Bundle in project karaf by apache.
the class AddToRepositoryMojo method copyBundlesToDestRepository.
private void copyBundlesToDestRepository(List<? extends Bundle> artifactRefs) throws MojoExecutionException {
for (Bundle artifactRef : artifactRefs) {
Artifact artifact = resourceToArtifact(artifactRef.getLocation(), skipNonMavenProtocols);
// Avoid getting NPE on artifact.getFile in some cases
resolveArtifact(artifact, remoteRepos);
if (artifact != null) {
copy(artifact, repository);
}
}
}
use of org.apache.karaf.features.internal.model.Bundle in project karaf by apache.
the class Builder method installStage.
private void installStage(Profile installedProfile, Set<Feature> allBootFeatures) throws Exception {
LOGGER.info("Install stage");
//
// Handle installed profiles
//
Profile installedOverlay = Profiles.getOverlay(installedProfile, allProfiles, environment);
Profile installedEffective = Profiles.getEffective(installedOverlay, false);
Downloader downloader = manager.createDownloader();
// Load startup repositories
Map<String, Features> installedRepositories = loadRepositories(manager, installedEffective.getRepositories(), true);
// Compute startup feature dependencies
Set<Feature> allInstalledFeatures = new HashSet<>();
for (Features repo : installedRepositories.values()) {
allInstalledFeatures.addAll(repo.getFeature());
}
Set<Feature> installedFeatures = new LinkedHashSet<>();
// Add boot features for search
allInstalledFeatures.addAll(allBootFeatures);
for (String feature : installedEffective.getFeatures()) {
addFeatures(allInstalledFeatures, feature, installedFeatures, true);
}
for (Feature feature : installedFeatures) {
LOGGER.info(" Feature {} is defined as an installed feature", feature.getId());
for (Bundle bundle : feature.getBundle()) {
if (!ignoreDependencyFlag || !bundle.isDependency()) {
installArtifact(downloader, bundle.getLocation().trim());
}
}
// Install config files
for (ConfigFile configFile : feature.getConfigfile()) {
installArtifact(downloader, configFile.getLocation().trim());
}
for (Conditional cond : feature.getConditional()) {
for (Bundle bundle : cond.getBundle()) {
if (!ignoreDependencyFlag || !bundle.isDependency()) {
installArtifact(downloader, bundle.getLocation().trim());
}
}
}
}
for (String location : installedEffective.getBundles()) {
installArtifact(downloader, location);
}
downloader.await();
}
use of org.apache.karaf.features.internal.model.Bundle in project karaf by apache.
the class Builder method bootStage.
private Set<Feature> bootStage(Profile bootProfile, Profile startupEffective) throws Exception {
LOGGER.info("Boot stage");
//
// Handle boot profiles
//
Profile bootOverlay = Profiles.getOverlay(bootProfile, allProfiles, environment);
Profile bootEffective = Profiles.getEffective(bootOverlay, false);
// Load startup repositories
Map<String, Features> bootRepositories = loadRepositories(manager, bootEffective.getRepositories(), true);
// Compute startup feature dependencies
Set<Feature> allBootFeatures = new HashSet<>();
for (Features repo : bootRepositories.values()) {
allBootFeatures.addAll(repo.getFeature());
}
// Generate a global feature
Map<String, Dependency> generatedDep = new HashMap<>();
Feature generated = new Feature();
generated.setName(UUID.randomUUID().toString());
// Add feature dependencies
for (String dependency : bootEffective.getFeatures()) {
Dependency dep = generatedDep.get(dependency);
if (dep == null) {
dep = createDependency(dependency);
generated.getFeature().add(dep);
generatedDep.put(dep.getName(), dep);
}
dep.setDependency(false);
}
// Add bundles
for (String location : bootEffective.getBundles()) {
location = location.replace("profile:", "file:etc/");
Bundle bun = new Bundle();
bun.setLocation(location);
generated.getBundle().add(bun);
}
Features rep = new Features();
rep.setName(UUID.randomUUID().toString());
rep.getRepository().addAll(bootEffective.getRepositories());
rep.getFeature().add(generated);
allBootFeatures.add(generated);
Downloader downloader = manager.createDownloader();
// Compute startup feature dependencies
Set<Feature> bootFeatures = new HashSet<>();
addFeatures(allBootFeatures, generated.getName(), bootFeatures, true);
for (Feature feature : bootFeatures) {
// the feature is a startup feature, updating startup.properties file
LOGGER.info(" Feature " + feature.getId() + " is defined as a boot feature");
// add the feature in the system folder
Set<String> locations = new HashSet<>();
for (Bundle bundle : feature.getBundle()) {
if (!ignoreDependencyFlag || !bundle.isDependency()) {
locations.add(bundle.getLocation().trim());
}
}
for (Conditional cond : feature.getConditional()) {
for (Bundle bundle : cond.getBundle()) {
if (!ignoreDependencyFlag || !bundle.isDependency()) {
locations.add(bundle.getLocation().trim());
}
}
}
// Build optional features and known prerequisites
Map<String, List<String>> prereqs = new HashMap<>();
prereqs.put("blueprint:", Arrays.asList("deployer", "aries-blueprint"));
prereqs.put("spring:", Arrays.asList("deployer", "spring"));
prereqs.put("wrap:", Arrays.asList("wrap"));
prereqs.put("war:", Arrays.asList("war"));
for (String location : locations) {
installArtifact(downloader, location);
for (Map.Entry<String, List<String>> entry : prereqs.entrySet()) {
if (location.startsWith(entry.getKey())) {
for (String prereq : entry.getValue()) {
Dependency dep = generatedDep.get(prereq);
if (dep == null) {
dep = new Dependency();
dep.setName(prereq);
generated.getFeature().add(dep);
generatedDep.put(dep.getName(), dep);
}
dep.setPrerequisite(true);
}
}
}
}
List<Content> contents = new ArrayList<>();
contents.add(feature);
contents.addAll(feature.getConditional());
for (Content content : contents) {
// Install config files
for (Config config : content.getConfig()) {
if (config.isExternal()) {
installArtifact(downloader, config.getValue().trim());
}
}
for (ConfigFile configFile : content.getConfigfile()) {
installArtifact(downloader, configFile.getLocation().trim());
}
// Extract configs
for (Config config : content.getConfig()) {
if (pidMatching(config.getName())) {
Path configFile = etcDirectory.resolve(config.getName() + ".cfg");
LOGGER.info(" adding config file: {}", homeDirectory.relativize(configFile));
if (config.isExternal()) {
downloader.download(config.getValue().trim(), provider -> {
synchronized (provider) {
Files.copy(provider.getFile().toPath(), configFile, StandardCopyOption.REPLACE_EXISTING);
}
});
} else {
Files.write(configFile, config.getValue().getBytes());
}
}
}
}
// Install libraries
List<String> libraries = new ArrayList<>();
for (Library library : feature.getLibraries()) {
String lib = library.getLocation() + ";type:=" + library.getType() + ";export:=" + library.isExport() + ";delegate:=" + library.isDelegate();
libraries.add(lib);
}
Path configPropertiesPath = etcDirectory.resolve("config.properties");
Properties configProperties = new Properties(configPropertiesPath.toFile());
downloadLibraries(downloader, configProperties, libraries, " ");
downloader.await();
// Reformat clauses
reformatClauses(configProperties, Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA);
reformatClauses(configProperties, Constants.FRAMEWORK_BOOTDELEGATION);
configProperties.save();
}
// If there are bundles to install, we can't use the boot features only
// so keep the generated feature
Path featuresCfgFile = etcDirectory.resolve("org.apache.karaf.features.cfg");
if (!generated.getBundle().isEmpty()) {
File output = etcDirectory.resolve(rep.getName() + ".xml").toFile();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JaxbUtil.marshal(rep, baos);
ByteArrayInputStream bais;
String repoUrl;
if (karafVersion == KarafVersion.v24) {
String str = baos.toString();
str = str.replace("http://karaf.apache.org/xmlns/features/v1.3.0", "http://karaf.apache.org/xmlns/features/v1.2.0");
str = str.replaceAll(" dependency=\".*?\"", "");
str = str.replaceAll(" prerequisite=\".*?\"", "");
for (Feature f : rep.getFeature()) {
for (Dependency d : f.getFeature()) {
if (d.isPrerequisite()) {
if (!startupEffective.getFeatures().contains(d.getName())) {
LOGGER.warn("Feature " + d.getName() + " is a prerequisite and should be installed as a startup feature.");
}
}
}
}
bais = new ByteArrayInputStream(str.getBytes());
repoUrl = "file:etc/" + output.getName();
} else {
bais = new ByteArrayInputStream(baos.toByteArray());
repoUrl = "file:${karaf.home}/etc/" + output.getName();
}
Files.copy(bais, output.toPath());
Properties featuresProperties = new Properties(featuresCfgFile.toFile());
featuresProperties.put(FEATURES_REPOSITORIES, repoUrl);
featuresProperties.put(FEATURES_BOOT, generated.getName());
featuresProperties.save();
} else {
String repos = getRepos(rep);
String boot = getBootFeatures(generatedDep);
Properties featuresProperties = new Properties(featuresCfgFile.toFile());
featuresProperties.put(FEATURES_REPOSITORIES, repos);
featuresProperties.put(FEATURES_BOOT, boot);
reformatClauses(featuresProperties, FEATURES_REPOSITORIES);
reformatClauses(featuresProperties, FEATURES_BOOT);
featuresProperties.save();
}
downloader.await();
return allBootFeatures;
}
Aggregations