001/**
002 * Copyright 2015 DuraSpace, Inc.
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 */
016package org.fcrepo.auth.roles.common;
017
018import java.io.IOException;
019import java.net.URL;
020
021import javax.annotation.PostConstruct;
022import javax.jcr.RepositoryException;
023import javax.jcr.Session;
024import javax.jcr.nodetype.NodeType;
025import javax.jcr.nodetype.NodeTypeIterator;
026
027import org.fcrepo.http.commons.session.SessionFactory;
028import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
029import org.modeshape.jcr.api.nodetype.NodeTypeManager;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032import org.springframework.beans.factory.annotation.Autowired;
033import org.springframework.stereotype.Component;
034
035/**
036 * @author Gregory Jansen
037 *
038 */
039@Component
040public class AccessRolesTypes {
041
042    private static final Logger LOGGER = LoggerFactory
043            .getLogger(AccessRolesTypes.class);
044
045    @Autowired
046    private final SessionFactory sessionFactory = null;
047
048    /**
049     * Initialize, register role assignment node types.
050     *
051     * @throws IOException if io exception occurred
052     */
053    @PostConstruct
054    public void setUpRepositoryConfiguration() throws IOException {
055        registerNodeTypes(sessionFactory);
056    }
057
058    private void registerNodeTypes(final SessionFactory sessions) throws IOException {
059        Session session = null;
060        try {
061            session = sessions.getInternalSession();
062            final NodeTypeManager mgr =
063                    (NodeTypeManager) session.getWorkspace()
064                            .getNodeTypeManager();
065            final URL cnd =
066                    AccessRoles.class
067                            .getResource("/cnd/access-control.cnd");
068            final NodeTypeIterator nti =
069                    mgr.registerNodeTypes(cnd, true);
070            while (nti.hasNext()) {
071                final NodeType nt = nti.nextNodeType();
072                LOGGER.debug("registered node type: {}", nt.getName());
073            }
074            session.save();
075            LOGGER.debug("Registered access role node types");
076
077        } catch (final RepositoryException e) {
078            throw new RepositoryRuntimeException(e);
079        } finally {
080            if (session != null) {
081                session.logout();
082            }
083        }
084    }
085}