0c0f9cec60c1f50458565ef014a2f7d8a807e2c1
[mir.git] / source / mircoders / localizer / basic / MirBasicProducerLocalizer.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.localizer.basic;
33
34 import java.util.*;
35 import java.io.*;
36 import mir.producer.*;
37 import mir.generator.*;
38 import mir.producer.reader.*;
39 import mir.misc.*;
40 import mir.util.*;
41 import mir.entity.adapter.*;
42 import mircoders.global.*;
43 import mircoders.global.*;
44 import mircoders.localizer.*;
45 import mircoders.producer.reader.*;
46 import mircoders.producer.*;
47
48 public class MirBasicProducerLocalizer implements MirProducerLocalizer {
49   private List producerFactories;
50   private Map nameToFactory;
51   private List allNewProducerTasks;
52
53   protected FileMonitor fileMonitor;
54   protected EntityAdapterModel model;
55   protected Generator.GeneratorLibrary generatorLibrary;
56   protected WriterEngine writerEngine;
57
58   protected static Logfile logger = Logfile.getInstance( MirGlobal.getConfigProperty("Home") + "/" + MirGlobal.getConfigProperty("Mir.Localizer.Logfile"));
59
60   public MirBasicProducerLocalizer() {
61
62
63
64     try {
65       String allNewProducers = MirGlobal.getConfigProperty("Mir.Localizer.Producer.AllNewProducers");
66       allNewProducerTasks = ProducerEngine.ProducerTask.parseProducerTaskList(allNewProducers);
67
68       producerFactories = new Vector();
69       model = MirGlobal.localizer().dataModel().adapterModel();
70       generatorLibrary = MirGlobal.localizer().generators().makeProducerGeneratorLibrary();
71       writerEngine = MirGlobal.localizer().generators().makeWriterEngine();
72       nameToFactory = new HashMap();
73     }
74     catch (Throwable t) {
75       logger.printError("MirBasicProducerLocalizer(): Exception "+t.getMessage());
76       model = new EntityAdapterModel();
77     }
78   }
79
80   public List factories() throws MirLocalizerException {
81     if (fileMonitor==null || producerFactories == null || fileMonitor.hasChanged()) {
82       try {
83         List newProducers = new Vector();
84         FileMonitor newFileMonitor = new FileMonitor();
85         setupFactories(newProducers, newFileMonitor);
86
87         producerFactories = newProducers;
88         fileMonitor = newFileMonitor;
89         logger.printInfo("MirBasicProducerLocalizer.factories(): successfully setup factories");
90
91         nameToFactory.clear();
92         Iterator i = producerFactories.iterator();
93         while (i.hasNext()) {
94           ProducerFactory factory = (ProducerFactory) i.next();
95           nameToFactory.put(factory.getName(), factory);
96         }
97       }
98       catch (Throwable t) {
99         logger.printError("MirBasicProducerLocalizer.factories(): Unable to setup factories: "+t.getMessage());
100       }
101     }
102
103     return producerFactories;
104   };
105
106   protected void setupProducerNodeBuilderLibrary(ProducerNodeBuilderLibrary aLibrary) {
107     DefaultProducerNodeBuilders.registerBuilders(
108       aLibrary, model, generatorLibrary, writerEngine,
109       MirGlobal.getConfigProperty("Home"), MirGlobal.getConfigProperty("Producer.StorageRoot"));
110     SupplementalProducerNodeBuilders.registerBuilders(aLibrary, model);
111   }
112
113   protected void setupFactories(List aFactories, FileMonitor aFileMonitor) throws MirLocalizerException, MirLocalizerFailure {
114     ProducerConfigReader reader;
115     ProducerNodeBuilderLibrary library = new ProducerNodeBuilderLibrary();
116     setupProducerNodeBuilderLibrary(library);
117     List usedFiles = new Vector();
118     Iterator i;
119
120     aFileMonitor.clear();
121     reader = new ProducerConfigReader();
122     reader.parseFile(MirGlobal.getConfigProperty("Home") + File.separatorChar + MirGlobal.getConfigProperty("Mir.Localizer.ProducerConfigFile"), library, aFactories, usedFiles);
123
124     i = usedFiles.iterator();
125     while (i.hasNext())
126       aFileMonitor.addFile((File) i.next());
127
128     setupFactories(aFactories);
129   }
130
131   protected void setupFactories(List aFactories) throws MirLocalizerException, MirLocalizerFailure {
132     CompositeProducerNode node;
133
134     try {
135       aFactories.add(
136                    new CompositeProducerFactory("media", new ProducerFactory[] {
137                       new OldProducerAdapterFactory("images", new ProducerImages()),
138                       new OldProducerAdapterFactory("audio", new ProducerAudio()),
139                       new OldProducerAdapterFactory("video", new ProducerVideo()),
140                       new OldProducerAdapterFactory("other", new ProducerOther())
141                   } )
142       );
143     }
144     catch (Exception e) {
145       throw new MirLocalizerFailure(e);
146     }
147   };
148
149   public void produceAllNew() {
150     MirGlobal.producerEngine().addTasks(allNewProducerTasks);
151   };
152
153   public ProducerFactory getFactoryForName(String aName) {
154     return (ProducerFactory) nameToFactory.get(aName);
155   }
156 }