001/* 002 * (C) Copyright 2006-2017 Nuxeo (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 */ 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 unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 063 if (MAPPER_EP.equalsIgnoreCase(extensionPoint)) { 064 UserMapperDescriptor desc = (UserMapperDescriptor) contribution; 065 UserMapper um = mappers.get(desc.name); 066 if (um != null) { 067 um.release(); 068 mappers.remove(desc.name); 069 } 070 } 071 } 072 073 @Override 074 public void start(ComponentContext context) { 075 for (UserMapperDescriptor desc : descriptors) { 076 try { 077 mappers.put(desc.name, desc.getInstance()); 078 } catch (Exception e) { 079 log.error("Unable to register mapper " + desc.name, e); 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}