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