@Override public Session getSession(String logicalWorkspaceName, Credentials credentials) throws RepositoryException { WorkspaceMappingDefinition mapping = this.workspaceMapping.getWorkspaceMapping(logicalWorkspaceName); if (mapping == null) throw new NoSuchWorkspaceException(logicalWorkspaceName); Repository repository = getRepository(mapping.getRepositoryName()); String physicalWorkspaceName = mapping.getPhysicalWorkspaceName(); Session session = repository.login(credentials, physicalWorkspaceName); return wrapSession(session, logicalWorkspaceName); }
@Override public void loadWorkspace(String repositoryId, String physicalWorkspaceName) throws RepositoryException { String logicalWorkspaceName; // fallback to old pattern if repo id is empty if (StringUtils.isNotBlank(repositoryId) && (RepositoryConstants.SYSTEM.equals(physicalWorkspaceName) || RepositoryConstants.VERSION_STORE.equals(physicalWorkspaceName))) { logicalWorkspaceName = repositoryId + "-" + physicalWorkspaceName; } else { logicalWorkspaceName = physicalWorkspaceName; } log.info("Loading workspace {} (logical name {}).", physicalWorkspaceName, logicalWorkspaceName); workspaceMapping.addWorkspaceMapping(new WorkspaceMappingDefinition(logicalWorkspaceName, repositoryId, physicalWorkspaceName)); Provider provider = getRepositoryProvider(repositoryId); provider.registerWorkspace(physicalWorkspaceName); RepositoryDefinition repositoryDefinition = workspaceMapping.getRepositoryDefinition(repositoryId); registerNameSpacesAndNodeTypes(physicalWorkspaceName, repositoryDefinition, provider); }
/** * Check if repositories has any content, exclude mgnlVersion repository. */ private boolean checkIfInitialized() throws RepositoryException { boolean isClusteredWorkspace = false; boolean isMasterCluster = false; Collection<String> workspaceNames = repositoryManager.getWorkspaceNames(); for (String workspace : workspaceNames) { if (repositoryManager instanceof DefaultRepositoryManager) { isClusteredWorkspace = ((DefaultRepositoryManager) repositoryManager).isClusteredWorkspace(workspace); isMasterCluster = ((DefaultRepositoryManager) repositoryManager).isClusterMaster(); } if (isClusteredWorkspace && !isMasterCluster) { log.info("Skipped clustered workspace '{}' initializing check because it is not cluster master", workspace); continue; } if (!workspace.endsWith(RepositoryConstants.VERSION_STORE) && repositoryManager.checkIfInitialized(workspace)) { return true; } } return false; } }
@Override public void createWorkspace(String repository, String logicalWorkspaceName) throws RepositoryException { for (WorkspaceMappingDefinition mapping : workspaceMapping.getWorkspaceMappings()) { Session session = getSystemSession(mapping.getLogicalWorkspaceName()); try { if (mapping.getRepositoryName().equals(repository)) { Workspace workspace = session.getWorkspace(); workspace.createWorkspace(logicalWorkspaceName); workspaceMapping.addWorkspaceMapping(new WorkspaceMappingDefinition(logicalWorkspaceName, repository, logicalWorkspaceName)); return; } } finally { session.logout(); } } throw new RepositoryException("Repository [" + repository + "] doesn't exist."); }
@Override public Session getSystemSession(String logicalWorkspaceName) throws RepositoryException { WorkspaceMappingDefinition mapping = this.workspaceMapping.getWorkspaceMapping(logicalWorkspaceName); if (mapping == null) { throw new NoSuchWorkspaceException(logicalWorkspaceName); } Provider provider = getRepositoryProvider(mapping.getRepositoryName()); return wrapSession(provider.getSystemSession(mapping.getPhysicalWorkspaceName()), logicalWorkspaceName); }
@Test public void testSystemAndVersionAreCreatedForAllRepositories() throws Exception { // GIVEN // WHEN // on repo startup // THEN assertNotNull(defaultRepositoryManager.getRepository("magnolia")); assertNotNull(defaultRepositoryManager.getRepository("custom")); assertNotNull(defaultRepositoryManager.getWorkspaceMapping("magnolia-mgnlSystem")); assertNotNull(defaultRepositoryManager.getWorkspaceMapping("magnolia-mgnlVersion")); assertNotNull(defaultRepositoryManager.getWorkspaceMapping("custom-mgnlSystem")); assertNotNull(defaultRepositoryManager.getWorkspaceMapping("custom-mgnlVersion")); }
loadRepository(repositoryDefinition); try { for (String wksName : session.getWorkspace().getAccessibleWorkspaceNames()) { if (!hasWorkspace(wksName)) { loadWorkspace(repoName, wksName);
private boolean isClustered(String repositoryName) { return repositoryManager instanceof DefaultRepositoryManager && ((DefaultRepositoryManager) repositoryManager).isClusteredWorkspace(repositoryName); }
private boolean isMasterCluster() { return repositoryManager instanceof DefaultRepositoryManager && ((DefaultRepositoryManager) repositoryManager).isClusterMaster(); }
protected RepositoryManager initRepositoryManager(MagnoliaConfigurationProperties configurationProperties, FileSystemHelper fileSystemHelper) { return new DefaultRepositoryManager(configurationProperties, new WorkspaceMapping(), fileSystemHelper); }
@Test public void testUnknownRepositoryShouldAlsoYieldMeaningfulExceptionMessageForRepositoryProviders() { assertThat(() -> { defaultRepositoryManager.getRepositoryProvider("dummy"); }, throwsAnException(strictlyInstanceOf(IllegalArgumentException.class) .withMessage("Failed to retrieve repository provider 'dummy'. Your Magnolia instance might not have been initialized properly."))); } }
@Test(expected = RepositoryException.class) public void createWorkspaceInWrongRepo() throws Exception { // GIVEN try { MgnlContext.getJCRSession("some-random-workspace"); fail(); } catch (Exception e) { // expected } // WHEN defaultRepositoryManager.createWorkspace("someNonExistentRepo", "some-random-workspace"); }
@Override public boolean checkIfInitialized() throws RepositoryException { Collection<String> workspaceNames = workspaceMapping.getLogicalWorkspaceNames(); for (String workspace : workspaceNames) { if (checkIfInitialized(workspace)) { return true; } } return false; }
@Override public String getRepositoryNameForWorkspace(String logicalWorkspaceName) { if (hasWorkspace(logicalWorkspaceName)) { return workspaceMapping.getWorkspaceMapping(logicalWorkspaceName).getRepositoryName(); } throw new IllegalStateException("No repository known for logical workspace name " + logicalWorkspaceName); }
@Test public void verifyVersionStoreSessionDontWrapVersioningSession() throws Exception { // GIVEN // WHEN Session session = defaultRepositoryManager.getSystemSession("magnolia-" + RepositoryConstants.VERSION_STORE); // THEN assertThat(session, instanceOf(MagnoliaSessionWrapper.class)); // THEN session = ((DelegateSessionWrapper) session).deepUnwrap(MagnoliaSessionWrapper.class); assertThat(session, instanceOf(MgnlAuditLoggingContentDecoratorSessionWrapper.class)); // THEN session = ((DelegateSessionWrapper) session).deepUnwrap(MgnlAuditLoggingContentDecoratorSessionWrapper.class); assertThat(session, instanceOf(MgnlPropertySettingContentDecorator.MgnlPropertySettingSessionWrapper.class)); // THEN session = ((DelegateSessionWrapper) session).deepUnwrap(MgnlPropertySettingContentDecorator.MgnlPropertySettingSessionWrapper.class); assertThat(session, not(instanceOf(MgnlVersioningSession.class))); }
@Test public void verifyClusteredWorkspace() throws Exception { // GIVEN String clusteredWorkspace = "website"; // WHEN boolean isClusteredWorkspace = defaultRepositoryManager.isClusteredWorkspace(clusteredWorkspace); // THEN assertTrue(isClusteredWorkspace); }
@Test public void verifyClusterMasterWorkspace() throws Exception { // GIVEN // WHEN boolean isClusteredWorkspace = defaultRepositoryManager.isClusterMaster(); // THEN assertTrue(isClusteredWorkspace); }
@Override @Before public void setUp() throws Exception { super.setUp(); UserContext ctx = mock(UserContext.class); RepositoryManager repositoryManager = new DefaultRepositoryManager(); strategy = new DefaultRepositoryStrategy(repositoryManager, ctx); }
@Test public void createWorkspace() throws Exception { // GIVEN try { MgnlContext.getJCRSession("some-random-workspace"); fail(); } catch (Exception e) { // expected } // WHEN defaultRepositoryManager.createWorkspace("magnolia", "some-random-workspace"); // THEN // check we registered all right assertNotNull(MgnlContext.getJCRSession("some-random-workspace")); }
boolean isClusteredWorkspace = ((DefaultRepositoryManager) repoManager).isClusteredWorkspace(workspace); boolean isMasterCluster = ((DefaultRepositoryManager) repoManager).isClusterMaster();