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));
}
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());
}
}
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());
}
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());
}
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;
}
Aggregations