001/* 002 * (C) Copyright 2014 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 * Nicolas Chapurlat <nchapurlat@nuxeo.com> 018 */ 019 020package org.nuxeo.ecm.core.schema.types.resolver; 021 022import java.util.HashMap; 023import java.util.Map; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.nuxeo.runtime.model.ComponentContext; 028import org.nuxeo.runtime.model.ComponentInstance; 029import org.nuxeo.runtime.model.DefaultComponent; 030 031/** 032 * Simple Map base implementation. 033 * 034 * @since 7.1 035 */ 036public class ObjectResolverServiceImpl extends DefaultComponent implements ObjectResolverService { 037 038 private static final Log log = LogFactory.getLog(ObjectResolverServiceImpl.class); 039 040 private Map<String, Class<? extends ObjectResolver>> resolvers; 041 042 @Override 043 public void activate(ComponentContext context) { 044 super.activate(context); 045 resolvers = new HashMap<String, Class<? extends ObjectResolver>>(); 046 } 047 048 @Override 049 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 050 if (extensionPoint.equals("resolvers")) { 051 ObjectResolverDescriptor erd = (ObjectResolverDescriptor) contribution; 052 resolvers.put(erd.getType(), erd.getResolver()); 053 } 054 } 055 056 @Override 057 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 058 if (extensionPoint.equals("resolvers")) { 059 ObjectResolverDescriptor erd = (ObjectResolverDescriptor) contribution; 060 resolvers.remove(erd.getType()); 061 } 062 } 063 064 @Override 065 public ObjectResolver getResolver(String type, Map<String, String> parameters) { 066 Class<? extends ObjectResolver> resolverClass = resolvers.get(type); 067 if (resolverClass == null) { 068 return null; 069 } 070 ObjectResolver resolver = null; 071 try { 072 resolver = resolverClass.newInstance(); 073 } catch (InstantiationException | IllegalAccessException e) { 074 log.warn(String.format("Unable to instanciate %s - missing public constructor with no param", 075 resolverClass.getCanonicalName())); 076 return null; 077 } 078 try { 079 resolver.configure(parameters != null ? parameters : new HashMap<String, String>()); 080 } catch (IllegalArgumentException e) { 081 log.info(String.format("Unable to configure %s with parameters %s", resolverClass.getCanonicalName(), 082 parameters.toString())); 083 return null; 084 } 085 return resolver; 086 } 087 088}