/*
- * Copyright (C) 2001, 2002 The Mir-coders group
+ * Copyright (C) 2001, 2002 The Mir-coders group
*
* This file is part of Mir.
*
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with the com.oreilly.servlet library, any library
- * licensed under the Apache Software License, The Sun (tm) Java Advanced
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of
- * the above that use the same license as the above), and distribute linked
- * combinations including the two. You must obey the GNU General Public
- * License in all respects for all of the code used other than the above
- * mentioned libraries. If you modify this file, you may extend this exception
- * to your version of the file, but you are not obligated to do so. If you do
- * not wish to do so, delete this exception statement from your version.
+ * the code of this program with any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two. You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries. If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
*/
-
package mircoders.localizer.basic;
import java.io.File;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import java.util.Vector;
import mir.entity.adapter.EntityAdapterModel;
import mir.generator.Generator;
import mir.generator.WriterEngine;
import mir.log.LoggerWrapper;
-import mir.producer.ProducerFactory;
import mir.producer.reader.DefaultProducerNodeBuilders;
import mir.producer.reader.ProducerConfigReader;
import mir.producer.reader.ProducerNodeBuilderLibrary;
-import mir.util.FileMonitor;
+import mir.util.StringRoutines;
import mircoders.global.MirGlobal;
import mircoders.global.ProducerEngine;
import mircoders.localizer.MirLocalizerExc;
import mircoders.producer.reader.SupplementalProducerNodeBuilders;
public class MirBasicProducerLocalizer implements MirProducerLocalizer {
- private List producerFactories;
- private Map nameToFactory;
- private List allNewProducerTasks;
+ private Map producerRecipes;
+ private List producerRecipeNames;
- protected FileMonitor fileMonitor;
protected EntityAdapterModel model;
- protected Generator.GeneratorLibrary generatorLibrary;
+ protected Generator.Library generatorLibrary;
protected WriterEngine writerEngine;
protected LoggerWrapper logger;
try {
logger = new LoggerWrapper("Localizer.Basic.Producer");
+ producerRecipes = new HashMap();
+ producerRecipeNames = new ArrayList();
+
+ String[] recipes = MirGlobal.config().getStringArray("Mir.Localizer.Producer.ProducerRecipes");
+ for (int i = 0; i<recipes.length; i++) {
+ try {
+ List parts = StringRoutines.separateString(recipes[i], "=");
+ if (parts.size() == 2) {
+ String key = ((String) parts.get(0)).trim();
+ producerRecipes.put(key, ProducerEngine.ProducerTask.parseProducerTaskList(
+ ((String) parts.get(1)).trim()));
+ producerRecipeNames.add(key);
+ }
+ else {
+ throw new Exception("'=' expected");
+ }
+ }
+ catch (Throwable t) {
+ logger.error("Error while processing producer recipe '" + recipes[i] + "': " + t.toString());
+ }
+ }
+
+ // for backward compatibility:
String allNewProducers = MirGlobal.config().getString("Mir.Localizer.Producer.AllNewProducers");
- allNewProducerTasks = ProducerEngine.ProducerTask.parseProducerTaskList(allNewProducers);
+ if (allNewProducers!=null && allNewProducers.length()>0) {
+ producerRecipes.put("allnew", ProducerEngine.ProducerTask.parseProducerTaskList(allNewProducers));
+
+ if (!producerRecipeNames.contains("allnew")) {
+ producerRecipeNames.add("allnew");
+ }
+ }
- producerFactories = new Vector();
model = MirGlobal.localizer().dataModel().adapterModel();
generatorLibrary = MirGlobal.localizer().generators().makeProducerGeneratorLibrary();
writerEngine = MirGlobal.localizer().generators().makeWriterEngine();
- nameToFactory = new HashMap();
}
catch (Throwable t) {
logger.error("MirBasicProducerLocalizer(): Exception "+t.getMessage());
}
}
- public List factories() throws MirLocalizerExc {
- if (fileMonitor==null || producerFactories == null || fileMonitor.hasChanged()) {
- try {
- List newProducers = new Vector();
- FileMonitor newFileMonitor = new FileMonitor();
- setupFactories(newProducers, newFileMonitor);
-
- producerFactories = newProducers;
- fileMonitor = newFileMonitor;
- logger.info("MirBasicProducerLocalizer.factories(): successfully setup factories");
-
- nameToFactory.clear();
- Iterator i = producerFactories.iterator();
- while (i.hasNext()) {
- ProducerFactory factory = (ProducerFactory) i.next();
- nameToFactory.put(factory.getName(), factory);
- }
- }
- catch (Throwable t) {
- logger.error("MirBasicProducerLocalizer.factories(): Unable to setup factories: "+t.getMessage());
- t.printStackTrace(logger.asPrintWriter(logger.DEBUG_MESSAGE));
- }
- }
+ public List getRecipeNames() throws MirLocalizerExc, MirLocalizerFailure {
+ return producerRecipeNames;
+ }
- return producerFactories;
- };
+ public void produceRecipe(String aName) throws MirLocalizerExc, MirLocalizerFailure {
+ if (producerRecipes.containsKey(aName)) {
+ Iterator i = ((List) producerRecipes.get(aName)).iterator();
- protected void setupProducerNodeBuilderLibrary(ProducerNodeBuilderLibrary aLibrary) throws MirLocalizerFailure {
- try {
- DefaultProducerNodeBuilders.registerBuilders(
- aLibrary, model, generatorLibrary, writerEngine,
- MirGlobal.config().getString("Home"), MirGlobal.config().getString("Producer.StorageRoot"));
- SupplementalProducerNodeBuilders.registerBuilders(aLibrary, model);
- }
- catch (Throwable t) {
- throw new MirLocalizerFailure(t.getMessage(), t);
+ while (i.hasNext()) {
+ ProducerEngine.ProducerTask task = (ProducerEngine.ProducerTask) i.next();
+
+ try {
+ MirGlobal.getProducerEngine().addTask(task);
+ }
+ catch (Throwable t) {
+ logger.error("Error recipe "+aName+" tasks "+
+ task.getProducer()+"::"+task.getVerb()+": " + t.toString());
+ }
+ }
}
+ else
+ throw new MirLocalizerExc("Unknown recipe name: " + aName);
}
- protected void setupFactories(List aFactories, FileMonitor aFileMonitor) throws MirLocalizerExc, MirLocalizerFailure {
- ProducerConfigReader reader;
- ProducerNodeBuilderLibrary library = new ProducerNodeBuilderLibrary();
- setupProducerNodeBuilderLibrary(library);
- List usedFiles = new Vector();
- Iterator i;
-
- aFileMonitor.clear();
- reader = new ProducerConfigReader();
- reader.parseFile(MirGlobal.config().getString("Home") + File.separatorChar + MirGlobal.config().getString("Mir.Localizer.ProducerConfigFile"), library, aFactories, usedFiles);
+ public void beforeProducerTask(String aFactory, String aVerb) {
+ }
- i = usedFiles.iterator();
- while (i.hasNext())
- aFileMonitor.addFile((File) i.next());
+ public void afterProducerTask(String aFactory, String aVerb) {
}
- public void produceAllNew() {
- MirGlobal.producerEngine().addTasks(allNewProducerTasks);
- };
+ /**
+ * Loads factories from a file with a {@link ProducerConfigReader}
+ */
+ public List loadFactories() throws MirLocalizerExc {
+ try {
+ List producers = new ArrayList();
+ ProducerConfigReader reader;
+ ProducerNodeBuilderLibrary library = new ProducerNodeBuilderLibrary();
+ setupProducerNodeBuilderLibrary(library);
+ reader = new ProducerConfigReader();
+ File inputFile =
+ MirGlobal.config().getFile("Mir.Localizer.ProducerConfigFile");
+ reader.parse(inputFile, library, producers);
+
+ logger.info("MirBasicProducerLocalizer.loadFactories(): successfully loaded factories");
+
+ return producers;
+ }
+ catch (Throwable t) {
+ logger.error("MirBasicProducerLocalizer.loadFactories(): Unable to load factories: "+
+ t.getMessage());
+ throw new MirLocalizerFailure(t);
+ }
+ }
- public ProducerFactory getFactoryForName(String aName) {
+ /**
+ * Sets up a {@link ProducerNodeBuilderLibrary} for use by the producer
+ * definition reader. Can be overridden by subclasses to tweak the standard
+ * library.
+ */
+ protected void setupProducerNodeBuilderLibrary(ProducerNodeBuilderLibrary aLibrary) throws MirLocalizerFailure {
try {
- factories();
+ DefaultProducerNodeBuilders.registerBuilders(
+ aLibrary, model, generatorLibrary,
+ MirGlobal.localizer().producerAssistant().createGenerationInterceptor(),
+ writerEngine,
+ MirGlobal.config().getHome(),
+ MirGlobal.config().getFile("Producer.StorageRoot"));
+ SupplementalProducerNodeBuilders.registerBuilders(aLibrary, MirGlobal.config().getHome());
}
catch (Throwable t) {
+ throw new MirLocalizerFailure(t.getMessage(), t);
}
-
- return (ProducerFactory) nameToFactory.get(aName);
}
+
}