Search in sources :

Example 1 with Statistics

use of org.opensolaris.opengrok.web.Statistics in project OpenGrok by OpenGrok.

the class AuthorizationFrameworkReloadTest method testReloadSimple.

/**
 * After {@code reload()} the session attributes should be invalidated.
 * It is assumed that invalidation of HttpSession objects means that all
 * the attributes will be unset.
 */
@Test
public void testReloadSimple() {
    DummyHttpServletRequest req = new DummyHttpServletRequest();
    AuthorizationFramework framework = new AuthorizationFramework(pluginDirectory.getPath());
    // to avoid noise when loading classes of other tests
    framework.setLoadClasses(false);
    framework.reload();
    Statistics stats = RuntimeEnvironment.getInstance().getStatistics();
    // Ensure the framework was setup correctly.
    assertNotNull(framework.getPluginDirectory());
    assertEquals(pluginDirectory, framework.getPluginDirectory());
    // Create pre-requisite objects - mainly the HTTP session with attribute.
    Project p = new Project("project" + Math.random());
    HttpSession session = req.getSession();
    String attrName = "foo";
    session.setAttribute(attrName, "bar");
    assertNotNull(session.getAttribute(attrName));
    // Reload the framework to increment the plugin generation version.
    framework.reload();
    // Let the framework check the request. This should invalidate the session
    // since the version was incremented. In this test we are not interested
    // in the actual result.
    framework.isAllowed(req, p);
    assertNull(stats.getRequest("authorization_cache_hits"));
    // Verify that the session no longer has the attribute.
    assertNull(session.getAttribute(attrName));
}
Also used : Project(org.opensolaris.opengrok.configuration.Project) DummyHttpServletRequest(org.opensolaris.opengrok.web.DummyHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) Statistics(org.opensolaris.opengrok.web.Statistics) Test(org.junit.Test)

Example 2 with Statistics

use of org.opensolaris.opengrok.web.Statistics in project OpenGrok by OpenGrok.

the class RuntimeEnvironmentTest method testSaveStatistics.

@Test
public void testSaveStatistics() throws IOException {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    env.setStatistics(new Statistics());
    env.getStatistics().addRequest();
    env.getStatistics().addRequest("root");
    env.getStatistics().addRequestTime("root", 10L);
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        env.saveStatistics(out);
        Assert.assertNotEquals("{}", out.toString());
        Assert.assertEquals(env.getStatistics().toJson().toJSONString(), out.toString());
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Statistics(org.opensolaris.opengrok.web.Statistics) Test(org.junit.Test)

Example 3 with Statistics

use of org.opensolaris.opengrok.web.Statistics in project OpenGrok by OpenGrok.

the class RuntimeEnvironmentTest method testLoadEmptyStatistics.

@Test
public void testLoadEmptyStatistics() throws IOException, ParseException {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    String json = "{}";
    try (InputStream in = new StringInputStream(json)) {
        env.loadStatistics(in);
    }
    Assert.assertEquals(new Statistics().toJson(), env.getStatistics().toJson());
}
Also used : StringInputStream(org.apache.tools.ant.filters.StringInputStream) StringInputStream(org.apache.tools.ant.filters.StringInputStream) InputStream(java.io.InputStream) Statistics(org.opensolaris.opengrok.web.Statistics) Test(org.junit.Test)

Example 4 with Statistics

use of org.opensolaris.opengrok.web.Statistics in project OpenGrok by OpenGrok.

the class StatsMessageTest method testGetValidJson.

@Test
public void testGetValidJson() {
    testGet();
    Message m = new StatsMessage();
    m.setText("get");
    byte[] out = null;
    try {
        out = m.apply(env);
    } catch (Exception ex) {
        Assert.fail("Should not throw any exception");
    }
    JSONParser p = new JSONParser();
    Object o = null;
    try {
        o = p.parse(new String(out));
    } catch (ParseException ex) {
        Assert.fail("Should not throw any exception");
    }
    Assert.assertNotNull(o);
    Statistics stat = Statistics.from((JSONObject) o);
    Assert.assertTrue(stat instanceof Statistics);
    Assert.assertEquals(1, stat.getRequests());
    Assert.assertEquals(1, stat.getMinutes());
    Assert.assertEquals(0, stat.getRequestCategories().size());
    Assert.assertEquals(0, stat.getTiming().size());
}
Also used : JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) Statistics(org.opensolaris.opengrok.web.Statistics) ParseException(org.json.simple.parser.ParseException) Test(org.junit.Test)

