Search in sources :

Example 56 with RuntimeEnvironment

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

the class IndexDatabase method optimizeAll.

/**
 * Optimize all index databases
 *
 * @param parallelizer a defined instance
 * @throws IOException if an error occurs
 */
static void optimizeAll(IndexerParallelizer parallelizer) throws IOException {
    List<IndexDatabase> dbs = new ArrayList<>();
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    if (env.hasProjects()) {
        for (Project project : env.getProjectList()) {
            dbs.add(new IndexDatabase(project));
        }
    } else {
        dbs.add(new IndexDatabase());
    }
    for (IndexDatabase d : dbs) {
        final IndexDatabase db = d;
        if (db.isDirty()) {
            parallelizer.getFixedExecutor().submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        db.update(parallelizer);
                    } catch (Throwable e) {
                        LOGGER.log(Level.SEVERE, "Problem updating lucene index database: ", e);
                    }
                }
            });
        }
    }
}
Also used : Project(org.opensolaris.opengrok.configuration.Project) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList)

Example 57 with RuntimeEnvironment

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

the class ProjectMessage method applyMessage.

@Override
protected byte[] applyMessage(RuntimeEnvironment env) throws Exception {
    String command = getText();
    validateMore(env);
    switch(command) {
        case "add":
            for (String projectName : getTags()) {
                File srcRoot = env.getSourceRootFile();
                File projDir = new File(srcRoot, projectName);
                if (!env.getProjects().containsKey(projectName)) {
                    Project project = new Project(projectName, "/" + projectName);
                    project.setTabSize(env.getConfiguration().getTabSize());
                    // Add repositories in this project.
                    List<RepositoryInfo> repos = getRepositoriesInDir(env, projDir);
                    env.addRepositories(repos);
                    env.getProjectRepositoriesMap().put(project, repos);
                    // Finally introduce the project to the configuration.
                    // Note that the project is inactive in the UI until it is indexed.
                    // See {@code isIndexed()}
                    env.getProjects().put(projectName, project);
                    Set<Project> projectSet = new TreeSet<>();
                    projectSet.add(project);
                    env.populateGroups(env.getGroups(), projectSet);
                } else {
                    Project project = env.getProjects().get(projectName);
                    Map<Project, List<RepositoryInfo>> map = env.getProjectRepositoriesMap();
                    // Refresh the list of repositories of this project.
                    // This is the goal of this action: if an existing project
                    // is re-added, this means its list of repositories has changed.
                    List<RepositoryInfo> repos = getRepositoriesInDir(env, projDir);
                    List<RepositoryInfo> allrepos = env.getRepositories();
                    synchronized (allrepos) {
                        // newly added repository
                        for (RepositoryInfo repo : repos) {
                            if (!allrepos.contains(repo)) {
                                allrepos.add(repo);
                            }
                        }
                        // deleted repository
                        for (RepositoryInfo repo : map.get(project)) {
                            if (!repos.contains(repo)) {
                                allrepos.remove(repo);
                            }
                        }
                    }
                    map.put(project, repos);
                }
            }
            break;
        case "delete":
            for (String projectName : getTags()) {
                Project proj = env.getProjects().get(projectName);
                if (proj == null) {
                    throw new Exception("cannot get project \"" + projectName + "\"");
                }
                LOGGER.log(Level.INFO, "deleting configuration for project " + projectName);
                // Remove the project from its groups.
                for (Group group : proj.getGroups()) {
                    group.getRepositories().remove(proj);
                    group.getProjects().remove(proj);
                }
                // Now remove the repositories associated with this project.
                List<RepositoryInfo> repos = env.getProjectRepositoriesMap().get(proj);
                env.getRepositories().removeAll(repos);
                env.getProjectRepositoriesMap().remove(proj);
                env.getProjects().remove(projectName, proj);
                // Prevent the project to be included in new searches.
                env.refreshSearcherManagerMap();
                // Lastly, remove data associated with the project.
                LOGGER.log(Level.INFO, "deleting data for project " + projectName);
                for (String dirName : new String[] { IndexDatabase.INDEX_DIR, IndexDatabase.XREF_DIR }) {
                    IOUtils.removeRecursive(Paths.get(env.getDataRootPath() + File.separator + dirName + File.separator + projectName));
                }
                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.INFO, "cannot remove files for repository {0}", x.getDirectoryName());
                        // {@code removeCache()} will return nothing.
                        return "";
                    }
                }).collect(Collectors.toSet()));
            }
            break;
        case "indexed":
            for (String projectName : getTags()) {
                Project project;
                if ((project = env.getProjects().get(projectName)) != null) {
                    project.setIndexed(true);
                    // Refresh current version of the project's repositories.
                    List<RepositoryInfo> riList = env.getProjectRepositoriesMap().get(project);
                    if (riList != null) {
                        for (RepositoryInfo ri : riList) {
                            Repository repo = getRepository(ri);
                            if (repo != null && repo.getCurrentVersion() != null && repo.getCurrentVersion().length() > 0) {
                                // getRepository() always creates fresh instance
                                // of the Repository object so there is no need
                                // to call setCurrentVersion() on it.
                                ri.setCurrentVersion(repo.determineCurrentVersion());
                            }
                        }
                    }
                } else {
                    LOGGER.log(Level.WARNING, "cannot find project " + projectName + " to mark as indexed");
                }
            }
            // In case this project has just been incrementally indexed,
            // its IndexSearcher needs a poke.
            env.maybeRefreshIndexSearchers(getTags());
            env.refreshDateForLastIndexRun();
            break;
        case "list":
            return (env.getProjectNames().stream().collect(Collectors.joining("\n")).getBytes());
        case "list-indexed":
            return (env.getProjectList().stream().filter(p -> p.isIndexed()).map(p -> p.getName()).collect(Collectors.joining("\n")).getBytes());
        case "get-repos":
            List<String> repos = new ArrayList<>();
            for (String projectName : getTags()) {
                Project project;
                if ((project = env.getProjects().get(projectName)) == null) {
                    continue;
                }
                List<RepositoryInfo> infos = env.getProjectRepositoriesMap().get(project);
                if (infos != null) {
                    repos.addAll(infos.stream().map(ri -> ri.getDirectoryNameRelative()).collect(Collectors.toList()));
                }
            }
            return repos.stream().collect(Collectors.joining("\n")).getBytes();
        case "get-repos-type":
            Set<String> types = new TreeSet<>();
            for (String projectName : getTags()) {
                Project project;
                if ((project = env.getProjects().get(projectName)) == null) {
                    continue;
                }
                List<RepositoryInfo> infos = env.getProjectRepositoriesMap().get(project);
                if (infos != null) {
                    types.addAll(infos.stream().map(ri -> ri.getType()).collect(Collectors.toList()));
                }
            }
            return types.stream().collect(Collectors.joining("\n")).getBytes();
    }
    return ("command \"" + getText() + "\" for projects " + String.join(",", getTags()) + " done").getBytes();
}
Also used : Repository(org.opensolaris.opengrok.history.Repository) Arrays(java.util.Arrays) ForbiddenSymlinkException(org.opensolaris.opengrok.util.ForbiddenSymlinkException) IOUtils(org.opensolaris.opengrok.util.IOUtils) HistoryGuru(org.opensolaris.opengrok.history.HistoryGuru) Set(java.util.Set) IOException(java.io.IOException) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) File(java.io.File) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) LoggerFactory(org.opensolaris.opengrok.logger.LoggerFactory) List(java.util.List) Paths(java.nio.file.Paths) Project(org.opensolaris.opengrok.configuration.Project) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) IndexDatabase(org.opensolaris.opengrok.index.IndexDatabase) Map(java.util.Map) RepositoryFactory.getRepository(org.opensolaris.opengrok.history.RepositoryFactory.getRepository) Group(org.opensolaris.opengrok.configuration.Group) RepositoryInfo(org.opensolaris.opengrok.history.RepositoryInfo) Group(org.opensolaris.opengrok.configuration.Group) ForbiddenSymlinkException(org.opensolaris.opengrok.util.ForbiddenSymlinkException) RepositoryInfo(org.opensolaris.opengrok.history.RepositoryInfo) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ForbiddenSymlinkException(org.opensolaris.opengrok.util.ForbiddenSymlinkException) IOException(java.io.IOException) Project(org.opensolaris.opengrok.configuration.Project) Repository(org.opensolaris.opengrok.history.Repository) RepositoryFactory.getRepository(org.opensolaris.opengrok.history.RepositoryFactory.getRepository) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) HistoryGuru(org.opensolaris.opengrok.history.HistoryGuru) File(java.io.File)

