Search in sources :

Example 16 with RuntimeEnvironment

use of org.opengrok.indexer.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.

the class RepositoryInfo method setDirectoryName.

/**
 * Specify the name of the root directory for this repository.
 *
 * @param dir the new root directory
 */
public void setDirectoryName(File dir) {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    String rootPath = env.getSourceRootPath();
    String path;
    String originalPath = dir.getPath();
    try {
        path = PathUtils.getRelativeToCanonical(dir.toPath(), Paths.get(rootPath));
        // as it is elsewhere directly appended to env.getSourceRootPath() and also stored as such.
        if (!path.equals(originalPath)) {
            path = File.separator + path;
        }
    } catch (IOException e) {
        path = originalPath;
        LOGGER.log(Level.SEVERE, String.format("Failed to get canonical path for %s", path), e);
    }
    if (path.startsWith(rootPath)) {
        setDirectoryNameRelative(path.substring(rootPath.length()));
    } else {
        setDirectoryNameRelative(path);
    }
}
Also used : RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) IOException(java.io.IOException)

Example 17 with RuntimeEnvironment

use of org.opengrok.indexer.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.

the class Repository method getCommand.

protected static String getCommand(Class<? extends Repository> repoClass, String propertyKey, String fallbackCommand) {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    String className = repoClass.getCanonicalName();
    String command = env.getRepoCmd(className);
    if (command == null) {
        command = System.getProperty(propertyKey, fallbackCommand);
        env.setRepoCmd(className, command);
    }
    return command;
}
Also used : RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment)

Example 18 with RuntimeEnvironment

use of org.opengrok.indexer.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.

the class DirectoryListingTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    directory = Files.createTempDirectory("directory").toFile();
    entries = new FileEntry[3];
    entries[0] = new FileEntry("foo.c", "foo.c", 0, 112);
    entries[1] = new FileEntry("bar.h", "bar.h", Long.MAX_VALUE, 0);
    // Will test getSimplifiedPath() behavior for ignored directories.
    // Use DIRECTORY_INTERNAL_SIZE value for length so it is checked as the directory
    // should contain "-" (DIRECTORY_SIZE_PLACEHOLDER) string.
    entries[2] = new FileEntry("subdir", "subdir/", 0, Arrays.asList(new FileEntry("SCCS", "SCCS/", 0, Arrays.asList(new FileEntry("version", "version", 0, 312)))));
    for (FileEntry entry : entries) {
        entry.create();
    }
    // Create the entry that will be ignored separately.
    FileEntry hgtags = new FileEntry(".hgtags", ".hgtags", 0, 1);
    hgtags.create();
    // Need to populate list of ignored entries for all repository types.
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    RepositoryFactory.initializeIgnoredNames(env);
}
Also used : RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 19 with RuntimeEnvironment

use of org.opengrok.indexer.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.

the class PlainAnalyzer method analyze.

