1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package com.rapiddweller.benerator.composite;
28
29 import com.rapiddweller.benerator.BeneratorConstants;
30 import com.rapiddweller.benerator.Generator;
31 import com.rapiddweller.benerator.GeneratorContext;
32 import com.rapiddweller.benerator.engine.BeneratorContext;
33 import com.rapiddweller.benerator.wrapper.GeneratorProxy;
34 import com.rapiddweller.benerator.wrapper.ProductWrapper;
35 import com.rapiddweller.common.MessageHolder;
36 import org.apache.logging.log4j.LogManager;
37 import org.apache.logging.log4j.Logger;
38
39 import java.util.List;
40
41
42
43
44
45
46
47
48
49
50 public class SourceAwareGenerator<E> extends GeneratorProxy<E> implements MessageHolder {
51
52 private static final Logger LOGGER = LogManager.getLogger(SourceAwareGenerator.class);
53 private static final Logger STATE_LOGGER = LogManager.getLogger(BeneratorConstants.STATE_LOGGER);
54
55 private final String instanceName;
56 private E currentInstance;
57 private String message;
58 private final ComponentAndVariableSupport<E> support;
59
60
61
62
63
64
65
66
67
68
69 public SourceAwareGenerator(String instanceName, Generator<E> source,
70 List<GeneratorComponent<E>> components, BeneratorContext context) {
71 super(source);
72 this.instanceName = instanceName;
73 this.support = new ComponentAndVariableSupport<>(instanceName, components, context);
74 this.context = context;
75 }
76
77
78
79 @Override
80 public void init(GeneratorContext context) {
81 support.init((BeneratorContext) context);
82 super.init(context);
83 }
84
85 @SuppressWarnings("null")
86 @Override
87 public ProductWrapper<E> generate(ProductWrapper<E> wrapper) {
88 wrapper = getSource().generate(wrapper);
89 boolean available = (wrapper != null);
90 if (!available) {
91 STATE_LOGGER.debug("Source for entity '{}' is not available: {}", instanceName, getSource());
92 }
93 if (available) {
94 currentInstance = wrapper.unwrap();
95 if (instanceName != null) {
96 context.set(instanceName, currentInstance);
97 }
98 available = support.apply(currentInstance, (BeneratorContext) context);
99 }
100 if (available) {
101 LOGGER.debug("Generated {}", currentInstance);
102 return wrapper.wrap(currentInstance);
103 } else {
104 currentInstance = null;
105 if (instanceName != null) {
106 context.remove(instanceName);
107 }
108 return null;
109 }
110 }
111
112 @Override
113 public void reset() {
114 support.reset();
115 super.reset();
116 }
117
118 @Override
119 public void close() {
120 support.close();
121 super.close();
122 }
123
124 @Override
125 public String getMessage() {
126 if (message != null) {
127 return message;
128 }
129 Generator<E> source = getSource();
130 if (source instanceof MessageHolder && ((MessageHolder) source).getMessage() != null) {
131 return ((MessageHolder) source).getMessage();
132 }
133 return support.getMessage();
134 }
135
136
137
138
139 @Override
140 public String toString() {
141 return getClass().getSimpleName() + "[" + getSource() + "]";
142 }
143
144 @Override
145 public boolean isParallelizable() {
146 return getSource().isParallelizable() && support.isParallelizable();
147 }
148
149 @Override
150 public boolean isThreadSafe() {
151 return getSource().isThreadSafe() && support.isThreadSafe();
152 }
153
154 }