|
14 | 14 |
|
15 | 15 | package com.predic8.membrane.annot.beanregistry; |
16 | 16 |
|
17 | | -import com.fasterxml.jackson.databind.JsonNode; |
| 17 | +import com.predic8.membrane.annot.Grammar; |
| 18 | +import com.predic8.membrane.annot.bean.BeanFactory; |
| 19 | +import com.predic8.membrane.annot.yaml.GenericYamlParser; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +import java.util.concurrent.atomic.*; |
18 | 24 |
|
19 | 25 | public class BeanContainer { |
| 26 | + private static final Logger log = LoggerFactory.getLogger(BeanContainer.class); |
| 27 | + |
20 | 28 | private final BeanDefinition definition; |
21 | 29 | /** |
22 | 30 | * Constructed bean after initialization. |
23 | 31 | */ |
24 | | - private volatile Object singleton; |
| 32 | + private final AtomicReference<Object> singleton = new AtomicReference<>(); |
25 | 33 |
|
| 34 | + /** |
| 35 | + * Creates a BeanDefinition where the bean has not yet been instantiated and initialized. |
| 36 | + */ |
26 | 37 | public BeanContainer(BeanDefinition definition) { |
27 | 38 | this.definition = definition; |
28 | 39 | } |
29 | 40 |
|
| 41 | + /** |
| 42 | + * Creates a BeanDefinition where the bean has already been instantiated and initialized. |
| 43 | + */ |
| 44 | + public BeanContainer(BeanDefinition definition, Object singleton) { |
| 45 | + this.definition = definition; |
| 46 | + this.singleton.set(singleton); |
| 47 | + } |
30 | 48 |
|
31 | | - public Object getSingleton() { |
32 | | - return singleton; |
| 49 | + /** |
| 50 | + * Only to be used within this class. |
| 51 | + * Use {@link #getOrCreate(BeanRegistryImplementation, Grammar)} instead. |
| 52 | + */ |
| 53 | + private Object getSingleton() { |
| 54 | + return singleton.get(); |
33 | 55 | } |
34 | 56 |
|
35 | | - public void setSingleton(Object singleton) { |
36 | | - this.singleton = singleton; |
| 57 | + /** |
| 58 | + * Only to be used within this class. |
| 59 | + * Use {@link #getOrCreate(BeanRegistryImplementation, Grammar)} instead. |
| 60 | + */ |
| 61 | + private void setSingleton(Object singleton) { |
| 62 | + this.singleton.set(singleton); |
37 | 63 | } |
38 | 64 |
|
39 | 65 | public BeanDefinition getDefinition() { |
40 | 66 | return definition; |
41 | 67 | } |
| 68 | + |
| 69 | + @Override |
| 70 | + public String toString() { |
| 71 | + return "BeanContainer: %s of %s singleton: %s".formatted( definition.getName(),definition.getKind(),singleton.get()); |
| 72 | + } |
| 73 | + |
| 74 | + private synchronized Object define(BeanRegistryImplementation registry, Grammar grammar) { |
| 75 | + log.debug("defining bean: {}", definition.getNode()); |
| 76 | + try { |
| 77 | + if ("bean".equals(definition.getKind())) { |
| 78 | + return new BeanFactory(registry).create(definition.getNode().path("bean")); |
| 79 | + } |
| 80 | + return GenericYamlParser.readMembraneObject(definition.getKind(), |
| 81 | + grammar, |
| 82 | + definition.getNode(), |
| 83 | + registry); |
| 84 | + } catch (Exception e) { |
| 85 | + throw new RuntimeException(e); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public Object getOrCreate(BeanRegistryImplementation registry, Grammar grammar) { |
| 90 | + boolean prototype = isPrototypeScope(getDefinition()); |
| 91 | + |
| 92 | + // Prototypes are created anew every time. |
| 93 | + if (prototype) { |
| 94 | + return define(registry, grammar); |
| 95 | + } |
| 96 | + |
| 97 | + // Singleton: ensure define() runs at most once per BeanContainer. |
| 98 | + synchronized (this) { |
| 99 | + Object existing = getSingleton(); |
| 100 | + if (existing != null) { |
| 101 | + return existing; |
| 102 | + } |
| 103 | + |
| 104 | + Object created = define(registry, grammar); |
| 105 | + setSingleton(created); |
| 106 | + return created; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private static boolean isPrototypeScope(BeanDefinition bd) { |
| 111 | + if (!bd.isBean()) |
| 112 | + return bd.isPrototype(); |
| 113 | + |
| 114 | + return "PROTOTYPE".equalsIgnoreCase( |
| 115 | + bd.getNode().path("bean").path("scope").asText("SINGLETON") |
| 116 | + ); |
| 117 | + } |
| 118 | + |
42 | 119 | } |
0 commit comments