001/*
002 * (C) Copyright 2006-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 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.runtime.deploy;
023
024import java.util.HashMap;
025import java.util.Map;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.runtime.model.ComponentContext;
030import org.nuxeo.runtime.model.DefaultComponent;
031import org.nuxeo.runtime.model.Extension;
032
033/**
034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
035 */
036public class ManagedComponent extends DefaultComponent {
037
038    protected static final Log log = LogFactory.getLog(ManagedComponent.class);
039
040    protected Map<String, ContributionManager> contributionManagers;
041
042    @Override
043    public void activate(ComponentContext context) {
044        contributionManagers = new HashMap<>();
045    }
046
047    @Override
048    public void deactivate(ComponentContext context) {
049        contributionManagers = null;
050    }
051
052    @Override
053    public void registerExtension(Extension extension) {
054        Object[] contribs = extension.getContributions();
055        if (contribs == null) {
056            return;
057        }
058        for (Object contrib : contribs) {
059            if (contrib instanceof Contribution) {
060                Contribution c = (Contribution) contrib;
061                c.setExtension(extension);
062                ContributionManager mgr = contributionManagers.get(c.getExtensionPoint());
063                if (mgr != null) {
064                    mgr.registerContribution(c);
065                } else {
066                    log.warn("Unable to register contribution: " + c.getContributionId() + " for extension point "
067                            + c.getExtensionPoint() + ". No manager registered.");
068                }
069            } else {
070                registerContribution(contrib, extension.getExtensionPoint(), extension.getComponent());
071            }
072        }
073    }
074
075    @Override
076    public void unregisterExtension(Extension extension) {
077        Object[] contribs = extension.getContributions();
078        if (contribs == null) {
079            return;
080        }
081        for (Object contrib : contribs) {
082            if (contrib instanceof Contribution) {
083                Contribution c = (Contribution) contrib;
084                c.setExtension(extension);
085                ContributionManager mgr = contributionManagers.get(c.getExtensionPoint());
086                if (mgr != null) {
087                    mgr.unregisterContribution(c);
088                } else {
089                    log.warn("Unable to unregister contribution: " + c.getContributionId() + " for extension point "
090                            + c.getExtensionPoint() + ". No manager registered.");
091                }
092            } else {
093                unregisterContribution(contrib, extension.getExtensionPoint(), extension.getComponent());
094            }
095        }
096    }
097
098    public void registerContributionManager(String extensionPoint, ContributionManager mgr) {
099        contributionManagers.put(extensionPoint, mgr);
100    }
101
102}