Indexer.java revision 290
850N/A/*
850N/A * CDDL HEADER START
850N/A *
850N/A * The contents of this file are subject to the terms of the
850N/A * Common Development and Distribution License (the "License").
850N/A * You may not use this file except in compliance with the License.
850N/A *
850N/A * See LICENSE.txt included in this distribution for the specific
850N/A * language governing permissions and limitations under the License.
850N/A *
850N/A * When distributing Covered Code, include this CDDL HEADER in each
850N/A * file and include the License file at LICENSE.txt.
850N/A * If applicable, add the following below this CDDL HEADER, with the
850N/A * fields enclosed by brackets "[]" replaced with your own identifying
850N/A * information: Portions Copyright [yyyy] [name of copyright owner]
850N/A *
850N/A * CDDL HEADER END
850N/A */
850N/A
850N/A/*
1058N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
850N/A * Use is subject to license terms.
850N/A */
850N/Apackage org.opensolaris.opengrok.index;
850N/A
1058N/Aimport java.awt.GraphicsEnvironment;
850N/Aimport java.io.BufferedReader;
1058N/Aimport java.io.File;
850N/Aimport java.io.FileReader;
1020N/Aimport java.io.IOException;
943N/Aimport java.net.InetAddress;
857N/Aimport java.text.ParseException;
850N/Aimport java.util.ArrayList;
850N/Aimport java.util.Collections;
1020N/Aimport java.util.Comparator;
850N/Aimport java.util.List;
850N/Aimport java.util.concurrent.ExecutorService;
1020N/Aimport java.util.concurrent.Executors;
850N/Aimport org.opensolaris.opengrok.analysis.AnalyzerGuru;
850N/Aimport org.opensolaris.opengrok.configuration.Project;
850N/Aimport org.opensolaris.opengrok.history.HistoryGuru;
1020N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
1121N/Aimport org.opensolaris.opengrok.search.scope.MainFrame;
1121N/Aimport org.opensolaris.opengrok.util.Getopt;
1121N/A
1121N/A/**
1121N/A * Creates and updates an inverted source index
1121N/A * as well as generates Xref, file stats etc., if specified
1121N/A * in the options
1020N/A */
1121N/Apublic class Indexer {
1121N/A /**
1121N/A * Program entry point
1121N/A * @param argv argument vector
1121N/A */
1121N/A public static void main(String argv[]) {
1121N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
1121N/A boolean runIndex = true;
1121N/A CommandLineOptions cmdOptions = new CommandLineOptions();
1121N/A
1121N/A if(argv.length == 0) {
1121N/A if (GraphicsEnvironment.isHeadless()) {
1121N/A System.err.println("No display available for the Graphical User Interface");
1121N/A System.err.println(cmdOptions.getUsage());
1121N/A System.exit(1);
1121N/A } else {
1121N/A MainFrame.main(argv);
1121N/A }
1020N/A } else {
1121N/A boolean searchRepositories = false;
1121N/A ArrayList<String> subFiles = new ArrayList<String>();
1121N/A ArrayList<String> repositories = new ArrayList<String>();
1121N/A String configFilename = null;
1121N/A String configHost = null;
1121N/A boolean addProjects = false;
1121N/A boolean refreshHistory = false;
1121N/A String defaultProject = null;
1121N/A boolean listFiles = false;
1121N/A boolean createDict = false;
1121N/A int noThreads = Runtime.getRuntime().availableProcessors();
1121N/A
1121N/A // Parse command line options:
1121N/A Getopt getopt = new Getopt(argv, cmdOptions.getCommandString());
1121N/A
1121N/A try {
1121N/A getopt.parse();
1121N/A } catch (ParseException ex) {
1121N/A System.err.println("OpenGrok: " + ex.getMessage());
1121N/A System.err.println(cmdOptions.getUsage());
1121N/A System.exit(1);
1121N/A }
1121N/A
1121N/A try{
1121N/A int cmd;
1058N/A
1121N/A // We need to read the configuration file first, since we
1121N/A // will try to overwrite options..
1121N/A while ((cmd = getopt.getOpt()) != -1) {
1121N/A if (cmd == 'R') {
1121N/A env.readConfiguration(new File(getopt.getOptarg()));
1121N/A break;
1121N/A }
1121N/A }
1121N/A
1121N/A // Now we can handle all the other options..
1121N/A getopt.reset();
850N/A while ((cmd = getopt.getOpt()) != -1) {
1121N/A switch (cmd) {
1121N/A case 'l':
1121N/A listFiles = true;
1121N/A runIndex = false;
1121N/A break;
1121N/A case 't':
1121N/A createDict = true;
1121N/A runIndex = false;
1121N/A break;
1121N/A
1121N/A case 'q': env.setVerbose(false); break;
1121N/A case 'e': env.setGenerateHtml(false); break;
1121N/A case 'P': addProjects = true; break;
1121N/A case 'p': defaultProject = getopt.getOptarg(); break;
1020N/A case 'c': env.setCtags(getopt.getOptarg()); break;
1121N/A case 'w': {
1121N/A String webapp = getopt.getOptarg();
1121N/A if (webapp.startsWith("/") || webapp.startsWith("http")) {
1121N/A ;
1121N/A } else {
1121N/A webapp = "/" + webapp;
1121N/A }
1121N/A if (webapp.endsWith("/")) {
1121N/A env.setUrlPrefix(webapp + "s?");
1121N/A } else {
1121N/A env.setUrlPrefix(webapp + "/s?");
1020N/A }
1121N/A }
1121N/A break;
1121N/A case 'W': configFilename = getopt.getOptarg(); break;
1121N/A case 'U': configHost = getopt.getOptarg(); break;
1121N/A case 'R':
1121N/A // already handled
1121N/A break;
1121N/A case 'n': runIndex = false; break;
1121N/A case 'H': refreshHistory = true; break;
1121N/A case 'h' : repositories.add(getopt.getOptarg()); break;
1121N/A case 'r': {
1121N/A if (getopt.getOptarg().equalsIgnoreCase("on")) {
1121N/A env.setRemoteScmSupported(true);
1020N/A } else if (getopt.getOptarg().equalsIgnoreCase("off")) {
1121N/A env.setRemoteScmSupported(false);
1121N/A } else {
1121N/A System.err.println("ERROR: You should pass either \"on\" or \"off\" as argument to -r");
1108N/A System.err.println(" Ex: \"-r on\" will allow retrival for remote SCM systems");
1121N/A System.err.println(" \"-r off\" will ignore SCM for remote systems");
1121N/A }
1121N/A }
1121N/A break;
943N/A case 'O': {
1121N/A if (getopt.getOptarg().equalsIgnoreCase("on")) {
1121N/A env.setOptimizeDatabase(true);
1121N/A } else if (getopt.getOptarg().equalsIgnoreCase("off")) {
1121N/A env.setOptimizeDatabase(false);
1121N/A } else {
1121N/A System.err.println("ERROR: You should pass either \"on\" or \"off\" as argument to -O");
1121N/A System.err.println(" Ex: \"-O on\" will optimize the database as part of the index generation");
1121N/A System.err.println(" \"-O off\" disable optimization of the index database");
1121N/A }
1121N/A }
1121N/A break;
1121N/A case 'v': env.setVerbose(true); break;
1121N/A
1121N/A case 's': {
1121N/A File file = new File(getopt.getOptarg());
1121N/A if (!file.isDirectory()) {
1121N/A System.err.println("ERROR: No such directory: " + file.toString());
1121N/A System.exit(1);
1121N/A }
1121N/A
1121N/A env.setSourceRootFile(file);
1121N/A break;
1121N/A }
1121N/A case 'd':
1121N/A env.setDataRoot(getopt.getOptarg());
1121N/A break;
1121N/A case 'i':
1121N/A env.getIgnoredNames().add(getopt.getOptarg());
1121N/A break;
1121N/A case 'S' : searchRepositories = true; break;
1121N/A case 'Q' :
1121N/A if (getopt.getOptarg().equalsIgnoreCase("on")) {
1121N/A env.setQuickContextScan(true);
1121N/A } else if (getopt.getOptarg().equalsIgnoreCase("off")) {
1121N/A env.setQuickContextScan(false);
1121N/A } else {
1121N/A System.err.println("ERROR: You should pass either \"on\" or \"off\" as argument to -Q");
1121N/A System.err.println(" Ex: \"-Q on\" will just scan a \"chunk\" of the file and insert \"[..all..]\"");
1121N/A System.err.println(" \"-Q off\" will try to build a more accurate list by reading the complete file.");
1121N/A }
1121N/A
1121N/A break;
1121N/A case 'm' : {
1121N/A try {
1121N/A env.setIndexWordLimit(Integer.parseInt(getopt.getOptarg()));
1121N/A } catch (NumberFormatException exp) {
1121N/A System.err.println("ERROR: Failed to parse argument to \"-m\": " + exp.getMessage());
1108N/A System.exit(1);
1121N/A }
1121N/A break;
1121N/A }
1121N/A case 'a' :
1121N/A if (getopt.getOptarg().equalsIgnoreCase("on")) {
1121N/A env.setAllowLeadingWildcard(true);
1121N/A } else if (getopt.getOptarg().equalsIgnoreCase("off")) {
1121N/A env.setAllowLeadingWildcard(false);
1121N/A } else {
1121N/A System.err.println("ERROR: You should pass either \"on\" or \"off\" as argument to -a");
1121N/A System.err.println(" Ex: \"-a on\" will allow a search to start with a wildcard");
1121N/A System.err.println(" \"-a off\" will disallow a search to start with a wildcard");
1121N/A System.exit(1);
1121N/A }
1121N/A
1121N/A break;
1121N/A
1121N/A case 'A': {
1121N/A String[] arg = getopt.getOptarg().split(":");
1121N/A if (arg.length != 2) {
1121N/A System.err.println("ERROR: You must specify: -A extension:class");
1121N/A System.err.println(" Ex: -A foo:org.opensolaris.opengrok.analysis.c.CAnalyzer");
1121N/A System.err.println(" will use the C analyzer for all files ending with .foo");
1121N/A System.err.println(" Ex: -A c:-");
1121N/A System.err.println(" will disable the c-analyzer for for all files ending with .c");
1121N/A System.exit(1);
1121N/A }
1121N/A
1121N/A arg[0] = arg[0].substring(arg[0].lastIndexOf('.') + 1).toUpperCase();
943N/A if (arg[1].equals("-")) {
1121N/A AnalyzerGuru.addExtension(arg[0], null);
1121N/A break;
1121N/A }
1121N/A
1121N/A try {
1121N/A AnalyzerGuru.addExtension(
1121N/A arg[0],
1121N/A AnalyzerGuru.findFactory(arg[1]));
1121N/A } catch (Exception e) {
1121N/A System.err.println("Unable to use " + arg[1] +
1121N/A " as a FileAnalyzerFactory");
1121N/A e.printStackTrace();
1121N/A System.exit(1);
1121N/A }
1121N/A }
943N/A break;
1121N/A case 'L' :
1121N/A env.setWebappLAF(getopt.getOptarg());
1121N/A break;
1121N/A case 'T' :
1121N/A try {
1121N/A noThreads = Integer.parseInt(getopt.getOptarg());
1121N/A } catch (NumberFormatException exp) {
1121N/A System.err.println("ERROR: Failed to parse argument to \"-T\": " + exp.getMessage());
1121N/A System.exit(1);
1121N/A }
1121N/A break;
1121N/A default:
1122N/A System.err.println("Unknown option: " + (char)cmd);
1122N/A System.exit(1);
1122N/A }
1122N/A }
1122N/A
1122N/A int optind = getopt.getOptind();
1122N/A if (optind != -1) {
1122N/A while (optind < argv.length) {
1122N/A subFiles.add(argv[optind]);
1122N/A ++optind;
850N/A }
}
if (env.getDataRootPath() == null) {
System.err.println("ERROR: Please specify a DATA ROOT path");
System.err.println(cmdOptions.getUsage());
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(cmdOptions.getUsage());
System.exit(1);
}
env.setSourceRoot(line);
if (!env.getSourceRootFile().isDirectory()) {
System.err.println("ERROR: No such directory:" + line);
System.err.println(cmdOptions.getUsage());
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().addRepositories(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);
}
ExecutorService executor = Executors.newFixedThreadPool(noThreads);
IndexChangedListener progress = new DefaultIndexChangedListener();
if (subFiles.isEmpty()) {
if (runIndex) {
IndexDatabase.updateAll(executor, progress);
} else if (env.isOptimizeDatabase()) {
IndexDatabase.optimizeAll(executor);
}
} else {
List<IndexDatabase> dbs = new ArrayList<IndexDatabase>();
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);
int idx = dbs.indexOf(db);
if (idx != -1) {
db = dbs.get(idx);
}
if (db.addDirectory(path)) {
if (idx == -1) {
dbs.add(db);
}
} else {
System.err.println("Warning: Directory does not exist \"" + path + "\"");
}
}
}
for (final IndexDatabase db : dbs) {
final boolean update = runIndex;
final boolean optimize = env.isOptimizeDatabase();
db.addIndexChangedListener(progress);
executor.submit(new Runnable() {
public void run() {
try {
if (update) {
db.update();
} else if (optimize) {
db.optimize();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
executor.shutdown();
while (!executor.isTerminated()) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
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);
}
}
}
private Indexer() {
}
}