Indexer.java revision 261
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/*
119N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/Apackage org.opensolaris.opengrok.index;
65N/A
0N/Aimport java.awt.GraphicsEnvironment;
125N/Aimport java.io.BufferedReader;
125N/Aimport java.io.File;
125N/Aimport java.io.FileReader;
125N/Aimport java.io.IOException;
58N/Aimport java.net.InetAddress;
77N/Aimport java.text.ParseException;
125N/Aimport java.util.ArrayList;
125N/Aimport java.util.Collections;
125N/Aimport java.util.Comparator;
125N/Aimport java.util.List;
261N/Aimport java.util.concurrent.ExecutorService;
261N/Aimport java.util.concurrent.Executors;
312N/Aimport org.opensolaris.opengrok.analysis.AnalyzerGuru;
312N/Aimport org.opensolaris.opengrok.configuration.Project;
126N/Aimport org.opensolaris.opengrok.history.HistoryGuru;
58N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
8N/Aimport org.opensolaris.opengrok.search.scope.MainFrame;
30N/Aimport org.opensolaris.opengrok.util.Getopt;
77N/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 {
0N/A /**
312N/A * Program entry point
312N/A * @param argv argument vector
320N/A */
312N/A public static void main(String argv[]) {
312N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
312N/A boolean runIndex = true;
312N/A CommandLineOptions cmdOptions = new CommandLineOptions();
65N/A
65N/A if(argv.length == 0) {
65N/A if (GraphicsEnvironment.isHeadless()) {
65N/A System.err.println("No display available for the Graphical User Interface");
0N/A System.err.println(cmdOptions.getUsage());
30N/A System.exit(1);
58N/A } else {
312N/A MainFrame.main(argv);
312N/A }
260N/A } else {
112N/A boolean searchRepositories = false;
0N/A ArrayList<String> subFiles = new ArrayList<String>();
376N/A ArrayList<String> repositories = new ArrayList<String>();
376N/A String configFilename = null;
0N/A String configHost = null;
11N/A boolean addProjects = false;
0N/A boolean refreshHistory = false;
240N/A String defaultProject = null;
58N/A boolean listFiles = false;
58N/A boolean createDict = false;
58N/A int noThreads = Runtime.getRuntime().availableProcessors();
58N/A
77N/A // Parse command line options:
207N/A Getopt getopt = new Getopt(argv, cmdOptions.getCommandString());
207N/A
261N/A try {
260N/A getopt.parse();
77N/A } catch (ParseException ex) {
260N/A System.err.println("OpenGrok: " + ex.getMessage());
112N/A System.err.println(cmdOptions.getUsage());
77N/A System.exit(1);
77N/A }
77N/A
77N/A try{
260N/A int cmd;
77N/A
77N/A // We need to read the configuration file first, since we
77N/A // will try to overwrite options..
0N/A while ((cmd = getopt.getOpt()) != -1) {
77N/A if (cmd == 'R') {
111N/A env.readConfiguration(new File(getopt.getOptarg()));
111N/A break;
111N/A }
111N/A }
111N/A
111N/A // Now we can handle all the other options..
111N/A getopt.reset();
111N/A while ((cmd = getopt.getOpt()) != -1) {
111N/A switch (cmd) {
111N/A case 'l':
111N/A listFiles = true;
111N/A runIndex = false;
77N/A break;
77N/A case 't':
77N/A createDict = true;
207N/A runIndex = false;
207N/A break;
77N/A
77N/A case 'q': env.setVerbose(false); break;
99N/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 ;
77N/A } else {
0N/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':
111N/A // already handled
111N/A break;
77N/A case 'n': runIndex = false; break;
77N/A case 'H': refreshHistory = true; break;
77N/A case 'h' : repositories.add(getopt.getOptarg()); break;
240N/A case 'r': {
173N/A if (getopt.getOptarg().equalsIgnoreCase("on")) {
173N/A env.setRemoteScmSupported(true);
173N/A } else if (getopt.getOptarg().equalsIgnoreCase("off")) {
173N/A env.setRemoteScmSupported(false);
254N/A } else {
173N/A System.err.println("ERROR: You should pass either \"on\" or \"off\" as argument to -r");
173N/A System.err.println(" Ex: \"-r on\" will allow retrival for remote SCM systems");
173N/A System.err.println(" \"-r off\" will ignore SCM for remote systems");
254N/A }
173N/A }
173N/A break;
173N/A case 'O': {
253N/A if (getopt.getOptarg().equalsIgnoreCase("on")) {
312N/A env.setOptimizeDatabase(true);
253N/A } else if (getopt.getOptarg().equalsIgnoreCase("off")) {
253N/A env.setOptimizeDatabase(false);
253N/A } else {
253N/A System.err.println("ERROR: You should pass either \"on\" or \"off\" as argument to -O");
253N/A System.err.println(" Ex: \"-O on\" will optimize the database as part of the index generation");
253N/A System.err.println(" \"-O off\" disable optimization of the index database");
253N/A }
253N/A }
253N/A break;
312N/A case 'v': env.setVerbose(true); break;
312N/A
312N/A case 's': {
253N/A File file = new File(getopt.getOptarg());
253N/A if (!file.isDirectory()) {
99N/A System.err.println("ERROR: No such directory: " + file.toString());
77N/A System.exit(1);
77N/A }
77N/A
77N/A env.setSourceRootFile(file);
77N/A break;
0N/A }
0N/A case 'd':
77N/A env.setDataRoot(getopt.getOptarg());
125N/A break;
77N/A case 'i':
77N/A env.getIgnoredNames().add(getopt.getOptarg());
205N/A break;
205N/A case 'S' : searchRepositories = true; break;
205N/A case 'Q' :
112N/A if (getopt.getOptarg().equalsIgnoreCase("on")) {
112N/A env.setQuickContextScan(true);
112N/A } else if (getopt.getOptarg().equalsIgnoreCase("off")) {
77N/A env.setQuickContextScan(false);
106N/A } else {
119N/A System.err.println("ERROR: You should pass either \"on\" or \"off\" as argument to -Q");
106N/A System.err.println(" Ex: \"-Q on\" will just scan a \"chunk\" of the file and insert \"[..all..]\"");
119N/A System.err.println(" \"-Q off\" will try to build a more accurate list by reading the complete file.");
106N/A }
119N/A
119N/A break;
119N/A case 'm' : {
119N/A try {
106N/A env.setIndexWordLimit(Integer.parseInt(getopt.getOptarg()));
106N/A } catch (NumberFormatException exp) {
106N/A System.err.println("ERROR: Failed to parse argument to \"-m\": " + exp.getMessage());
99N/A System.exit(1);
99N/A }
99N/A break;
99N/A }
99N/A case 'a' :
99N/A if (getopt.getOptarg().equalsIgnoreCase("on")) {
99N/A env.setAllowLeadingWildcard(true);
99N/A } else if (getopt.getOptarg().equalsIgnoreCase("off")) {
99N/A env.setAllowLeadingWildcard(false);
125N/A } else {
125N/A System.err.println("ERROR: You should pass either \"on\" or \"off\" as argument to -a");
125N/A System.err.println(" Ex: \"-a on\" will allow a search to start with a wildcard");
125N/A System.err.println(" \"-a off\" will disallow a search to start with a wildcard");
125N/A System.exit(1);
125N/A }
125N/A
125N/A break;
125N/A
126N/A case 'A': {
125N/A String[] arg = getopt.getOptarg().split(":");
125N/A if (arg.length != 2) {
125N/A System.err.println("ERROR: You must specify: -A extension:class");
126N/A System.err.println(" Ex: -A foo:org.opensolaris.opengrok.analysis.c.CAnalyzer");
126N/A System.err.println(" will use the C analyzer for all files ending with .foo");
126N/A System.err.println(" Ex: -A c:-");
126N/A System.err.println(" will disable the c-analyzer for for all files ending with .c");
126N/A System.exit(1);
126N/A }
126N/A
126N/A arg[0] = arg[0].substring(arg[0].lastIndexOf('.') + 1).toUpperCase();
126N/A if (arg[1].equals("-")) {
126N/A AnalyzerGuru.addExtension(arg[0], null);
126N/A break;
126N/A }
126N/A
126N/A try {
126N/A AnalyzerGuru.addExtension(
126N/A arg[0],
126N/A AnalyzerGuru.findFactory(arg[1]));
126N/A } catch (Exception e) {
126N/A System.err.println("Unable to use " + arg[1] +
210N/A " as a FileAnalyzerFactory");
210N/A e.printStackTrace();
210N/A System.exit(1);
210N/A }
210N/A }
210N/A break;
210N/A case 'L' :
126N/A env.setWebappLAF(getopt.getOptarg());
126N/A break;
126N/A case 'T' :
126N/A try {
144N/A noThreads = Integer.parseInt(getopt.getOptarg());
144N/A } catch (NumberFormatException exp) {
144N/A System.err.println("ERROR: Failed to parse argument to \"-T\": " + exp.getMessage());
261N/A System.exit(1);
261N/A }
261N/A break;
261N/A default:
261N/A System.err.println("Unknown option: " + (char)cmd);
261N/A System.exit(1);
261N/A }
261N/A }
296N/A
296N/A int optind = getopt.getOptind();
296N/A if (optind != -1) {
296N/A while (optind < argv.length) {
296N/A subFiles.add(argv[optind]);
296N/A ++optind;
296N/A }
296N/A }
296N/A
296N/A if (env.getDataRootPath() == null) {
296N/A System.err.println("ERROR: Please specify a DATA ROOT path");
293N/A System.err.println(cmdOptions.getUsage());
293N/A System.exit(1);
293N/A }
293N/A
293N/A if (env.getSourceRootFile() == null) {
77N/A File srcConfig = new File(env.getDataRootPath(), "SRC_ROOT");
293N/A String line = null;
0N/A if(srcConfig.exists()) {
0N/A try {
0N/A BufferedReader sr = new BufferedReader(new FileReader(srcConfig));
77N/A line = sr.readLine();
77N/A sr.close();
205N/A } catch (IOException e) {
77N/A }
77N/A }
77N/A if(line == null) {
77N/A System.err.println("ERROR: please specify a SRC_ROOT with option -s !");
77N/A System.err.println(cmdOptions.getUsage());
58N/A System.exit(1);
312N/A }
312N/A env.setSourceRoot(line);
312N/A
312N/A if (!env.getSourceRootFile().isDirectory()) {
312N/A System.err.println("ERROR: No such directory:" + line);
312N/A System.err.println(cmdOptions.getUsage());
312N/A System.exit(1);
312N/A }
312N/A }
312N/A
312N/A if (!env.validateExuberantCtags()) {
312N/A System.exit(1);
312N/A }
312N/A
312N/A if (searchRepositories) {
312N/A if (env.isVerbose()) {
312N/A System.out.println("Scanning for repositories...");
312N/A }
312N/A env.getRepositories().clear();
312N/A long start = System.currentTimeMillis();
312N/A HistoryGuru.getInstance().addExternalRepositories(env.getSourceRootPath());
312N/A long time = (System.currentTimeMillis() - start) / 1000;
312N/A if (env.isVerbose()) {
312N/A System.out.println("Done searching for repositories (" + time + "s)");
312N/A }
312N/A }
312N/A
312N/A if (addProjects) {
312N/A File files[] = env.getSourceRootFile().listFiles();
312N/A List<Project> projects = env.getProjects();
312N/A projects.clear();
312N/A for (File file : files) {
312N/A if (!file.getName().startsWith(".") && file.isDirectory()) {
312N/A projects.add(new Project(file.getName(), "/" + file.getName()));
58N/A }
312N/A }
0N/A
77N/A // The projects should be sorted...
58N/A Collections.sort(projects, new Comparator<Project>() {
58N/A public int compare(Project p1, Project p2){
58N/A String s1 = p1.getDescription();
0N/A String s2 = p2.getDescription();
0N/A
0N/A int ret;
58N/A if (s1 == null) {
0N/A ret = (s2 == null) ? 0 : 1;
0N/A } else {
0N/A ret = s1.compareTo(s2);
0N/A }
58N/A return ret;
312N/A }
0N/A });
58N/A }
77N/A
58N/A if (defaultProject != null) {
312N/A for (Project p : env.getProjects()) {
0N/A if (p.getPath().equals(defaultProject)) {
0N/A env.setDefaultProject(p);
77N/A break;
99N/A }
0N/A }
0N/A }
77N/A
11N/A if (configFilename != null) {
99N/A if (env.isVerbose()) {
99N/A System.out.println("Writing configuration to " + configFilename);
99N/A System.out.flush();
99N/A }
11N/A env.writeConfiguration(new File(configFilename));
290N/A if (env.isVerbose()) {
11N/A System.out.println("Done...");
99N/A System.out.flush();
99N/A }
99N/A }
11N/A
77N/A if (refreshHistory) {
58N/A HistoryGuru.getInstance().createCache();
77N/A } else if (repositories.size() > 0) {
70N/A HistoryGuru.getInstance().createCache(repositories);
70N/A }
58N/A
77N/A if (listFiles) {
70N/A IndexDatabase.listAllFiles(subFiles);
58N/A }
58N/A
77N/A if (createDict) {
77N/A IndexDatabase.listFrequentTokens(subFiles);
99N/A }
99N/A
99N/A if (runIndex) {
99N/A ExecutorService executor = Executors.newFixedThreadPool(noThreads);
77N/A
77N/A IndexChangedListener progress = new DefaultIndexChangedListener();
77N/A if (subFiles.isEmpty() || !env.hasProjects()) {
77N/A IndexDatabase.updateAll(executor, progress);
77N/A } else {
77N/A for (String path : subFiles) {
77N/A Project project = Project.getProject(path);
77N/A if (project == null) {
77N/A System.err.println("Warning: Could not find a project for \"" + path + "\"");
77N/A } else {
58N/A final IndexDatabase db = new IndexDatabase(project);
77N/A db.addIndexChangedListener(progress);
77N/A executor.submit(new Runnable() {
77N/A
77N/A public void run() {
77N/A try {
77N/A db.update();
77N/A } catch (Exception e) {
77N/A e.printStackTrace();
77N/A }
99N/A }
58N/A });
99N/A }
99N/A }
99N/A }
99N/A }
58N/A
99N/A if (configHost != null) {
99N/A String[] cfg = configHost.split(":");
99N/A if (env.isVerbose()) {
99N/A System.out.println("Send configuration to: " + configHost);
58N/A }
112N/A
58N/A if (cfg.length == 2) {
203N/A try {
352N/A InetAddress host = InetAddress.getByName(cfg[0]);
240N/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);
207N/A ex.printStackTrace();
207N/A }
207N/A } else {
207N/A System.err.println("Syntax error: ");
207N/A for (String s : cfg) {
207N/A System.err.print("[" + s + "]");
207N/A }
312N/A System.err.println();
312N/A }
312N/A if (env.isVerbose()) {
312N/A System.out.println("Configuration successfully updated");
312N/A }
312N/A }
312N/A } catch (Exception e) {
312N/A System.err.println("Error: [ main ] " + e);
312N/A if (env.isVerbose()) {
207N/A e.printStackTrace();
270N/A }
312N/A System.exit(1);
352N/A }
312N/A }
264N/A }
270N/A
270N/A private Indexer() {
270N/A }
270N/A}
274N/A