1 /** 2 * 3 * Copyright 2005 The Apache Software Foundation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.geronimo.kernel.repository; 18 19 import java.util.ArrayList; 20 import java.util.Collection; 21 import java.util.HashMap; 22 import java.util.Iterator; 23 import java.util.List; 24 import java.util.Map; 25 import java.util.Set; 26 import java.util.SortedSet; 27 import java.util.TreeSet; 28 29 import org.apache.geronimo.gbean.GBeanInfo; 30 import org.apache.geronimo.gbean.GBeanInfoBuilder; 31 32 /** 33 * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $ 34 */ 35 public class DefaultArtifactManager implements ArtifactManager { 36 private final Map artifactsByLoader = new HashMap(); 37 private final Map artifactsByArtifact = new HashMap(); 38 39 public void loadArtifacts(Artifact loader, Set artifacts) { 40 if (!loader.isResolved()) throw new IllegalArgumentException("loader is not a resolved artifact: " + loader); 41 for (Iterator iterator = artifacts.iterator(); iterator.hasNext();) { 42 Artifact artifact = (Artifact) iterator.next(); 43 if (!artifact.isResolved()) { 44 throw new IllegalArgumentException("artifact is not a resolved artifact: " + artifact); 45 } 46 } 47 48 synchronized (this) { 49 if (artifactsByLoader.containsKey(loader)) throw new IllegalArgumentException("loader has already declared artifacts: "+ loader); 50 artifactsByLoader.put(loader, artifacts); 51 processArtifact(loader); 52 53 for (Iterator iterator = artifacts.iterator(); iterator.hasNext();) { 54 Artifact artifact = (Artifact) iterator.next(); 55 processArtifact(artifact); 56 } 57 } 58 } 59 60 private void processArtifact(Artifact artifact) { 61 List values = (List) artifactsByArtifact.get(artifact.getArtifactId()); 62 if (values == null) { 63 values = new ArrayList(); 64 artifactsByArtifact.put(artifact.getArtifactId(), values); 65 } 66 values.add(artifact); 67 } 68 69 public synchronized void unloadAllArtifacts(Artifact loader) { 70 removeArtifact(loader); 71 72 Collection artifacts = (Collection) artifactsByLoader.remove(loader); 73 if (artifacts == null) { 74 return; 75 } 76 77 for (Iterator iterator = artifacts.iterator(); iterator.hasNext();) { 78 Artifact artifact = (Artifact) iterator.next(); 79 removeArtifact(artifact); 80 } 81 } 82 83 private void removeArtifact(Artifact artifact) { 84 List values = (List) artifactsByArtifact.get(artifact.getArtifactId()); 85 if (values != null) { 86 values.remove(artifact); 87 if (values.isEmpty()) { 88 artifactsByArtifact.remove(artifact.getArtifactId()); 89 } 90 } 91 } 92 93 public SortedSet getLoadedArtifacts(Artifact query) { 94 List values = (List) artifactsByArtifact.get(query.getArtifactId()); 95 SortedSet results = new TreeSet(); 96 if (values != null) { 97 for (int i = 0; i < values.size(); i++) { 98 Artifact test = (Artifact) values.get(i); 99 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 }