Example 58 with RuntimeEnvironment

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

the class PascalAnalyzerFactoryTest method setUpClass.

@BeforeClass
public static void setUpClass() throws Exception {
    ctags = new Ctags();
    ctags.setBinary(RuntimeEnvironment.getInstance().getCtags());
    repository = new TestRepository();
    repository.create(PascalAnalyzerFactoryTest.class.getResourceAsStream("/org/opensolaris/opengrok/index/source.zip"));
    PascalAnalyzerFactory analyzerFactory = new PascalAnalyzerFactory();
    analyzer = analyzerFactory.getAnalyzer();
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    if (env.validateExuberantCtags()) {
        analyzer.setCtags(new Ctags());
    }
}
Also used : TestRepository(org.opensolaris.opengrok.util.TestRepository) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) Ctags(org.opensolaris.opengrok.analysis.Ctags) BeforeClass(org.junit.BeforeClass)

Example 59 with RuntimeEnvironment

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

the class AuthorizationEntityTest method setUp.

@Before
public void setUp() {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    envGroups = env.getGroups();
    envProjects = env.getProjects();
    env.setGroups(new TreeSet<>());
    env.setProjects(new TreeMap<>());
}
Also used : RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) Before(org.junit.Before)

Example 60 with RuntimeEnvironment

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

