Search in sources :

Example 6 with RuntimeEnvironment

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

the class WebappListener method contextInitialized.

/**
 * {@inheritDoc}
 */
@Override
public void contextInitialized(final ServletContextEvent servletContextEvent) {
    Instant start = Instant.now();
    ServletContext context = servletContextEvent.getServletContext();
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    LOGGER.log(Level.INFO, "Starting webapp with version {0} ({1})", new Object[] { Info.getVersion(), Info.getRevision() });
    String config = context.getInitParameter("CONFIGURATION");
    if (config == null) {
        throw new Error("CONFIGURATION parameter missing in the web.xml file");
    } else {
        try {
            env.readConfiguration(new File(config), CommandTimeoutType.WEBAPP_START);
        } catch (IOException ex) {
            LOGGER.log(Level.WARNING, "Configuration error. Failed to read config file: ", ex);
        }
    }
    /*
         * Create a new instance of authorization framework. If the code above
         * (reading the configuration) failed then the plugin directory is
         * possibly {@code null} causing the framework to allow every request.
         */
    env.setAuthorizationFramework(new AuthorizationFramework(env.getPluginDirectory(), env.getPluginStack()));
    env.getAuthorizationFramework().reload();
    if (env.isWebappCtags() && !env.validateUniversalCtags()) {
        LOGGER.warning("Didn't find Universal Ctags for --webappCtags");
    }
    String pluginDirectory = env.getPluginDirectory();
    if (pluginDirectory != null && env.isAuthorizationWatchdog()) {
        env.getWatchDog().start(new File(pluginDirectory));
    }
    // Check index(es).
    checkIndex(env);
    env.startExpirationTimer();
    ApiTaskManager.getInstance().setContextPath(context.getContextPath());
    // register API task queues
    ApiTaskManager.getInstance().addPool(ProjectsController.PROJECTS_PATH, 1);
    // Used by ConfigurationController#reloadAuthorization()
    ApiTaskManager.getInstance().addPool("authorization", 1);
    ApiTaskManager.getInstance().addPool(ConfigurationController.PATH, 1);
    startupTimer.record(Duration.between(start, Instant.now()));
}
Also used : RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) AuthorizationFramework(org.opengrok.indexer.authorization.AuthorizationFramework) Instant(java.time.Instant) ServletContext(jakarta.servlet.ServletContext) IOException(java.io.IOException) File(java.io.File)

Example 7 with RuntimeEnvironment

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

the class WebappListener method contextDestroyed.

/**
 * {@inheritDoc}
 */
@Override
public void contextDestroyed(final ServletContextEvent servletContextEvent) {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    env.getIndexerParallelizer().bounce();
    env.getWatchDog().stop();
    env.stopExpirationTimer();
    try {
        env.shutdownRevisionExecutor();
    } catch (InterruptedException e) {
        LOGGER.log(Level.WARNING, "Could not shutdown revision executor", e);
    }
    // need to explicitly close the suggester service because it might have scheduled rebuild which could prevent
    // the web application from closing
    SuggesterServiceFactory.getDefault().close();
    // destroy queue(s) of API tasks
    try {
        ApiTaskManager.getInstance().shutdown();
    } catch (InterruptedException e) {
        LOGGER.log(Level.WARNING, "could not shutdown API task manager cleanly", e);
    }
}
Also used : RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment)

Example 8 with RuntimeEnvironment

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

the class Indexer method doIndexerExecution.

/**
 * This is the second phase of the indexer which generates Lucene index
 * by passing source code files through ctags, generating xrefs
 * and storing data from the source files in the index (along with history,
 * if any).
 *
 * @param update if set to true, index database is updated, otherwise optimized
 * @param subFiles index just some subdirectories
 * @param progress object to receive notifications as indexer progress is made
 * @throws IOException if I/O exception occurred
 */
