001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     "Stephane Lacoin [aka matic] <slacoin at nuxeo.com>"
011 */
012package org.nuxeo.ecm.core.management.storage;
013
014import java.util.HashMap;
015import java.util.Map;
016
017import org.nuxeo.ecm.core.api.CoreSession;
018import org.nuxeo.ecm.core.api.DocumentModel;
019import org.nuxeo.ecm.core.api.DocumentRef;
020import org.nuxeo.ecm.core.api.PathRef;
021import org.nuxeo.ecm.core.api.repository.RepositoryManager;
022import org.nuxeo.ecm.core.api.security.ACE;
023import org.nuxeo.ecm.core.api.security.ACL;
024import org.nuxeo.ecm.core.api.security.ACP;
025import org.nuxeo.ecm.core.api.security.SecurityConstants;
026import org.nuxeo.ecm.core.management.CoreManagementComponent;
027import org.nuxeo.ecm.core.repository.RepositoryInitializationHandler;
028import org.nuxeo.runtime.api.Framework;
029
030/**
031 * Initialize document store by invoking registered handlers
032 *
033 * @author "Stephane Lacoin [aka matic] <slacoin at nuxeo.com>"
034 */
035public class DocumentStoreManager extends RepositoryInitializationHandler {
036
037    public static final String MANAGEMENT_ROOT_TYPE = "ManagementRoot";
038
039    public static final String MANAGEMENT_ROOT_NAME = "management";
040
041    public static final String MANAGEMENT_ROOT_PATH = "/" + MANAGEMENT_ROOT_NAME;
042
043    public static PathRef newPath(String... components) {
044        StringBuilder sb = new StringBuilder();
045        sb.append(MANAGEMENT_ROOT_PATH);
046        for (String component : components) {
047            sb.append("/").append(component);
048        }
049        return new PathRef(sb.toString());
050    }
051
052    protected final Map<String, DocumentStoreHandlerDescriptor> handlers = new HashMap<String, DocumentStoreHandlerDescriptor>();
053
054    public void registerHandler(DocumentStoreHandlerDescriptor desc) {
055        if (desc.handler == null) {
056            throw new RuntimeException("Class wasn't resolved or new instance failed, check logs");
057        }
058        handlers.put(desc.id, desc);
059    }
060
061    protected DocumentStoreConfigurationDescriptor config = new DocumentStoreConfigurationDescriptor();
062
063    public void registerConfig(DocumentStoreConfigurationDescriptor config) {
064        this.config = config;
065        DocumentStoreSessionRunner.repositoryName = config.repositoryName;
066    }
067
068    protected String defaultRepositoryName;
069
070    protected boolean mgmtInitialized;
071
072    protected boolean defaultInitialized;
073
074    protected DocumentRef rootletRef;
075
076    @Override
077    public void doInitializeRepository(CoreSession session) {
078        if (defaultRepositoryName == null) {
079            RepositoryManager mgr = Framework.getLocalService(RepositoryManager.class);
080            defaultRepositoryName = mgr.getDefaultRepositoryName();
081            if (DocumentStoreSessionRunner.repositoryName == null) {
082                DocumentStoreSessionRunner.repositoryName = defaultRepositoryName;
083            }
084        }
085        String repositoryName = session.getRepositoryName();
086
087        if (repositoryName.equals(DocumentStoreSessionRunner.repositoryName)) {
088            mgmtInitialized = true;
089            rootletRef = setupRootlet(session);
090            for (DocumentStoreHandlerDescriptor desc : handlers.values()) {
091                desc.handler.onStorageInitialization(session, rootletRef);
092            }
093        }
094
095        if (repositoryName.equals(defaultRepositoryName)) {
096            defaultInitialized = true;
097        }
098
099        if (defaultInitialized && mgmtInitialized) {
100            CoreManagementComponent.getDefault().onNuxeoServerStartup();
101        }
102    }
103
104    protected DocumentModel createRootlet(CoreSession session) {
105        DocumentModel rootlet = session.createDocumentModel("/", MANAGEMENT_ROOT_NAME, MANAGEMENT_ROOT_TYPE);
106        rootlet = session.createDocument(rootlet);
107
108        ACP acp = rootlet.getACP();
109        ACL acl = acp.getOrCreateACL();
110
111        for (ACE ace : acl.getACEs()) {
112            acl.remove(ace);
113        }
114
115        acl.add(new ACE(config.groupName, SecurityConstants.EVERYTHING, true));
116        acl.add(new ACE(SecurityConstants.EVERYONE, SecurityConstants.EVERYTHING, false));
117        rootlet.setACP(acp, true);
118
119        session.save();
120
121        return rootlet;
122    }
123
124    protected DocumentRef setupRootlet(CoreSession session) {
125        DocumentModel rootlet;
126        if (!session.exists(new PathRef(MANAGEMENT_ROOT_PATH))) {
127            rootlet = createRootlet(session);
128        } else {
129            rootlet = session.getDocument(new PathRef(MANAGEMENT_ROOT_PATH));
130        }
131        return rootlet.getRef();
132    }
133
134}