|
| 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