001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     "Stephane Lacoin [aka matic] <slacoin at nuxeo.com>"
018 */
019package org.nuxeo.ecm.core.management.storage;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.DocumentRef;
027import org.nuxeo.ecm.core.api.PathRef;
028import org.nuxeo.ecm.core.api.repository.RepositoryManager;
029import org.nuxeo.ecm.core.api.security.ACE;
030import org.nuxeo.ecm.core.api.security.ACL;
031import org.nuxeo.ecm.core.api.security.ACP;
032import org.nuxeo.ecm.core.api.security.SecurityConstants;
033import org.nuxeo.ecm.core.management.CoreManagementComponent;
034import org.nuxeo.ecm.core.repository.RepositoryInitializationHandler;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * Initialize document store by invoking registered handlers
039 *
040 * @author "Stephane Lacoin [aka matic] <slacoin at nuxeo.com>"
041 */
042public class DocumentStoreManager extends RepositoryInitializationHandler {
043
044    public static final String MANAGEMENT_ROOT_TYPE = "ManagementRoot";
045
046    public static final String MANAGEMENT_ROOT_NAME = "management";
047
048    public static final String MANAGEMENT_ROOT_PATH = "/" + MANAGEMENT_ROOT_NAME;
049
050    public static PathRef newPath(String... components) {
051        StringBuilder sb = new StringBuilder();
052        sb.append(MANAGEMENT_ROOT_PATH);
053        for (String component : components) {
054            sb.append("/").append(component);
055        }
056        return new PathRef(sb.toString());
057    }
058
059    protected final Map<String, DocumentStoreHandlerDescriptor> handlers = new HashMap<String, DocumentStoreHandlerDescriptor>();
060
061    public void registerHandler(DocumentStoreHandlerDescriptor desc) {
062        if (desc.handler == null) {
063            throw new RuntimeException("Class wasn't resolved or new instance failed, check logs");
064        }
065        handlers.put(desc.id, desc);
066    }
067
068    protected DocumentStoreConfigurationDescriptor config = new DocumentStoreConfigurationDescriptor();
069
070    public void registerConfig(DocumentStoreConfigurationDescriptor config) {
071        this.config = config;
072        DocumentStoreSessionRunner.repositoryName = config.repositoryName;
073    }
074
075    protected String defaultRepositoryName;
076
077    protected boolean mgmtInitialized;
078
079    protected boolean defaultInitialized;
080
081    protected DocumentRef rootletRef;
082
083    @Override
084    public void doInitializeRepository(CoreSession session) {
085        if (defaultRepositoryName == null) {
086            RepositoryManager mgr = Framework.getService(RepositoryManager.class);
087            defaultRepositoryName = mgr.getDefaultRepositoryName();
088            if (DocumentStoreSessionRunner.repositoryName == null) {
089                DocumentStoreSessionRunner.repositoryName = defaultRepositoryName;
090            }
091        }
092        String repositoryName = session.getRepositoryName();
093
094        if (repositoryName.equals(DocumentStoreSessionRunner.repositoryName)) {
095            mgmtInitialized = true;
096            rootletRef = setupRootlet(session);
097            for (DocumentStoreHandlerDescriptor desc : handlers.values()) {
098                desc.handler.onStorageInitialization(session, rootletRef);
099            }
100        }
101
102        if (repositoryName.equals(defaultRepositoryName)) {
103            defaultInitialized = true;
104        }
105
106        if (defaultInitialized && mgmtInitialized) {
107            CoreManagementComponent.getDefault().onNuxeoServerStartup();
108        }
109    }
110
111    protected DocumentModel createRootlet(CoreSession session) {
112        DocumentModel rootlet = session.createDocumentModel("/", MANAGEMENT_ROOT_NAME, MANAGEMENT_ROOT_TYPE);
113        rootlet = session.createDocument(rootlet);
114
115        ACP acp = rootlet.getACP();
116        ACL acl = acp.getOrCreateACL();
117
118        for (ACE ace : acl.getACEs()) {
119            acl.remove(ace);
120        }
121
122        acl.add(new ACE(config.groupName, SecurityConstants.EVERYTHING, true));
123        acl.add(new ACE(SecurityConstants.EVERYONE, SecurityConstants.EVERYTHING, false));
124        rootlet.setACP(acp, true);
125
126        session.save();
127
128        return rootlet;
129    }
130
131    protected DocumentRef setupRootlet(CoreSession session) {
132        DocumentModel rootlet;
133        if (!session.exists(new PathRef(MANAGEMENT_ROOT_PATH))) {
134            rootlet = createRootlet(session);
135        } else {
136            rootlet = session.getDocument(new PathRef(MANAGEMENT_ROOT_PATH));
137        }
138        return rootlet.getRef();
139    }
140
141}