Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for GAL-362, FeaturePackDescriber should not limit to packages and layers #346

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 Red Hat, Inc. and/or its affiliates
* Copyright 2016-2024 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -24,6 +24,8 @@
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

import javax.xml.stream.XMLStreamException;
import org.jboss.galleon.BaseErrors;
Expand All @@ -32,12 +34,16 @@
import org.jboss.galleon.Errors;
import org.jboss.galleon.ProvisioningDescriptionException;
import org.jboss.galleon.ProvisioningException;
import org.jboss.galleon.config.ConfigModel;
import org.jboss.galleon.spec.ConfigLayerSpec;
import org.jboss.galleon.spec.FeaturePackSpec;
import org.jboss.galleon.spec.FeatureSpec;
import org.jboss.galleon.spec.PackageSpec;
import org.jboss.galleon.util.ZipUtils;
import org.jboss.galleon.xml.ConfigLayerSpecXmlParser;
import org.jboss.galleon.xml.ConfigXmlParser;
import org.jboss.galleon.xml.FeaturePackXmlParser;
import org.jboss.galleon.xml.FeatureSpecXmlParser;
import org.jboss.galleon.xml.PackageXmlParser;
import org.jboss.galleon.xml.XmlParsers;

Expand Down Expand Up @@ -103,10 +109,39 @@ public static FeaturePackDescription describeFeaturePack(Path fpDir, String enco
if(Files.exists(layersDir)) {
processLayers(layoutBuilder, layersDir, encoding);
}

final Path featuresDir = fpDir.resolve(Constants.FEATURES);
if(Files.exists(featuresDir)) {
processFeatures(layoutBuilder, featuresDir, encoding);
}
final Path configsDir = fpDir.resolve(Constants.CONFIGS);
if(Files.exists(configsDir)) {
processConfigs(layoutBuilder, configsDir, encoding);
}
return layoutBuilder.build();
}

private static void processFeatures(FeaturePackDescription.Builder fpBuilder, Path layersDir, String encoding) throws ProvisioningDescriptionException {
assertDirectory(layersDir);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(layersDir)) {
for(Path path : stream) {
fpBuilder.addFeature(processFeature(path, encoding));
}
} catch (IOException e) {
failedToReadDirectory(layersDir, e);
}
}

private static void processConfigs(FeaturePackDescription.Builder fpBuilder, Path configsDir, String encoding) throws ProvisioningDescriptionException {
assertDirectory(configsDir);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(configsDir)) {
for(Path path : stream) {
processConfigModel(path, encoding, fpBuilder);
}
} catch (IOException e) {
failedToReadDirectory(configsDir, e);
}
}

private static void processLayers(FeaturePackDescription.Builder fpBuilder, Path layersDir, String encoding) throws ProvisioningDescriptionException {
assertDirectory(layersDir);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(layersDir)) {
Expand Down Expand Up @@ -142,6 +177,56 @@ private static ConfigLayerSpec processLayer(Path layerDir, String encoding) thro
throw new ProvisioningDescriptionException(Errors.parseXml(layerXml), e);
}
}
private static FeatureSpec processFeature(Path featureDir, String encoding) throws ProvisioningDescriptionException {
assertDirectory(featureDir);
final Path featureXml = featureDir.resolve(Constants.SPEC_XML);
if(!Files.exists(featureXml)) {
throw new ProvisioningDescriptionException(BaseErrors.pathDoesNotExist(featureXml));
}
try (Reader in = Files.newBufferedReader(featureXml, Charset.forName(encoding))) {
return FeatureSpecXmlParser.getInstance().parse(in);
} catch (IOException e) {
throw new ProvisioningDescriptionException(Errors.openFile(featureXml), e);
} catch (XMLStreamException e) {
throw new ProvisioningDescriptionException(Errors.parseXml(featureXml), e);
}
}

