Indexer.java revision 77
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * See LICENSE.txt included in this distribution for the specific
0N/A * language governing permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at LICENSE.txt.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A
0N/A/*
0N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/A
0N/Apackage org.opensolaris.opengrok.index;
65N/A
0N/Aimport java.awt.GraphicsEnvironment;
0N/Aimport java.io.*;
58N/Aimport java.net.InetAddress;
77N/Aimport java.text.ParseException;
0N/Aimport java.util.*;
0N/Aimport org.apache.lucene.document.*;
0N/Aimport org.apache.lucene.index.*;
2N/Aimport org.apache.oro.io.GlobFilenameFilter;
0N/Aimport org.opensolaris.opengrok.analysis.*;
58N/Aimport org.opensolaris.opengrok.configuration.Project;
58N/Aimport org.opensolaris.opengrok.history.ExternalRepository;
8N/Aimport org.opensolaris.opengrok.history.HistoryGuru;
30N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
0N/Aimport org.opensolaris.opengrok.search.scope.MainFrame;
77N/Aimport org.opensolaris.opengrok.util.Getopt;
0N/A
0N/A/**
0N/A * Creates and updates an inverted source index
0N/A * as well as generates Xref, file stats etc., if specified
0N/A * in the options
0N/A */
0N/Apublic class Indexer {
77N/A private static boolean verbose = false;
0N/A private static String usage = "Usage: " +
77N/A "opengrok.jar [-qe] [-c ctagsToUse] [-H] [-R filename] [-W filename] [-U hostname:port] [-P] [-p project-path] [-w webapproot] [-i ignore_name [ -i ..]] [-n] [-s SRC_ROOT] DATA_ROOT [subtree .. ]\n" +
0N/A " opengrok.jar [-O | -l | -t] DATA_ROOT\n" +
0N/A "\t-q run quietly\n" +
0N/A "\t-e economical - consumes less disk space\n" +
0N/A "\t-c path to ctags\n" +
58N/A "\t-R Read configuration from file\n" +
58N/A "\t-W Write the current running configuration\n" +
58N/A "\t-U Send configuration to hostname:port\n" +
58N/A "\t-P Generate a project for each toplevel directory\n" +
77N/A "\t-p Use the project specified by the project path as the default project\n" +
58N/A "\t-n Do not generate indexes\n" +
58N/A "\t-H Start a threadpool to read history history\n" +
0N/A "\t-w root URL of the webapp, default is /source\n" +
0N/A "\t-i ignore named files or directories\n" +
77N/A "\t-S Search and add \"External\" repositories (Mercurial etc)\n" +
0N/A "\t-s SRC_ROOT is root directory of source tree\n" +
0N/A "\t default: last used SRC_ROOT\n" +
0N/A "\tDATA_ROOT - is where output of indexer is stored\n" +
0N/A "\tsubtree - only specified files or directories under SRC_ROOT are processed\n" +
0N/A "\t if not specified all files under SRC_ROOT are processed\n" +
0N/A "\n\t-O optimize the index \n" +
0N/A "\t-l list all files in the index \n" +
0N/A "\t-t lists tokens occuring more than 5 times. Useful for building a unix dictionary\n" +
0N/A "\n Eg. java -jar opengrok.jar -s /usr/include /var/tmp/opengrok_data rpc";
77N/A
77N/A private static String options = "qec:R:W:U:Pp:nHw:i:Ss:O:l:t:v";
77N/A
65N/A /**
65N/A * Program entry point
65N/A * @param argv argument vector
65N/A */
0N/A public static void main(String argv[]) {
30N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
58N/A boolean runIndex = true;
77N/A
0N/A if(argv.length == 0) {
0N/A if (GraphicsEnvironment.isHeadless()) {
0N/A System.err.println("No display available for the Graphical User Interface");
0N/A System.err.println(usage);
0N/A System.exit(1);
0N/A } else {
0N/A MainFrame.main(argv);
0N/A }
0N/A //Run Scope GUI here I am running Indexing GUI for testing
0N/A //new IndexerWizard(null).setVisible(true);
0N/A } else {
11N/A boolean searchRepositories = false;
0N/A ArrayList<String> subFiles = new ArrayList<String>();
58N/A String configFilename = null;
58N/A String configHost = null;
58N/A boolean addProjects = false;
58N/A boolean refreshHistory = false;
77N/A String defaultProject = null;
77N/A
77N/A // Parse command line options:
77N/A Getopt getopt = new Getopt(argv, options);
77N/A try {
77N/A getopt.parse();
77N/A } catch (ParseException ex) {
77N/A System.err.println("OpenGrok: " + ex.getMessage());
77N/A System.err.println(usage);
77N/A System.exit(1);
77N/A }
77N/A
0N/A try{
77N/A int cmd;
77N/A while ((cmd = getopt.getOpt()) != -1) {
77N/A switch (cmd) {
77N/A case 'O':
77N/A Index.doOptimize(new File(getopt.getOptarg()));
77N/A System.exit(0);
77N/A break;
77N/A case 'l':
77N/A Index.doList(new File(getopt.getOptarg()));
77N/A System.exit(0);
77N/A break;
77N/A case 't':
77N/A Index.doDict(new File(getopt.getOptarg()));
77N/A System.exit(0);
77N/A break;
77N/A case 'g':
77N/A IgnoredNames.glob = new GlobFilenameFilter(getopt.getOptarg());
77N/A break;
77N/A
77N/A case 'q': verbose = false; break;
77N/A case 'e': env.setGenerateHtml(false); break;
77N/A case 'P': addProjects = true; break;
77N/A case 'p': defaultProject = getopt.getOptarg(); break;
77N/A case 'c': env.setCtags(getopt.getOptarg()); break;
77N/A case 'w': {
77N/A String webapp = getopt.getOptarg();
77N/A if (webapp.startsWith("/") || webapp.startsWith("http")) {
77N/A ;
0N/A } else {
77N/A webapp = "/" + webapp;
77N/A }
77N/A if (webapp.endsWith("/")) {
77N/A env.setUrlPrefix(webapp + "s?");
77N/A } else {
77N/A env.setUrlPrefix(webapp + "/s?");
77N/A }
77N/A }
77N/A break;
77N/A case 'W': configFilename = getopt.getOptarg(); break;
77N/A case 'U': configHost = getopt.getOptarg(); break;
77N/A case 'R':
77N/A env.readConfiguration(new File(getopt.getOptarg()));
77N/A break;
77N/A case 'n': runIndex = false; break;
77N/A case 'H': refreshHistory = true; break;
77N/A case 'v': verbose = true; break;
77N/A
77N/A case 's': {
77N/A File file = new File(getopt.getOptarg());
77N/A if (!file.isDirectory()) {
77N/A System.err.println("ERROR: No such directory: " + file.toString());
0N/A System.exit(1);
0N/A }
77N/A
77N/A env.setSourceRoot(file);
77N/A break;
77N/A }
77N/A
77N/A case 'i': IgnoredNames.add(getopt.getOptarg()); break;
77N/A case 'S' : searchRepositories = true; break;
77N/A default:
77N/A System.err.println("Unhandled option: " + (char)cmd);
0N/A System.exit(1);
0N/A }
0N/A }
77N/A
77N/A int optind = getopt.getOptind();
77N/A if (optind != -1) {
77N/A if (optind < argv.length) {
77N/A env.setDataRoot(argv[optind]);
77N/A ++optind;
77N/A }
77N/A
77N/A while (optind < argv.length) {
77N/A subFiles.add(argv[optind]);
77N/A ++optind;
77N/A }
77N/A }
58N/A
58N/A if (env.getDataRootPath() == null) {
77N/A System.err.println("ERROR: Please specify a DATA ROOT path");
77N/A System.err.println(usage);
0N/A System.exit(1);
0N/A }
77N/A
58N/A if (env.getSourceRootFile() == null) {
58N/A File srcConfig = new File(env.getDataRootPath(), "SRC_ROOT");
58N/A String line = null;
0N/A if(srcConfig.exists()) {
0N/A try {
0N/A BufferedReader sr = new BufferedReader(new FileReader(srcConfig));
58N/A line = sr.readLine();
0N/A sr.close();
0N/A } catch (IOException e) {
0N/A }
0N/A }
58N/A if(line == null) {
0N/A System.err.println("ERROR: please specify a SRC_ROOT with option -s !");
0N/A System.err.println(usage);
0N/A System.exit(1);
0N/A }
58N/A env.setSourceRoot(line);
77N/A
58N/A if (!env.getSourceRootFile().isDirectory()) {
58N/A System.err.println("ERROR: No such directory:" + line);
0N/A System.err.println(usage);
0N/A System.exit(1);
0N/A }
0N/A }
77N/A
58N/A if (! Index.setExuberantCtags(env.getCtags())) {
0N/A System.exit(1);
0N/A }
77N/A
11N/A if (searchRepositories) {
11N/A System.out.println("Scanning for repositories...");
11N/A long start = System.currentTimeMillis();
58N/A HistoryGuru.getInstance().addExternalRepositories(env.getSourceRootPath());
11N/A long time = (System.currentTimeMillis() - start) / 1000;
11N/A System.out.println("Done searching for repositories (" + time + "s)");
11N/A }
77N/A
58N/A if (addProjects) {
77N/A File files[] = env.getSourceRootFile().listFiles();
70N/A List<Project> projects = env.getProjects();
70N/A projects.clear();
58N/A for (File file : files) {
77N/A if (!file.getName().startsWith(".") && file.isDirectory()) {
70N/A projects.add(new Project(file.getName(), "/" + file.getName()));
58N/A }
58N/A }
77N/A
77N/A // The projects should be sorted...
77N/A Collections.sort(projects, new Comparator() {
77N/A public int compare(Object obj1, Object obj2){
77N/A String s1 = ((Project)obj1).getDescription();
77N/A String s2 = ((Project)obj2).getDescription();
77N/A
77N/A int ret;
77N/A if (s1 == null) {
77N/A ret = (s2 == null) ? 0 : 1;
77N/A } else {
77N/A ret = s1.compareTo(s2);
77N/A }
77N/A return ret;
77N/A }
77N/A });
58N/A }
77N/A
77N/A if (defaultProject != null) {
77N/A for (Project p : env.getProjects()) {
77N/A if (p.getPath().equals(defaultProject)) {
77N/A env.setDefaultProject(p);
77N/A break;
77N/A }
77N/A }
77N/A }
77N/A
58N/A if (configFilename != null) {
58N/A System.out.println("Writing configuration to " + configFilename);
58N/A System.out.flush();
58N/A env.writeConfiguration(new File(configFilename));
58N/A System.out.println("Done...");
58N/A System.out.flush();
58N/A }
77N/A
58N/A if (refreshHistory) {
58N/A for (Map.Entry<String, ExternalRepository> entry : RuntimeEnvironment.getInstance().getRepositories().entrySet()) {
58N/A try {
58N/A entry.getValue().createCache();
58N/A } catch (Exception e) {
58N/A System.err.println("Failed to generate history cache.");
58N/A e.printStackTrace();
58N/A }
58N/A }
58N/A }
58N/A
58N/A if (runIndex) {
58N/A Index idx = new Index(verbose ? new StandardPrinter(System.out) : new NullPrinter(), new StandardPrinter(System.err));
65N/A idx.runIndexer(env.getDataRootFile(), env.getSourceRootFile(), subFiles, env.isGenerateHtml());
58N/A }
58N/A
58N/A if (configHost != null) {
58N/A String[] cfg = configHost.split(":");
58N/A System.out.println("Send configuration to: " + configHost);
58N/A
58N/A if (cfg.length == 2) {
58N/A try {
58N/A InetAddress host = InetAddress.getByName(cfg[0]);
58N/A RuntimeEnvironment.getInstance().writeConfiguration(host, Integer.parseInt(cfg[1]));
58N/A } catch (Exception ex) {
58N/A System.err.println("Failed to send configuration to " + configHost);
58N/A ex.printStackTrace();
58N/A }
58N/A } else {
58N/A System.err.println("Syntax error: ");
58N/A for (String s : cfg) {
58N/A System.err.print("[" + s + "]");
58N/A }
58N/A System.err.println();
58N/A }
58N/A System.out.println("Configuration successfully updated");
58N/A }
58N/A } catch (Exception e) {
0N/A System.err.println("Error: [ main ] " + e);
0N/A if (verbose) e.printStackTrace();
0N/A System.exit(1);
0N/A }
0N/A }
0N/A }
0N/A}