the class AuthorizationEntityTest method testForGroupsAndForProjectsDiscoveryInvalidProjectInGroup.

/**
 * Listed projects in the group don't exist.
 */
@Test
public void testForGroupsAndForProjectsDiscoveryInvalidProjectInGroup() {
    AuthorizationEntity authEntity = authEntityFactory.apply(null);
    authEntity.setForGroups(new TreeSet<>(Arrays.asList(new String[] { "group 1", "group 2" })));
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    Group g1 = new Group();
    g1.setName("group 1");
    g1.addProject(new Project("project 1"));
    g1.addProject(new Project("project 2"));
    g1.addProject(new Project("project 3"));
    env.getGroups().add(g1);
    authEntity.load(new TreeMap<>());
    assertEquals(new TreeSet<>(Arrays.asList(new String[] { "group 1" })), authEntity.forGroups());
    assertEquals(new TreeSet<>(), authEntity.forProjects());
}
Also used : Group(org.opensolaris.opengrok.configuration.Group) Project(org.opensolaris.opengrok.configuration.Project) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) Test(org.junit.Test)

Aggregations

RuntimeEnvironment (org.opensolaris.opengrok.configuration.RuntimeEnvironment)81 File (java.io.File)31 Project (org.opensolaris.opengrok.configuration.Project)27 IOException (java.io.IOException)23 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)17 TestRepository (org.opensolaris.opengrok.util.TestRepository)13 ForbiddenSymlinkException (org.opensolaris.opengrok.util.ForbiddenSymlinkException)12 BeforeClass (org.junit.BeforeClass)11 Ctags (org.opensolaris.opengrok.analysis.Ctags)6 BufferedReader (java.io.BufferedReader)5 FileNotFoundException (java.io.FileNotFoundException)5 InputStreamReader (java.io.InputStreamReader)5 ParseException (java.text.ParseException)5 DateFormat (java.text.DateFormat)4 Date (java.util.Date)4 RepositoryInfo (org.opensolaris.opengrok.history.RepositoryInfo)4 OutputStreamWriter (java.io.OutputStreamWriter)3 ConnectException (java.net.ConnectException)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3