Skip to content

Commit 87d362b

Browse files
committed
#503 Improve the performance of applying groups and users
1 parent 7e6f450 commit 87d362b

File tree

2 files changed

+128
-3
lines changed

2 files changed

+128
-3
lines changed

accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/authorizableinstaller/impl/AuthorizableInstallerServiceImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,12 @@ public void installAuthorizables(
106106
final Session session, InstallationLogger installLog)
107107
throws RepositoryException, AuthorizableCreatorException, LoginException, IOException, GeneralSecurityException {
108108

109+
UserManager userManager = new PrefetchedAuthorizablesUserManager(AccessControlUtils.getUserManagerAutoSaveDisabled(session), installLog);
110+
109111
Set<String> authorizablesFromConfigurations = authorizablesConfigBeans.getAuthorizableIds();
110112
for (AuthorizableConfigBean authorizableConfigBean : authorizablesConfigBeans) {
111113

112-
installAuthorizableConfigurationBean(session, acConfiguration,
114+
installAuthorizableConfigurationBean(session, userManager, acConfiguration,
113115
authorizableConfigBean, installLog, authorizablesFromConfigurations);
114116
}
115117

@@ -118,6 +120,7 @@ public void installAuthorizables(
118120
}
119121

120122
private void installAuthorizableConfigurationBean(final Session session,
123+
UserManager userManager,
121124
AcConfiguration acConfiguration,
122125
AuthorizableConfigBean authorizableConfigBean,
123126
InstallationLogger installLog, Set<String> authorizablesFromConfigurations)
@@ -126,8 +129,6 @@ private void installAuthorizableConfigurationBean(final Session session,
126129
String authorizableId = authorizableConfigBean.getAuthorizableId();
127130
LOG.debug("- start installation of authorizable: {}", authorizableId);
128131

129-
UserManager userManager = AccessControlUtils.getUserManagerAutoSaveDisabled(session);
130-
131132
// if current authorizable from config doesn't exist yet
132133
Authorizable authorizableToInstall = userManager.getAuthorizable(authorizableId);
133134

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package biz.netcentric.cq.tools.actool.authorizableinstaller.impl;
2+
3+
import static biz.netcentric.cq.tools.actool.history.PersistableInstallationLogger.msHumanReadable;
4+
5+
import java.security.Principal;
6+
import java.util.HashMap;
7+
import java.util.Iterator;
8+
import java.util.Map;
9+
10+
import javax.jcr.RepositoryException;
11+
12+
import org.apache.jackrabbit.api.security.user.Authorizable;
13+
import org.apache.jackrabbit.api.security.user.Group;
14+
import org.apache.jackrabbit.api.security.user.Query;
15+
import org.apache.jackrabbit.api.security.user.QueryBuilder;
16+
import org.apache.jackrabbit.api.security.user.User;
17+
import org.apache.jackrabbit.api.security.user.UserManager;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
import biz.netcentric.cq.tools.actool.history.InstallationLogger;
22+
23+
/** Prefetches all authorizables in constructor to be able to answer getAuthorizable(userId) requests much quicker than the ootb oak
24+
* UserManager itself. */
25+
class PrefetchedAuthorizablesUserManager implements UserManager {
26+
27+
private static final Logger LOG = LoggerFactory.getLogger(PrefetchedAuthorizablesUserManager.class);
28+
29+
private final UserManager delegate;
30+
private final Map<String, Authorizable> authorizableCache;
31+
32+
public PrefetchedAuthorizablesUserManager(UserManager delegate, InstallationLogger installLog) throws RepositoryException {
33+
this.delegate = delegate;
34+
this.authorizableCache = new HashMap<>();
35+
36+
long startPrefetch = System.currentTimeMillis();
37+
Iterator<Authorizable> allAuthorizables = delegate.findAuthorizables(new Query() {
38+
public <T> void build(QueryBuilder<T> builder) {
39+
// fetch all authorizables
40+
}
41+
});
42+
43+
while (allAuthorizables.hasNext()) {
44+
Authorizable auth = allAuthorizables.next();
45+
authorizableCache.put(auth.getID(), auth);
46+
}
47+
48+
installLog.addMessage(LOG, "Prefetched " + authorizableCache.size() + " authorizables in "
49+
+ msHumanReadable(System.currentTimeMillis() - startPrefetch));
50+
}
51+
52+
@Override
53+
public Authorizable getAuthorizable(String id) throws RepositoryException {
54+
Authorizable authorizable = authorizableCache.get(id);
55+
if (authorizable == null) {
56+
authorizable = delegate.getAuthorizable(id);
57+
authorizableCache.put(id, authorizable);
58+
}
59+
return authorizable;
60+
}
61+
62+
public int getCacheSize() {
63+
return authorizableCache.size();
64+
}
65+
66+
// --- delegated methods
67+
68+
public Authorizable getAuthorizable(Principal principal) throws RepositoryException {
69+
return delegate.getAuthorizable(principal);
70+
}
71+
72+
public Authorizable getAuthorizableByPath(String path) throws RepositoryException {
73+
return delegate.getAuthorizableByPath(path);
74+
}
75+
76+
public Iterator<Authorizable> findAuthorizables(String relPath, String value) throws RepositoryException {
77+
return delegate.findAuthorizables(relPath, value);
78+
}
79+
80+
public Iterator<Authorizable> findAuthorizables(String relPath, String value, int searchType) throws RepositoryException {
81+
return delegate.findAuthorizables(relPath, value, searchType);
82+
}
83+
84+
public Iterator<Authorizable> findAuthorizables(Query query) throws RepositoryException {
85+
return delegate.findAuthorizables(query);
86+
}
87+
88+
public User createUser(String userID, String password) throws RepositoryException {
89+
return delegate.createUser(userID, password);
90+
}
91+
92+
public User createUser(String userID, String password, Principal principal, String intermediatePath) throws RepositoryException {
93+
return delegate.createUser(userID, password, principal, intermediatePath);
94+
}
95+
96+
public User createSystemUser(String userID, String intermediatePath) throws RepositoryException {
97+
return delegate.createSystemUser(userID, intermediatePath);
98+
}
99+
100+
public Group createGroup(String groupID) throws RepositoryException {
101+
return delegate.createGroup(groupID);
102+
}
103+
104+
public Group createGroup(Principal principal) throws RepositoryException {
105+
return delegate.createGroup(principal);
106+
}
107+
108+
public Group createGroup(Principal principal, String intermediatePath) throws RepositoryException {
109+
return delegate.createGroup(principal, intermediatePath);
110+
}
111+
112+
public Group createGroup(String groupID, Principal principal, String intermediatePath) throws RepositoryException {
113+
return delegate.createGroup(groupID, principal, intermediatePath);
114+
}
115+
116+
public boolean isAutoSave() {
117+
return delegate.isAutoSave();
118+
}
119+
120+
public void autoSave(boolean enable) throws RepositoryException {
121+
delegate.autoSave(enable);
122+
}
123+
124+
}

0 commit comments

Comments
 (0)