2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
30 package mircoders.localizer.basic;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
37 import java.util.Vector;
39 import mir.entity.adapter.EntityAdapterModel;
40 import mir.generator.Generator;
41 import mir.generator.WriterEngine;
42 import mir.log.LoggerWrapper;
43 import mir.producer.ProducerFactory;
44 import mir.producer.reader.DefaultProducerNodeBuilders;
45 import mir.producer.reader.ProducerConfigReader;
46 import mir.producer.reader.ProducerNodeBuilderLibrary;
47 import mir.util.FileMonitor;
48 import mir.util.StringRoutines;
49 import mircoders.global.MirGlobal;
50 import mircoders.global.ProducerEngine;
51 import mircoders.localizer.MirLocalizerExc;
52 import mircoders.localizer.MirLocalizerFailure;
53 import mircoders.localizer.MirProducerLocalizer;
54 import mircoders.producer.reader.SupplementalProducerNodeBuilders;
56 public class MirBasicProducerLocalizer implements MirProducerLocalizer {
57 private List producerFactories;
58 private Map nameToFactory;
60 private Map producerRecipes;
61 private List producerRecipeNames;
63 protected FileMonitor fileMonitor;
64 protected EntityAdapterModel model;
65 protected Generator.GeneratorLibrary generatorLibrary;
66 protected WriterEngine writerEngine;
68 protected LoggerWrapper logger;
70 public MirBasicProducerLocalizer() {
72 logger = new LoggerWrapper("Localizer.Basic.Producer");
74 producerRecipes = new HashMap();
75 producerRecipeNames = new Vector();
77 String[] recipes = MirGlobal.config().getStringArray("Mir.Localizer.Producer.ProducerRecipes");
78 for (int i = 0; i<recipes.length; i++) {
80 List parts = StringRoutines.separateString(recipes[i], "=");
81 if (parts.size() == 2) {
82 producerRecipes.put(parts.get(0), ProducerEngine.ProducerTask.parseProducerTaskList( (String) parts.get(1)));
83 producerRecipeNames.add(parts.get(0));
86 throw new Exception("'=' expected");
90 logger.error("Error while processing producer recipe '" + recipes[i] + "': " + t.toString());
94 // for backward compatibility:
95 String allNewProducers = MirGlobal.config().getString("Mir.Localizer.Producer.AllNewProducers");
96 if (allNewProducers!=null && allNewProducers.length()>0) {
97 producerRecipes.put("allnew", ProducerEngine.ProducerTask.parseProducerTaskList(allNewProducers));
98 producerRecipeNames.add("allnew");
101 producerFactories = new Vector();
102 model = MirGlobal.localizer().dataModel().adapterModel();
103 generatorLibrary = MirGlobal.localizer().generators().makeProducerGeneratorLibrary();
104 writerEngine = MirGlobal.localizer().generators().makeWriterEngine();
105 nameToFactory = new HashMap();
107 catch (Throwable t) {
108 logger.error("MirBasicProducerLocalizer(): Exception "+t.getMessage());
109 model = new EntityAdapterModel();
113 public List getRecipeNames() throws MirLocalizerExc, MirLocalizerFailure {
114 return producerRecipeNames;
117 public void produceRecipe(String aName) throws MirLocalizerExc, MirLocalizerFailure {
118 if (producerRecipes.containsKey(aName))
119 MirGlobal.producerEngine().addTasks((List) producerRecipes.get(aName));
121 throw new MirLocalizerExc("Unknown recipe name: " + aName);
124 public List factories() throws MirLocalizerExc {
125 if (fileMonitor==null || producerFactories == null || fileMonitor.hasChanged()) {
127 List newProducers = new Vector();
128 FileMonitor newFileMonitor = new FileMonitor();
129 setupFactories(newProducers, newFileMonitor);
131 producerFactories = newProducers;
132 fileMonitor = newFileMonitor;
133 logger.info("MirBasicProducerLocalizer.factories(): successfully setup factories");
135 nameToFactory.clear();
136 Iterator i = producerFactories.iterator();
137 while (i.hasNext()) {
138 ProducerFactory factory = (ProducerFactory) i.next();
139 nameToFactory.put(factory.getName(), factory);
142 catch (Throwable t) {
143 logger.error("MirBasicProducerLocalizer.factories(): Unable to setup factories: "+t.getMessage());
144 t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
148 return producerFactories;
151 protected void setupProducerNodeBuilderLibrary(ProducerNodeBuilderLibrary aLibrary) throws MirLocalizerFailure {
153 DefaultProducerNodeBuilders.registerBuilders(
154 aLibrary, model, generatorLibrary, writerEngine,
155 MirGlobal.config().getString("Home"), MirGlobal.config().getString("Producer.StorageRoot"));
156 SupplementalProducerNodeBuilders.registerBuilders(aLibrary, model);
158 catch (Throwable t) {
159 throw new MirLocalizerFailure(t.getMessage(), t);
163 protected void setupFactories(List aFactories, FileMonitor aFileMonitor) throws MirLocalizerExc, MirLocalizerFailure {
164 ProducerConfigReader reader;
165 ProducerNodeBuilderLibrary library = new ProducerNodeBuilderLibrary();
166 setupProducerNodeBuilderLibrary(library);
167 List usedFiles = new Vector();
170 aFileMonitor.clear();
171 reader = new ProducerConfigReader();
172 reader.parseFile(MirGlobal.config().getString("Home") + File.separatorChar + MirGlobal.config().getString("Mir.Localizer.ProducerConfigFile"), library, aFactories, usedFiles);
174 i = usedFiles.iterator();
176 aFileMonitor.addFile((File) i.next());
179 public ProducerFactory getFactoryForName(String aName) {
183 catch (Throwable t) {
186 return (ProducerFactory) nameToFactory.get(aName);