source: main/trunk/gli/src/org/greenstone/gatherer/greenstone/Plugins.java@ 31636

Last change on this file since 31636 was 31636, checked in by ak19, 7 years ago

First phase of shifting gli code to use SafeProcess instead of Java Process. This phase takes care of all the easy cases.

  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: Michael Dewsnip, NZDL Project, University of Waikato
9 *
10 * Copyright (C) 2006 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.greenstone;
28
29import java.io.*;
30import java.util.*;
31import javax.swing.*;
32import org.greenstone.gatherer.Configuration;
33import org.greenstone.gatherer.DebugStream;
34import org.greenstone.gatherer.Dictionary;
35import org.greenstone.gatherer.Gatherer;
36import org.greenstone.gatherer.cdm.Argument;
37import org.greenstone.gatherer.cdm.Plugin;
38import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
39import org.greenstone.gatherer.util.SafeProcess;
40import org.greenstone.gatherer.util.StaticStrings;
41import org.greenstone.gatherer.util.Utility;
42import org.greenstone.gatherer.util.XMLTools;
43import org.w3c.dom.*;
44import org.xml.sax.*;
45
46
47/** This class is for maintaining a list of known plug-ins, and importing new plugins using the parser. */
48public class Plugins
49{
50 // A list of all the plugins in the core Greenstone "perllib/plugins" folder (arguments may not be loaded)
51 static private ArrayList core_greenstone_plugins_list = null;
52 // The name of the loaded collection
53 static private String collection_name = null;
54 // A list of all the plugins in the loaded collection's "perllib/plugins" folder (arguments may not be loaded)
55 static private ArrayList collection_specific_plugins_list = new ArrayList();
56
57
58 static public Plugin getPlugin(String plugin_name, boolean arguments_required)
59 {
60 Plugin plugin = null;
61 boolean collection_specific = false;
62
63 // Check the collection-specific plugins first
64 for (int i = 0; i < collection_specific_plugins_list.size(); i++) {
65 Plugin collection_specific_plugin = (Plugin) collection_specific_plugins_list.get(i);
66 if (collection_specific_plugin.getName().equals(plugin_name)) {
67 plugin = collection_specific_plugin;
68 collection_specific = true;
69 break;
70 }
71 }
72
73 // Try the core Greenstone plugins if necessary
74 if (plugin == null) {
75 for (int i = 0; i < core_greenstone_plugins_list.size(); i++) {
76 Plugin core_greenstone_plugin = (Plugin) core_greenstone_plugins_list.get(i);
77 if (core_greenstone_plugin.getName().equals(plugin_name)) {
78 plugin = core_greenstone_plugin;
79 break;
80 }
81 }
82 }
83
84 // If we've found the plugin, load its arguments now, if required
85 if (plugin != null && arguments_required) {
86 if (!plugin.hasLoadedOptions()) {
87 loadPluginInfo(plugin, collection_specific);
88 }
89 else {
90 DebugStream.println("Already loaded arguments for " + plugin_name + "!");
91 }
92 }
93
94 return plugin;
95 }
96
97
98 /** Returns a new list from merging the collection-specific and the core Greenstone plugins. */
99 static public ArrayList getPluginsList()
100 {
101 ArrayList plugins_list = new ArrayList();
102 plugins_list.addAll(collection_specific_plugins_list);
103
104 // Add in the core Greenstone plugins, taking care not to overwrite any collection-specific ones
105 for (int i = 0; i < core_greenstone_plugins_list.size(); i++) {
106 Plugin core_greenstone_plugin = (Plugin) core_greenstone_plugins_list.get(i);
107
108 boolean found = false;
109 for (int j = 0; j < collection_specific_plugins_list.size(); j++) {
110 Plugin collection_specific_plugin = (Plugin) collection_specific_plugins_list.get(j);
111 if (core_greenstone_plugin.getName().equals(collection_specific_plugin.getName())) {
112 found = true;
113 break;
114 }
115 }
116
117 if (!found) {
118 plugins_list.add(core_greenstone_plugin);
119 }
120 }
121
122 return plugins_list;
123 }
124
125
126 static private void loadPluginInfo(Plugin plugin, boolean collection_specific)
127 {
128 DebugStream.println("Loading arguments for " + plugin.getName() + "...");
129
130 // Run pluginfo.pl to get the list of plugins
131 try {
132 String pluginfo_xml = null;
133 if (Gatherer.isGsdlRemote) {
134 String pluginfo_options = "&plugin=" + plugin;
135 if (collection_specific) {
136 pluginfo_options += "&collection=" + collection_name;
137 }
138 pluginfo_xml = Gatherer.remoteGreenstoneServer.getScriptOptions("pluginfo.pl", pluginfo_options);
139 }
140 else {
141 ArrayList args = new ArrayList();
142 args.add(Configuration.perl_path);
143 args.add("-S");
144 args.add(LocalGreenstone.getBinScriptDirectoryPath() + "pluginfo.pl");
145 args.add("-gs_version");
146 if (Gatherer.GS3) {
147 args.add("3");
148 } else {
149 args.add("2");
150 }
151 if (collection_specific) {
152 args.add("-collection");
153 args.add(collection_name);
154 if (Gatherer.GS3) {
155 args.add("-site");
156 args.add(Configuration.site_name);
157 }
158 }
159 args.add("-xml");
160 args.add("-language");
161 args.add(Configuration.getLanguage());
162 args.add(plugin.getName());
163
164 // Run the pluginfo.pl process:
165 // Create the process.
166 SafeProcess process = new SafeProcess((String[]) args.toArray(new String[] { }));
167
168 // run the SafeProcess
169 int exitVal = process.runProcess();
170 if(exitVal != 0) {
171 throw new Exception("*** Error running pluginfo.pl loadPlugInfo, process exited with: "
172 + exitVal);
173 }
174 // get the result: We expect XML to have come out of the process std error stream.
175 pluginfo_xml = process.getStdError();
176 ///System.err.println("*********\nPluginInfo, got:\n" + pluginfo_xml + "\n**********\n");
177 }
178
179 // Check the XML output was obtained successfully
180 if (pluginfo_xml == null || pluginfo_xml.length() == 0) {
181 plugin.setHasLoadedOptions(false); // failure to load options
182 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.PlugInManager.PlugIn_XML_Parse_Failed", plugin.getName()), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
183 return;
184 } else {
185 plugin.setHasLoadedOptions(true);
186 }
187
188 parsePluginInfoXML(plugin, pluginfo_xml);
189 }
190 catch (Exception exception) {
191 DebugStream.printStackTrace(exception);
192 }
193 }
194
195
196 static public void loadPluginsList(String collection_name_arg)
197 {
198 DebugStream.println("In loadPluginsList()...");
199
200 // If we're getting the collection-specific plugins, clear the old list no matter what
201 if (collection_name_arg != null) {
202 collection_name = collection_name_arg;
203 collection_specific_plugins_list = new ArrayList();
204 }
205
206 // Run pluginfo.pl to get the list of plugins
207 try {
208 StringBuffer xml = null;
209 if (Gatherer.isGsdlRemote) {
210 String pluginfo_options = "&listall";
211 if (collection_name != null) {
212 pluginfo_options += "&collection=" + collection_name;
213 }
214 String pluginfo_output = Gatherer.remoteGreenstoneServer.getScriptOptions("pluginfo.pl", pluginfo_options);
215 xml = new StringBuffer(pluginfo_output);
216 }
217 else {
218 ArrayList args = new ArrayList();
219 args.add(Configuration.perl_path);
220 args.add("-S");
221 args.add(LocalGreenstone.getBinScriptDirectoryPath() + "pluginfo.pl");
222 args.add("-gs_version");
223 if (Gatherer.GS3) {
224 args.add("3");
225 } else {
226 args.add("2");
227 }
228 if (collection_name != null) {
229 args.add("-collection");
230 args.add(collection_name);
231 if (Gatherer.GS3) {
232 args.add("-site");
233 args.add(Configuration.site_name);
234 }
235 }
236 args.add("-listall");
237 args.add("-xml");
238
239 // Run the pluginfo.pl process:
240 // Create the process.
241 SafeProcess process = new SafeProcess((String[]) args.toArray(new String[] { }));
242
243 // run the SafeProcess
244 int exitVal = process.runProcess();
245 if(exitVal != 0) {
246 throw new Exception("*** Error running pluginfo.pl loadPluginsList, process exited with: "
247 + exitVal);
248 }
249 // get the result: We expect XML to have come out of the process std error stream.
250 xml = new StringBuffer(process.getStdError());
251 ///System.err.println("*********\nPluginsList, got:\n" + xml + "\n**********\n");
252 }
253
254 // Check the XML output was obtained successfully
255 if (xml == null || xml.length() == 0) {
256 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.PluginManager.Plugin_List_XML_Parse_Failed"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
257 return;
258 }
259
260 if (collection_name != null) {
261 collection_specific_plugins_list = parsePluginsListXML(xml.toString());
262 }
263 else {
264 core_greenstone_plugins_list = parsePluginsListXML(xml.toString());
265 }
266 }
267 catch (Exception exception) {
268 DebugStream.printStackTrace(exception);
269 }
270 }
271
272
273 static private void parsePluginInfoXML(Plugin plugin, String xml)
274 {
275 Document document = XMLTools.parseXML(new StringReader(xml));
276 if (document == null) {
277 plugin.setHasLoadedOptions(false); // failure to load the options/failed plugin
278 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.PlugInManager.PlugIn_XML_Parse_Failed", plugin.getName()), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
279 return;
280 } else {
281 plugin.setHasLoadedOptions(true);
282 }
283
284 parsePluginInfoXMLNode(plugin, document.getDocumentElement());
285 }
286
287
288 static private void parsePluginInfoXMLNode(Plugin plugin, Node root_node)
289 {
290 for (Node node = root_node.getFirstChild(); node != null; node = node.getNextSibling()) {
291 String node_name = node.getNodeName();
292
293 if (node_name.equalsIgnoreCase("Name")) {
294 plugin.setName(XMLTools.getValue(node));
295 }
296 else if (node_name.equals("Desc")) {
297 plugin.setDescription(XMLTools.getValue(node));
298 }
299 else if (node_name.equals("Abstract")) {
300 plugin.setIsAbstract(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
301 }
302 else if (node_name.equalsIgnoreCase("Explodes")) {
303 plugin.setDoesExplodeMetadataDatabases(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
304 }
305 else if (node_name.equalsIgnoreCase("SourceReplaceable")) { // looking for <SourceReplaceable> tag
306 plugin.setDoesReplaceSrcDocsWithHtml(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
307 }
308 else if (node_name.equalsIgnoreCase("Processes")) {
309 plugin.setDefaultProcessExpression(XMLTools.getValue(node));
310 }
311 else if (node_name.equalsIgnoreCase("Blocks")) {
312 plugin.setDefaultBlockExpression(XMLTools.getValue(node));
313 }
314 // Parse the plugin arguments
315 else if (node_name.equalsIgnoreCase("Arguments")) {
316 for (Node argument_node = node.getFirstChild(); argument_node != null; argument_node = argument_node.getNextSibling()) {
317 // An option
318 if (argument_node.getNodeName().equalsIgnoreCase("Option")) {
319 Argument argument = new Argument();
320 argument.parseXML((Element) argument_node);
321 plugin.addArgument(argument);
322 }
323 }
324 }
325 // A super plugin class
326 else if (node_name.equalsIgnoreCase("PlugInfo")) {
327 Plugin super_plugin = new Plugin();
328 parsePluginInfoXMLNode(super_plugin, node);
329 plugin.setSuper(super_plugin);
330 }
331 }
332 }
333
334
335 static private ArrayList parsePluginsListXML(String xml)
336 {
337 ArrayList plugins_list = new ArrayList();
338
339 Document document = XMLTools.parseXML(new StringReader(xml));
340 Node root = document.getDocumentElement();
341 for (Node node = root.getFirstChild(); node != null; node = node.getNextSibling()) {
342 String node_name = node.getNodeName();
343
344 if (node_name.equals("PlugInfo")) {
345 Plugin plugin = new Plugin();
346 parsePluginInfoXMLNode(plugin, node);
347 plugins_list.add(plugin);
348 }
349 }
350
351 return plugins_list;
352 }
353}
Note: See TracBrowser for help on using the repository browser.