|
77 | 77 | import java.util.Map; |
78 | 78 | import java.util.Set; |
79 | 79 | import java.util.TreeSet; |
| 80 | +import java.util.function.Supplier; |
80 | 81 |
|
81 | 82 | import static java.util.Objects.requireNonNull; |
82 | 83 | import static java.util.concurrent.TimeUnit.NANOSECONDS; |
@@ -169,7 +170,8 @@ class UserImporter implements ProtectedPropertyImporter, ProtectedNodeImporter, |
169 | 170 | */ |
170 | 171 | private final Map<String, Principal> principals = new HashMap<>(); |
171 | 172 |
|
172 | | - private UserMonitor userMonitor = UserMonitor.NOOP; |
| 173 | + private UserMonitor userMonitor; // do not access directly, but only via the userMonitorSupplier |
| 174 | + private Supplier<UserMonitor> userMonitorSupplier; |
173 | 175 |
|
174 | 176 | UserImporter(ConfigurationParameters config) { |
175 | 177 | importBehavior = UserUtil.getImportBehavior(config); |
@@ -207,18 +209,35 @@ public boolean init(@NotNull Session session, @NotNull Root root, @NotNull NameP |
207 | 209 | return false; |
208 | 210 | } |
209 | 211 |
|
210 | | - if (securityProvider instanceof WhiteboardAware) { |
211 | | - Whiteboard whiteboard = ((WhiteboardAware) securityProvider).getWhiteboard(); |
212 | | - UserMonitor monitor = WhiteboardUtils.getService(whiteboard, UserMonitor.class); |
213 | | - if (monitor != null) { |
214 | | - userMonitor = monitor; |
215 | | - } |
216 | | - } |
| 212 | + /* |
| 213 | + * resolve the userMonitor lazily, as resolving that service via the OSGi service registry |
| 214 | + * can be costly; this is often a redundant operation, because a UserImporter is typically much more |
| 215 | + * frequent initialized than actually used. |
| 216 | + */ |
| 217 | + userMonitorSupplier = getUserMonitorSupplier(securityProvider); |
217 | 218 |
|
218 | 219 | initialized = true; |
219 | 220 | return initialized; |
220 | 221 | } |
221 | 222 |
|
| 223 | + private Supplier<UserMonitor> getUserMonitorSupplier(SecurityProvider securityProvider) { |
| 224 | + return () -> { |
| 225 | + if (userMonitor == null) { |
| 226 | + if (securityProvider instanceof WhiteboardAware) { |
| 227 | + Whiteboard whiteboard = ((WhiteboardAware) securityProvider).getWhiteboard(); |
| 228 | + UserMonitor monitor = WhiteboardUtils.getService(whiteboard, UserMonitor.class); |
| 229 | + if (monitor != null) { |
| 230 | + userMonitor = monitor; |
| 231 | + } |
| 232 | + } |
| 233 | + if (userMonitor == null) { // always fall back to the NOOP version |
| 234 | + userMonitor = UserMonitor.NOOP; |
| 235 | + } |
| 236 | + } |
| 237 | + return userMonitor; |
| 238 | + }; |
| 239 | + } |
| 240 | + |
222 | 241 | private static boolean canInitUserManager(@NotNull JackrabbitSession session, boolean isWorkspaceImport) { |
223 | 242 | try { |
224 | 243 | if (!isWorkspaceImport && session.getUserManager().isAutoSave()) { |
@@ -647,7 +666,7 @@ void process() throws RepositoryException { |
647 | 666 | memberContentIds.removeAll(failedContentIds); |
648 | 667 |
|
649 | 668 | userManager.onGroupUpdate(gr, false, true, memberContentIds, failedContentIds); |
650 | | - userMonitor.doneUpdateMembers(watch.elapsed(NANOSECONDS), totalSize, failedContentIds.size(), false); |
| 669 | + userMonitorSupplier.get().doneUpdateMembers(watch.elapsed(NANOSECONDS), totalSize, failedContentIds.size(), false); |
651 | 670 | } |
652 | 671 | } |
653 | 672 |
|
|
0 commit comments