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