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 static BinaryMetadataComponent self;
041
042    protected BinaryMetadataService metadataService = new BinaryMetadataServiceImpl();
043
044    protected final MetadataMappingRegistry mappingRegistry = new MetadataMappingRegistry();
045
046    protected final MetadataProcessorRegistry processorRegistry = new MetadataProcessorRegistry();
047
048    protected final MetadataRuleRegistry ruleRegistry = new MetadataRuleRegistry();
049
050    @Override
051    public void activate(ComponentContext context) {
052        super.activate(context);
053        if (Boolean.valueOf(Framework.getProperty(BinaryMetadataConstants.BINARY_METADATA_MONITOR,
054                Boolean.toString(log.isTraceEnabled())))) {
055            metadataService = MetricInvocationHandler.newProxy(metadataService, BinaryMetadataService.class);
056        }
057        self = this;
058    }
059
060    @Override
061    public void deactivate(ComponentContext context) {
062        self = null;
063        super.deactivate(context);
064    }
065
066    @Override
067    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
068        if (BinaryMetadataConstants.METADATA_MAPPING_EP.equals(extensionPoint)) {
069            mappingRegistry.addContribution((MetadataMappingDescriptor) contribution);
070        } else if (BinaryMetadataConstants.METADATA_RULES_EP.equals(extensionPoint)) {
071            ruleRegistry.addContribution((MetadataRuleDescriptor) contribution);
072        } else if (BinaryMetadataConstants.METADATA_PROCESSORS_EP.equals(extensionPoint)) {
073            processorRegistry.addContribution((MetadataProcessorDescriptor) contribution);
074        } else {
075            log.error("Unknown extension point " + extensionPoint);
076        }
077    }
078
079    @Override
080    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
081        if (BinaryMetadataConstants.METADATA_MAPPING_EP.equals(extensionPoint)) {
082            mappingRegistry.removeContribution((MetadataMappingDescriptor) contribution);
083        } else if (BinaryMetadataConstants.METADATA_RULES_EP.equals(extensionPoint)) {
084            ruleRegistry.removeContribution((MetadataRuleDescriptor) contribution);
085        } else if (BinaryMetadataConstants.METADATA_PROCESSORS_EP.equals(extensionPoint)) {
086            processorRegistry.removeContribution((MetadataProcessorDescriptor) contribution);
087        } else {
088            log.error("Unknown extension point " + extensionPoint);
089        }
090    }
091
092    @Override
093    public void applicationStarted(ComponentContext context) {
094        super.applicationStarted(context);
095        ruleRegistry.handleApplicationStarted();
096    }
097
098    @Override
099    public <T> T getAdapter(Class<T> adapter) {
100        if (adapter.isAssignableFrom(BinaryMetadataService.class)) {
101            return adapter.cast(metadataService);
102        }
103        return null;
104    }
105
106}