use of org.jboss.galleon.spec.FeatureParameterSpec in project galleon by wildfly.
the class ResolvedFeatureSpec method resolveRefId.
private List<ResolvedFeatureId> resolveRefId(final ResolvedFeature feature, final FeatureReferenceSpec refSpec, final ResolvedFeatureSpec targetSpec, boolean assertRefMapping) throws ProvisioningException {
if (assertRefMapping) {
assertRefParamMapping(refSpec, targetSpec);
}
ArrayList<Map<String, Object>> paramsList = null;
Map<String, Object> params = Collections.emptyMap();
// no id is considered a child to make the list-add not break the branch
boolean child = feature.hasId() ? false : true;
if (refSpec.hasMappedParams()) {
for (Map.Entry<String, String> mapping : refSpec.getMappedParams().entrySet()) {
final String paramName = mapping.getKey();
final String refParamName = mapping.getValue();
final ResolvedFeatureParam resolvedParam = resolvedParamSpecs.get(paramName);
if (!child && (resolvedParam.spec.isFeatureId() && targetSpec.getSpec().getParam(refParamName).isFeatureId())) {
child = true;
}
Object paramValue = feature.getResolvedParam(paramName);
if (paramValue == null) {
paramValue = feature.isUnset(paramName) ? null : resolvedParam.defaultValue;
if (paramValue == null) {
assertRefNotNillable(feature, refSpec);
return Collections.emptyList();
}
}
if (paramValue.equals(Constants.GLN_UNDEFINED)) {
continue;
}
if (resolvedParam.type.isCollection()) {
final Collection<?> col = (Collection<?>) paramValue;
if (col.isEmpty()) {
assertRefNotNillable(feature, refSpec);
return Collections.emptyList();
}
if (paramsList == null) {
paramsList = new ArrayList<>(col.size());
paramsList.add(params);
} else {
paramsList.ensureCapacity(paramsList.size() * col.size());
}
final int listSize = paramsList.size();
for (int i = 0; i < listSize; ++i) {
final Map<String, Object> idParams = paramsList.get(i);
int colI = 0;
for (Object item : col) {
if (item.equals(Constants.GLN_UNDEFINED)) {
continue;
}
if (colI++ == 0) {
final Map<String, Object> clone = col.size() == 1 ? idParams : CollectionUtils.clone(idParams);
paramsList.set(i, CollectionUtils.put(clone, refParamName, item));
continue;
}
paramsList.add(CollectionUtils.put(CollectionUtils.clone(idParams), refParamName, item));
}
}
continue;
}
if (paramsList != null) {
for (int i = 0; i < paramsList.size(); ++i) {
paramsList.set(i, CollectionUtils.put(paramsList.get(i), refParamName, paramValue));
}
continue;
}
params = CollectionUtils.put(params, refParamName, paramValue);
}
} else {
for (FeatureParameterSpec targetIdParam : targetSpec.xmlSpec.getIdParams()) {
final String paramName = targetIdParam.getName();
final String refParamName = paramName;
final ResolvedFeatureParam resolvedParam = resolvedParamSpecs.get(paramName);
if (!child && resolvedParam.spec.isFeatureId()) {
child = true;
}
Object paramValue = feature.getResolvedParam(paramName);
if (paramValue == null) {
paramValue = feature.isUnset(paramName) ? null : resolvedParam.defaultValue;
if (paramValue == null) {
assertRefNotNillable(feature, refSpec);
return Collections.emptyList();
}
}
if (paramValue.equals(Constants.GLN_UNDEFINED)) {
continue;
}
if (resolvedParam.type.isCollection()) {
final Collection<?> col = (Collection<?>) paramValue;
if (col.isEmpty()) {
assertRefNotNillable(feature, refSpec);
return Collections.emptyList();
}
if (paramsList == null) {
paramsList = new ArrayList<>(col.size());
paramsList.add(params);
} else {
paramsList.ensureCapacity(paramsList.size() * col.size());
}
final int listSize = paramsList.size();
for (int i = 0; i < listSize; ++i) {
final Map<String, Object> idParams = paramsList.get(i);
int colI = 0;
for (Object item : col) {
if (item.equals(Constants.GLN_UNDEFINED)) {
continue;
}
if (colI++ == 0) {
final Map<String, Object> clone = col.size() == 1 ? idParams : CollectionUtils.clone(idParams);
paramsList.set(i, CollectionUtils.put(clone, refParamName, item));
continue;
}
paramsList.add(CollectionUtils.put(CollectionUtils.clone(idParams), refParamName, item));
}
}
continue;
}
if (paramsList != null) {
for (int i = 0; i < paramsList.size(); ++i) {
paramsList.set(i, CollectionUtils.put(paramsList.get(i), refParamName, paramValue));
}
continue;
}
params = CollectionUtils.put(params, refParamName, paramValue);
}
}
if (paramsList != null) {
final List<ResolvedFeatureId> refIds = new ArrayList<>(paramsList.size());
for (int i = 0; i < paramsList.size(); ++i) {
final Map<String, Object> idParams = paramsList.get(i);
if (idParams.isEmpty()) {
// TODO
continue;
}
refIds.add(new ResolvedFeatureId(targetSpec.id, idParams, child));
}
if (refIds.isEmpty()) {
assertRefNotNillable(feature, refSpec);
}
return refIds;
}
if (params.isEmpty()) {
assertRefNotNillable(feature, refSpec);
return Collections.emptyList();
}
return Collections.singletonList(new ResolvedFeatureId(targetSpec.id, params, child));
}
use of org.jboss.galleon.spec.FeatureParameterSpec in project galleon by wildfly.
the class ResolvedFeatureSpec method resolveIdFromForeignKey.
ResolvedFeatureId resolveIdFromForeignKey(ResolvedFeatureId parentId, String parentRef, Map<String, String> params) throws ProvisioningException {
if (!xmlSpec.hasId()) {
return null;
}
if (parentId == null) {
final StringBuilder buf = new StringBuilder();
buf.append("Failed to initialize foreign key parameters of ").append(id).append(": the referenced feature has not ID ");
throw new ProvisioningException(buf.toString());
}
if (parentRef == null) {
parentRef = parentId.specId.name;
}
final List<FeatureParameterSpec> idParamSpecs = xmlSpec.getIdParams();
final Map<String, Object> resolvedParams = new HashMap<>(idParamSpecs.size());
final FeatureReferenceSpec refSpec = xmlSpec.getFeatureRef(parentRef);
try {
if (refSpec.hasMappedParams()) {
for (Map.Entry<String, String> mapping : refSpec.getMappedParams().entrySet()) {
final FeatureParameterSpec param = xmlSpec.getParam(mapping.getKey());
if (!param.isFeatureId()) {
continue;
}
final Object idValue = parentId.params.get(mapping.getValue());
if (idValue == null) {
throw new ProvisioningDescriptionException(id + " expects ID parameter '" + mapping.getValue() + "' in " + parentId);
}
resolvedParams.put(mapping.getKey(), idValue);
}
for (FeatureParameterSpec idParamSpec : idParamSpecs) {
String configValue = params.get(idParamSpec.getName());
if (configValue != null) {
final Object childValue = paramFromString(idParamSpec.getName(), configValue);
final Object idValue = resolvedParams.put(idParamSpec.getName(), childValue);
if (idValue != null && !idValue.equals(childValue)) {
throw new ProvisioningDescriptionException(Errors.idParamForeignKeyInitConflict(id, idParamSpec.getName(), childValue, idValue));
}
continue;
}
if (resolvedParams.containsKey(idParamSpec.getName())) {
continue;
}
final Object childValue = getResolvedParam(idParamSpec.getName()).defaultValue;
if (childValue == null) {
throw new ProvisioningDescriptionException(Errors.nonNillableParameterIsNull(id, idParamSpec.getName()));
}
resolvedParams.put(idParamSpec.getName(), childValue);
}
} else {
for (FeatureParameterSpec idParamSpec : idParamSpecs) {
final Object parentValue = parentId.params.get(idParamSpec.getName());
String configValue = params.get(idParamSpec.getName());
if (configValue != null) {
final Object childValue = paramFromString(idParamSpec.getName(), configValue);
if (parentValue != null && !parentValue.equals(childValue)) {
throw new ProvisioningDescriptionException(Errors.idParamForeignKeyInitConflict(id, idParamSpec.getName(), childValue, parentValue));
}
resolvedParams.put(idParamSpec.getName(), childValue);
continue;
}
if (parentValue != null) {
resolvedParams.put(idParamSpec.getName(), parentValue);
continue;
}
final Object childValue = getResolvedParam(idParamSpec.getName()).defaultValue;
if (childValue == null) {
throw new ProvisioningDescriptionException(Errors.nonNillableParameterIsNull(id, idParamSpec.getName()));
}
resolvedParams.put(idParamSpec.getName(), childValue);
}
}
return new ResolvedFeatureId(id, resolvedParams);
} catch (ProvisioningException e) {
final StringBuilder buf = new StringBuilder();
buf.append("Failed to initialize foreign key parameters of ").append(id).append(" spec referencing feature ").append(parentId).append(" with parameters ");
StringUtils.append(buf, params.entrySet());
throw new ProvisioningException(Errors.failedToInitializeForeignKeyParams(id, parentId, params), e);
}
}
use of org.jboss.galleon.spec.FeatureParameterSpec in project galleon by wildfly.
the class FeatureSpecXmlParser10 method parseParameter.
private FeatureParameterSpec parseParameter(XMLExtendedStreamReader reader) throws XMLStreamException {
final FeatureParameterSpec.Builder builder = FeatureParameterSpec.builder();
for (int i = 0; i < reader.getAttributeCount(); i++) {
final Attribute attribute = Attribute.of(reader.getAttributeName(i));
switch(attribute) {
case NAME:
builder.setName(reader.getAttributeValue(i));
break;
case FEATURE_ID:
if (Boolean.parseBoolean(reader.getAttributeValue(i))) {
builder.setFeatureId();
}
break;
case DEFAULT:
builder.setDefaultValue(reader.getAttributeValue(i));
break;
case NILLABLE:
if (Boolean.parseBoolean(reader.getAttributeValue(i))) {
builder.setNillable();
}
break;
case TYPE:
builder.setType(reader.getAttributeValue(i));
break;
default:
throw ParsingUtils.unexpectedAttribute(reader, i);
}
}
if (builder.getName() == null) {
throw ParsingUtils.missingAttributes(reader.getLocation(), Collections.singleton(Attribute.NAME));
}
ParsingUtils.parseNoContent(reader);
try {
return builder.build();
} catch (ProvisioningDescriptionException e) {
throw new XMLStreamException("Failed to create feature parameter", reader.getLocation(), e);
}
}
use of org.jboss.galleon.spec.FeatureParameterSpec in project galleon by wildfly.
the class FeatureSpecXmlWriter method toElement.
protected ElementNode toElement(FeatureSpec featureSpec) {
final ElementNode specE = addElement(null, Element.FEATURE_SPEC);
addAttribute(specE, Attribute.NAME, featureSpec.getName());
if (featureSpec.hasAnnotations()) {
for (FeatureAnnotation fa : featureSpec.getAnnotations()) {
final ElementNode annotationE = addElement(specE, Element.ANNOTATION);
addAttribute(annotationE, Attribute.NAME, fa.getName());
if (fa.hasElements()) {
for (Map.Entry<String, String> entry : fa.getElements().entrySet()) {
final ElementNode elemE = addElement(annotationE, Element.ELEM);
addAttribute(elemE, Attribute.NAME, entry.getKey());
if (entry.getValue() != null) {
addAttribute(elemE, Attribute.VALUE, entry.getValue());
}
}
}
}
}
if (featureSpec.providesCapabilities()) {
writeCaps(addElement(specE, Element.PROVIDES), featureSpec.getProvidedCapabilities());
}
if (featureSpec.requiresCapabilities()) {
writeCaps(addElement(specE, Element.REQUIRES), featureSpec.getRequiredCapabilities());
}
if (featureSpec.hasFeatureDeps()) {
final ElementNode depsE = addElement(specE, Element.DEPENDENCIES);
for (FeatureDependencySpec dep : featureSpec.getFeatureDeps()) {
final ElementNode depE = addElement(depsE, Element.DEPENDENCY);
addAttribute(depE, Attribute.FEATURE_ID, dep.getFeatureId().toString());
if (dep.getOrigin() != null) {
addAttribute(depE, Attribute.DEPENDENCY, dep.getOrigin());
}
if (dep.isInclude()) {
addAttribute(depE, Attribute.INCLUDE, TRUE);
}
}
}
if (featureSpec.hasFeatureRefs()) {
final ElementNode refsE = addElement(specE, Element.REFERENCES);
for (FeatureReferenceSpec ref : featureSpec.getFeatureRefs()) {
final ElementNode refE = addElement(refsE, Element.REFERENCE);
final String feature = ref.getFeature().toString();
if (ref.getOrigin() != null) {
addAttribute(refE, Attribute.DEPENDENCY, ref.getOrigin());
}
addAttribute(refE, Attribute.FEATURE, feature);
if (!feature.equals(ref.getName())) {
addAttribute(refE, Attribute.NAME, ref.getName());
}
if (ref.isNillable()) {
addAttribute(refE, Attribute.NILLABLE, TRUE);
}
if (ref.isInclude()) {
addAttribute(refE, Attribute.INCLUDE, TRUE);
}
for (Map.Entry<String, String> mapping : ref.getMappedParams().entrySet()) {
final ElementNode paramE = addElement(refE, Element.PARAMETER);
addAttribute(paramE, Attribute.NAME, mapping.getKey());
addAttribute(paramE, Attribute.MAPS_TO, mapping.getValue());
}
}
}
if (featureSpec.hasParams()) {
final ElementNode paramsE = addElement(specE, Element.PARAMETERS);
for (FeatureParameterSpec paramSpec : featureSpec.getParams().values()) {
final ElementNode paramE = addElement(paramsE, Element.PARAMETER);
addAttribute(paramE, Attribute.NAME, paramSpec.getName());
if (paramSpec.isFeatureId()) {
addAttribute(paramE, Attribute.FEATURE_ID, TRUE);
} else if (paramSpec.isNillable()) {
addAttribute(paramE, Attribute.NILLABLE, TRUE);
}
if (paramSpec.hasDefaultValue()) {
addAttribute(paramE, Attribute.DEFAULT, paramSpec.getDefaultValue());
}
if (paramSpec.getType() != null && !Constants.BUILT_IN_TYPE_STRING.equals(paramSpec.getType())) {
addAttribute(paramE, Attribute.TYPE, paramSpec.getType());
}
}
}
if (featureSpec.hasPackageDeps()) {
PackageXmlWriter.writePackageDeps(featureSpec, addElement(specE, Element.PACKAGES));
}
return specE;
}
use of org.jboss.galleon.spec.FeatureParameterSpec in project galleon by wildfly.
the class StateInfoUtil method displayFeatureSpec.
private static void displayFeatureSpec(PmCommandInvocation session, Group grp) throws IOException {
FeatureSpecInfo f = grp.getSpec();
session.println("");
session.println("Feature type : " + f.getSpecId().getName());
session.println("Feature origin : " + f.getSpecId().getProducer());
session.println("Feature description: " + f.getDescription());
if (!f.isEnabled()) {
session.println("WARNING! The feature is not enabled.");
session.println("Missing packages");
for (Identity m : f.getMissingPackages()) {
session.println(m.toString());
}
}
List<FeatureParameterSpec> idparams = f.getSpec().getIdParams();
String tab = " ";
session.println(Config.getLineSeparator() + "Feature Id parameters");
if (idparams.isEmpty()) {
session.println("NONE");
} else {
for (FeatureParameterSpec param : idparams) {
StringBuilder builder = new StringBuilder();
builder.append(tab + param.getName()).append(Config.getLineSeparator());
builder.append(tab + tab + "description : " + "no description available").append(Config.getLineSeparator());
builder.append(tab + tab + "type : " + param.getType()).append(Config.getLineSeparator());
builder.append(tab + tab + "default-value: " + param.getDefaultValue()).append(Config.getLineSeparator());
builder.append(tab + tab + "nillable : " + param.isNillable()).append(Config.getLineSeparator());
session.print(builder.toString());
}
}
// Add spec parameters
session.println(Config.getLineSeparator() + "Feature parameters");
Map<String, FeatureParameterSpec> params = f.getSpec().getParams();
if (params.isEmpty()) {
session.println("NONE");
} else {
for (Entry<String, FeatureParameterSpec> entry : params.entrySet()) {
FeatureParameterSpec param = entry.getValue();
if (!param.isFeatureId()) {
StringBuilder builder = new StringBuilder();
builder.append(tab + param.getName()).append(Config.getLineSeparator());
builder.append(tab + tab + "description : " + "no description available").append(Config.getLineSeparator());
builder.append(tab + tab + "type : " + param.getType()).append(Config.getLineSeparator());
builder.append(tab + tab + "default-value: " + param.getDefaultValue()).append(Config.getLineSeparator());
builder.append(tab + tab + "nillable : " + param.isNillable()).append(Config.getLineSeparator());
session.println(builder.toString());
}
}
}
session.println(Config.getLineSeparator() + "Packages");
if (f.getPackages().isEmpty()) {
session.println(tab + "NONE");
} else {
for (PackageInfo p : f.getPackages()) {
session.println(p.getIdentity().toString());
}
}
session.println(Config.getLineSeparator() + "Provided capabilities");
if (f.getSpec().getProvidedCapabilities().isEmpty()) {
session.println(tab + "NONE");
} else {
for (CapabilitySpec c : f.getSpec().getProvidedCapabilities()) {
session.println(tab + c.toString());
}
}
session.println(Config.getLineSeparator() + "Consumed capabilities");
if (f.getSpec().getRequiredCapabilities().isEmpty()) {
session.println(tab + "NONE");
} else {
for (CapabilitySpec c : f.getSpec().getRequiredCapabilities()) {
session.println(tab + c.toString());
}
}
session.println(Config.getLineSeparator() + "Features dependencies");
if (f.getSpec().getFeatureDeps().isEmpty()) {
session.println(tab + "NONE");
} else {
for (FeatureDependencySpec c : f.getSpec().getFeatureDeps()) {
session.println(tab + c.getFeatureId().toString());
}
}
session.println(Config.getLineSeparator() + "Features references");
if (f.getSpec().getFeatureRefs().isEmpty()) {
session.println(tab + "NONE");
} else {
for (FeatureReferenceSpec c : f.getSpec().getFeatureRefs()) {
session.println(tab + c.getFeature());
}
}
session.println(Config.getLineSeparator() + "Features Annotations");
if (f.getSpec().getAnnotations().isEmpty()) {
session.println(tab + "NONE");
} else {
for (FeatureAnnotation c : f.getSpec().getAnnotations()) {
session.println(tab + c.toString());
}
}
}
Aggregations