public final Session loginService(final String subServiceName, final String workspace) throws LoginException, RepositoryException { Session s = createServiceSession(usingBundle, subServiceName, workspace); if (s != null) { return s;
/** * Creates a new service-session. This method is used by {@link #loginService(String, String)} * and {@link #impersonateFromService(String, Credentials, String)} to avoid * code duplication. * * @param usingBundle The bundle using a service session. * @param subServiceName The optional name of the subservice. * @param workspaceName The JCR workspace name or {@code null}. * @return A new service session or {@code null} if neither a user-name nor a * principal names mapping has been defined. * @throws RepositoryException If an error occurs while creating the service session. */ private Session createServiceSession(Bundle usingBundle, String subServiceName, String workspaceName) throws RepositoryException { final ServiceUserMapper serviceUserMapper = this.getSlingRepositoryManager().getServiceUserMapper(); if (serviceUserMapper != null) { final Iterable<String> principalNames = serviceUserMapper.getServicePrincipalNames(usingBundle, subServiceName); if (principalNames != null) { return createServiceSession(principalNames, workspaceName); } final String userName = serviceUserMapper.getServiceUserID(usingBundle, subServiceName); if (userName != null) { return createServiceSession(userName, workspaceName); } } return null; }
Session serviceSession = null; try { serviceSession = createServiceSession(usingBundle, subServiceName, workspaceName); if (serviceSession == null) { throw new LoginException("Cannot create service session for bundle " + usingBundle + " and sub service " + subServiceName);