001/*
002 * (C) Copyright 2006-2007 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.runtime.deployment.preprocessor;
023
024import java.util.ArrayList;
025import java.util.Collection;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.nuxeo.common.collections.DependencyTree;
033
034/**
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037public class FragmentRegistry extends DependencyTree<String, FragmentDescriptor> {
038
039    private static final Log log = LogFactory.getLog(FragmentRegistry.class);
040
041    // this is needed to handle requiredBy dependencies
042    protected final Map<String, FragmentDescriptor> fragments = new HashMap<>();
043
044    public void add(FragmentDescriptor fragment) {
045        if (fragments.containsKey(fragment.name)) {
046            FragmentDescriptor existing = fragments.get(fragment.name);
047            log.error(String.format("Overriding fragment with name '%s' and path '%s' "
048                    + "that is already present with path '%s'", fragment.name, fragment.filePath, existing.filePath));
049        }
050        fragments.put(fragment.name, fragment);
051    }
052
053    @Override
054    public List<Entry<String, FragmentDescriptor>> getResolvedEntries() {
055        if (!fragments.isEmpty()) {
056            commitFragments();
057        }
058        return super.getResolvedEntries();
059    }
060
061    @Override
062    public List<Entry<String, FragmentDescriptor>> getMissingRequirements() {
063        if (!fragments.isEmpty()) {
064            commitFragments();
065        }
066        return super.getMissingRequirements();
067    }
068
069    @Override
070    public FragmentDescriptor get(String key) {
071        if (!fragments.isEmpty()) {
072            commitFragments();
073        }
074        return super.get(key);
075    }
076
077    @Override
078    public Collection<Entry<String, FragmentDescriptor>> getEntries() {
079        if (!fragments.isEmpty()) {
080            commitFragments();
081        }
082        return super.getEntries();
083    }
084
085    @Override
086    public List<FragmentDescriptor> getResolvedObjects() {
087        if (!fragments.isEmpty()) {
088            commitFragments();
089        }
090        return super.getResolvedObjects();
091    }
092
093    @Override
094    public List<FragmentDescriptor> getPendingObjects() {
095        if (!fragments.isEmpty()) {
096            commitFragments();
097        }
098        return super.getPendingObjects();
099    }
100
101    @Override
102    public Entry<String, FragmentDescriptor> getEntry(String key) {
103        if (!fragments.isEmpty()) {
104            commitFragments();
105        }
106        return super.getEntry(key);
107    }
108
109    @Override
110    public List<Entry<String, FragmentDescriptor>> getPendingEntries() {
111        if (!fragments.isEmpty()) {
112            commitFragments();
113        }
114        return super.getPendingEntries();
115    }
116
117    protected void commitFragments() {
118
119        // update requires depending on requiredBy
120        for (FragmentDescriptor fd : fragments.values()) {
121            if (fd.requiredBy != null && fd.requiredBy.length > 0) {
122                for (String reqBy : fd.requiredBy) {
123                    FragmentDescriptor fdRegBy = fragments.get(reqBy);
124                    if (fdRegBy != null) {
125                        if (fdRegBy.requires == null) {
126                            fdRegBy.requires = new ArrayList<>();
127                        }
128                        fdRegBy.requires.add(fd.name);
129                    }
130                }
131            }
132        }
133
134        // add fragments to the dependency tree
135        for (FragmentDescriptor fd : fragments.values()) {
136            add(fd.name, fd, fd.requires);
137        }
138
139        // add the "all" marker fragment
140        add(FragmentDescriptor.ALL.name, FragmentDescriptor.ALL, (Collection<String>) null);
141
142        fragments.clear();
143    }
144
145}