View | Details | Raw Unified | Return to bug 40931
Collapse All | Expand All

(-)java/src/org/apache/xindice/tools/XMLTools.java (-76 / +69 lines)
Lines 35-41 Link Here
35
import java.io.FileInputStream;
35
import java.io.FileInputStream;
36
import java.io.FileNotFoundException;
36
import java.io.FileNotFoundException;
37
import java.util.Hashtable;
37
import java.util.Hashtable;
38
import java.util.NoSuchElementException;
39
38
40
/**
39
/**
41
 * XMLAdmin is designed to take command line arguments and give
40
 * XMLAdmin is designed to take command line arguments and give
Lines 56-61 Link Here
56
    public static final String VERBOSE = "verbose";
55
    public static final String VERBOSE = "verbose";
57
    public static final String TYPE = "type";
56
    public static final String TYPE = "type";
58
    public static final String PAGE_SIZE = "pagesize";
57
    public static final String PAGE_SIZE = "pagesize";
58
    public static final String PAGE_COUNT = "pagecount";
59
    public static final String MAX_KEY_SIZE = "maxkeysize";
59
    public static final String MAX_KEY_SIZE = "maxkeysize";
60
    public static final String DB_SERVER = "dbServ";
60
    public static final String DB_SERVER = "dbServ";
61
    public static final String PORT = "port";
61
    public static final String PORT = "port";
Lines 67-77 Link Here
67
    public static final String AUTO_KEY = "autoKey";
67
    public static final String AUTO_KEY = "autoKey";
68
    public static final String NAMESPACES = "namespaces";
68
    public static final String NAMESPACES = "namespaces";
69
    public static final String IMPL_CLASS = "implClass";
69
    public static final String IMPL_CLASS = "implClass";
70
    private static final String HELP = "help";
70
71
71
    private Hashtable table;
72
    private Hashtable table;
72
    protected String location = null;
73
    protected String location = null;
73
    private boolean initialized = false;
74
    private boolean initialized = false;
74
    private static boolean verbose = false;
75
75
76
    private Document commandsDocument = null;
76
    private Document commandsDocument = null;
77
    protected NodeList commandsList = null;
77
    protected NodeList commandsList = null;
Lines 82-90 Link Here
82
            new XMLTools().process(args);
82
            new XMLTools().process(args);
83
        } catch (Exception e) {
83
        } catch (Exception e) {
84
            System.out.println(e.getMessage());
84
            System.out.println(e.getMessage());
85
            if (verbose) {
86
                e.printStackTrace(System.err);
87
            }
88
        }
85
        }
89
    }
86
    }
90
87
Lines 165-249 Link Here
165
     **/
162
     **/
166
    public void process(String[] args) throws XindiceException, Exception {
163
    public void process(String[] args) throws XindiceException, Exception {
167
        try {
164
        try {
168
            init();
169
            parseArguments(args);
165
            parseArguments(args);
166
        } catch (IllegalArgumentException e) {
167
            System.err.println("ERROR : " + e.getMessage());
168
            return;
169
        }
170
170
171
            if (!execute()) {
171
        try {
172
                printHelp();
172
            execute();
173
        }  catch (XMLDBException e) {
174
            System.err.println("XMLDB Exception " + e.errorCode + ": " + e.getMessage());
175
            if (table.get(VERBOSE).equals("true")) {
176
                e.printStackTrace(System.err);
173
            }
177
            }
174
        } catch (IllegalArgumentException e) {
175
            printHelp();
176
            throw new XindiceException("ERROR : " + e.getMessage(), e);
177
        } catch (NoSuchElementException e) {
178
            throw new NoSuchElementException("ERROR : " + e + " Switch found. Parameter missing.");
179
        } catch (NullPointerException e) {
180
            e.printStackTrace(System.err);
181
            throw new NullPointerException("ERROR : " + e);
182
        } catch (Exception e) {
178
        } catch (Exception e) {
183
            e.printStackTrace(System.err);
179
            System.err.println("ERROR : " + e.getMessage());
184
            throw new XindiceException("ERROR : " + e.getMessage(), e);
180
            if (table.get(VERBOSE).equals("true")) {
181
                e.printStackTrace(System.err);
182
            }
185
        }
183
        }
186
    }
184
    }
