KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > repository > DefaultArtifactManager


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.SortedSet JavaDoc;
27 import java.util.TreeSet JavaDoc;
28
29 import org.apache.geronimo.gbean.GBeanInfo;
30 import org.apache.geronimo.gbean.GBeanInfoBuilder;
31
32 /**
33  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
34  */

35 public class DefaultArtifactManager implements ArtifactManager {
36     private final Map JavaDoc artifactsByLoader = new HashMap JavaDoc();
37     private final Map JavaDoc artifactsByArtifact = new HashMap JavaDoc();
38
39     public void loadArtifacts(Artifact loader, Set JavaDoc artifacts) {
40         if (!loader.isResolved()) throw new IllegalArgumentException JavaDoc("loader is not a resolved artifact: " + loader);
41         for (Iterator JavaDoc iterator = artifacts.iterator(); iterator.hasNext();) {
42             Artifact artifact = (Artifact) iterator.next();
43             if (!artifact.isResolved()) {
44                 throw new IllegalArgumentException JavaDoc("artifact is not a resolved artifact: " + artifact);
45             }
46         }
47
48         synchronized (this) {
49             if (artifactsByLoader.containsKey(loader)) throw new IllegalArgumentException JavaDoc("loader has already declared artifacts: "+ loader);
50             artifactsByLoader.put(loader, artifacts);
51             processArtifact(loader);
52
53             for (Iterator JavaDoc 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 JavaDoc values = (List JavaDoc) artifactsByArtifact.get(artifact.getArtifactId());
62         if (values == null) {
63             values = new ArrayList JavaDoc();
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 JavaDoc artifacts = (Collection JavaDoc) artifactsByLoader.remove(loader);
73         if (artifacts == null) {
74             return;
75         }
76
77         for (Iterator JavaDoc 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 JavaDoc values = (List JavaDoc) 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 JavaDoc getLoadedArtifacts(Artifact query) {
94         List JavaDoc values = (List JavaDoc) artifactsByArtifact.get(query.getArtifactId());
95         SortedSet JavaDoc results = new TreeSet JavaDoc();
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 }
119
Popular Tags