@Override
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException, InterruptedException {
    Definitions defs = null;
    NullWriter nullWriter = null;
    doc.add(new OGKTextField(QueryBuilder.FULL, getReader(src.getStream())));
    String fullPath = doc.get(QueryBuilder.FULLPATH);
    if (fullPath != null && ctags != null) {
        defs = ctags.doCtags(fullPath);
        if (defs != null && defs.numberOfSymbols() > 0) {
            tryAddingDefs(doc, defs, src);
            byte[] tags = defs.serialize();
            doc.add(new StoredField(QueryBuilder.TAGS, tags));
        }
    }
    /*
         * This is to explicitly use appropriate analyzer's token stream to
         * work around #1376: symbols search works like full text search.
         */
    JFlexTokenizer symbolTokenizer = symbolTokenizerFactory.get();
    OGKTextField ref = new OGKTextField(QueryBuilder.REFS, symbolTokenizer);
    symbolTokenizer.setReader(getReader(src.getStream()));
    doc.add(ref);
    if (scopesEnabled && xrefOut == null) {
        /*
             * Scopes are generated during xref generation. If xrefs are
             * turned off we still need to run writeXref() to produce scopes,
             * we use a dummy writer that will throw away any xref output.
             */
        nullWriter = new NullWriter();
        xrefOut = nullWriter;
    }
    if (xrefOut != null) {
        try (Reader in = getReader(src.getStream())) {
            RuntimeEnvironment env = RuntimeEnvironment.getInstance();
            WriteXrefArgs args = new WriteXrefArgs(in, xrefOut);
            args.setDefs(defs);
            args.setProject(project);
            CompletableFuture<XrefWork> future = CompletableFuture.supplyAsync(() -> {
                try {
                    return new XrefWork(writeXref(args));
                } catch (IOException e) {
                    return new XrefWork(e);
                }
            }, env.getIndexerParallelizer().getXrefWatcherExecutor()).orTimeout(env.getXrefTimeout(), TimeUnit.SECONDS);
            // Will throw ExecutionException wrapping TimeoutException on timeout.
            XrefWork xrefWork = future.get();
            Xrefer xref = xrefWork.xrefer;
            if (xref != null) {
                Scopes scopes = xref.getScopes();
                if (scopes.size() > 0) {
                    byte[] scopesSerialized = scopes.serialize();
                    doc.add(new StoredField(QueryBuilder.SCOPES, scopesSerialized));
                }
                String path = doc.get(QueryBuilder.PATH);
                addNumLinesLOC(doc, new NumLinesLOC(path, xref.getLineNumber(), xref.getLOC()));
            } else {
                // Re-throw the exception from writeXref().
                throw new IOException(xrefWork.exception);
            }
        } catch (ExecutionException e) {
            throw new InterruptedException("failed to generate xref :" + e);
        } finally {
            if (nullWriter != null) {
                nullWriter.close();
            }
        }
    }
}
Also used : JFlexTokenizer(org.opengrok.indexer.analysis.JFlexTokenizer) OGKTextField(org.opengrok.indexer.analysis.OGKTextField) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) NumLinesLOC(org.opengrok.indexer.analysis.NumLinesLOC) Definitions(org.opengrok.indexer.analysis.Definitions) Xrefer(org.opengrok.indexer.analysis.Xrefer) Reader(java.io.Reader) ExpandTabsReader(org.opengrok.indexer.analysis.ExpandTabsReader) IOException(java.io.IOException) WriteXrefArgs(org.opengrok.indexer.analysis.WriteXrefArgs) NullWriter(org.opengrok.indexer.util.NullWriter) StoredField(org.apache.lucene.document.StoredField) Scopes(org.opengrok.indexer.analysis.Scopes) ExecutionException(java.util.concurrent.ExecutionException)

Example 20 with RuntimeEnvironment

use of org.opengrok.indexer.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.

the class Context method getContext.

/**
 * ???.
 * Closes the given <var>in</var> reader on return.
 *
 * @param in File to be matched
 * @param out to write the context
 * @param urlPrefix URL prefix
 * @param morePrefix to link to more... page
 * @param path path of the file
 * @param tags format to highlight defs.
 * @param limit should the number of matching lines be limited?
 * @param isDefSearch is definition search
 * @param hits list of hits
 * @param scopes scopes object
 * @return Did it get any matching context?
 */
