001/* 002 * (C) Copyright 2013-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 * dmetzler 018 */ 019package org.nuxeo.ecm.automation.io.services; 020 021import java.util.List; 022 023import org.apache.logging.log4j.LogManager; 024import org.apache.logging.log4j.Logger; 025import org.nuxeo.ecm.automation.io.services.codec.CodecDescriptor; 026import org.nuxeo.ecm.automation.io.services.codec.ObjectCodecService; 027import org.nuxeo.ecm.webengine.JsonFactoryManager; 028import org.nuxeo.runtime.api.Framework; 029import org.nuxeo.runtime.model.ComponentContext; 030import org.nuxeo.runtime.model.DefaultComponent; 031 032/** 033 * @since 5.7.3 034 */ 035public class IOComponent extends DefaultComponent { 036 037 private static final Logger log = LogManager.getLogger(IOComponent.class); 038 039 /** 040 * @since 10.3 041 */ 042 public static final String XP_CODECS = "codecs"; 043 044 private JsonFactoryManager jsonFactoryManager; 045 046 private ObjectCodecService codecs; 047 048 @Override 049 public <T> T getAdapter(Class<T> adapter) { 050 if (ObjectCodecService.class.isAssignableFrom(adapter)) { 051 return adapter.cast(codecs); 052 } else if (JsonFactoryManager.class.isAssignableFrom(adapter)) { 053 return adapter.cast(jsonFactoryManager); 054 } 055 return super.getAdapter(adapter); 056 } 057 058 @Override 059 public void start(ComponentContext context) { 060 super.start(context); 061 jsonFactoryManager = Framework.getService(JsonFactoryManager.class); 062 codecs = new ObjectCodecService(jsonFactoryManager.getJsonFactory()); 063 List<CodecDescriptor> descriptors = getDescriptors(XP_CODECS); 064 descriptors.forEach(d -> { 065 try { 066 codecs.addCodec(d.klass.getDeclaredConstructor().newInstance()); 067 } catch (ReflectiveOperationException e) { 068 log.error("Unable to instantiate codec", e); 069 } 070 }); 071 codecs.postInit(); 072 } 073 074 @Override 075 public void stop(ComponentContext context) throws InterruptedException { 076 super.stop(context); 077 codecs = null; 078 } 079 080}