private static void processConfigModel(Path configDir, String encoding, FeaturePackDescription.Builder fpBuilder) throws ProvisioningDescriptionException {
assertDirectory(configDir);
ConfigModel model = null;
Map<String, ConfigModel> configs = new HashMap<>();
String modelName = configDir.getFileName().toString();
final Path modelXml = configDir.resolve(Constants.MODEL_XML);
if (Files.exists(modelXml)) {
try (Reader in = Files.newBufferedReader(modelXml, Charset.forName(encoding))) {
model = ConfigXmlParser.getInstance().parse(in);
} catch (IOException e) {
throw new ProvisioningDescriptionException(Errors.openFile(modelXml), e);
} catch (XMLStreamException e) {
throw new ProvisioningDescriptionException(Errors.parseXml(modelXml), e);
}
configs.put(Constants.MODEL_XML, model);
}
try (DirectoryStream<Path> stream = Files.newDirectoryStream(configDir)) {
for (Path path : stream) {
String configName = path.getFileName().toString();
final Path configXml = path.resolve(Constants.CONFIG_XML);
if (Files.exists(configXml)) {
try (Reader in = Files.newBufferedReader(configXml, Charset.forName(encoding))) {
configs.put(configName, ConfigXmlParser.getInstance().parse(in));
} catch (IOException e) {
throw new ProvisioningDescriptionException(Errors.openFile(modelXml), e);
} catch (XMLStreamException e) {
throw new ProvisioningDescriptionException(Errors.parseXml(modelXml), e);
}
}
}
} catch (IOException e) {
failedToReadDirectory(configDir, e);
}
fpBuilder.addConfigModel(modelName, configs);
}

private static PackageSpec processPackage(Path pkgDir, String encoding) throws ProvisioningDescriptionException {
assertDirectory(pkgDir);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 Red Hat, Inc. and/or its affiliates
* Copyright 2016-2024 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -26,8 +26,10 @@

import org.jboss.galleon.Errors;
import org.jboss.galleon.ProvisioningDescriptionException;
import org.jboss.galleon.config.ConfigModel;
import org.jboss.galleon.spec.ConfigLayerSpec;
import org.jboss.galleon.spec.FeaturePackSpec;
import org.jboss.galleon.spec.FeatureSpec;
import org.jboss.galleon.spec.PackageDependencySpec;
import org.jboss.galleon.spec.PackageSpec;
import org.jboss.galleon.universe.FeaturePackLocation;
Expand All @@ -49,6 +51,8 @@ public static class Builder {
private final FeaturePackSpec.Builder spec;
private Map<String, PackageSpec> packages = Collections.emptyMap();
private Map<String, ConfigLayerSpec> layers = Collections.emptyMap();
private Map<String, FeatureSpec> features = Collections.emptyMap();
private Map<String, Map<String, ConfigModel>> configModels = Collections.emptyMap();

private Builder(FeaturePackLocation.FPID fpid, FeaturePackSpec.Builder spec) {
this.fpid = fpid;
Expand All @@ -65,6 +69,11 @@ public Builder addLayer(ConfigLayerSpec layer) {
return this;
}

public Builder addFeature(FeatureSpec spec) {
features = CollectionUtils.put(features, spec.getName(), spec);
return this;
}

public boolean hasPackage(String name) {
return packages.containsKey(name);
}
Expand All @@ -76,6 +85,11 @@ public FeaturePackSpec.Builder getSpecBuilder() {
public FeaturePackDescription build() throws ProvisioningDescriptionException {
return new FeaturePackDescription(this);
}

public Builder addConfigModel(String modelName, Map<String, ConfigModel> configs) {
configModels = CollectionUtils.put(configModels, modelName, configs);
return this;
}
}

public static Builder builder(FeaturePackSpec.Builder spec) {
Expand All @@ -86,6 +100,8 @@ public static Builder builder(FeaturePackSpec.Builder spec) {
private final FeaturePackSpec spec;
private final Map<String, PackageSpec> packages;
private final Map<String, ConfigLayerSpec> layers;
private final Map<String, FeatureSpec> features;
private final Map<String, Map<String, ConfigModel>> configModels;
final List<String> unresolvedLocalPkgs;
final boolean externalPkgDeps;

Expand Down Expand Up @@ -131,6 +147,8 @@ private FeaturePackDescription(Builder builder) throws ProvisioningDescriptionEx
}
}
this.layers = CollectionUtils.unmodifiable(builder.layers);
this.features = CollectionUtils.unmodifiable(builder.features);
this.configModels = CollectionUtils.unmodifiable(builder.configModels);
this.externalPkgDeps = externalPkgDeps;
this.unresolvedLocalPkgs = CollectionUtils.unmodifiable(notFound);
}
Expand Down Expand Up @@ -165,4 +183,10 @@ public Collection<PackageSpec> getPackages() {
public Collection<ConfigLayerSpec> getLayers() {
return layers.values();
}
public Collection<FeatureSpec> getFeatures() {
return features.values();
}
public Map<String, Map<String, ConfigModel>> getConfigs() {
return configModels;
}
}
Loading