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 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.
32 package mircoders.servlet;
37 import javax.servlet.*;
38 import javax.servlet.http.*;
40 import org.apache.struts.util.MessageResources;
42 import freemarker.template.*;
46 import mir.producer.*;
47 import mir.generator.*;
48 import mir.producer.*;
49 import mir.entity.adapter.*;
53 import mircoders.global.*;
55 public class ServletModuleProducer extends ServletModule
57 private static ServletModuleProducer instance = new ServletModuleProducer();
58 public static ServletModule getInstance() { return instance; }
66 void generateResponse(String aGeneratorIdentifier, PrintWriter aWriter, Map aResponseData, Locale aLocale) throws ServletModuleException {
68 generator = MirGlobal.localizer().generators().makeAdminGeneratorLibrary().makeGenerator(aGeneratorIdentifier);
69 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(aResponseData);
70 aResponseData.put( "lang", new ResourceBundleGeneratorFunction( aLocale, MessageResources.getMessageResources("bundles.admin")));
71 generator.generate(aWriter, aResponseData, new PrintWriter(new NullWriter()));
74 throw new ServletModuleException(t.getMessage());
78 private ServletModuleProducer() {
79 logger = new LoggerWrapper("ServletModule.Producer");
80 defaultAction="showProducerQueueStatus";
83 public void showMessage(PrintWriter aWriter, Locale aLocale, String aMessage, String anArgument1, String anArgument2) throws ServletModuleException {
86 responseData = new HashMap();
87 responseData.put("message", aMessage);
88 responseData.put("argument1", anArgument1);
89 responseData.put("argument2", anArgument2);
90 generateResponse("infomessage.template", aWriter, responseData, aLocale);
93 throw new ServletModuleException(t.getMessage());
98 public void showProducerQueueStatus(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {
106 generator = MirGlobal.localizer().generators().makeAdminGeneratorLibrary().makeGenerator("producerqueue.template");
108 generationData = ServletHelper.makeGenerationData(getLocale(aRequest));
109 generationData.put( "thisurl", "module=Producer&do=showProducerQueueStatus");
111 producersData = new Vector();
112 Iterator i = MirGlobal.localizer().producers().factories().iterator();
113 while (i.hasNext()) {
114 ProducerFactory factory = (ProducerFactory) i.next();
116 List producerVerbs = new Vector();
117 Iterator j = factory.verbs();
118 while (j.hasNext()) {
119 Map verbData = new HashMap();
120 ProducerFactory.ProducerVerb verb = (ProducerFactory.ProducerVerb) j.next();
121 verbData.put("name", verb.getName());
122 verbData.put("description", verb.getDescription());
124 producerVerbs.add(verbData);
127 Map producerData = new HashMap();
128 producerData.put("name", factory.getName());
129 producerData.put("verbs", producerVerbs);
131 producersData.add(producerData);
133 generationData.put("producers", producersData);
135 generationData.put("queue", MirGlobal.producerEngine().getQueueStatus());
136 generator.generate(aResponse.getWriter(), generationData, new PrintWriter(new NullWriter()));
138 catch (Throwable t) {
139 t.printStackTrace(System.out);
140 throw new ServletModuleException(t.getMessage());
144 public void produce(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
146 * This method will only be called by external scripts (e.g. from cron jobs).
147 * The output therefore is very simple.
152 PrintWriter out = res.getWriter();
154 if (req.getParameter("producer")!=null) {
155 String producerParam = req.getParameter("producer");
156 String verbParam = req.getParameter("verb");
158 MirGlobal.producerEngine().addJob(producerParam, verbParam);
159 out.println("job added");
162 catch (Exception e) {
163 throw new ServletModuleException(e.getMessage());
167 public void produceAllNew(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {
169 MirGlobal.localizer().producers().produceAllNew();
170 showMessage(aResponse.getWriter(), getLocale(aRequest), "produceAllNewAddedToQueue", "", "");
172 catch (Exception e) {
173 throw new ServletModuleException(e.getMessage());
177 public void enqueue(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {
179 if (aRequest.getParameter("producer")!=null) {
180 String producerParam = aRequest.getParameter("producer");
181 String verbParam = aRequest.getParameter("verb");
183 MirGlobal.producerEngine().addJob(producerParam, verbParam);
185 showProducerQueueStatus(aRequest, aResponse);
188 catch (Exception e) {
189 throw new ServletModuleException(e.getMessage());
193 public void cancelAbortJob(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {