Search in sources :

Example 1 with RepositoryInfo

use of org.opengrok.indexer.history.RepositoryInfo in project OpenGrok by OpenGrok.

the class ProjectsController method getRepositories.

@GET
@Path("/{project}/repositories")
@Produces(MediaType.APPLICATION_JSON)
public List<String> getRepositories(@PathParam("project") String projectName) {
    // Avoid classification as a taint bug.
    projectName = Laundromat.launderInput(projectName);
    Project project = env.getProjects().get(projectName);
    if (project != null) {
        List<RepositoryInfo> infos = env.getProjectRepositoriesMap().get(project);
        if (infos != null) {
            return infos.stream().map(RepositoryInfo::getDirectoryNameRelative).collect(Collectors.toList());
        }
    }
    return Collections.emptyList();
}
Also used : Project(org.opengrok.indexer.configuration.Project) RepositoryInfo(org.opengrok.indexer.history.RepositoryInfo) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 2 with RepositoryInfo

use of org.opengrok.indexer.history.RepositoryInfo in project OpenGrok by OpenGrok.

the class Indexer method main.

/**
 * Program entry point.
 *
 * @param argv argument vector
 */
@SuppressWarnings("PMD.UseStringBufferForStringAppends")
public static void main(String[] argv) {
    // this won't count JVM creation though
    Statistics stats = new Statistics();
    boolean update = true;
    Executor.registerErrorHandler();
    List<String> subFiles = RuntimeEnvironment.getInstance().getSubFiles();
    ArrayList<String> subFilesList = new ArrayList<>();
    boolean createDict = false;
    try {
        argv = parseOptions(argv);
        if (webappURI != null && !HostUtil.isReachable(webappURI, WEBAPP_CONNECT_TIMEOUT)) {
            System.err.println(webappURI + " is not reachable.");
            System.exit(1);
        }
        /*
             * Attend to disabledRepositories here in case exitWithHelp() will
             * need to report about repos.
             */
        disabledRepositories.addAll(cfg.getDisabledRepositories());
        cfg.setDisabledRepositories(disabledRepositories);
        for (String repoName : disabledRepositories) {
            LOGGER.log(Level.FINEST, "Disabled {0}", repoName);
        }
        if (help) {
            exitWithHelp();
        }
        checkConfiguration();
        if (awaitProfiler) {
            pauseToAwaitProfiler();
        }
        env = RuntimeEnvironment.getInstance();
        env.setIndexer(true);
        // Complete the configuration of repository types.
        List<Class<? extends Repository>> repositoryClasses = RepositoryFactory.getRepositoryClasses();
        for (Class<? extends Repository> clazz : repositoryClasses) {
            // Set external repository binaries from System properties.
            try {
                Field f = clazz.getDeclaredField("CMD_PROPERTY_KEY");
                Object key = f.get(null);
                if (key != null) {
                    cfg.setRepoCmd(clazz.getCanonicalName(), System.getProperty(key.toString()));
                }
            } catch (Exception e) {
            // don't care
            }
        }
        // Logging starts here.
        if (verbose) {
            String fn = LoggerUtil.getFileHandlerPattern();
            if (fn != null) {
                System.out.println("Logging filehandler pattern: " + fn);
            }
        }
        // automatically allow symlinks that are directly in source root
        File sourceRootFile = new File(cfg.getSourceRoot());
        File[] projectDirs = sourceRootFile.listFiles();
        if (projectDirs != null) {
            for (File projectDir : projectDirs) {
                if (!projectDir.getCanonicalPath().equals(projectDir.getAbsolutePath())) {
                    allowedSymlinks.add(projectDir.getAbsolutePath());
                }
            }
        }
        allowedSymlinks.addAll(cfg.getAllowedSymlinks());
        cfg.setAllowedSymlinks(allowedSymlinks);
        canonicalRoots.addAll(cfg.getCanonicalRoots());
        cfg.setCanonicalRoots(canonicalRoots);
        // This will be used to perform more fine-grained checking in invalidateRepositories().
        for (String arg : argv) {
            String path = Paths.get(cfg.getSourceRoot(), arg).toString();
            subFilesList.add(path);
        }
        // according to the project key which is the same.
        for (Entry<String, Project> entry : cfg.getProjects().entrySet()) {
            if (entry.getValue().getName() == null) {
                entry.getValue().setName(entry.getKey());
            }
        }
        // with return code upon failure.
        if (checkIndex) {
            if (cfg.getDataRoot() == null || cfg.getDataRoot().isEmpty()) {
                System.err.println("Need data root in configuration for index check (use -R)");
                System.exit(1);
            }
            if (!IndexCheck.check(cfg, subFilesList)) {
                System.err.printf("Index check failed%n");
                System.err.print("You might want to remove " + (!subFilesList.isEmpty() ? "data for projects " + String.join(",", subFilesList) : "all data") + " under the data root and reindex\n");
                System.exit(1);
            }
            System.exit(0);
        }
        // Set updated configuration in RuntimeEnvironment.
        env.setConfiguration(cfg, subFilesList, CommandTimeoutType.INDEXER);
        // Let repository types to add items to ignoredNames.
        // This changes env so is called after the setConfiguration()
        // call above.
        RepositoryFactory.initializeIgnoredNames(env);
        if (bareConfig) {
            getInstance().sendToConfigHost(env, webappURI);
            writeConfigToFile(env, configFilename);
            System.exit(0);
        }
        /*
             * Add paths to directories under source root. If projects
             * are enabled the path should correspond to a project because
             * project path is necessary to correctly set index directory
             * (otherwise the index files will end up in index data root
             * directory and not per project data root directory).
             * For the check we need to have 'env' already set.
             */
        for (String path : subFilesList) {
            String srcPath = env.getSourceRootPath();
            if (srcPath == null) {
                System.err.println("Error getting source root from environment. Exiting.");
                System.exit(1);
            }
            path = path.substring(srcPath.length());
            if (env.hasProjects()) {
                // The paths need to correspond to a project.
                Project project;
                if ((project = Project.getProject(path)) != null) {
                    subFiles.add(path);
                    List<RepositoryInfo> repoList = env.getProjectRepositoriesMap().get(project);
                    if (repoList != null) {
                        repositories.addAll(repoList.stream().map(RepositoryInfo::getDirectoryNameRelative).collect(Collectors.toSet()));
                    }
                } else {
                    System.err.println("The path " + path + " does not correspond to a project");
                }
            } else {
                subFiles.add(path);
            }
        }
        if (!subFilesList.isEmpty() && subFiles.isEmpty()) {
            System.err.println("None of the paths were added, exiting");
            System.exit(1);
        }
        Metrics.updateSubFiles(subFiles);
        // emitted during indexing do not cause validation error.
        if (addProjects && webappURI != null) {
            try {
                IndexerUtil.enableProjects(webappURI);
            } catch (Exception e) {
                LOGGER.log(Level.SEVERE, String.format("Couldn't notify the webapp on %s.", webappURI), e);
                System.err.printf("Couldn't notify the webapp on %s: %s.%n", webappURI, e.getLocalizedMessage());
            }
        }
        LOGGER.log(Level.INFO, "Indexer version {0} ({1}) running on Java {2}", new Object[] { Info.getVersion(), Info.getRevision(), System.getProperty("java.version") });
        // Create history cache first.
        if (searchRepositories) {
            if (searchPaths.isEmpty()) {
                String[] dirs = env.getSourceRootFile().list((f, name) -> f.isDirectory() && env.getPathAccepter().accept(f));
                if (dirs != null) {
                    searchPaths.addAll(Arrays.asList(dirs));
                }
            }
            searchPaths = searchPaths.stream().map(t -> Paths.get(env.getSourceRootPath(), t).toString()).collect(Collectors.toSet());
        }
        getInstance().prepareIndexer(env, searchPaths, addProjects, createDict, runIndex, subFiles, new ArrayList<>(repositories));
        // prepareIndexer() populated the list of projects so now default projects can be set.
        env.setDefaultProjectsFromNames(defaultProjects);
        // And now index it all.
        if (runIndex || (optimizedChanged && env.isOptimizeDatabase())) {
            IndexChangedListener progress = new DefaultIndexChangedListener();
            getInstance().doIndexerExecution(update, subFiles, progress);
        }
        writeConfigToFile(env, configFilename);
        // or send new configuration to the web application in the case of full reindex.
        if (webappURI != null) {
            if (!subFiles.isEmpty()) {
                getInstance().refreshSearcherManagers(env, subFiles, webappURI);
            } else {
                getInstance().sendToConfigHost(env, webappURI);
            }
        }
        env.getIndexerParallelizer().bounce();
    } catch (ParseException e) {
        System.err.println("** " + e.getMessage());
        System.exit(1);
    } catch (IndexerException ex) {
        LOGGER.log(Level.SEVERE, "Exception running indexer", ex);
        System.err.println("Exception: " + ex.getLocalizedMessage());
        System.err.println(optParser.getUsage());
        System.exit(1);
    } catch (Throwable e) {
        LOGGER.log(Level.SEVERE, "Unexpected Exception", e);
        System.err.println("Exception: " + e.getLocalizedMessage());
        System.exit(1);
    } finally {
        stats.report(LOGGER, "Indexer finished", "indexer.total");
    }
}
Also used : ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) RepositoryInfo(org.opengrok.indexer.history.RepositoryInfo) Statistics(org.opengrok.indexer.util.Statistics) URISyntaxException(java.net.URISyntaxException) ParseException(java.text.ParseException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Project(org.opengrok.indexer.configuration.Project) Repository(org.opengrok.indexer.history.Repository) ParseException(java.text.ParseException) File(java.io.File)

Example 3 with RepositoryInfo

use of org.opengrok.indexer.history.RepositoryInfo in project OpenGrok by OpenGrok.

the class ProjectsController method deleteHistoryCacheWorkHorse.

private void deleteHistoryCacheWorkHorse(String projectName, boolean clearHistoryGuru) {
    Project project = disableProject(projectName);
    LOGGER.log(Level.INFO, "deleting history cache for project {0}", projectName);
    List<RepositoryInfo> repos = env.getProjectRepositoriesMap().get(project);
    if (repos == null || repos.isEmpty()) {
        LOGGER.log(Level.INFO, "history cache for project {0} is not present", projectName);
        return;
    }
    // Delete history cache data.
    HistoryGuru guru = HistoryGuru.getInstance();
    guru.removeCache(repos.stream().map(x -> {
        try {
            return env.getPathRelativeToSourceRoot(new File((x).getDirectoryName()));
        } catch (ForbiddenSymlinkException e) {
            LOGGER.log(Level.FINER, e.getMessage());
            return "";
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, "cannot remove files for repository {0}", x.getDirectoryName());
            // {@code removeCache()} will return nothing.
            return "";
        }
    }).filter(x -> !x.isEmpty()).collect(Collectors.toSet()), clearHistoryGuru);
}
Also used : Repository(org.opengrok.indexer.history.Repository) SuggesterService(org.opengrok.web.api.v1.suggester.provider.service.SuggesterService) Context(jakarta.ws.rs.core.Context) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Group(org.opengrok.indexer.configuration.Group) Project(org.opengrok.indexer.configuration.Project) CompletableFuture(java.util.concurrent.CompletableFuture) GET(jakarta.ws.rs.GET) ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) PUT(jakarta.ws.rs.PUT) WebApplicationException(jakarta.ws.rs.WebApplicationException) TreeSet(java.util.TreeSet) ApiTask(org.opengrok.web.api.ApiTask) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Path(jakarta.ws.rs.Path) ClassUtil(org.opengrok.indexer.util.ClassUtil) Response(jakarta.ws.rs.core.Response) RepositoryFactory.getRepository(org.opengrok.indexer.history.RepositoryFactory.getRepository) Map(java.util.Map) RepositoryInfo(org.opengrok.indexer.history.RepositoryInfo) ApiTaskManager(org.opengrok.web.api.ApiTaskManager) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) Laundromat(org.opengrok.indexer.web.Laundromat) Produces(jakarta.ws.rs.Produces) CommandTimeoutType(org.opengrok.indexer.configuration.CommandTimeoutType) Consumes(jakarta.ws.rs.Consumes) NotFoundException(jakarta.ws.rs.NotFoundException) POST(jakarta.ws.rs.POST) IOUtils(org.opengrok.indexer.util.IOUtils) Set(java.util.Set) IOException(java.io.IOException) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) IndexDatabase(org.opengrok.indexer.index.IndexDatabase) File(java.io.File) List(java.util.List) MediaType(jakarta.ws.rs.core.MediaType) Paths(java.nio.file.Paths) LoggerFactory(org.opengrok.indexer.logger.LoggerFactory) DELETE(jakarta.ws.rs.DELETE) HistoryGuru(org.opengrok.indexer.history.HistoryGuru) Inject(jakarta.inject.Inject) PathParam(jakarta.ws.rs.PathParam) Collections(java.util.Collections) Project(org.opengrok.indexer.configuration.Project) ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) RepositoryInfo(org.opengrok.indexer.history.RepositoryInfo) HistoryGuru(org.opengrok.indexer.history.HistoryGuru) IOException(java.io.IOException) File(java.io.File)

