001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 *
012 * $Id$
013 */
014
015package org.nuxeo.runtime.deploy;
016
017import java.util.HashMap;
018import java.util.Map;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.nuxeo.runtime.model.ComponentContext;
023import org.nuxeo.runtime.model.DefaultComponent;
024import org.nuxeo.runtime.model.Extension;
025
026/**
027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
028 */
029public class ManagedComponent extends DefaultComponent {
030
031    protected static final Log log = LogFactory.getLog(ManagedComponent.class);
032
033    protected Map<String, ContributionManager> contributionManagers;
034
035    @Override
036    public void activate(ComponentContext context) {
037        contributionManagers = new HashMap<String, ContributionManager>();
038    }
039
040    @Override
041    public void deactivate(ComponentContext context) {
042        contributionManagers = null;
043    }
044
045    @Override
046    public void registerExtension(Extension extension) {
047        Object[] contribs = extension.getContributions();
048        if (contribs == null) {
049            return;
050        }
051        for (Object contrib : contribs) {
052            if (contrib instanceof Contribution) {
053                Contribution c = (Contribution) contrib;
054                c.setExtension(extension);
055                ContributionManager mgr = contributionManagers.get(c.getExtensionPoint());
056                if (mgr != null) {
057                    mgr.registerContribution(c);
058                } else {
059                    log.warn("Unable to register contribution: " + c.getContributionId() + " for extension point "
060                            + c.getExtensionPoint() + ". No manager registered.");
061                }
062            } else {
063                registerContribution(contrib, extension.getExtensionPoint(), extension.getComponent());
064            }
065        }
066    }
067
068    @Override
069    public void unregisterExtension(Extension extension) {
070        Object[] contribs = extension.getContributions();
071        if (contribs == null) {
072            return;
073        }
074        for (Object contrib : contribs) {
075            if (contrib instanceof Contribution) {
076                Contribution c = (Contribution) contrib;
077                c.setExtension(extension);
078                ContributionManager mgr = contributionManagers.get(c.getExtensionPoint());
079                if (mgr != null) {
080                    mgr.unregisterContribution(c);
081                } else {
082                    log.warn("Unable to unregister contribution: " + c.getContributionId() + " for extension point "
083                            + c.getExtensionPoint() + ". No manager registered.");
084                }
085            } else {
086                unregisterContribution(contrib, extension.getExtensionPoint(), extension.getComponent());
087            }
088        }
089    }
090
091    public void registerContributionManager(String extensionPoint, ContributionManager mgr) {
092        contributionManagers.put(extensionPoint, mgr);
093    }
094
095}