187
185
186
    private String getRequiredSwitchArgument(ArgTokenizer at, String name) throws IllegalArgumentException {
187
        String value = at.nextSwitchToken();
188
        if ("".equals(value)) {
189
            throw new IllegalArgumentException("Command line switch " + name + " requires value\n" +
190
           "Type 'bin" + File.separatorChar + "xindice help' to see command usage");
191
        }
192
193
        return value;
194
    }
195
188
    /**
196
    /**
189
     * Parses and validated the arguments of the command line. The arguments are
197
     * Parses and validated the arguments of the command line. The arguments are
190
     * stored into the <tt>table</tt> array.
198
     * stored into the <tt>table</tt> array.
191
     *
199
     *
192
     * @exception IllegalArgumentException if an error is found
200
     * @exception IllegalArgumentException if an error is found
193
     */
201
     */
194
    protected void parseArguments(String[] args)
202
    protected void parseArguments(String[] args) throws IllegalArgumentException {
195
            throws IllegalArgumentException {
196
        // parsing arguments for the command tools
203
        // parsing arguments for the command tools
197
        ArgTokenizer at = new ArgTokenizer(args);
204
        ArgTokenizer at = new ArgTokenizer(args);
198
205
199
        if (!at.hasMoreTokens()) {
200
            throw new IllegalArgumentException("No argument found");
201
        }
202
203
        // Action should always be the second token, if not there show help
206
        // Action should always be the second token, if not there show help
204
        table.put(ACTION, at.nextToken());
207
        String action = at.nextToken();
208
        table.put(ACTION, "".equals(action) ? HELP : action);
205
209
206
        // Loop over remaining command line arguments, populating hashtable
210
        // Loop over remaining command line arguments, populating hashtable
207
        while (at.hasMoreTokens()) {
211
        while (at.hasMoreTokens()) {
208
            String token = at.nextToken();
212
            String token = at.nextToken();
209
213
210
            if (token.equalsIgnoreCase("-c") || token.equalsIgnoreCase("--collection")) {
214
            if (token.equalsIgnoreCase("-c") || token.equalsIgnoreCase("--collection")) {
211
                String colname = at.nextSwitchToken();
215
                String colname = getRequiredSwitchArgument(at, token);
212
                if (!colname.startsWith("/") &&
216
                if (!colname.startsWith("/") &&
213
                        !colname.startsWith("xmldb:xindice")) {
217
                        !colname.startsWith("xmldb:xindice")) {
214
                    throw new IllegalArgumentException("The name of a collection must start with '/'");
218
                    throw new IllegalArgumentException("The name of a collection must start with '/'");
215
                }
219
                }
216
                table.put(COLLECTION, colname);
220
                table.put(COLLECTION, colname);
217
            } else if (token.equalsIgnoreCase("-e") || token.equalsIgnoreCase("--extension")) {
221
            } else if (token.equalsIgnoreCase("-e") || token.equalsIgnoreCase("--extension")) {
218
                table.put(EXTENSION, at.nextSwitchToken());
222
                table.put(EXTENSION, getRequiredSwitchArgument(at, token));
219
            } else if (token.equalsIgnoreCase("-f") || token.equalsIgnoreCase("--filepath")) {
223
            } else if (token.equalsIgnoreCase("-f") || token.equalsIgnoreCase("--filepath")) {
220
                table.put(FILE_PATH, at.nextSwitchToken());
224
                table.put(FILE_PATH, getRequiredSwitchArgument(at, token));
221
            } else if (token.equalsIgnoreCase("-h") || token.equalsIgnoreCase("--help")) {
222
                table.put(ACTION, "help");
223
            } else if (token.equalsIgnoreCase("-n") || token.equalsIgnoreCase("--nameOf")) {
225
            } else if (token.equalsIgnoreCase("-n") || token.equalsIgnoreCase("--nameOf")) {
224
                table.put(NAME_OF, at.nextSwitchToken());
226
                table.put(NAME_OF, getRequiredSwitchArgument(at, token));
225
            } else if (token.equalsIgnoreCase("-p") || token.equalsIgnoreCase("--pattern")) {
227
            } else if (token.equalsIgnoreCase("-p") || token.equalsIgnoreCase("--pattern")) {
226
                table.put(PATTERN, at.nextSwitchToken());
228
                table.put(PATTERN, getRequiredSwitchArgument(at, token));
227
            } else if (token.equalsIgnoreCase("-q") || token.equalsIgnoreCase("--query")) {
229
            } else if (token.equalsIgnoreCase("-q") || token.equalsIgnoreCase("--query")) {
228
                table.put(QUERY, at.nextSwitchToken());
230
                table.put(QUERY, getRequiredSwitchArgument(at, token));
229
            } else if (token.equalsIgnoreCase("-u") || token.equalsIgnoreCase("--uri")) {
231
            } else if (token.equalsIgnoreCase("-u") || token.equalsIgnoreCase("--uri")) {
230
                table.put(URI, at.nextSwitchToken());
232
                table.put(URI, getRequiredSwitchArgument(at, token));
231
            } else if (token.equalsIgnoreCase("-v") || token.equalsIgnoreCase("--verbose")) {
233
            } else if (token.equalsIgnoreCase("-v") || token.equalsIgnoreCase("--verbose")) {
232
                table.put(VERBOSE, "true");
234
                table.put(VERBOSE, "true");
233
            } else if (token.equalsIgnoreCase("-l") || token.equalsIgnoreCase("--localdb")) {
235
            } else if (token.equalsIgnoreCase("-l") || token.equalsIgnoreCase("--localdb")) {
234
                table.put(LOCAL, "true");
236
                table.put(LOCAL, "true");
235
            } else if (token.equalsIgnoreCase("-d") || token.equalsIgnoreCase("--dbconfig")) {
237
            } else if (token.equalsIgnoreCase("-d") || token.equalsIgnoreCase("--dbconfig")) {
236
                String configFile = at.nextSwitchToken();
238
                String configFile = getRequiredSwitchArgument(at, token);
237
                if (!new File(configFile).isAbsolute()) {
239
                if (!new File(configFile).isAbsolute()) {
238
                    configFile = new File(System.getProperty("user.dir"), configFile).getAbsolutePath();
240
                    configFile = new File(System.getProperty("user.dir"), configFile).getAbsolutePath();
239
                }
241
                }
240
                System.setProperty(Xindice.PROP_XINDICE_CONFIGURATION, configFile);
242
                System.setProperty(Xindice.PROP_XINDICE_CONFIGURATION, configFile);
241
                table.put(DB_CONFIG, configFile);
243
                table.put(DB_CONFIG, configFile);
242
            } else if (token.equalsIgnoreCase("-s") || token.equalsIgnoreCase("--namespaces")) {
244
            } else if (token.equalsIgnoreCase("-s") || token.equalsIgnoreCase("--namespaces")) {
243
                table.put(NAMESPACES, at.nextSwitchToken());
245
                table.put(NAMESPACES, getRequiredSwitchArgument(at, token));
244
                // Index specific options
246
                // Index specific options
245
            } else if (token.equalsIgnoreCase("-t") || token.equalsIgnoreCase("--type")) {
247
            } else if (token.equalsIgnoreCase("-t") || token.equalsIgnoreCase("--type")) {
246
                table.put(TYPE, at.nextSwitchToken());
248
                table.put(TYPE, getRequiredSwitchArgument(at, token));
247
            } else if (token.equalsIgnoreCase("+trim")) {
249
            } else if (token.equalsIgnoreCase("+trim")) {
248
                if (!table.containsKey(TYPE)) {
250
                if (!table.containsKey(TYPE)) {
249
                    table.put(TYPE, "trimmed");
251
                    table.put(TYPE, "trimmed");
Lines 253-272 Link Here
253
                    table.put(TYPE, "string");
255
                    table.put(TYPE, "string");
254
                }
256
                }
255
            } else if (token.equalsIgnoreCase("--pagesize")) {
257
            } else if (token.equalsIgnoreCase("--pagesize")) {
256
                table.put(PAGE_SIZE, at.nextSwitchToken());
258
                table.put(PAGE_SIZE, getRequiredSwitchArgument(at, token));
259
            } else if (token.equalsIgnoreCase("--pagecount")) {
260
                table.put(PAGE_COUNT, getRequiredSwitchArgument(at, token));
257
            } else if (token.equalsIgnoreCase("--maxkeysize")) {
261
            } else if (token.equalsIgnoreCase("--maxkeysize")) {
258
                table.put(MAX_KEY_SIZE, at.nextSwitchToken());
262
                table.put(MAX_KEY_SIZE, getRequiredSwitchArgument(at, token));
259
            }
263
            }
260
        } // End of while loop
264
        } // End of while loop
261
    }
265
    }