public boolean getContext(Reader in, Writer out, String urlPrefix, String morePrefix, String path, Definitions tags, boolean limit, boolean isDefSearch, List<Hit> hits, Scopes scopes) {
    if (m == null) {
        IOUtils.close(in);
        return false;
    }
    boolean anything = false;
    TreeMap<Integer, String[]> matchingTags = null;
    String urlPrefixE = (urlPrefix == null) ? "" : Util.uriEncodePath(urlPrefix);
    String pathE = Util.uriEncodePath(path);
    if (tags != null) {
        matchingTags = new TreeMap<>();
        try {
            for (Definitions.Tag tag : tags.getTags()) {
                for (LineMatcher lineMatcher : m) {
                    if (lineMatcher.match(tag.symbol) == LineMatcher.MATCHED) {
                        String scope = null;
                        String scopeUrl = null;
                        if (scopes != null) {
                            Scope scp = scopes.getScope(tag.line);
                            scope = scp.getName() + "()";
                            scopeUrl = "<a href=\"" + urlPrefixE + pathE + "#" + scp.getLineFrom() + "\">" + scope + "</a>";
                        }
                        /* desc[0] is matched symbol
                             * desc[1] is line number
                             * desc[2] is type
                             * desc[3] is matching line;
                             * desc[4] is scope
                             */
                        String[] desc = { tag.symbol, Integer.toString(tag.line), tag.type, tag.text, scope };
                        if (in == null) {
                            if (out == null) {
                                Hit hit = new Hit(path, Util.htmlize(desc[3]).replace(desc[0], "<b>" + desc[0] + "</b>"), desc[1], false, alt);
                                hits.add(hit);
                            } else {
                                out.write("<a class=\"s\" href=\"");
                                out.write(urlPrefixE);
                                out.write(pathE);
                                out.write("#");
                                out.write(desc[1]);
                                out.write("\"><span class=\"l\">");
                                out.write(desc[1]);
                                out.write("</span> ");
                                out.write(Util.htmlize(desc[3]).replace(desc[0], "<b>" + desc[0] + "</b>"));
                                out.write("</a> ");
                                if (desc[4] != null) {
                                    out.write("<span class=\"scope\"><a href\"");
                                    out.write(scopeUrl);
                                    out.write("\">in ");
                                    out.write(desc[4]);
                                    out.write("</a></span> ");
                                }
                                out.write("<i>");
                                out.write(desc[2]);
                                out.write("</i><br/>");
                            }
                            anything = true;
                        } else {
                            matchingTags.put(tag.line, desc);
                        }
                        break;
                    }
                }
            }
        } catch (Exception e) {
            if (hits != null) {
                // @todo verify why we ignore all exceptions?
                LOGGER.log(Level.WARNING, "Could not get context for " + path, e);
            }
        }
    }
    // Just to get the matching tag send a null in
    if (in == null) {
        return anything;
    }
    PlainLineTokenizer tokens = new PlainLineTokenizer(null);
    boolean truncated = false;
    boolean lim = limit;
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    if (!env.isQuickContextScan()) {
        lim = false;
    }
    if (lim) {
        char[] buffer = new char[MAXFILEREAD];
        int charsRead;
        try {
            charsRead = in.read(buffer);
            if (charsRead == MAXFILEREAD) {
                // we probably only read parts of the file, so set the
                // truncated flag to enable the [all...] link that
                // requests all matches
                truncated = true;
                // characters back)
                for (int i = charsRead - 1; i > charsRead - 100; i--) {
                    if (buffer[i] == '\n') {
                        charsRead = i;
                        break;
                    }
                }
            }
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, "An error occurred while reading data", e);
            return anything;
        }
        if (charsRead == 0) {
            return anything;
        }
        tokens.reInit(buffer, charsRead, out, urlPrefixE + pathE + "#", matchingTags, scopes);
    } else {
        tokens.reInit(in, out, urlPrefixE + pathE + "#", matchingTags, scopes);
    }
    if (hits != null) {
        tokens.setAlt(alt);
        tokens.setHitList(hits);
        tokens.setFilename(path);
    }
    int limit_max_lines = env.getContextLimit();
    try {
        String token;
        int matchState;
        int matchedLines = 0;
        while ((token = tokens.yylex()) != null && (!lim || matchedLines < limit_max_lines)) {
            for (LineMatcher lineMatcher : m) {
                matchState = lineMatcher.match(token);
                if (matchState == LineMatcher.MATCHED) {
                    if (!isDefSearch) {
                        tokens.printContext();
                    } else if (tokens.tags.containsKey(tokens.markedLine)) {
                        tokens.printContext();
                    }
                    matchedLines++;
                    break;
                } else if (matchState == LineMatcher.WAIT) {
                    tokens.holdOn();
                } else {
                    tokens.neverMind();
                }
            }
        }
        anything = matchedLines > 0;
        tokens.dumpRest();
        if (lim && (truncated || matchedLines == limit_max_lines) && out != null) {
            out.write("<a href=\"" + Util.uriEncodePath(morePrefix) + pathE + "?" + queryAsURI + "\">[all...]</a>");
        }
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, "Could not get context for " + path, e);
    } finally {
        IOUtils.close(in);
        if (out != null) {
            try {
                out.flush();
            } catch (IOException e) {
                LOGGER.log(Level.WARNING, "Failed to flush stream: ", e);
            }
        }
    }
    return anything;
}
Also used : RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) Definitions(org.opengrok.indexer.analysis.Definitions) IOException(java.io.IOException) IOException(java.io.IOException) Hit(org.opengrok.indexer.search.Hit) Scope(org.opengrok.indexer.analysis.Scopes.Scope)

Aggregations

RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)81 File (java.io.File)26 Project (org.opengrok.indexer.configuration.Project)24 Test (org.junit.jupiter.api.Test)22 IOException (java.io.IOException)18 BeforeAll (org.junit.jupiter.api.BeforeAll)13 ArrayList (java.util.ArrayList)12 TestRepository (org.opengrok.indexer.util.TestRepository)12 Path (java.nio.file.Path)8 ForbiddenSymlinkException (org.opengrok.indexer.util.ForbiddenSymlinkException)8 Document (org.apache.lucene.document.Document)6 Ctags (org.opengrok.indexer.analysis.Ctags)6 Executor (org.opengrok.indexer.util.Executor)6 BufferedReader (java.io.BufferedReader)5 FileNotFoundException (java.io.FileNotFoundException)5 InputStreamReader (java.io.InputStreamReader)5 EnabledForRepository (org.opengrok.indexer.condition.EnabledForRepository)5 HistoryGuru (org.opengrok.indexer.history.HistoryGuru)5 BeforeEach (org.junit.jupiter.api.BeforeEach)4 RepositoryInfo (org.opengrok.indexer.history.RepositoryInfo)4