001/*
002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.usermapper.service;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.Set;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.core.api.NuxeoPrincipal;
032import org.nuxeo.runtime.model.ComponentContext;
033import org.nuxeo.runtime.model.ComponentInstance;
034import org.nuxeo.runtime.model.DefaultComponent;
035import org.nuxeo.usermapper.extension.UserMapper;
036
037/**
038 * Component to manage extension point and expose the {@link UserMapperService} interface.
039 *
040 * @author tiry
041 * @since 7.4
042 */
043public class UserMapperComponent extends DefaultComponent implements UserMapperService {
044
045    protected static final Log log = LogFactory.getLog(UserMapperComponent.class);
046
047    protected Map<String, UserMapper> mappers = new HashMap<>();
048
049    protected List<UserMapperDescriptor> descriptors = new ArrayList<>();
050
051    public static final String MAPPER_EP = "mapper";
052
053    @Override
054    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
055        if (MAPPER_EP.equalsIgnoreCase(extensionPoint)) {
056            UserMapperDescriptor desc = (UserMapperDescriptor) contribution;
057            descriptors.add(desc);
058        }
059    }
060
061    @Override
062    public void applicationStarted(ComponentContext context) {
063        for (UserMapperDescriptor desc : descriptors) {
064            try {
065                mappers.put(desc.name, desc.getInstance());
066            } catch (Exception e) {
067                log.error("Unable to register mapper " + desc.name, e);
068            }
069        }
070    }
071
072    @Override
073    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
074        if (MAPPER_EP.equalsIgnoreCase(extensionPoint)) {
075            UserMapperDescriptor desc = (UserMapperDescriptor) contribution;
076            UserMapper um = mappers.get(desc.name);
077            if (um != null) {
078                um.release();
079                mappers.remove(desc.name);
080            }
081        }
082    }
083
084    @Override
085    public void deactivate(ComponentContext context) {
086        for (UserMapper um : mappers.values()) {
087            um.release();
088        }
089        super.deactivate(context);
090    }
091
092    @Override
093    public UserMapper getMapper(String mappingName) throws NuxeoException {
094        UserMapper um = mappers.get(mappingName);
095        if (um == null) {
096            throw new NuxeoException("No mapping defined for " + mappingName);
097        }
098        return um;
099    }
100
101    @Override
102    public NuxeoPrincipal getOrCreateAndUpdateNuxeoPrincipal(String mappingName, Object userObject)
103            throws NuxeoException {
104        return getOrCreateAndUpdateNuxeoPrincipal(mappingName, userObject, true, true, null);
105    }
106
107    @Override
108    public NuxeoPrincipal getOrCreateAndUpdateNuxeoPrincipal(String mappingName, Object userObject,
109            boolean createIfNeeded, boolean update, Map<String, Serializable> params) throws NuxeoException {
110        return getMapper(mappingName).getOrCreateAndUpdateNuxeoPrincipal(userObject, createIfNeeded, update, params);
111    }
112
113    @Override
114    public Object wrapNuxeoPrincipal(String mappingName, NuxeoPrincipal principal, Object nativePrincipal,
115            Map<String, Serializable> params) throws NuxeoException {
116        return getMapper(mappingName).wrapNuxeoPrincipal(principal, nativePrincipal, params);
117    }
118
119    @Override
120    public Set<String> getAvailableMappings() {
121        return mappers.keySet();
122    }
123
124}