001/*
002 * (C) Copyright 2011 Nuxeo SA (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 *    Mariana Cedica
016 */
017package org.nuxeo.ecm.platform.importer.service;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021import org.nuxeo.ecm.platform.importer.factories.DefaultDocumentModelFactory;
022import org.nuxeo.ecm.platform.importer.log.ImporterLogger;
023import org.nuxeo.ecm.platform.importer.source.FileSourceNode;
024import org.nuxeo.ecm.platform.importer.source.SourceNode;
025import org.nuxeo.runtime.model.ComponentContext;
026import org.nuxeo.runtime.model.ComponentInstance;
027import org.nuxeo.runtime.model.DefaultComponent;
028
029public class DefaultImporterComponent extends DefaultComponent {
030
031    private static final Log log = LogFactory.getLog(DefaultImporterComponent.class);
032
033    protected DefaultImporterService importerService;
034
035    public static final String IMPORTER_CONFIGURATION_XP = "importerConfiguration";
036
037    public static final String DEFAULT_FOLDERISH_DOC_TYPE = "Folder";
038
039    public static final String DEFAULT_LEAF_DOC_TYPE = "File";
040
041    @SuppressWarnings("unchecked")
042    @Override
043    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
044        if (IMPORTER_CONFIGURATION_XP.equals(extensionPoint)) {
045
046            ImporterConfigurationDescriptor descriptor = (ImporterConfigurationDescriptor) contribution;
047            Class<? extends SourceNode> sourceNodeClass = (Class<? extends SourceNode>) descriptor.getSourceNodeClass();
048            if (sourceNodeClass == null) {
049                sourceNodeClass = FileSourceNode.class;
050                log.info("No custom implementation defined for the SourceNode, using FileSourceNode");
051            }
052            importerService.setSourceNodeClass(sourceNodeClass);
053
054            Class<? extends DefaultDocumentModelFactory> docFactoryClass = descriptor.getDocumentModelFactory().getDocumentModelFactoryClass();
055            if (docFactoryClass == null) {
056                docFactoryClass = DefaultDocumentModelFactory.class;
057                log.info("No custom implementation provided for the documentModelFactory, using DefaultDocumentModelFactory");
058            }
059            importerService.setDocModelFactoryClass(docFactoryClass);
060
061            String folderishType = descriptor.getDocumentModelFactory().getFolderishType();
062            if (folderishType == null) {
063                folderishType = DEFAULT_FOLDERISH_DOC_TYPE;
064                log.info("No folderish type defined, using Folder by default");
065            }
066            importerService.setFolderishDocType(folderishType);
067
068            String leafType = descriptor.getDocumentModelFactory().getLeafType();
069            if (leafType == null) {
070                leafType = DEFAULT_LEAF_DOC_TYPE;
071                log.info("No leaf type doc defined, using File by deafult");
072            }
073            importerService.setLeafDocType(leafType);
074
075            Class<? extends ImporterLogger> logClass = descriptor.getImporterLog();
076            if (logClass == null) {
077                log.info("No specific ImporterLogger configured for this importer");
078            } else {
079                try {
080                    importerService.setImporterLogger(logClass.newInstance());
081                } catch (ReflectiveOperationException e) {
082                    throw new RuntimeException(e);
083                }
084            }
085            
086            if (descriptor.getRepository()!=null) {
087                importerService.setRepository(descriptor.getRepository());
088            }
089
090        }
091    }
092
093    @Override
094    public void activate(ComponentContext context) {
095        importerService = new DefaultImporterServiceImpl();
096    }
097
098    @Override
099    public void deactivate(ComponentContext context) {
100        importerService = null;
101    }
102
103    @Override
104    public <T> T getAdapter(Class<T> adapter) {
105        if (adapter.isAssignableFrom(DefaultImporterService.class)) {
106            return adapter.cast(importerService);
107        }
108        return super.getAdapter(adapter);
109    }
110}