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 org.apache.commons.logging.Log;
018import org.apache.commons.logging.LogFactory;
019
020import java.util.ArrayList;
021import java.util.Collection;
022
023/**
024 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
025 */
026public class ContributionManager extends DependencyTree<String, Contribution> {
027
028    private static final Log log = LogFactory.getLog(ContributionManager.class);
029
030    private final ManagedComponent component;
031
032    public ContributionManager(ManagedComponent component) {
033        this.component = component;
034    }
035
036    public ManagedComponent getComponent() {
037        return component;
038    }
039
040    public void registerContribution(Contribution contrib) {
041        String baseId = null;
042        if (contrib instanceof ExtensibleContribution) {
043            ExtensibleContribution c = (ExtensibleContribution) contrib;
044            baseId = c.getBaseContributionId();
045        }
046        Collection<String> deps = new ArrayList<String>();
047        if (baseId != null) {
048            deps.add(baseId);
049        }
050        Collection<String> cdeps = contrib.getDependencies();
051        if (cdeps != null) {
052            deps.addAll(cdeps);
053        }
054        add(contrib.getContributionId(), contrib, deps);
055    }
056
057    public void unregisterContribution(Contribution contrib) {
058        remove(contrib.getContributionId());
059    }
060
061    @Override
062    protected void resolved(Entry<String, Contribution> entry) {
063        Contribution contrib = entry.get();
064        contrib.resolve(this);
065        try {
066            contrib.install(component);
067        } catch (RuntimeException e) {
068            log.error(e, e);
069        }
070    }
071
072    @Override
073    protected void unresolved(Entry<String, Contribution> entry) {
074        Contribution contrib = entry.get();
075        try {
076            contrib.uninstall(component);
077        } catch (RuntimeException e) {
078            log.error(e, e);
079        }
080        contrib.unresolve(this);
081    }
082
083}