Indexer.java revision 254
58N/A/*
58N/A * CDDL HEADER START
58N/A *
58N/A * The contents of this file are subject to the terms of the
58N/A * Common Development and Distribution License (the "License").
58N/A * You may not use this file except in compliance with the License.
58N/A *
58N/A * See LICENSE.txt included in this distribution for the specific
58N/A * language governing permissions and limitations under the License.
58N/A *
58N/A * When distributing Covered Code, include this CDDL HEADER in each
58N/A * file and include the License file at LICENSE.txt.
58N/A * If applicable, add the following below this CDDL HEADER, with the
58N/A * fields enclosed by brackets "[]" replaced with your own identifying
58N/A * information: Portions Copyright [yyyy] [name of copyright owner]
58N/A *
58N/A * CDDL HEADER END
58N/A */
58N/A
58N/A/*
77N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
58N/A * Use is subject to license terms.
58N/A */
58N/Apackage org.opensolaris.opengrok.index;
58N/A
234N/Aimport java.awt.GraphicsEnvironment;
234N/Aimport java.io.BufferedReader;
234N/Aimport java.io.File;
234N/Aimport java.io.FileReader;
234N/Aimport java.io.IOException;
234N/Aimport java.net.InetAddress;
234N/Aimport java.text.ParseException;
234N/Aimport java.util.ArrayList;
58N/Aimport java.util.Collections;
58N/Aimport java.util.Comparator;
58N/Aimport java.util.List;
58N/Aimport org.opensolaris.opengrok.analysis.AnalyzerGuru;
290N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzerFactory;
112N/Aimport org.opensolaris.opengrok.configuration.Project;
58N/Aimport org.opensolaris.opengrok.history.HistoryGuru;
58N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
77N/Aimport org.opensolaris.opengrok.history.ExternalRepository;
77N/Aimport org.opensolaris.opengrok.search.scope.MainFrame;
77N/Aimport org.opensolaris.opengrok.util.Getopt;
77N/A
58N/A/**
58N/A * Creates and updates an inverted source index
58N/A * as well as generates Xref, file stats etc., if specified
58N/A * in the options
58N/A */
58N/Apublic class Indexer {
58N/A private static String usage = "Usage: " +
58N/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] [-d DATA_ROOT] [subtree .. ]\n" +
290N/A " opengrok.jar [-l | -t] [-d DATA_ROOT]\n" +
58N/A "\t-q run quietly\n" +
65N/A "\t-v Print progress information\n" +
77N/A "\t-e economical - consumes less disk space\n" +
99N/A "\t-c path to ctags\n" +
99N/A "\t-R Read configuration from file\n" +
125N/A "\t-W Write the current running configuration\n" +
112N/A "\t-U Send configuration to hostname:port\n" +
129N/A "\t-P Generate a project for each toplevel directory\n" +
129N/A "\t-p Use the project specified by the project path as the default project\n" +
129N/A "\t-Q on/off Turn on / off quick context scan. By default only the first 32k\n" +
144N/A "\t of a file is scanned and a '[..all..]' link is inserted if the\n" +
173N/A "\t is bigger. Activating this option may slow down the server.\n" +
253N/A "\t-n Do not generate indexes\n" +
144N/A "\t-H Generate history cache for all external repositories\n" +
58N/A "\t-h /path/to/repos Generate history cache for the specified repos (absolute path from source root)\n" +
58N/A "\t-r on/off Turn on / off support for remote SCM systems\n" +
58N/A "\t-L laf Use \"laf\" as the look'n'feel for the webapp\n" +
58N/A "\t-w root URL of the webapp, default is /source\n" +
58N/A "\t-i ignore named files or directories\n" +
290N/A "\t-A ext:analyzer Files with extension ext should be analyzed with the named class\n" +
58N/A "\t-m Maximum words in a file to index\n" +
99N/A "\t-O on/off Turn on / off database optimization\n" +
99N/A "\t-a on/off Allow or disallow leading wildcards in a search\n" +
99N/A "\t-S Search and add \"External\" repositories (Mercurial etc)\n" +
101N/A "\t-s SRC_ROOT is root directory of source tree\n" +
106N/A "\t default: last used SRC_ROOT\n" +
112N/A "\t-d DATA_ROOT - is where output of indexer is stored\n" +
129N/A "\tsubtree - only specified files or directories under SRC_ROOT are processed\n" +
129N/A "\t if not specified all files under SRC_ROOT are processed\n" +
129N/A "\n" +
144N/A "\t-l list all files in the index\n" +
173N/A "\t-t lists tokens occuring more than 5 times. Useful for building a unix dictionary\n" +
253N/A "\n Eg. java -jar opengrok.jar -s /usr/include /var/tmp/opengrok_data rpc";
58N/A
58N/A private static String options = "d:r:a:qec:Q:R:W:U:Pp:nHh:w:i:Ss:ltvm:O:A:L:";
58N/A
58N/A /**
58N/A * Program entry point
58N/A * @param argv argument vector
58N/A */
58N/A public static void main(String argv[]) {
58N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
58N/A boolean runIndex = true;
58N/A
58N/A if(argv.length == 0) {
58N/A if (GraphicsEnvironment.isHeadless()) {
58N/A System.err.println("No display available for the Graphical User Interface");
58N/A System.err.println(usage);
58N/A System.exit(1);
58N/A } else {
58N/A MainFrame.main(argv);
58N/A }
58N/A //Run Scope GUI here I am running Indexing GUI for testing
58N/A //new IndexerWizard(null).setVisible(true);
58N/A } else {
58N/A boolean searchRepositories = false;
58N/A ArrayList<String> subFiles = new ArrayList<String>();
58N/A ArrayList<String> repositories = new ArrayList<String>();
58N/A String configFilename = null;
58N/A String configHost = null;
58N/A boolean addProjects = false;
58N/A boolean refreshHistory = false;
58N/A String defaultProject = null;
58N/A boolean listFiles = false;
58N/A boolean createDict = false;
58N/A
58N/A // Parse command line options:
58N/A Getopt getopt = new Getopt(argv, options);
58N/A
58N/A try {
58N/A getopt.parse();
58N/A } catch (ParseException ex) {
58N/A System.err.println("OpenGrok: " + ex.getMessage());
58N/A System.err.println(usage);
58N/A System.exit(1);
58N/A }
58N/A
58N/A try{
58N/A int cmd;
58N/A
58N/A // We need to read the configuration file first, since we
58N/A // will try to overwrite options..
58N/A while ((cmd = getopt.getOpt()) != -1) {
290N/A if (cmd == 'R') {
58N/A env.readConfiguration(new File(getopt.getOptarg()));
58N/A break;
58N/A }
290N/A }
58N/A
58N/A // Now we can handle all the other options..
58N/A getopt.reset();
58N/A while ((cmd = getopt.getOpt()) != -1) {
58N/A switch (cmd) {
58N/A case 'l':
58N/A listFiles = true;
58N/A runIndex = false;
58N/A break;
58N/A case 't':
77N/A createDict = true;
65N/A runIndex = false;
65N/A break;
65N/A
65N/A case 'q': env.setVerbose(false); break;
65N/A case 'e': env.setGenerateHtml(false); break;
65N/A case 'P': addProjects = true; break;
65N/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 ;
77N/A } else {
77N/A webapp = "/" + webapp;
77N/A }
99N/A if (webapp.endsWith("/")) {
99N/A env.setUrlPrefix(webapp + "s?");
99N/A } else {
99N/A env.setUrlPrefix(webapp + "/s?");
99N/A }
99N/A }
99N/A break;
99N/A case 'W': configFilename = getopt.getOptarg(); break;
99N/A case 'U': configHost = getopt.getOptarg(); break;
99N/A case 'R':
99N/A // already handled
99N/A break;
99N/A case 'n': runIndex = false; break;
99N/A case 'H': refreshHistory = true; break;
99N/A case 'h' : repositories.add(getopt.getOptarg()); break;
99N/A case 'r': {
125N/A if (getopt.getOptarg().equalsIgnoreCase("on")) {
125N/A env.setRemoteScmSupported(true);
125N/A } else if (getopt.getOptarg().equalsIgnoreCase("off")) {
125N/A env.setRemoteScmSupported(false);
125N/A } else {
125N/A System.err.println("ERROR: You should pass either \"on\" or \"off\" as argument to -r");
125N/A System.err.println(" Ex: \"-r on\" will allow retrival for remote SCM systems");
125N/A System.err.println(" \"-r off\" will ignore SCM for remote systems");
106N/A }
106N/A }
106N/A break;
106N/A case 'O': {
106N/A if (getopt.getOptarg().equalsIgnoreCase("on")) {
106N/A env.setOptimizeDatabase(true);
106N/A } else if (getopt.getOptarg().equalsIgnoreCase("off")) {
106N/A env.setOptimizeDatabase(false);
106N/A } else {
106N/A System.err.println("ERROR: You should pass either \"on\" or \"off\" as argument to -O");
112N/A System.err.println(" Ex: \"-O on\" will optimize the database as part of the index generation");
112N/A System.err.println(" \"-O off\" disable optimization of the index database");
112N/A }
112N/A }
112N/A break;
112N/A case 'v': env.setVerbose(true); break;
112N/A
112N/A case 's': {
129N/A File file = new File(getopt.getOptarg());
129N/A if (!file.isDirectory()) {
129N/A System.err.println("ERROR: No such directory: " + file.toString());
129N/A System.exit(1);
129N/A }
129N/A
129N/A env.setSourceRootFile(file);
129N/A break;
129N/A }
129N/A case 'd':
129N/A env.setDataRoot(getopt.getOptarg());
129N/A break;
129N/A case 'i':
129N/A env.getIgnoredNames().add(getopt.getOptarg());
129N/A break;
129N/A case 'S' : searchRepositories = true; break;
129N/A case 'Q' :
129N/A if (getopt.getOptarg().equalsIgnoreCase("on")) {
129N/A env.setQuickContextScan(true);
129N/A } else if (getopt.getOptarg().equalsIgnoreCase("off")) {
129N/A env.setQuickContextScan(false);
129N/A } else {
129N/A System.err.println("ERROR: You should pass either \"on\" or \"off\" as argument to -Q");
129N/A System.err.println(" Ex: \"-Q on\" will just scan a \"chunk\" of the file and insert \"[..all..]\"");
144N/A System.err.println(" \"-Q off\" will try to build a more accurate list by reading the complete file.");
144N/A }
144N/A
144N/A break;
144N/A case 'm' : {
144N/A try {
144N/A env.setIndexWordLimit(Integer.parseInt(getopt.getOptarg()));
144N/A } catch (NumberFormatException exp) {
173N/A System.err.println("ERROR: Failed to parse argument to \"-m\": " + exp.getMessage());
173N/A System.exit(1);
173N/A }
173N/A break;
173N/A }
173N/A case 'a' :
173N/A if (getopt.getOptarg().equalsIgnoreCase("on")) {
173N/A env.setAllowLeadingWildcard(true);
234N/A } else if (getopt.getOptarg().equalsIgnoreCase("off")) {
253N/A env.setAllowLeadingWildcard(false);
253N/A } else {
253N/A System.err.println("ERROR: You should pass either \"on\" or \"off\" as argument to -a");
253N/A System.err.println(" Ex: \"-a on\" will allow a search to start with a wildcard");
253N/A System.err.println(" \"-a off\" will disallow a search to start with a wildcard");
253N/A System.exit(1);
253N/A }
253N/A
234N/A break;
234N/A
234N/A case 'A': {
234N/A String[] arg = getopt.getOptarg().split(":");
234N/A if (arg.length != 2) {
234N/A System.err.println("ERROR: You must specify: -A extension:class");
234N/A System.err.println(" Ex: -A foo:org.opensolaris.opengrok.analysis.c.CAnalyzer");
234N/A System.err.println(" will use the C analyzer for all files ending with .foo");
234N/A System.err.println(" Ex: -A c:-");
234N/A System.err.println(" will disable the c-analyzer for for all files ending with .c");
234N/A System.exit(1);
234N/A }
234N/A
234N/A arg[0] = arg[0].substring(arg[0].lastIndexOf('.') + 1).toUpperCase();
234N/A if (arg[1].equals("-")) {
234N/A AnalyzerGuru.addExtension(arg[0], null);
234N/A break;
234N/A }
234N/A
234N/A try {
234N/A AnalyzerGuru.addExtension(
234N/A arg[0],
234N/A AnalyzerGuru.findFactory(arg[1]));
58N/A } catch (Exception e) {
System.err.println("Unable to use " + arg[1] +
" as a FileAnalyzerFactory");
e.printStackTrace();
System.exit(1);
}
}
break;
case 'L' :
env.setWebappLAF(getopt.getOptarg());
break;
default:
System.err.println("Unknown option: " + (char)cmd);
System.exit(1);
}
}
int optind = getopt.getOptind();
if (optind != -1) {
while (optind < argv.length) {
subFiles.add(argv[optind]);
++optind;
}
}
if (env.getDataRootPath() == null) {
System.err.println("ERROR: Please specify a DATA ROOT path");
System.err.println(usage);
System.exit(1);
}
if (env.getSourceRootFile() == null) {
File srcConfig = new File(env.getDataRootPath(), "SRC_ROOT");
String line = null;
if(srcConfig.exists()) {
try {
BufferedReader sr = new BufferedReader(new FileReader(srcConfig));
line = sr.readLine();
sr.close();
} catch (IOException e) {
}
}
if(line == null) {
System.err.println("ERROR: please specify a SRC_ROOT with option -s !");
System.err.println(usage);
System.exit(1);
}
env.setSourceRoot(line);
if (!env.getSourceRootFile().isDirectory()) {
System.err.println("ERROR: No such directory:" + line);
System.err.println(usage);
System.exit(1);
}
}
if (!env.validateExuberantCtags()) {
System.exit(1);
}
if (searchRepositories) {
if (env.isVerbose()) {
System.out.println("Scanning for repositories...");
}
env.getRepositories().clear();
long start = System.currentTimeMillis();
HistoryGuru.getInstance().addExternalRepositories(env.getSourceRootPath());
long time = (System.currentTimeMillis() - start) / 1000;
if (env.isVerbose()) {
System.out.println("Done searching for repositories (" + time + "s)");
}
}
if (addProjects) {
File files[] = env.getSourceRootFile().listFiles();
List<Project> projects = env.getProjects();
projects.clear();
for (File file : files) {
if (!file.getName().startsWith(".") && file.isDirectory()) {
projects.add(new Project(file.getName(), "/" + file.getName()));
}
}
// The projects should be sorted...
Collections.sort(projects, new Comparator<Project>() {
public int compare(Project p1, Project p2){
String s1 = p1.getDescription();
String s2 = p2.getDescription();
int ret;
if (s1 == null) {
ret = (s2 == null) ? 0 : 1;
} else {
ret = s1.compareTo(s2);
}
return ret;
}
});
}
if (defaultProject != null) {
for (Project p : env.getProjects()) {
if (p.getPath().equals(defaultProject)) {
env.setDefaultProject(p);
break;
}
}
}
if (configFilename != null) {
if (env.isVerbose()) {
System.out.println("Writing configuration to " + configFilename);
System.out.flush();
}
env.writeConfiguration(new File(configFilename));
if (env.isVerbose()) {
System.out.println("Done...");
System.out.flush();
}
}
if (refreshHistory) {
HistoryGuru.getInstance().createCache();
} else if (repositories.size() > 0) {
HistoryGuru.getInstance().createCache(repositories);
}
if (listFiles) {
IndexDatabase.listAllFiles(subFiles);
}
if (createDict) {
IndexDatabase.listFrequentTokens(subFiles);
}
if (runIndex) {
IndexChangedListener progress = new DefaultIndexChangedListener();
if (subFiles.isEmpty() || !env.hasProjects()) {
IndexDatabase.updateAll(progress);
} else {
for (String path : subFiles) {
Project project = Project.getProject(path);
if (project == null) {
System.err.println("Warning: Could not find a project for \"" + path + "\"");
} else {
IndexDatabase db = new IndexDatabase(project);
db.addIndexChangedListener(progress);
db.update();
}
}
}
}
if (configHost != null) {
String[] cfg = configHost.split(":");
if (env.isVerbose()) {
System.out.println("Send configuration to: " + configHost);
}
if (cfg.length == 2) {
try {
InetAddress host = InetAddress.getByName(cfg[0]);
RuntimeEnvironment.getInstance().writeConfiguration(host, Integer.parseInt(cfg[1]));
} catch (Exception ex) {
System.err.println("Failed to send configuration to " + configHost);
ex.printStackTrace();
}
} else {
System.err.println("Syntax error: ");
for (String s : cfg) {
System.err.print("[" + s + "]");
}
System.err.println();
}
if (env.isVerbose()) {
System.out.println("Configuration successfully updated");
}
}
} catch (Exception e) {
System.err.println("Error: [ main ] " + e);
if (env.isVerbose()) e.printStackTrace();
System.exit(1);
}
}
}
}