Example 5 with Statistics

use of org.opensolaris.opengrok.web.Statistics in project OpenGrok by OpenGrok.

the class AuthorizationFramework method reload.

/**
 * Calling this function forces the framework to reload its stack.
 *
 * <p>
 * Plugins are taken from the pluginDirectory.</p>
 *
 * <p>
 * Old instances of stack are removed and new list of stack is constructed.
 * Unload and load event is fired on each plugin.</p>
 *
 * <p>
 * This method is thread safe with respect to the currently running
 * authorization checks.</p>
 *
 * @see IAuthorizationPlugin#load(java.util.Map)
 * @see IAuthorizationPlugin#unload()
 * @see Configuration#getPluginDirectory()
 */
@SuppressWarnings("unchecked")
public void reload() {
    if (pluginDirectory == null || !pluginDirectory.isDirectory() || !pluginDirectory.canRead()) {
        LOGGER.log(Level.WARNING, "Plugin directory not found or not readable: {0}. " + "All requests allowed.", pluginDirectory);
        return;
    }
    if (stack == null) {
        LOGGER.log(Level.WARNING, "Plugin stack not found in configuration: null. All requests allowed.");
        return;
    }
    LOGGER.log(Level.INFO, "Plugins are being reloaded from {0}", pluginDirectory.getAbsolutePath());
    // trashing out the old instance of the loaded enables us
    // to reload the stack at runtime
    loader = (AuthorizationPluginClassLoader) AccessController.doPrivileged(new PrivilegedAction() {

        @Override
        public Object run() {
            return new AuthorizationPluginClassLoader(pluginDirectory);
        }
    });
    AuthorizationStack newLocalStack;
    if (this.newStack == null) {
        // Clone a new stack not interfering with the current stack.
        newLocalStack = getStack().clone();
    } else {
        newLocalStack = this.newStack.clone();
    }
    // Load all other possible plugin classes.
    if (isLoadClassesEnabled()) {
        loadClassFiles(newLocalStack, IOUtils.listFilesRec(pluginDirectory, ".class"));
    }
    if (isLoadJarsEnabled()) {
        loadJarFiles(newLocalStack, IOUtils.listFiles(pluginDirectory, ".jar"));
    }
    // fire load events
    loadAllPlugins(newLocalStack);
    AuthorizationStack oldStack;
    /**
     * Replace the stack in a write lock to avoid inconsistent state between
     * the stack change and currently executing requests performing some
     * authorization on the same stack.
     *
     * @see #performCheck is controlled with a read lock
     */
    lock.writeLock().lock();
    try {
        oldStack = stack;
        stack = newLocalStack;
        // increase the current plugin version tracked by the framework
        increasePluginVersion();
    } finally {
        lock.writeLock().unlock();
    }
    Statistics stats = RuntimeEnvironment.getInstance().getStatistics();
    stats.addRequest("authorization_stack_reload");
    // clean the old stack
    removeAll(oldStack);
    oldStack = null;
}
Also used : PrivilegedAction(java.security.PrivilegedAction) Statistics(org.opensolaris.opengrok.web.Statistics)

Aggregations

Statistics (org.opensolaris.opengrok.web.Statistics)10 Test (org.junit.Test)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 HttpSession (javax.servlet.http.HttpSession)2 StringInputStream (org.apache.tools.ant.filters.StringInputStream)2 Project (org.opensolaris.opengrok.configuration.Project)2 DummyHttpServletRequest (org.opensolaris.opengrok.web.DummyHttpServletRequest)2 PrivilegedAction (java.security.PrivilegedAction)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1 ParseException (org.json.simple.parser.ParseException)1