262
266
263
264
    /**
267
    /**
265
     * This method is to carry out execution, after instance variables being setup by process( args )
268
     * This method is to carry out execution, after instance variables being setup by process( args )
266
     */
269
     */
267
    public boolean execute() throws Exception {
270
    public boolean execute() throws Exception {
268
        init();
271
        init();
269
        String action = (String) table.get(ACTION);
272
        String action = (String) table.get(ACTION);
273
        if (HELP.equals(action)) {
274
            printHelp();
275
            return true;
276
        }
270
277
271
        // get command class name
278
        // get command class name
272
        String commandClass = null;
279
        String commandClass = null;
Lines 278-327 Link Here
278
                if (action.equals(e.getAttribute("switch")) ||
285
                if (action.equals(e.getAttribute("switch")) ||
279
                        action.equals(e.getAttribute("name"))) {
286
                        action.equals(e.getAttribute("name"))) {
280
                    commandClass = e.getAttribute("class");
287
                    commandClass = e.getAttribute("class");
288
                    break;
281
                }
289
                }
282
            }
290
            }
283
        }
291
        }
284
292
285
        if (commandClass != null) {
293
        if (commandClass == null) {
286
            try {
294
            throw new XindiceException("Unknown action '" + action + "'\n" +
287
                // Register Xindice Database with xml:db
295
                "Type 'bin" + File.separatorChar + "xindice help' to see command usage");
288
                DatabaseImpl db = new DatabaseImpl();
296
        }
289
                DatabaseManager.registerDatabase(db);
290
297
291
                // Execute command class
298
        try {
292
                Command command = (Command) Class.forName(commandClass).newInstance();
299
            // Register Xindice Database with xml:db
293
                command.execute(table);
300
            DatabaseImpl db = new DatabaseImpl();
301
            DatabaseManager.registerDatabase(db);
294
302
295
                // Close Database
303
            // Execute command class
296
                if ("true".equals(table.get(LOCAL))) {
304
            Command command = (Command) Class.forName(commandClass).newInstance();
297
                    command = new org.apache.xindice.tools.command.Shutdown();
305
            command.execute(table);
298
                    command.execute(table);
306
        } finally {
299
                }
307
            // Close Database
300
308
            if ("true".equals(table.get(LOCAL))) {
301
                return true;
309
                Command command = new org.apache.xindice.tools.command.Shutdown();
302
            } catch (XMLDBException e) {
310
                command.execute(table);
303
                System.err.println("XMLDB Exception " + e.errorCode + ": " + e.getMessage());
304
                if (table.get(VERBOSE).equals("true")) {
305
                    e.printStackTrace(System.err);
306
                }
307
                return false;
308
            } catch (Exception e) {
309
                System.err.println("ERROR : " + e.getMessage());
310
                if (table.get(VERBOSE).equals("true")) {
311
                    e.printStackTrace(System.err);
312
                }
313
                return false;
314
            }
311
            }
315
        }
312
        }
