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