introduced some customization for the content list in admin
[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     try {
62       String allNewProducers = MirGlobal.getConfigProperty("Mir.Localizer.Producer.AllNewProducers");
63       allNewProducerTasks = ProducerEngine.ProducerTask.parseProducerTaskList(allNewProducers);
64
65       producerFactories = new Vector();
66       model = MirGlobal.localizer().dataModel().adapterModel();
67       generatorLibrary = MirGlobal.localizer().generators().makeProducerGeneratorLibrary();
68       writerEngine = MirGlobal.localizer().generators().makeWriterEngine();
69       nameToFactory = new HashMap();
70     }
71     catch (Throwable t) {
72       logger.printError("MirBasicProducerLocalizer(): Exception "+t.getMessage());
73       model = new EntityAdapterModel();
74     }
75   }
76
77   public List factories() throws MirLocalizerExc {
78     if (fileMonitor==null || producerFactories == null || fileMonitor.hasChanged()) {
79       try {
80         List newProducers = new Vector();
81         FileMonitor newFileMonitor = new FileMonitor();
82         setupFactories(newProducers, newFileMonitor);
83
84         producerFactories = newProducers;
85         fileMonitor = newFileMonitor;
86         logger.printInfo("MirBasicProducerLocalizer.factories(): successfully setup factories");
87
88         nameToFactory.clear();
89         Iterator i = producerFactories.iterator();
90         while (i.hasNext()) {
91           ProducerFactory factory = (ProducerFactory) i.next();
92           nameToFactory.put(factory.getName(), factory);
93         }
94       }
95       catch (Throwable t) {
96         logger.printError("MirBasicProducerLocalizer.factories(): Unable to setup factories: "+t.getMessage());
97       }
98     }
99
100     return producerFactories;
101   };
102
103   protected void setupProducerNodeBuilderLibrary(ProducerNodeBuilderLibrary aLibrary) throws MirLocalizerFailure {
104     try {
105       DefaultProducerNodeBuilders.registerBuilders(
106           aLibrary, model, generatorLibrary, writerEngine,
107           MirGlobal.getConfigProperty("Home"), MirGlobal.getConfigProperty("Producer.StorageRoot"));
108       SupplementalProducerNodeBuilders.registerBuilders(aLibrary, model);
109     }
110     catch (Throwable t) {
111       throw new MirLocalizerFailure(t.getMessage(), t);
112     }
113   }
114
115   protected void setupFactories(List aFactories, FileMonitor aFileMonitor) throws MirLocalizerExc, MirLocalizerFailure {
116     ProducerConfigReader reader;
117     ProducerNodeBuilderLibrary library = new ProducerNodeBuilderLibrary();
118     setupProducerNodeBuilderLibrary(library);
119     List usedFiles = new Vector();
120     Iterator i;
121
122     aFileMonitor.clear();
123     reader = new ProducerConfigReader();
124     reader.parseFile(MirGlobal.getConfigProperty("Home") + File.separatorChar + MirGlobal.getConfigProperty("Mir.Localizer.ProducerConfigFile"), library, aFactories, usedFiles);
125
126     i = usedFiles.iterator();
127     while (i.hasNext())
128       aFileMonitor.addFile((File) i.next());
129
130     setupFactories(aFactories);
131   }
132
133   protected void setupFactories(List aFactories) throws MirLocalizerExc, MirLocalizerFailure {
134     CompositeProducerNode node;
135
136     try {
137       aFactories.add(
138                    new CompositeProducerFactory("media", new ProducerFactory[] {
139                       new OldProducerAdapterFactory("images", new ProducerImages()),
140                       new OldProducerAdapterFactory("audio", new ProducerAudio()),
141                       new OldProducerAdapterFactory("video", new ProducerVideo()),
142                       new OldProducerAdapterFactory("other", new ProducerOther())
143                   } )
144       );
145     }
146     catch (Exception e) {
147       throw new MirLocalizerFailure(e);
148     }
149   };
150
151   public void produceAllNew() {
152     MirGlobal.producerEngine().addTasks(allNewProducerTasks);
153   };
154
155   public ProducerFactory getFactoryForName(String aName) {
156     try {
157       factories();
158     }
159     catch (Throwable t) {
160     }
161
162     return (ProducerFactory) nameToFactory.get(aName);
163   }
164 }