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.lang.reflect.Constructor; 022import java.util.List; 023 024import org.apache.logging.log4j.LogManager; 025import org.apache.logging.log4j.Logger; 026import org.nuxeo.ecm.automation.io.services.codec.CodecDescriptor; 027import org.nuxeo.ecm.automation.io.services.codec.ObjectCodec; 028import org.nuxeo.ecm.automation.io.services.codec.ObjectCodecService; 029import org.nuxeo.ecm.webengine.JsonFactoryManager; 030import org.nuxeo.runtime.RuntimeMessage.Level; 031import org.nuxeo.runtime.api.Framework; 032import org.nuxeo.runtime.model.ComponentContext; 033import org.nuxeo.runtime.model.DefaultComponent; 034 035/** 036 * @since 5.7.3 037 */ 038public class IOComponent extends DefaultComponent { 039 040 private static final Logger log = LogManager.getLogger(IOComponent.class); 041 042 /** 043 * @since 10.3 044 */ 045 public static final String XP_CODECS = "codecs"; 046 047 private JsonFactoryManager jsonFactoryManager; 048 049 private ObjectCodecService codecs; 050 051 @Override 052 public <T> T getAdapter(Class<T> adapter) { 053 if (ObjectCodecService.class.isAssignableFrom(adapter)) { 054 return adapter.cast(codecs); 055 } else if (JsonFactoryManager.class.isAssignableFrom(adapter)) { 056 return adapter.cast(jsonFactoryManager); 057 } 058 return super.getAdapter(adapter); 059 } 060 061 @Override 062 public void start(ComponentContext context) { 063 super.start(context); 064 jsonFactoryManager = Framework.getService(JsonFactoryManager.class); 065 codecs = new ObjectCodecService(jsonFactoryManager.getJsonFactory()); 066 List<CodecDescriptor> descriptors = getDescriptors(XP_CODECS); 067 for (CodecDescriptor d : descriptors) { 068 try { 069 Class<?> clazz = Class.forName(d.klass); 070 Constructor<?> constructor = clazz.getDeclaredConstructor(); 071 ObjectCodec<?> codec = (ObjectCodec<?>) constructor.newInstance(); 072 codecs.addCodec(codec); 073 } catch (ClassCastException | ReflectiveOperationException e) { 074 String msg = String.format("Failed to register codec on '%s': error initializing class '%s' (%s).", 075 name, d.getId(), e.toString()); 076 log.error(msg, e); 077 addRuntimeMessage(Level.ERROR, msg); 078 } 079 } 080 codecs.postInit(); 081 } 082 083 @Override 084 public void stop(ComponentContext context) throws InterruptedException { 085 super.stop(context); 086 codecs = null; 087 } 088 089}