001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 016 * 017 * $Id: IOManagerComponent.java 24959 2007-09-14 13:46:47Z atchertchian $ 018 */ 019 020package org.nuxeo.ecm.platform.io.impl; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024import org.nuxeo.ecm.platform.io.api.IOManager; 025import org.nuxeo.ecm.platform.io.api.IOResourceAdapter; 026import org.nuxeo.ecm.platform.io.descriptors.IOResourceAdapterDescriptor; 027import org.nuxeo.runtime.model.ComponentInstance; 028import org.nuxeo.runtime.model.ComponentName; 029import org.nuxeo.runtime.model.DefaultComponent; 030 031/** 032 * Component registering {@link IOResourceAdapter} instances to an {@link IOManager}. 033 * 034 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 035 */ 036public class IOManagerComponent extends DefaultComponent { 037 038 public static final ComponentName NAME = new ComponentName(IOManagerComponent.class.getName()); 039 040 public static final String ADAPTERS_EP_NAME = "adapters"; 041 042 private static final Log log = LogFactory.getLog(IOManagerComponent.class); 043 044 private final IOManager service; 045 046 public IOManagerComponent() { 047 service = new IOManagerImpl(); 048 } 049 050 public IOManager getIOManager() { 051 return service; 052 } 053 054 @Override 055 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 056 if (extensionPoint.equals(ADAPTERS_EP_NAME)) { 057 IOResourceAdapterDescriptor desc = (IOResourceAdapterDescriptor) contribution; 058 String name = desc.getName(); 059 String className = desc.getClassName(); 060 IOResourceAdapter adapter; 061 try { 062 // Thread context loader is not working in isolated EARs 063 adapter = (IOResourceAdapter) IOManagerComponent.class.getClassLoader().loadClass(className).newInstance(); 064 } catch (ReflectiveOperationException e) { 065 log.error("Caught error when instantiating adapter", e); 066 return; 067 } 068 adapter.setProperties(desc.getProperties()); 069 IOResourceAdapter existing = service.getAdapter(name); 070 if (existing != null) { 071 log.warn(String.format("Overriding IO Resource adapter definition %s", name)); 072 service.removeAdapter(name); 073 } 074 service.addAdapter(name, adapter); 075 log.info(String.format("IO resource adapter %s registered", name)); 076 } else { 077 log.error(String.format("Unknown extension point %s, can't register !", extensionPoint)); 078 } 079 } 080 081 @Override 082 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 083 if (extensionPoint.equals(ADAPTERS_EP_NAME)) { 084 IOResourceAdapterDescriptor desc = (IOResourceAdapterDescriptor) contribution; 085 service.removeAdapter(desc.getName()); 086 } else { 087 log.error(String.format("Unknown extension point %s, can't unregister !", extensionPoint)); 088 } 089 } 090 091 @Override 092 @SuppressWarnings("unchecked") 093 public <T> T getAdapter(Class<T> adapter) { 094 if (adapter.isAssignableFrom(IOManager.class)) { 095 return (T) service; 096 } 097 return null; 098 } 099 100}