001/*
002 * (C) Copyright 2006-2008 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 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.picture.core.libraryselector;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.platform.picture.core.ImageUtils;
025import org.nuxeo.ecm.platform.picture.core.MetadataUtils;
026import org.nuxeo.runtime.model.ComponentContext;
027import org.nuxeo.runtime.model.ComponentInstance;
028import org.nuxeo.runtime.model.DefaultComponent;
029
030public class LibrarySelectorService extends DefaultComponent implements LibrarySelector {
031
032    public static final String LIBRARY_SELECTOR = "LibrarySelector";
033
034    private static final Log log = LogFactory.getLog(LibrarySelectorService.class);
035
036    protected ImageUtils imageUtils;
037
038    protected MetadataUtils metadataUtils;
039
040    @Override
041    public void deactivate(ComponentContext context) {
042        imageUtils = null;
043        metadataUtils = null;
044    }
045
046    @Override
047    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
048
049        if (extensionPoint.equals(LIBRARY_SELECTOR)) {
050            LibrarySelectorServiceDescriptor libraryDescriptor = (LibrarySelectorServiceDescriptor) contribution;
051            registerLibrarySelector(libraryDescriptor);
052        } else {
053            log.error("Extension point " + extensionPoint + "is unknown");
054        }
055    }
056
057    public void registerLibrarySelector(LibrarySelectorServiceDescriptor libraryDescriptor) {
058        registerImageUtils(libraryDescriptor.getImageUtils());
059        registerMetadataUtils(libraryDescriptor.getMetadataUtils());
060    }
061
062    protected void registerImageUtils(ImageUtilsDescriptor imageUtilsDescriptor) {
063        if (imageUtilsDescriptor == null) {
064            return;
065        }
066        imageUtils = imageUtilsDescriptor.getNewInstance();
067        log.debug("Using " + imageUtils.getClass().getName() + " for ImageUtils.");
068    }
069
070    protected void registerMetadataUtils(MetadataUtilsDescriptor metadataUtilsDescriptor) {
071        if (metadataUtilsDescriptor == null) {
072            return;
073        }
074        metadataUtils = metadataUtilsDescriptor.getNewInstance();
075        log.debug("Using " + metadataUtils.getClass().getName() + " for MetadataUtils.");
076    }
077
078    @Override
079    public ImageUtils getImageUtils() {
080        return imageUtils;
081    }
082
083    @Override
084    public MetadataUtils getMetadataUtils() {
085        return metadataUtils;
086    }
087
088}