Skip to content

Commit 32b06eb

Browse files
committed
minor: refactoring
1 parent 63bdad8 commit 32b06eb

File tree

7 files changed

+41
-17
lines changed

7 files changed

+41
-17
lines changed

annot/src/main/java/com/predic8/membrane/annot/yaml/BeanDefinition.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
public class BeanDefinition {
2121

22-
public static String PROTOTYPE = "prototype";
22+
public static final String PROTOTYPE = "prototype";
2323

2424
private final String name;
2525
private final String namespace;
@@ -31,9 +31,8 @@ public class BeanDefinition {
3131

3232
/**
3333
* Only called from K8S.
34-
* TODO Maybe change constructur to static create4Kubernetes
3534
*/
36-
public BeanDefinition(WatchAction action, JsonNode node) {
35+
private BeanDefinition(WatchAction action, JsonNode node) {
3736
this.action = action;
3837
this.node = node;
3938
JsonNode metadata = node.get("metadata"); // TODO What if metadata is null?
@@ -48,6 +47,10 @@ public BeanDefinition(WatchAction action, JsonNode node) {
4847
uid = metadata.get("uid").asText();
4948
}
5049

50+
public static BeanDefinition create4Kubernetes(WatchAction action, JsonNode node) {
51+
return new BeanDefinition(action, node);
52+
}
53+
5154
public BeanDefinition(String kind, String name, String namespace, String uid, JsonNode node) {
5255
this.kind = kind;
5356
this.name = name;

annot/src/main/java/com/predic8/membrane/annot/yaml/BeanRegistry.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
limitations under the License. */
1414
package com.predic8.membrane.annot.yaml;
1515

16+
import com.predic8.membrane.annot.*;
17+
1618
import java.util.List;
1719

1820
public interface BeanRegistry {
@@ -23,4 +25,6 @@ public interface BeanRegistry {
2325

2426
void registerBeanDefinitions(List<BeanDefinition> beanDefinitions);
2527

28+
Grammar getGrammar();
29+
2630
}

annot/src/main/java/com/predic8/membrane/annot/yaml/BeanRegistryImplementation.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.concurrent.*;
2424
import java.util.concurrent.atomic.*;
2525

26+
import static com.predic8.membrane.annot.yaml.BeanDefinition.create4Kubernetes;
2627
import static com.predic8.membrane.annot.yaml.WatchAction.*;
2728

2829
public class BeanRegistryImplementation implements BeanRegistry {
@@ -105,7 +106,7 @@ private Object define(BeanDefinition bd) throws IOException, ParsingException {
105106
* May be called from multiple threads.
106107
*/
107108
public void handle(WatchAction action, JsonNode node) {
108-
changeEvents.add(new BeanDefinitionChanged(new BeanDefinition(action, node)));
109+
changeEvents.add(new BeanDefinitionChanged(create4Kubernetes(action, node)));
109110
}
110111

111112
/**
@@ -164,7 +165,7 @@ private void activationRun() {
164165
bds.remove(bd.getUid());
165166
}
166167
uidsToRemove.add(bd.getUid());
167-
} catch (Throwable e) {
168+
} catch (Exception e) {
168169
log.error("Could not handle {} {}/{}", bd.getAction(), bd.getNamespace(), bd.getName(), e);
169170
}
170171
}
@@ -206,4 +207,9 @@ public Object resolveReference(String url) {
206207
public List<Object> getBeans() {
207208
return bds.values().stream().map(BeanDefinition::getBean).filter(Objects::nonNull).toList();
208209
}
210+
211+
@Override
212+
public Grammar getGrammar() {
213+
return grammar;
214+
}
209215
}

annot/src/main/java/com/predic8/membrane/annot/yaml/ChangeEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ record BeanDefinitionChanged(BeanDefinition bd) implements ChangeEvent {
2121
}
2222

2323
/**
24-
* TODO What does this exactly mean? Is the complete configuration loaded?
24+
* Signals that all static configuration (e.g., from YAML files) has been
25+
* passed to the registry and initial activation can proceed.
2526
*/
2627
record StaticConfigurationLoaded() implements ChangeEvent {
2728
}

annot/src/main/java/com/predic8/membrane/annot/yaml/MethodSetter.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,16 @@ public <T> void setSetter(T instance, ParsingContext ctx, JsonNode node, String
8282

8383
private Object resolveSetterValue(ParsingContext ctx, JsonNode node, String key) throws WrongEnumConstantException, ParsingException {
8484
Class<?> wanted = getParameterType();
85-
if (wanted.equals(List.class) || wanted.equals(Collection.class)) return parseListIncludingStartEvent(ctx, node);
85+
if (Collection.class.isAssignableFrom(wanted))
86+
return parseListIncludingStartEvent(ctx, node);
8687

8788
if (wanted.isEnum()) return parseEnum(wanted, node);
8889
if (wanted.equals(String.class)) return node.asText();
89-
if (wanted.equals(Integer.TYPE)) return parseInt(node.asText());
90-
if (wanted.equals(Long.TYPE)) return parseLong(node.asText());
91-
if (wanted.equals(Boolean.TYPE)) return parseBoolean(node.asText());
90+
91+
if (wanted == Integer.TYPE || wanted == Integer.class) return parseInt(node.asText());
92+
if (wanted == Long.TYPE || wanted == Long.class) return parseLong(node.asText());
93+
if (wanted == Boolean.TYPE || wanted == Boolean.class) return parseBoolean(node.asText());
94+
9295
if (wanted.equals(Map.class) && McYamlIntrospector.hasOtherAttributes(setter)) return Map.of(key, node.asText());
9396
if (McYamlIntrospector.isStructured(setter)) {
9497
if (beanClass != null) return createAndPopulateNode(ctx.updateContext(key), beanClass, node);

core/src/main/java/com/predic8/membrane/core/kubernetes/KubernetesWatcher.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ public class KubernetesWatcher {
4040
private static final Logger LOG = LoggerFactory.getLogger(KubernetesWatcher.class);
4141

4242
private final Router router;
43-
private final BeanRegistryImplementation beanCache;
43+
private final BeanRegistryImplementation beanRegistry;
4444
private KubernetesClient client;
4545
private ExecutorService executors;
4646
private final ConcurrentHashMap<String, Closeable> watches = new ConcurrentHashMap<>();
4747

4848
public KubernetesWatcher(Router router) {
4949
this.router = router;
50-
this.beanCache = new BeanRegistryImplementation(router, new GrammarAutoGenerated());
50+
this.beanRegistry = new BeanRegistryImplementation(router, new GrammarAutoGenerated());
5151
}
5252

5353
public void start() {
@@ -56,11 +56,11 @@ public void start() {
5656
return;
5757
}
5858

59-
beanCache.start();
59+
beanRegistry.start();
6060

6161
client = getClient();
6262

63-
List<String> crds = new GrammarAutoGenerated().getCrdSingularNames();
63+
List<String> crds = beanRegistry.getGrammar().getCrdSingularNames();
6464
if (kvi.get().getResourcesList().size() > 0)
6565
crds = crds.stream().filter(s -> kvi.get().getResourcesList().contains(s)).toList();
6666
if (crds.size() > 0)
@@ -78,7 +78,7 @@ public void stop() {
7878
} catch (IOException e) {
7979
}
8080
});
81-
beanCache.stop();
81+
beanRegistry.stop();
8282
}
8383

8484
private KubernetesClient getClient() {
@@ -110,7 +110,7 @@ public void onEvent(WatchAction action, JsonNode node) {
110110
node.get("metadata").get("namespace").asText(),
111111
node.get("metadata").get("name").asText()));
112112

113-
beanCache.handle(action, node.get("spec"));
113+
beanRegistry.handle(action, node.get("spec"));
114114
} catch (Exception e) {
115115
e.printStackTrace();
116116
}

core/src/test/java/com/predic8/membrane/core/kubernetes/GenericYamlParserTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
@TestInstance(Lifecycle.PER_CLASS)
4949
public class GenericYamlParserTest {
5050

51+
private static final ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
52+
5153
private static final Grammar K8S_HELPER = new GrammarAutoGenerated();
5254

5355
@ParameterizedTest
@@ -342,6 +344,11 @@ public List<Object> getBeans() {
342344
public void registerBeanDefinitions(List<BeanDefinition> beanDefinitions) {
343345

344346
}
347+
348+
@Override
349+
public Grammar getGrammar() {
350+
return null;
351+
}
345352
}
346353

347354
private static APIProxy parse(String yaml, BeanRegistry reg) {
@@ -350,7 +357,7 @@ private static APIProxy parse(String yaml, BeanRegistry reg) {
350357

351358
public static JsonNode parse(String yaml) {
352359
try {
353-
return new ObjectMapper(new YAMLFactory()).readTree(yaml);
360+
return yamlMapper.readTree(yaml);
354361
} catch (JsonProcessingException e) {
355362
throw new RuntimeException(e);
356363
}

0 commit comments

Comments
 (0)