public void doIndexerExecution(final boolean update, List<String> subFiles, IndexChangedListener progress) throws IOException {
    Statistics elapsed = new Statistics();
    LOGGER.info("Starting indexing");
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    IndexerParallelizer parallelizer = env.getIndexerParallelizer();
    final CountDownLatch latch;
    if (subFiles == null || subFiles.isEmpty()) {
        if (update) {
            latch = IndexDatabase.updateAll(progress);
        } else if (env.isOptimizeDatabase()) {
            latch = IndexDatabase.optimizeAll();
        } else {
            latch = new CountDownLatch(0);
        }
    } else {
        List<IndexDatabase> dbs = new ArrayList<>();
        for (String path : subFiles) {
            Project project = Project.getProject(path);
            if (project == null && env.hasProjects()) {
                LOGGER.log(Level.WARNING, "Could not find a project for \"{0}\"", path);
            } else {
                IndexDatabase db;
                if (project == null) {
                    db = new IndexDatabase();
                } else {
                    db = new IndexDatabase(project);
                }
                int idx = dbs.indexOf(db);
                if (idx != -1) {
                    db = dbs.get(idx);
                }
                if (db.addDirectory(path)) {
                    if (idx == -1) {
                        dbs.add(db);
                    }
                } else {
                    LOGGER.log(Level.WARNING, "Directory does not exist \"{0}\"", path);
                }
            }
        }
        latch = new CountDownLatch(dbs.size());
        for (final IndexDatabase db : dbs) {
            final boolean optimize = env.isOptimizeDatabase();
            db.addIndexChangedListener(progress);
            parallelizer.getFixedExecutor().submit(() -> {
                try {
                    if (update) {
                        db.update();
                    } else if (optimize) {
                        db.optimize();
                    }
                } catch (Throwable e) {
                    LOGGER.log(Level.SEVERE, "An error occurred while " + (update ? "updating" : "optimizing") + " index", e);
                } finally {
                    latch.countDown();
                }
            });
        }
    }
    // Wait forever for the executors to finish.
    try {
        LOGGER.info("Waiting for the executors to finish");
        latch.await();
    } catch (InterruptedException exp) {
        LOGGER.log(Level.WARNING, "Received interrupt while waiting" + " for executor to finish", exp);
    }
    elapsed.report(LOGGER, "Done indexing data of all repositories", "indexer.repository.indexing");
    CtagsUtil.deleteTempFiles();
}
Also used : RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Statistics(org.opengrok.indexer.util.Statistics) Project(org.opengrok.indexer.configuration.Project)

Example 9 with RuntimeEnvironment

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

the class IndexDatabase method initialize.

@SuppressWarnings("PMD.CollapsibleIfStatements")
private void initialize() throws IOException {
    synchronized (INSTANCE_LOCK) {
        RuntimeEnvironment env = RuntimeEnvironment.getInstance();
        File indexDir = new File(env.getDataRootFile(), INDEX_DIR);
        if (project != null) {
            indexDir = new File(indexDir, project.getPath());
        }
        if (!indexDir.exists() && !indexDir.mkdirs()) {
            // to avoid race conditions, just recheck..
            if (!indexDir.exists()) {
                throw new FileNotFoundException("Failed to create root directory [" + indexDir.getAbsolutePath() + "]");
            }
        }
        lockfact = pickLockFactory(env);
        indexDirectory = FSDirectory.open(indexDir.toPath(), lockfact);
        pathAccepter = env.getPathAccepter();
        analyzerGuru = new AnalyzerGuru();
        xrefDir = new File(env.getDataRootFile(), XREF_DIR);
        listeners = new CopyOnWriteArrayList<>();
        dirtyFile = new File(indexDir, "dirty");
        dirty = dirtyFile.exists();
        directories = new ArrayList<>();
    }
}
Also used : RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) AnalyzerGuru(org.opengrok.indexer.analysis.AnalyzerGuru)

Example 10 with RuntimeEnvironment

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

the class IndexDatabase method listFrequentTokens.

static void listFrequentTokens(List<String> subFiles) throws IOException {
    final int limit = 4;
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    if (env.hasProjects()) {
        if (subFiles == null || subFiles.isEmpty()) {
            for (Project project : env.getProjectList()) {
                IndexDatabase db = new IndexDatabase(project);
                db.listTokens(limit);
            }
        } else {
            for (String path : subFiles) {
                Project project = Project.getProject(path);
                if (project == null) {
                    LOGGER.log(Level.WARNING, "Could not find a project for \"{0}\"", path);
                } else {
                    IndexDatabase db = new IndexDatabase(project);
                    db.listTokens(limit);
                }
            }
        }
    } else {
        IndexDatabase db = new IndexDatabase();
        db.listTokens(limit);
    }
}
Also used : Project(org.opengrok.indexer.configuration.Project) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment)

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