Skip to content

Commit

Permalink
Fix for Issue #93, Introduce a rule to discover layer based on Annota…
Browse files Browse the repository at this point in the history
…tion field value
  • Loading branch information
jfdenise committed Nov 22, 2024
1 parent 5b6ac81 commit 4fdbcea
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 0 deletions.
64 changes: 64 additions & 0 deletions core/src/main/java/org/wildfly/glow/AnnotationFieldValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2024 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wildfly.glow;

/**
*
* @author jdenise
*/
public class AnnotationFieldValue {

private final String annotation;
private final String fieldName;
private final String fieldValue;
private final Layer layer;

public AnnotationFieldValue(String annotation, String fieldName, String fieldValue, Layer layer) {
this.annotation = annotation;
this.fieldName = fieldName;
this.fieldValue = fieldValue;
this.layer = layer;
}

/**
* @return the annotation
*/
public String getAnnotation() {
return annotation;
}

/**
* @return the fieldName
*/
public String getFieldName() {
return fieldName;
}

/**
* @return the fieldValue
*/
public String getFieldValue() {
return fieldValue;
}

/**
* @return the layer
*/
public Layer getLayer() {
return layer;
}
}
24 changes: 24 additions & 0 deletions core/src/main/java/org/wildfly/glow/DeploymentScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -193,6 +194,29 @@ private void scanAnnotations(DeploymentScanContext ctx) throws IOException {
}
}
}
Map<String, AnnotationFieldValue> fields = ctx.mapping.getAnnotationFieldValues().get(ai.name().toString());
if (fields != null) {
Layer foundLayer = null;
for(Entry<String, AnnotationFieldValue> f : fields.entrySet()) {
String val = getAnnotationValue(ai, f.getKey());
if (val != null) {
if (Utils.isPattern(f.getValue().getFieldValue())) {
Pattern p = Pattern.compile(f.getValue().getFieldValue());
if (p.matcher(val).matches()) {
foundLayer = f.getValue().getLayer();
LayerMapping.addRule(LayerMapping.RULE.ANNOTATION_VALUE, foundLayer, ai.name().toString()+"_"+f.getKey()+"="+f.getValue().getFieldValue());
ctx.layers.add(f.getValue().getLayer());
}
} else {
if (val.equals(f.getValue().getFieldValue())) {
foundLayer = f.getValue().getLayer();
LayerMapping.addRule(LayerMapping.RULE.ANNOTATION_VALUE, foundLayer, ai.name().toString()+"_"+f.getKey()+"="+f.getValue().getFieldValue());
ctx.layers.add(f.getValue().getLayer());
}
}
}
}
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/org/wildfly/glow/LayerMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public enum RULE {
ADD_ON_ALWAYS_INCLUDED,
ALWAYS_INCLUDED,
ANNOTATION,
ANNOTATION_VALUE,
BASE_LAYER,
BRING_DATASOURCE,
EXPECTED_FILE,
Expand All @@ -48,6 +49,7 @@ public enum RULE {
}
private final Map<String, Set<Layer>> constantPoolClassInfos = new HashMap<>();
private final Map<String, Set<Layer>> annotations = new HashMap<>();
private final Map<String, Map<String, AnnotationFieldValue>> annotationFieldValues = new HashMap<>();
private final Map<String, Layer> activeProfilesLayers = new HashMap<>();
private final Map<String, Set<Layer>> allProfilesLayers = new HashMap<>();
private Layer defaultBaseLayer;
Expand Down Expand Up @@ -176,6 +178,10 @@ public Map<Layer, String> getHiddenConditions() {
return hiddenConditions;
}

public Map<String, Map<String, AnnotationFieldValue>> getAnnotationFieldValues() {
return annotationFieldValues;
}

public static String cleanupKey(String key) {
if (key.startsWith(LayerMetadata.HIDDEN_IF)) {
key = key.substring(key.indexOf(LayerMetadata.HIDDEN_IF) + LayerMetadata.HIDDEN_IF.length() + 1, key.length());
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/org/wildfly/glow/LayerMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public abstract class LayerMetadata {
public static final String ADD_ON_DESCRIPTION = PREFIX + "add-on-description";
public static final String ADD_ON_FIX = PREFIX + "add-on-fix-";
public static final String ANNOTATIONS = PREFIX + "annotations";
public static final String ANNOTATION_FIELD_VALUE = PREFIX + "annotation.field.value";
public static final String BRING_DATASOURCE = PREFIX + "bring-datasource";
public static final String CLASS = PREFIX + "class";
public static final String CONFIGURATION = PREFIX + "configuration";
Expand Down Expand Up @@ -67,6 +68,7 @@ public abstract class LayerMetadata {
FULLY_NAMED_RULES.add(KIND);

RULES_WITH_SUFFIX.add(ADD_ON_FIX);
RULES_WITH_SUFFIX.add(ANNOTATION_FIELD_VALUE);
RULES_WITH_SUFFIX.add(EXPECTED_FILE);
RULES_WITH_SUFFIX.add(NOT_EXPECTED_FILE);
RULES_WITH_SUFFIX.add(PROFILE);
Expand Down
15 changes: 15 additions & 0 deletions core/src/main/java/org/wildfly/glow/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,21 @@ public static LayerMapping buildMapping(Map<String, Layer> layers, Set<String> p
}
continue;
}
if (k.startsWith(LayerMetadata.ANNOTATION_FIELD_VALUE)) {
String val = l.getProperties().get(k);
int i = val.indexOf(",");
String annotation = val.substring(0, i);
String annotationFieldValue = val.substring(i+1);
int j = annotationFieldValue.indexOf("=");
String field = annotationFieldValue.substring(0,j);
String fieldValue = annotationFieldValue.substring(j+1);
if (Utils.isPattern(fieldValue)) {
fieldValue = Utils.escapePattern(fieldValue);
}
Map<String, AnnotationFieldValue> ll = mapping.getAnnotationFieldValues().computeIfAbsent(annotation, value -> new HashMap<>());
ll.put(field, new AnnotationFieldValue(annotation, field, fieldValue, l));
continue;
}
if (LayerMetadata.CLASS.equals(k)) {
String val = l.getProperties().get(k);
String[] split = val.split(",");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!--
~ JBoss, Home of Professional Open Source.
~ Copyright 2024 Red Hat, Inc., and individual contributors
~ as indicated by the @author tags.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<layer-spec xmlns="urn:jboss:galleon:layer-spec:2.0" name="annotation-field-value">
<props>
<prop name="org.wildfly.rule.annotation.field.value-prop" value="org.wildfly.glow.test.rules.classes.annotation.field.value.FieldValue,prop=foo*"/>
</props>
</layer-spec>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2024 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.glow.rules.test.annotation.field.value;

import org.junit.Test;
import org.wildfly.glow.rules.test.AbstractLayerMetaDataTestCase;

public class FieldValueAnnotationTestCase extends AbstractLayerMetaDataTestCase {

@Test
public void testValueAnnotationUsage() {
testSingleClassWar(FieldValueAnnotationUsage.class, "annotation-field-value");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2024 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.glow.rules.test.annotation.field.value;

import org.wildfly.glow.test.rules.classes.annotation.field.value.FieldValue;

@FieldValue(prop = "foo bar")
public class FieldValueAnnotationUsage {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2024 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.glow.test.rules.classes.annotation.field.value;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldValue {
public String prop() default "";
}

0 comments on commit 4fdbcea

Please sign in to comment.