316
313
317
        return false;
314
        return true;
318
    }
315
    }
319
316
320
321
    public boolean handleOption(String option, ArgTokenizer at) {
322
        return false;
323
    }
324
325
    /**
317
    /**
326
     * setAction sets the action type that will be passed to the command line.
318
     * setAction sets the action type that will be passed to the command line.
327
     *
319
     *
Lines 584-592 Link Here
584
    public void printHelp() {
576
    public void printHelp() {
585
        NodeList list = getCommands();
577
        NodeList list = getCommands();
586
578
587
        // This method relies on two things to format the output for help
579
        // This method relies on XML file Commands.xml attribute "helpclass" - used to order output
588
        //    Method isAdmin() - Tells us if this is an admin instance, used to hide certain output
589
        //    XML file Commands.xml attribute "helpclass" - used to order output
590
580
591
        String helpClass;  // Holds the helpclass for the current <command> node
581
        String helpClass;  // Holds the helpclass for the current <command> node
592
        String desc;       // Holds the description for the current <command> node
582
        String desc;       // Holds the description for the current <command> node
Lines 617-627 Link Here
617
        System.out.println("    -t           Specify the data type in collection index");
607
        System.out.println("    -t           Specify the data type in collection index");
618
        System.out.println("    -v           Verbose");
608
        System.out.println("    -v           Verbose");
619
        System.out.println("    --pagesize   Page size for file pages (default: 4096)");
609
        System.out.println("    --pagesize   Page size for file pages (default: 4096)");
610
        System.out.println("    --pagecount  Number of pages in the primary storage (default: 1024)");
620
        System.out.println("    --maxkeysize The maximum size for file keys (default: 0=none)");
611
        System.out.println("    --maxkeysize The maximum size for file keys (default: 0=none)");
621
        System.out.println();
612
        System.out.println();
622
613
623
        System.out.println("Actions:");
614
        System.out.println("Actions:");
624
615
616
        // show 'help' command
617
        System.out.println("    " + StringUtilities.leftJustify(HELP, 13) + "Prints this screen");
625
        // Show all elements with helpclass=document
618
        // Show all elements with helpclass=document
626
        // Loop over the commands, printing test from description attribute
619
        // Loop over the commands, printing test from description attribute
627
        for (int i = 0; i < list.getLength(); i++) {
620
        for (int i = 0; i < list.getLength(); i++) {
(-)java/src/org/apache/xindice/tools/command/AddCollection.java (+4 lines)
Lines 93-98 Link Here
93
                    filEle.setAttribute("pagesize", (String) table.get(XMLTools.PAGE_SIZE));
93
                    filEle.setAttribute("pagesize", (String) table.get(XMLTools.PAGE_SIZE));
94
                }
94
                }
95
95
96
                if (table.containsKey(XMLTools.PAGE_COUNT)) {
97
                    filEle.setAttribute("pagecount", (String) table.get(XMLTools.PAGE_COUNT));
98
                }
99
96
                if (table.containsKey(XMLTools.MAX_KEY_SIZE)) {
100
                if (table.containsKey(XMLTools.MAX_KEY_SIZE)) {
97
                    filEle.setAttribute("maxkeysize", (String) table.get(XMLTools.MAX_KEY_SIZE));
101
                    filEle.setAttribute("maxkeysize", (String) table.get(XMLTools.MAX_KEY_SIZE));
98
                }
102
                }
(-)java/src/org/apache/xindice/tools/command/AddIndexer.java (+4 lines)
Lines 82-87 Link Here
82
                        idxEle.setAttribute("pagesize", (String) table.get(XMLTools.PAGE_SIZE));
82
                        idxEle.setAttribute("pagesize", (String) table.get(XMLTools.PAGE_SIZE));
83
                    }
83
                    }
84
84
85
                    if (table.containsKey(XMLTools.PAGE_COUNT)) {
86
                        idxEle.setAttribute("pagecount", (String) table.get(XMLTools.PAGE_COUNT));
87
                    }
88
85
                    if (table.containsKey(XMLTools.MAX_KEY_SIZE)) {
89
                    if (table.containsKey(XMLTools.MAX_KEY_SIZE)) {
86
                        idxEle.setAttribute("maxkeysize", (String) table.get(XMLTools.MAX_KEY_SIZE));
90
                        idxEle.setAttribute("maxkeysize", (String) table.get(XMLTools.MAX_KEY_SIZE));
87
                    }
91
                    }

Return to bug 40931