001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.benerator.main;
028:
029: import java.util.Map;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.databene.benerator.Generator;
034: import org.databene.benerator.IllegalGeneratorStateException;
035: import org.databene.commons.Context;
036: import org.databene.model.data.Entity;
037:
038: /**
039: * Encapsulates variables, entity generator and context behind a Generator interface.<br/><br/>
040: * Created: 30.01.2008 20:45:21
041: * @since 0.4.0
042: * @author Volker Bergmann
043: */
044: public class ConfiguredGenerator implements Generator<Entity> {
045:
046: private static Log logger = LogFactory
047: .getLog(ConfiguredGenerator.class);
048:
049: private Generator<Entity> entityGenerator;
050: private Map<String, Generator<? extends Object>> variables;
051: private Context context;
052: private boolean variablesInitialized;
053:
054: public ConfiguredGenerator(Generator<Entity> entityGenerator,
055: Map<String, Generator<? extends Object>> variables,
056: Context context) {
057: this .entityGenerator = entityGenerator;
058: this .variables = variables;
059: this .context = context;
060: this .variablesInitialized = false;
061: }
062:
063: // Generator implementation ----------------------------------------------------------------------------------------
064:
065: public Class<Entity> getGeneratedType() {
066: return Entity.class;
067: }
068:
069: public void validate() {
070: for (Generator<? extends Object> varGen : variables.values())
071: varGen.validate();
072: entityGenerator.validate();
073: }
074:
075: public boolean available() {
076: if (!variablesInitialized) {
077: for (Generator<? extends Object> varGen : variables
078: .values()) {
079: if (!varGen.available()) {
080: logger.debug("No more available: " + varGen);
081: return false;
082: }
083: }
084: for (Map.Entry<String, Generator<? extends Object>> entry : variables
085: .entrySet())
086: context
087: .set(entry.getKey(), entry.getValue()
088: .generate());
089: variablesInitialized = true;
090: }
091: return entityGenerator.available();
092: }
093:
094: public Entity generate() {
095: if (!available())
096: throw new IllegalGeneratorStateException(
097: "Generator is not available");
098: Entity entity = entityGenerator.generate();
099: // for (String variableName : variables.keySet())
100: // context.remove(variableName);
101: variablesInitialized = false;
102: logger.debug("Generated " + entity);
103: return entity;
104: }
105:
106: public void reset() {
107: // for (String variableName : variables.keySet())
108: // context.set(variableName, null);
109: for (Generator<? extends Object> variable : variables.values())
110: variable.reset();
111: variablesInitialized = false;
112: entityGenerator.reset();
113: }
114:
115: public void close() {
116: for (Generator<? extends Object> variable : variables.values())
117: variable.close();
118: entityGenerator.close();
119: for (String variableName : variables.keySet())
120: context.remove(variableName);
121: }
122:
123: // java.lang.Object overrides --------------------------------------------------------------------------------------
124:
125: @Override
126: public String toString() {
127: return getClass().getSimpleName()
128: + "[\n"
129: + (variables.size() > 0 ? " variables" + variables
130: + "\n" : "") + " " + entityGenerator + "\n"
131: + "]";
132: }
133:
134: }
|