View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
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   * {@link Generator} proxy that combines a 'source' entity generator
43   * with variable support and ComponentBuilders.<br/><br/>
44   * Created: 29.08.2010 09:59:03
45   *
46   * @param <E> the type parameter
47   * @author Volker Bergmann
48   * @since 0.6.4
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     * Instantiates a new Source aware generator.
62     *
63     * @param instanceName instance name for the generated entities.
64     * @param source       another Generator of entities that serves as Entity builder.
65     *                     It may construct empty Entities or may import them (so this may overwrite imported attributes).
66     * @param components   the components
67     * @param context      the context
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    // Generator implementation ----------------------------------------------------------------------------------------
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   // java.lang.Object overrides --------------------------------------------------------------------------------------
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 }