001/*
002 * (C) Copyright 2014 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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
018 */
019package org.nuxeo.binary.metadata.internals;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.binary.metadata.api.BinaryMetadataConstants;
024import org.nuxeo.binary.metadata.api.BinaryMetadataService;
025import org.nuxeo.runtime.api.Framework;
026import org.nuxeo.runtime.management.metrics.MetricInvocationHandler;
027import org.nuxeo.runtime.model.ComponentContext;
028import org.nuxeo.runtime.model.ComponentInstance;
029import org.nuxeo.runtime.model.DefaultComponent;
030
031/**
032 * Binary metadata component which registers all binary metadata contributions.
033 *
034 * @since 7.1
035 */
036public class BinaryMetadataComponent extends DefaultComponent {
037
038    private static final Log log = LogFactory.getLog(BinaryMetadataComponent.class);
039
040    protected BinaryMetadataService metadataService = new BinaryMetadataServiceImpl(this);
041
042    protected final MetadataMappingRegistry mappingRegistry = new MetadataMappingRegistry();
043
044    protected final MetadataProcessorRegistry processorRegistry = new MetadataProcessorRegistry();
045
046    protected final MetadataRuleRegistry ruleRegistry = new MetadataRuleRegistry();
047
048    @Override
049    public void activate(ComponentContext context) {
050        super.activate(context);
051        if (Boolean.valueOf(Framework.getProperty(BinaryMetadataConstants.BINARY_METADATA_MONITOR,
052                Boolean.toString(log.isTraceEnabled())))) {
053            metadataService = MetricInvocationHandler.newProxy(metadataService, BinaryMetadataService.class);
054        }
055    }
056
057    @Override
058    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
059        if (BinaryMetadataConstants.METADATA_MAPPING_EP.equals(extensionPoint)) {
060            mappingRegistry.addContribution((MetadataMappingDescriptor) contribution);
061        } else if (BinaryMetadataConstants.METADATA_RULES_EP.equals(extensionPoint)) {
062            ruleRegistry.addContribution((MetadataRuleDescriptor) contribution);
063        } else if (BinaryMetadataConstants.METADATA_PROCESSORS_EP.equals(extensionPoint)) {
064            processorRegistry.addContribution((MetadataProcessorDescriptor) contribution);
065        } else {
066            log.error("Unknown extension point " + extensionPoint);
067        }
068    }
069
070    @Override
071    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
072        if (BinaryMetadataConstants.METADATA_MAPPING_EP.equals(extensionPoint)) {
073            mappingRegistry.removeContribution((MetadataMappingDescriptor) contribution);
074        } else if (BinaryMetadataConstants.METADATA_RULES_EP.equals(extensionPoint)) {
075            ruleRegistry.removeContribution((MetadataRuleDescriptor) contribution);
076        } else if (BinaryMetadataConstants.METADATA_PROCESSORS_EP.equals(extensionPoint)) {
077            processorRegistry.removeContribution((MetadataProcessorDescriptor) contribution);
078        } else {
079            log.error("Unknown extension point " + extensionPoint);
080        }
081    }
082
083    @Override
084    public void applicationStarted(ComponentContext context) {
085        super.applicationStarted(context);
086        ruleRegistry.handleApplicationStarted();
087    }
088
089    @Override
090    public <T> T getAdapter(Class<T> adapter) {
091        if (adapter.isAssignableFrom(BinaryMetadataService.class)) {
092            return adapter.cast(metadataService);
093        }
094        return null;
095    }
096
097}