Skip to content

Commit 8ae5dce

Browse files
author
Vladimir Kotal
committed
get rid of reload() in AuthorizationFramework constructor
fixes #1925
1 parent 5990576 commit 8ae5dce

File tree

8 files changed

+124
-46
lines changed

8 files changed

+124
-46
lines changed

plugins/LdapPlugin/test/opengrok/auth/plugin/LdapAttrPluginTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public void setUp() {
8080

8181
plugin.load(parameters);
8282

83-
framework = new AuthorizationFramework(null);
83+
framework = new AuthorizationFramework();
84+
framework.reload();
8485
}
8586

8687
private void prepareRequest(String username, String mail, String... ous) {

src/org/opensolaris/opengrok/authorization/AuthorizationFramework.java

Lines changed: 94 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public final class AuthorizationFramework {
7474
* Stack of available plugins/stacks in the order of the execution.
7575
*/
7676
AuthorizationStack stack;
77+
/**
78+
* New stack. This is set by {@code setStack()} and used for delayed
79+
* reconfiguration in {@code reload()}.
80+
*/
81+
AuthorizationStack newStack;
7782

7883
/**
7984
* Lock for safe reloads.
@@ -89,6 +94,20 @@ public final class AuthorizationFramework {
8994
*/
9095
private long pluginVersion = 0;
9196

97+
/**
98+
* Whether to load plugins from class files and jar files.
99+
*/
100+
private boolean loadClasses = true;
101+
private boolean loadJars = true;
102+
103+
/**
104+
* Create a new instance of authorization framework with no plugin
105+
* directory and the default plugin stack.
106+
*/
107+
public AuthorizationFramework() {
108+
this(null);
109+
}
110+
92111
/**
93112
* Create a new instance of authorization framework with the plugin
94113
* directory and the default plugin stack.
@@ -109,7 +128,6 @@ public AuthorizationFramework(String path) {
109128
public AuthorizationFramework(String path, AuthorizationStack stack) {
110129
this.stack = stack;
111130
setPluginDirectory(path);
112-
reload();
113131
}
114132

115133
/**
@@ -138,6 +156,38 @@ public void setPluginDirectory(String directory) {
138156
setPluginDirectory(directory != null ? new File(directory) : null);
139157
}
140158

159+
/**
160+
* Make {@code reload()} search for plugins in class files.
161+
* @param flag true or false
162+
*/
163+
public void setLoadClasses(boolean flag) {
164+
loadClasses = flag;
165+
}
166+
167+
/**
168+
* Whether to search for plugins in class files.
169+
* @return true if enabled, false otherwise
170+
*/
171+
public boolean isLoadClassesEnabled() {
172+
return loadClasses;
173+
}
174+
175+
/**
176+
* Make {@code reload()} search for plugins in jar files.
177+
* @param flag true or false
178+
*/
179+
public void setLoadJars(boolean flag) {
180+
loadJars = flag;
181+
}
182+
183+
/**
184+
* Whether to search for plugins in class files.
185+
* @return true if enabled, false otherwise
186+
*/
187+
public boolean isLoadJarsEnabled() {
188+
return loadJars;
189+
}
190+
141191
/**
142192
* Checks if the request should have an access to project. See
143193
* {@link #checkAll} for more information about invocation order.
@@ -248,17 +298,13 @@ public AuthorizationStack getStack() {
248298
}
249299

250300
/**
251-
* Set the internal stack to this new value.
301+
* Set new value of the authorization stack. This will come into effect
302+
* only after {@code reload()} is called.
252303
*
253304
* @param s new stack to be used
254305
*/
255306
public void setStack(AuthorizationStack s) {
256-
lock.writeLock().lock();
257-
try {
258-
this.stack = s;
259-
} finally {
260-
lock.writeLock().unlock();
261-
}
307+
this.newStack = s;
262308
}
263309

264310
/**
@@ -438,20 +484,19 @@ protected List<Class> getInterfaces(Class c) {
438484
}
439485

440486
/**
441-
* Traverse list of files which possibly contain a java class and then
442-
* traverse a list of jar files to load all classes which are contained
443-
* within them into the given stack. Each class is loaded with
444-
* {@link #handleLoadClass(String)} which delegates the loading to the
445-
* custom class loader {@link #loadClass(String)}.
487+
* Traverse list of files which possibly contain a java class
488+
* to load all classes which are contained within them into the given stack.
489+
* Each class is loaded with {@link #handleLoadClass(String)} which
490+
* delegates the loading to the custom class loader
491+
* {@link #loadClass(String)}.
446492
*
447493
* @param stack the stack where to add the loaded classes
448494
* @param classfiles list of files which possibly contain a java class
449-
* @param jarfiles list of jar files containing java classes
450495
*
451496
* @see #handleLoadClass(String)
452497
* @see #loadClass(String)
453498
*/
454-
private void loadClasses(AuthorizationStack stack, List<File> classfiles, List<File> jarfiles) {
499+
private void loadClassFiles(AuthorizationStack stack, List<File> classfiles) {
455500
IAuthorizationPlugin pf;
456501

457502
for (File file : classfiles) {
@@ -467,6 +512,23 @@ private void loadClasses(AuthorizationStack stack, List<File> classfiles, List<F
467512
}
468513
}
469514
}
515+
}
516+
517+
/**
518+
* Traverse list of jar files to load all classes which are contained within
519+
* them into the given stack.
520+
* Each class is loaded with {@link #handleLoadClass(String)} which
521+
* delegates the loading to the custom class loader
522+
* {@link #loadClass(String)}.
523+
*
524+
* @param stack the stack where to add the loaded classes
525+
* @param jarfiles list of jar files containing java classes
526+
*
527+
* @see #handleLoadClass(String)
528+
* @see #loadClass(String)
529+
*/
530+
private void loadJarFiles(AuthorizationStack stack, List<File> jarfiles) {
531+
IAuthorizationPlugin pf;
470532

471533
for (File file : jarfiles) {
472534
try (JarFile jar = new JarFile(file)) {
@@ -544,16 +606,24 @@ public Object run() {
544606
}
545607
});
546608

547-
// clone a new stack not interfering with the current stack
548-
AuthorizationStack newStack = getStack().clone();
549-
550-
// load all other possible plugin classes
551-
loadClasses(newStack,
552-
IOUtils.listFilesRec(pluginDirectory, ".class"),
553-
IOUtils.listFiles(pluginDirectory, ".jar"));
609+
AuthorizationStack newLocalStack;
610+
if (this.newStack == null) {
611+
// Clone a new stack not interfering with the current stack.
612+
newLocalStack = getStack().clone();
613+
} else {
614+
newLocalStack = this.newStack.clone();
615+
}
616+
617+
// Load all other possible plugin classes.
618+
if (isLoadClassesEnabled()) {
619+
loadClassFiles(newLocalStack, IOUtils.listFilesRec(pluginDirectory, ".class"));
620+
}
621+
if (isLoadJarsEnabled()) {
622+
loadJarFiles(newLocalStack, IOUtils.listFiles(pluginDirectory, ".jar"));
623+
}
554624

555625
// fire load events
556-
loadAllPlugins(newStack);
626+
loadAllPlugins(newLocalStack);
557627

558628
AuthorizationStack oldStack;
559629
/**
@@ -566,7 +636,7 @@ public Object run() {
566636
lock.writeLock().lock();
567637
try {
568638
oldStack = stack;
569-
stack = newStack;
639+
stack = newLocalStack;
570640

571641
// increase the current plugin version tracked by the framework
572642
increasePluginVersion();

src/org/opensolaris/opengrok/web/WebappListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public void contextInitialized(final ServletContextEvent servletContextEvent) {
8181
* possibly {@code null} causing the framework to allow every request.
8282
*/
8383
env.setAuthorizationFramework(new AuthorizationFramework(env.getPluginDirectory(), env.getPluginStack()));
84+
env.getAuthorizationFramework().reload();
8485

8586
String address = context.getInitParameter("ConfigAddress");
8687
if (address != null && address.length() > 0) {

test/org/opensolaris/opengrok/authorization/AuthorizationFrameworkReloadTest.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@
3939
/**
4040
* Test behavior of AuthorizationFramework {@code reload()} w.r.t. HTTP sessions.
4141
*
42-
* Note that the tests are accompanied with noise when reloading since
43-
* the plugin directory contains classes of other tests from this package
44-
* which the authorization framework is trying (unsuccessfully, since they
45-
* do not implement the plugin interface) to load.
46-
*
4742
* @author Vladimir Kotal
4843
*/
4944
public class AuthorizationFrameworkReloadTest {
@@ -66,6 +61,8 @@ public AuthorizationFrameworkReloadTest() {
6661
public void testReloadSimple() {
6762
DummyHttpServletRequest req = new DummyHttpServletRequest();
6863
AuthorizationFramework framework = new AuthorizationFramework(pluginDirectory.getPath());
64+
framework.setLoadClasses(false); // to avoid noise when loading classes of other tests
65+
framework.reload();
6966
Statistics stats = RuntimeEnvironment.getInstance().getStatistics();
7067

7168
// Ensure the framework was setup correctly.
@@ -109,6 +106,8 @@ public void testReloadCycle() {
109106
stack.setForProjects(projectName);
110107
AuthorizationFramework framework =
111108
new AuthorizationFramework(pluginDirectory.getPath(), stack);
109+
framework.setLoadClasses(false); // to avoid noise when loading classes of other tests
110+
framework.reload();
112111

113112
// Perform simple sanity check before long run is entered. If this fails,
114113
// it will be waste of time to continue with the test.
@@ -158,4 +157,20 @@ public void run() {
158157
System.out.println("number of reloads: " + reloads);
159158
assertTrue(reloads > 0);
160159
}
160+
161+
@Test
162+
public void testSetLoadClasses() {
163+
AuthorizationFramework framework = new AuthorizationFramework();
164+
assertTrue(framework.isLoadClassesEnabled());
165+
framework.setLoadClasses(false);
166+
assertFalse(framework.isLoadClassesEnabled());
167+
}
168+
169+
@Test
170+
public void testSetLoadJars() {
171+
AuthorizationFramework framework = new AuthorizationFramework();
172+
assertTrue(framework.isLoadJarsEnabled());
173+
framework.setLoadJars(false);
174+
assertFalse(framework.isLoadJarsEnabled());
175+
}
161176
}

test/org/opensolaris/opengrok/authorization/AuthorizationFrameworkTest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.stream.Collectors;
3131
import javax.servlet.http.HttpServletRequest;
3232
import org.junit.Assert;
33-
import org.junit.Before;
3433
import org.junit.Test;
3534
import org.junit.runner.RunWith;
3635
import org.junit.runners.Parameterized;
@@ -45,7 +44,6 @@ public class AuthorizationFrameworkTest {
4544

4645
private static final Random RANDOM = new Random();
4746

48-
private AuthorizationFramework framework;
4947
private final StackSetup setup;
5048

5149
public AuthorizationFrameworkTest(StackSetup setup) {
@@ -651,7 +649,7 @@ public static StackSetup[][] params() {
651649

652650
@Test
653651
public void testPluginsGeneric() {
654-
framework.setStack(setup.stack);
652+
AuthorizationFramework framework = new AuthorizationFramework(null, setup.stack);
655653
framework.loadAllPlugins(setup.stack);
656654

657655
boolean actual;
@@ -672,11 +670,6 @@ public void testPluginsGeneric() {
672670
}
673671
}
674672

675-
@Before
676-
public void setUp() {
677-
framework = new AuthorizationFramework(null);
678-
}
679-
680673
static private Project createAllowedProject() {
681674
Project p = new Project("allowed" + "_" + "project" + Math.random());
682675
return p;
@@ -782,7 +775,6 @@ public String toString() {
782775
}
783776

784777
};
785-
786778
}
787779

788780
static public class TestCase {

test/org/opensolaris/opengrok/index/IndexerRepoTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.index;
2424

2525
import static org.junit.Assert.assertEquals;
26-
import static org.junit.Assert.assertTrue;
2726

2827
import java.io.IOException;
2928

3029
import org.junit.After;
31-
import org.junit.AfterClass;
3230
import org.junit.Before;
33-
import org.junit.BeforeClass;
3431
import org.junit.Test;
3532
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
3633
import org.opensolaris.opengrok.history.HistoryGuru;

test/org/opensolaris/opengrok/web/PageConfigTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* CDDL HEADER END
1818
*/
1919

20-
/*
20+
/*
2121
* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.web;
@@ -187,7 +187,8 @@ public void testGetResourceFileList() {
187187
* - disabling "mercurial"
188188
* </pre>
189189
*/
190-
env.setAuthorizationFramework(new AuthorizationFramework(null));
190+
env.setAuthorizationFramework(new AuthorizationFramework());
191+
env.getAuthorizationFramework().reload();
191192
env.getAuthorizationFramework().getStack()
192193
.add(new AuthorizationPlugin(AuthControlFlag.REQUIRED, new TestPlugin() {
193194
@Override

test/org/opensolaris/opengrok/web/ProjectHelperTestBase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ public void setUp() {
266266
Assert.assertNotNull("Repository map should not be null", env.getProjectRepositoriesMap());
267267
Assert.assertEquals("Repository map should contain 20 project", 20, env.getProjectRepositoriesMap().size());
268268

269-
env.setAuthorizationFramework(new AuthorizationFramework(null));
269+
env.setAuthorizationFramework(new AuthorizationFramework());
270+
env.getAuthorizationFramework().reload();
270271

271272
IAuthorizationPlugin plugin = new TestPlugin() {
272273
@Override

0 commit comments

Comments
 (0)