001/* 002 * (C) Copyright 2018 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 * 017 * Contributors: 018 * bdelbosc 019 */ 020package org.nuxeo.runtime.codec; 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.lib.stream.codec.Codec; 028import org.nuxeo.runtime.model.ComponentContext; 029import org.nuxeo.runtime.model.ComponentInstance; 030import org.nuxeo.runtime.model.DefaultComponent; 031 032public class CodecServiceImpl extends DefaultComponent implements CodecService { 033 private static final Log log = LogFactory.getLog(CodecServiceImpl.class); 034 035 public static final String CODEC_XP = "codec"; 036 037 public static final int APPLICATION_STARTED_ORDER = -600; 038 039 protected final Map<String, CodecDescriptor> configs = new HashMap<>(); 040 041 protected final Map<String, CodecFactory> codecFactories = new HashMap<>(); 042 043 @Override 044 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 045 if (extensionPoint.equals(CODEC_XP)) { 046 CodecDescriptor descriptor = (CodecDescriptor) contribution; 047 configs.put(descriptor.getName(), descriptor); 048 log.debug(String.format("Register Codec contribution: %s", descriptor)); 049 codecFactories.put(descriptor.getName(), descriptor.getInstance()); 050 } 051 } 052 053 @Override 054 public int getApplicationStartedOrder() { 055 return APPLICATION_STARTED_ORDER; 056 } 057 058 @Override 059 public void deactivate(ComponentContext context) { 060 super.deactivate(context); 061 log.debug("Deactivating service"); 062 } 063 064 @Override 065 public void activate(ComponentContext context) { 066 super.activate(context); 067 log.debug("Activating service"); 068 } 069 070 @Override 071 public <T> Codec<T> getCodec(String codecName, Class<T> objectClass) { 072 if (!codecFactories.containsKey(codecName)) { 073 return null; 074 } 075 // TODO codec are thread safe so we can use a cache codecName, objectClass -> codec 076 return codecFactories.get(codecName).newCodec(objectClass); 077 } 078}