Search in sources :

Example 1 with SlingRepository

use of org.apache.sling.jcr.api.SlingRepository in project sling by apache.

the class OakVirtualInstanceBuilder method createNewRepository.

@Override
public VirtualInstanceBuilder createNewRepository() throws Exception {
    nodeStore = new MemoryNodeStore();
    SlingRepository repository = RepositoryTestHelper.newOakRepository(nodeStore);
    factory = MockFactory.mockResourceResolverFactory(repository);
    leaseCollection = new SimulatedLeaseCollection();
    return this;
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore)

Example 2 with SlingRepository

use of org.apache.sling.jcr.api.SlingRepository in project sling by apache.

the class DefaultLoginsHealthCheckTest method getTestResult.

private Result getTestResult(String login) throws Exception {
    final DefaultLoginsHealthCheck c = new DefaultLoginsHealthCheck();
    SetField.set(c, "logins", Arrays.asList(new String[] { login }));
    final SlingRepository repo = Mockito.mock(SlingRepository.class);
    SetField.set(c, "repository", repo);
    final Session s = Mockito.mock(Session.class);
    Mockito.when(repo.login(Matchers.any(Credentials.class))).thenAnswer(new Answer<Session>() {

        @Override
        public Session answer(InvocationOnMock invocation) {
            final SimpleCredentials c = (SimpleCredentials) invocation.getArguments()[0];
            if ("admin".equals(c.getUserID())) {
                return s;
            }
            return null;
        }
    });
    return c.execute();
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) SimpleCredentials(javax.jcr.SimpleCredentials) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultLoginsHealthCheck(org.apache.sling.hc.support.impl.DefaultLoginsHealthCheck) SimpleCredentials(javax.jcr.SimpleCredentials) Credentials(javax.jcr.Credentials) Session(javax.jcr.Session)

Example 3 with SlingRepository

use of org.apache.sling.jcr.api.SlingRepository in project sling by apache.

the class AbstractSlingRepositoryManager method initializeAndRegisterRepositoryService.

private void initializeAndRegisterRepositoryService() {
    Throwable t = null;
    try {
        log.debug("start: calling acquireRepository()");
        Repository newRepo = this.acquireRepository();
        if (newRepo != null) {
            // ensure we really have the repository
            log.debug("start: got a Repository");
            this.repository = newRepo;
            synchronized (this.repoInitLock) {
                this.masterSlingRepository = this.create(this.bundleContext.getBundle());
                log.debug("start: setting up Loader");
                this.loader = new Loader(this.masterSlingRepository, this.bundleContext);
                log.debug("start: calling SlingRepositoryInitializer");
                try {
                    executeRepositoryInitializers(this.masterSlingRepository);
                } catch (Throwable e) {
                    t = e;
                    log.error("Exception in a SlingRepositoryInitializer, SlingRepository service registration aborted", t);
                }
                log.debug("start: calling registerService()");
                this.repositoryService = registerService();
                log.debug("start: registerService() successful, registration=" + repositoryService);
            }
        }
    } catch (Throwable e) {
        // consider an uncaught problem an error
        log.error("start: Uncaught Throwable trying to access Repository, calling stopRepository()", e);
        t = e;
    } finally {
        if (t != null) {
            // repository might be partially started, stop anything left
            stop();
        }
    }
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) Repository(javax.jcr.Repository) Loader(org.apache.sling.jcr.base.internal.loader.Loader)

Example 4 with SlingRepository

use of org.apache.sling.jcr.api.SlingRepository in project sling by apache.

the class MockResolverProvider method getResourceResolver.

public static ResourceResolver getResourceResolver() throws RepositoryException, LoginException {
    final SlingRepository repository = RepositoryProvider.instance().getRepository();
    final BundleContext bundleContext = MockOsgi.newBundleContext();
    bundleContext.registerService(PathMapper.class.getName(), new PathMapper(), null);
    return new MockJcrResourceResolverFactory(repository, bundleContext).getAdministrativeResourceResolver(null);
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) PathMapper(org.apache.sling.jcr.resource.internal.helper.jcr.PathMapper) BundleContext(org.osgi.framework.BundleContext)

Example 5 with SlingRepository

use of org.apache.sling.jcr.api.SlingRepository in project sling by apache.

the class JcrProviderStateFactory method createProviderState.

@SuppressWarnings("deprecation")
JcrProviderState createProviderState(@Nonnull final Map<String, Object> authenticationInfo) throws LoginException {
    boolean isLoginAdministrative = Boolean.TRUE.equals(authenticationInfo.get(ResourceProvider.AUTH_ADMIN));
    // check whether a session is provided in the authenticationInfo
    Session session = getSession(authenticationInfo);
    if (session != null && !isLoginAdministrative) {
        // was provided in the authenticationInfo
        return createJcrProviderState(session, false, authenticationInfo, null);
    }
    BundleContext bc = null;
    try {
        final Bundle bundle = extractCallingBundle(authenticationInfo);
        if (bundle != null) {
            bc = bundle.getBundleContext();
            final SlingRepository repo = bc.getService(repositoryReference);
            if (repo == null) {
                logger.warn("Cannot login {} because cannot get SlingRepository on behalf of bundle {} ({})", isLoginAdministrative ? "admin" : "service", bundle.getSymbolicName(), bundle.getBundleId());
                // TODO: correct ??
                throw new LoginException("Repository unavailable");
            }
            try {
                if (isLoginAdministrative) {
                    session = repo.loginAdministrative(null);
                } else {
                    final Object subService = authenticationInfo.get(ResourceResolverFactory.SUBSERVICE);
                    final String subServiceName = subService instanceof String ? (String) subService : null;
                    session = repo.loginService(subServiceName, null);
                }
            } catch (Throwable t) {
                // closed and the session logged out
                if (session == null) {
                    bc.ungetService(repositoryReference);
                }
                throw t;
            }
        } else if (isLoginAdministrative) {
            throw new LoginException("Calling bundle missing in authentication info");
        } else {
            // requested non-admin session
            final Credentials credentials = getCredentials(authenticationInfo);
            session = repository.login(credentials, null);
        }
    } catch (final RepositoryException re) {
        throw getLoginException(re);
    }
    return createJcrProviderState(session, true, authenticationInfo, bc);
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) Bundle(org.osgi.framework.Bundle) LoginException(org.apache.sling.api.resource.LoginException) RepositoryException(javax.jcr.RepositoryException) SimpleCredentials(javax.jcr.SimpleCredentials) Credentials(javax.jcr.Credentials) Session(javax.jcr.Session) BundleContext(org.osgi.framework.BundleContext)

Aggregations

SlingRepository (org.apache.sling.jcr.api.SlingRepository)33 Test (org.junit.Test)18 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)14 Session (javax.jcr.Session)12 Event (javax.jcr.observation.Event)10 Scheduler (org.apache.sling.commons.scheduler.Scheduler)9 DistributionRequest (org.apache.sling.distribution.DistributionRequest)9 Credentials (javax.jcr.Credentials)4 Node (javax.jcr.Node)4 DefaultDistributionLog (org.apache.sling.distribution.log.impl.DefaultDistributionLog)4 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)4 DistributionPackageImporter (org.apache.sling.distribution.packaging.DistributionPackageImporter)4 Before (org.junit.Before)4 SimpleCredentials (javax.jcr.SimpleCredentials)3 Workspace (javax.jcr.Workspace)3 ObservationManager (javax.jcr.observation.ObservationManager)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 Repository (javax.jcr.Repository)2 RepositoryException (javax.jcr.RepositoryException)2 AccessControlManager (javax.jcr.security.AccessControlManager)2