Example 4 with RepositoryInfo

use of org.opengrok.indexer.history.RepositoryInfo in project OpenGrok by OpenGrok.

the class ProjectHelperTest method testUnAllowedGetRepositoryInfo.

/**
 * Test of getRepositoryInfo method, of class ProjectHelper.
 */
@Test
public void testUnAllowedGetRepositoryInfo() {
    Project p = new Project("repository_2_1");
    p.setIndexed(true);
    List<RepositoryInfo> result = helper.getRepositoryInfo(p);
    assertEquals(0, result.size(), "this project is not allowed");
}
Also used : Project(org.opengrok.indexer.configuration.Project) RepositoryInfo(org.opengrok.indexer.history.RepositoryInfo) Test(org.junit.jupiter.api.Test)

Example 5 with RepositoryInfo

use of org.opengrok.indexer.history.RepositoryInfo in project OpenGrok by OpenGrok.

the class RuntimeEnvironmentTest method testPopulateGroupsMultipleTimes.

@Test
public void testPopulateGroupsMultipleTimes() {
    // create a structure with two repositories
    final RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    Project project1 = new Project("bar", "/bar");
    env.getProjects().put(project1.getName(), project1);
    Project project2 = new Project("barfoo", "/barfoo");
    env.getProjects().put(project2.getName(), project2);
    final Group group1 = new Group("group1", "bar");
    env.getGroups().add(group1);
    final Group group2 = new Group("group2", "bar.*");
    env.getGroups().add(group2);
    final RepositoryInfo repository1 = new RepositoryInfo();
    repository1.setDirectoryNameRelative("/bar");
    env.getRepositories().add(repository1);
    final RepositoryInfo repo2 = new RepositoryInfo();
    repository1.setDirectoryNameRelative("/barfoo");
    env.getRepositories().add(repo2);
    env.getProjectRepositoriesMap().put(project1, Arrays.asList(repository1));
    env.getProjectRepositoriesMap().put(project2, Arrays.asList(repo2));
    assertEquals(2, env.getProjects().size());
    assertEquals(2, env.getRepositories().size());
    assertEquals(2, env.getProjectRepositoriesMap().size());
    assertEquals(2, env.getGroups().size());
    // populate groups for the first time
    env.populateGroups(env.getGroups(), new TreeSet<>(env.getProjects().values()));
    assertEquals(2, env.getProjects().size());
    assertEquals(2, env.getRepositories().size());
    assertEquals(2, env.getProjectRepositoriesMap().size());
    assertEquals(2, env.getGroups().size());
    assertEquals(0, group1.getProjects().size());
    assertEquals(1, group1.getRepositories().size());
    assertEquals(0, group2.getProjects().size());
    assertEquals(2, group2.getRepositories().size());
    // remove a single repository object => project1 will become a simple project
    env.getProjectRepositoriesMap().remove(project1);
    env.getRepositories().remove(repository1);
    // populate groups for the second time
    env.populateGroups(env.getGroups(), new TreeSet<>(env.getProjects().values()));
    assertEquals(2, env.getProjects().size());
    assertEquals(1, env.getRepositories().size());
    assertEquals(1, env.getProjectRepositoriesMap().size());
    assertEquals(2, env.getGroups().size());
    assertEquals(1, group1.getProjects().size());
    assertEquals(0, group1.getRepositories().size());
    assertEquals(1, group2.getProjects().size());
    assertEquals(1, group2.getRepositories().size());
}
Also used : RepositoryInfo(org.opengrok.indexer.history.RepositoryInfo) Test(org.junit.jupiter.api.Test)

Aggregations

RepositoryInfo (org.opengrok.indexer.history.RepositoryInfo)21 Project (org.opengrok.indexer.configuration.Project)16 Test (org.junit.jupiter.api.Test)10 File (java.io.File)9 ArrayList (java.util.ArrayList)7 List (java.util.List)6 Path (jakarta.ws.rs.Path)5 Group (org.opengrok.indexer.configuration.Group)5 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)5 MercurialRepositoryTest (org.opengrok.indexer.history.MercurialRepositoryTest)5 IOException (java.io.IOException)4 Path (java.nio.file.Path)4 EnabledForRepository (org.opengrok.indexer.condition.EnabledForRepository)4 GET (jakarta.ws.rs.GET)3 Produces (jakarta.ws.rs.Produces)3 Response (jakarta.ws.rs.core.Response)3 Paths (java.nio.file.Paths)3 Collections (java.util.Collections)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3