Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
Expand Down Expand Up @@ -169,7 +170,8 @@ class UserImporter implements ProtectedPropertyImporter, ProtectedNodeImporter,
*/
private final Map<String, Principal> principals = new HashMap<>();

private UserMonitor userMonitor = UserMonitor.NOOP;
private UserMonitor userMonitor; // do not access directly, but only via the userMonitorSupplier
private Supplier<UserMonitor> userMonitorSupplier;

UserImporter(ConfigurationParameters config) {
importBehavior = UserUtil.getImportBehavior(config);
Expand Down Expand Up @@ -207,18 +209,35 @@ public boolean init(@NotNull Session session, @NotNull Root root, @NotNull NameP
return false;
}

if (securityProvider instanceof WhiteboardAware) {
Whiteboard whiteboard = ((WhiteboardAware) securityProvider).getWhiteboard();
UserMonitor monitor = WhiteboardUtils.getService(whiteboard, UserMonitor.class);
if (monitor != null) {
userMonitor = monitor;
}
}
/*
* resolve the userMonitor lazily, as resolving that service via the OSGi service registry
* can be costly; this is often a redundant operation, because a UserImporter is typically much more
* frequent initialized than actually used.
*/
userMonitorSupplier = getUserMonitorSupplier(securityProvider);

initialized = true;
return initialized;
}

private Supplier<UserMonitor> getUserMonitorSupplier(SecurityProvider securityProvider) {
return () -> {
if (userMonitor == null) {
if (securityProvider instanceof WhiteboardAware) {
Whiteboard whiteboard = ((WhiteboardAware) securityProvider).getWhiteboard();
UserMonitor monitor = WhiteboardUtils.getService(whiteboard, UserMonitor.class);
if (monitor != null) {
userMonitor = monitor;
}
}
if (userMonitor == null) { // always fall back to the NOOP version
userMonitor = UserMonitor.NOOP;
}
}
return userMonitor;
};
}

private static boolean canInitUserManager(@NotNull JackrabbitSession session, boolean isWorkspaceImport) {
try {
if (!isWorkspaceImport && session.getUserManager().isAutoSave()) {
Expand Down Expand Up @@ -647,7 +666,7 @@ void process() throws RepositoryException {
memberContentIds.removeAll(failedContentIds);

userManager.onGroupUpdate(gr, false, true, memberContentIds, failedContentIds);
userMonitor.doneUpdateMembers(watch.elapsed(NANOSECONDS), totalSize, failedContentIds.size(), false);
userMonitorSupplier.get().doneUpdateMembers(watch.elapsed(NANOSECONDS), totalSize, failedContentIds.size(), false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import java.lang.reflect.Field;

import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.atLeastOnce;
Expand Down Expand Up @@ -84,11 +84,10 @@ private static void replaceUserMonitor(@NotNull UserConfigurationImpl uc, @NotNu
boolean init(boolean createAction, Class<?>... extraInterfaces) throws Exception {
boolean b = super.init(createAction, extraInterfaces);

// verify that the usermonitor has been properly initialized and replace it with the spied instance
// verify that the userMonitor is not initialized on init, but only on demand
Field f = UserImporter.class.getDeclaredField("userMonitor");
f.setAccessible(true);
assertSame(userMonitor, f.get(importer));

assertNull(f.get(importer));
return b;
}
}