001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.kernel.repository;
018    
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.HashMap;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.Map;
025    import java.util.Set;
026    import java.util.SortedSet;
027    import java.util.TreeSet;
028    
029    import org.apache.geronimo.gbean.GBeanInfo;
030    import org.apache.geronimo.gbean.GBeanInfoBuilder;
031    
032    /**
033     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
034     */
035    public class DefaultArtifactManager implements ArtifactManager {
036        private final Map artifactsByLoader = new HashMap();
037        private final Map artifactsByArtifact = new HashMap();
038    
039        public void loadArtifacts(Artifact loader, Set artifacts) {
040            if (!loader.isResolved()) throw new IllegalArgumentException("loader is not a resolved artifact: " + loader);
041            for (Iterator iterator = artifacts.iterator(); iterator.hasNext();) {
042                Artifact artifact = (Artifact) iterator.next();
043                if (!artifact.isResolved()) {
044                    throw new IllegalArgumentException("artifact is not a resolved artifact: " + artifact);
045                }
046            }
047    
048            synchronized (this) {
049                if (artifactsByLoader.containsKey(loader)) throw new IllegalArgumentException("loader has already declared artifacts: "+ loader);
050                artifactsByLoader.put(loader, artifacts);
051                processArtifact(loader);
052    
053                for (Iterator iterator = artifacts.iterator(); iterator.hasNext();) {
054                    Artifact artifact = (Artifact) iterator.next();
055                    processArtifact(artifact);
056                }
057            }
058        }
059    
060        private void processArtifact(Artifact artifact) {
061            List values = (List) artifactsByArtifact.get(artifact.getArtifactId());
062            if (values == null) {
063                values = new ArrayList();
064                artifactsByArtifact.put(artifact.getArtifactId(), values);
065            }
066            values.add(artifact);
067        }
068    
069        public synchronized void unloadAllArtifacts(Artifact loader) {
070            removeArtifact(loader);
071    
072            Collection artifacts = (Collection) artifactsByLoader.remove(loader);
073            if (artifacts == null) {
074                return;
075            }
076    
077            for (Iterator iterator = artifacts.iterator(); iterator.hasNext();) {
078                Artifact artifact = (Artifact) iterator.next();
079                removeArtifact(artifact);
080            }
081        }
082    
083        private void removeArtifact(Artifact artifact) {
084            List values = (List) artifactsByArtifact.get(artifact.getArtifactId());
085            if (values != null) {
086                values.remove(artifact);
087                if (values.isEmpty()) {
088                    artifactsByArtifact.remove(artifact.getArtifactId());
089                }
090            }
091        }
092    
093        public SortedSet getLoadedArtifacts(Artifact query) {
094            List values = (List) artifactsByArtifact.get(query.getArtifactId());
095            SortedSet results = new TreeSet();
096            if (values != null) {
097                for (int i = 0; i < values.size(); i++) {
098                    Artifact test = (Artifact) values.get(i);
099                    if(query.matches(test)) {
100                        results.add(test);
101                    }
102                }
103            }
104            return results;
105        }
106    
107        public static final GBeanInfo GBEAN_INFO;
108    
109        static {
110            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(DefaultArtifactManager.class, "ArtifactManager");
111            infoFactory.addInterface(ArtifactManager.class);
112            GBEAN_INFO = infoFactory.getBeanInfo();
113        }
114    
115        public static GBeanInfo getGBeanInfo() {
116            return GBEAN_INFO;
117        }
118    }