001/*
002 * (C) Copyright 2006-2015 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 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.usermapper.service;
022
023import java.io.Serializable;
024import java.util.ArrayList;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028import java.util.Set;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.nuxeo.ecm.core.api.NuxeoException;
033import org.nuxeo.ecm.core.api.NuxeoPrincipal;
034import org.nuxeo.runtime.model.ComponentContext;
035import org.nuxeo.runtime.model.ComponentInstance;
036import org.nuxeo.runtime.model.DefaultComponent;
037import org.nuxeo.usermapper.extension.UserMapper;
038
039/**
040 * Component to manage extension point and expose the {@link UserMapperService} interface.
041 *
042 * @author tiry
043 * @since 7.4
044 */
045public class UserMapperComponent extends DefaultComponent implements UserMapperService {
046
047    protected static final Log log = LogFactory.getLog(UserMapperComponent.class);
048
049    protected Map<String, UserMapper> mappers = new HashMap<>();
050
051    protected List<UserMapperDescriptor> descriptors = new ArrayList<>();
052
053    public static final String MAPPER_EP = "mapper";
054
055    @Override
056    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
057        if (MAPPER_EP.equalsIgnoreCase(extensionPoint)) {
058            UserMapperDescriptor desc = (UserMapperDescriptor) contribution;
059            descriptors.add(desc);
060        }
061    }
062
063    @Override
064    public void applicationStarted(ComponentContext context) {
065        for (UserMapperDescriptor desc : descriptors) {
066            try {
067                mappers.put(desc.name, desc.getInstance());
068            } catch (Exception e) {
069                log.error("Unable to register mapper " + desc.name, e);
070            }
071        }
072    }
073
074    @Override
075    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
076        if (MAPPER_EP.equalsIgnoreCase(extensionPoint)) {
077            UserMapperDescriptor desc = (UserMapperDescriptor) contribution;
078            UserMapper um = mappers.get(desc.name);
079            if (um != null) {
080                um.release();
081                mappers.remove(desc.name);
082            }
083        }
084    }
085
086    @Override
087    public void deactivate(ComponentContext context) {
088        for (UserMapper um : mappers.values()) {
089            um.release();
090        }
091        super.deactivate(context);
092    }
093
094    @Override
095    public UserMapper getMapper(String mappingName) throws NuxeoException {
096        UserMapper um = mappers.get(mappingName);
097        if (um == null) {
098            throw new NuxeoException("No mapping defined for " + mappingName);
099        }
100        return um;
101    }
102
103    @Override
104    public NuxeoPrincipal getOrCreateAndUpdateNuxeoPrincipal(String mappingName, Object userObject)
105            throws NuxeoException {
106        return getOrCreateAndUpdateNuxeoPrincipal(mappingName, userObject, true, true, null);
107    }
108
109    @Override
110    public NuxeoPrincipal getOrCreateAndUpdateNuxeoPrincipal(String mappingName, Object userObject,
111            boolean createIfNeeded, boolean update, Map<String, Serializable> params) throws NuxeoException {
112        return getMapper(mappingName).getOrCreateAndUpdateNuxeoPrincipal(userObject, createIfNeeded, update, params);
113    }
114
115    @Override
116    public Object wrapNuxeoPrincipal(String mappingName, NuxeoPrincipal principal, Object nativePrincipal,
117            Map<String, Serializable> params) throws NuxeoException {
118        return getMapper(mappingName).wrapNuxeoPrincipal(principal, nativePrincipal, params);
119    }
120
121    @Override
122    public Set<String> getAvailableMappings() {
123        return mappers.keySet();
124    }
125
126}