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