KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > savant > DependencyMediator


1 /*
2  * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.savant;
8
9
10 import java.io.File JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19
20 /**
21  * <p>
22  * This is used to iterate over depedency lists (artifacts
23  * and artifact groups) and call out to interested listeners.
24  * </p>
25  *
26  * @author Brian Pontarelli
27  */

28 public class DependencyMediator {
29
30     private static Set JavaDoc projectsBuilt = new HashSet JavaDoc();
31     private static Map JavaDoc artifactsFetched = new HashMap JavaDoc();
32
33     private List JavaDoc listeners = new ArrayList JavaDoc();
34     private Dependencies dependencies = new Dependencies();
35     private LocalCacheStore localCacheStore;
36     private Workflow workflow;
37
38
39     /**
40      * Creates a new DepedencyMediator.
41      */

42     public DependencyMediator() {
43     }
44
45
46     /**
47      * Fetches the local cache location from this mediators {@link LocalCacheStore}.
48      * If the local cache hasn't been constructed, this returns null.
49      */

50     public File JavaDoc getLocalCacheDir() {
51         return (localCacheStore == null) ? null : localCacheStore.getLocation();
52     }
53
54     /**
55      * Sets the local cache location for this mediators {@link LocalCacheStore}.
56      *
57      * @param localCacheDir The directory that is used as the LocalCacheStore
58      * location
59      * @throws SavantException If the File given is invalid
60      */

61     public void setLocalCacheDir(File JavaDoc localCacheDir) throws SavantException {
62         this.localCacheStore = new LocalCacheStore(localCacheDir);
63     }
64
65     /**
66      * Adds the given listener from the list of lisetners.
67      *
68      * @param listener The listener to add
69      */

70     public void addListener(DependencyListener listener) {
71         listeners.add(listener);
72     }
73
74     /**
75      * Removes the given listener from the list of lisetners.
76      *
77      * @param listener The listener to remove
78      */

79     public void removeListener(DependencyListener listener) {
80         listeners.remove(listener);
81     }
82
83     /**
84      * Fetches the dependencies that will be mediated over during mediation.
85      *
86      * @return The {@link Dependencies} object or null if one was never set
87      */

88     public Dependencies getDependencies() {
89         return dependencies;
90     }
91
92     /**
93      * Sets the dependencies that will be mediated over during mediation.
94      *
95      * @param dependencies The new dependencies to mediate over
96      */

97     public void setDependencies(Dependencies dependencies) {
98         this.dependencies = dependencies;
99     }
100
101     /**
102      * Retrieves the workflow.
103      */

104     public Workflow getWorkflow() {
105         return workflow;
106     }
107
108     /**
109      * Sets the workflow.
110      */

111     public void setWorkflow(Workflow workflow) {
112         this.workflow = workflow;
113     }
114
115     /**
116      * Executes this task that builds up the dependencies and such.
117      */

118     public void mediate() throws SavantException {
119         workflow.validate();
120
121         handleGroups(dependencies.getArtifactGroups());
122
123         Iterator JavaDoc aIter = dependencies.getArtifacts().iterator();
124         while (aIter.hasNext()) {
125             Artifact artifact = (Artifact) aIter.next();
126             handleArtifact(artifact, null);
127         }
128     }
129
130     /**
131      * Handles iteration over ArtifactGroups. This calls the
132      * {@link #handleArtifact(Artifact, ArtifactGroup)} for each artifact in the
133      * group and then recurses for each ArtifactGroup in the group.
134      *
135      * @param groups The groups to retrieve the artifacts and sub-groups from
136      * @throws SavantException If the mediation failed for any reason
137      */

138     protected void handleGroups(List JavaDoc groups) throws SavantException {
139         // Loop over all the artifact group and try to fetch the artifacts
140
for (int i = 0; i < groups.size(); i++) {
141             ArtifactGroup group = (ArtifactGroup) groups.get(i);
142             List JavaDoc artifacts = group.getArtifacts();
143             for (int j = 0; j < artifacts.size(); j++) {
144                 Artifact artifact = (Artifact) artifacts.get(j);
145                 handleArtifact(artifact, group);
146             }
147
148             List JavaDoc subGroups = group.getArtifactGroups();
149             if (subGroups.size() > 0) {
150                 handleGroups(subGroups);
151             }
152         }
153     }
154
155     /**
156      * Handles the fetching of a single artifact. This method also optionally takes
157      * a file set and path (classpath) that the artifact is added to. If these
158      * are null, they are ignored.
159      *
160      * @param artifact The artifact to fetch and store
161      */

162     protected void handleArtifact(Artifact artifact, ArtifactGroup group)
163     throws SavantException {
164         // Setup the dependencies (if any)
165
workflow.resolveArtifactDependencies(artifact, localCacheStore);
166
167         // Go find the file and build the projects
168
File JavaDoc file = (File JavaDoc) artifactsFetched.get(artifact);
169         if (file == null) {
170             String JavaDoc artifactProject = artifact.getProjectname();
171             String JavaDoc artifactGroup = artifact.getGroup();
172
173             // Figure out if this project is local and if so, build it
174
String JavaDoc projectID = LocalProject.makeProjectID(artifactGroup,
175                 artifactProject);
176             LocalProject project = dependencies.getProject(projectID);
177             if (project != null && !projectsBuilt.contains(project)) {
178                 project.build();
179                 projectsBuilt.add(project);
180
181                 for (int i = 0; i < listeners.size(); i++) {
182                     DependencyListener listener = (DependencyListener) listeners.get(i);
183                     listener.projectBuilt(project);
184                 }
185             }
186
187             // Let the workflow handle the resolution from both the local cache and
188
// the process objects.
189
file = workflow.findArtifact(artifact, localCacheStore);
190             if (file == null) {
191                 throw new SavantException("Unable to locate dependency [" +
192                     artifact.toString() + "]");
193             }
194
195             artifactsFetched.put(artifact, file);
196         }
197
198         for (int i = 0; i < listeners.size(); i++) {
199             DependencyListener listener = (DependencyListener) listeners.get(i);
200             listener.artifactFound(file, artifact, group);
201         }
202
203         // Recurse to handle the dependencies of the artifact
204
List JavaDoc deps = artifact.getDependencies();
205         if (deps.size() > 0) {
206             for (int i = 0; i < deps.size(); i++) {
207                 handleArtifact((Artifact) deps.get(i), group);
208             }
209         }
210     }
211 }
Popular Tags