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