KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > repository > Maven2Repository


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.system.repository;
18
19 import java.io.File JavaDoc;
20 import java.io.FilenameFilter JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.SortedSet JavaDoc;
25 import java.util.TreeSet JavaDoc;
26
27 import org.apache.geronimo.gbean.GBeanInfo;
28 import org.apache.geronimo.gbean.GBeanInfoBuilder;
29 import org.apache.geronimo.kernel.repository.Artifact;
30 import org.apache.geronimo.kernel.repository.WritableListableRepository;
31 import org.apache.geronimo.system.serverinfo.ServerInfo;
32
33 /**
34  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
35  */

36 public class Maven2Repository extends AbstractRepository implements WritableListableRepository {
37     public Maven2Repository(URI JavaDoc root, ServerInfo serverInfo) {
38         super(root, serverInfo);
39     }
40
41     public Maven2Repository(File JavaDoc rootFile) {
42         super(rootFile);
43     }
44
45     public File JavaDoc getLocation(Artifact artifact) {
46         if(!artifact.isResolved()) {
47             throw new IllegalArgumentException JavaDoc("Artifact "+artifact+" is not fully resolved");
48         }
49         File JavaDoc path = new File JavaDoc(rootFile, artifact.getGroupId().replace('.', File.separatorChar));
50         path = new File JavaDoc(path, artifact.getArtifactId());
51         path = new File JavaDoc(path, artifact.getVersion().toString());
52         path = new File JavaDoc(path, artifact.getArtifactId() + "-" + artifact.getVersion() + "." + artifact.getType());
53
54         return path;
55     }
56
57     public SortedSet JavaDoc list() {
58         return listInternal(null, null, null);
59     }
60
61     public SortedSet JavaDoc list(Artifact query) {
62         if(query.getGroupId() != null) { // todo: see if more logic can be shared with the other case
63
File JavaDoc path = new File JavaDoc(rootFile, query.getGroupId().replace('.', File.separatorChar));
64             path = new File JavaDoc(path, query.getArtifactId());
65             if(!path.canRead() || !path.isDirectory()) {
66                 return new TreeSet JavaDoc();
67             }
68
69             SortedSet JavaDoc artifacts = new TreeSet JavaDoc();
70
71             File JavaDoc[] versionDirs = path.listFiles();
72             for (int i = 0; i < versionDirs.length; i++) {
73                 File JavaDoc versionDir = versionDirs[i];
74                 if (versionDir.canRead() && versionDir.isDirectory()) {
75                     String JavaDoc version = versionDir.getName();
76                     if(query.getVersion() != null && !query.getVersion().toString().equals(version)) {
77                         continue;
78                     }
79                     // Assumes that artifactId is set
80
final String JavaDoc filePrefix = query.getArtifactId() + "-" + version + ".";
81                     File JavaDoc[] list = versionDir.listFiles(new FilenameFilter JavaDoc() {
82                         public boolean accept(File JavaDoc dir, String JavaDoc name) {
83                             return name.startsWith(filePrefix);
84                         }
85                     });
86                     for (int j = 0; j < list.length; j++) {
87                         File JavaDoc file = list[j];
88                         String JavaDoc end = file.getName().substring(filePrefix.length());
89                         if(query.getType() != null && !query.getType().equals(end)) {
90                             continue;
91                         }
92                         if(end.indexOf('.') < 0) {
93                             artifacts.add(new Artifact(query.getGroupId(), query.getArtifactId(), version, end));
94                         }
95                     }
96                 }
97             }
98             return artifacts;
99         } else {
100             return listInternal(query.getArtifactId(), query.getType(), query.getVersion() == null ? null : query.getVersion().toString());
101         }
102     }
103
104     private SortedSet JavaDoc listInternal(String JavaDoc artifactMatch, String JavaDoc typeMatch, String JavaDoc versionMatch) {
105         SortedSet JavaDoc artifacts = new TreeSet JavaDoc();
106         File JavaDoc[] groupIds = rootFile.listFiles();
107         for (int i = 0; i < groupIds.length; i++) {
108             File JavaDoc groupId = groupIds[i];
109             if (groupId.canRead() && groupId.isDirectory()) {
110                 File JavaDoc[] versionDirs = groupId.listFiles();
111                 for (int j = 0; j < versionDirs.length; j++) {
112                     File JavaDoc versionDir = versionDirs[j];
113                     if (versionDir.canRead() && versionDir.isDirectory()) {
114                         artifacts.addAll(getArtifacts(null, versionDir, artifactMatch, typeMatch, versionMatch));
115                     }
116                 }
117             }
118         }
119         return artifacts;
120     }
121
122     private List JavaDoc getArtifacts(String JavaDoc groupId, File JavaDoc versionDir, String JavaDoc artifactMatch, String JavaDoc typeMatch, String JavaDoc versionMatch) {
123         // org/apache/xbean/xbean-classpath/2.2-SNAPSHOT/xbean-classpath-2.2-SNAPSHOT.jar
124
List JavaDoc artifacts = new ArrayList JavaDoc();
125         String JavaDoc artifactId = versionDir.getParentFile().getName();
126
127         File JavaDoc[] files = versionDir.listFiles();
128         for (int i = 0; i < files.length; i++) {
129             File JavaDoc file = files[i];
130             if (file.canRead()) {
131                 if (file.isDirectory()) {
132                     File JavaDoc test = new File JavaDoc(file, "META-INF");
133                     if(test.exists() && test.isDirectory() && test.canRead() && groupId != null) {
134                         String JavaDoc version = versionDir.getName();
135                         String JavaDoc fileHeader = artifactId + "-" + version + ".";
136
137                         String JavaDoc fileName = file.getName();
138                         if (fileName.startsWith(fileHeader)) {
139                             // type is everything after the file header
140
String JavaDoc type = fileName.substring(fileHeader.length());
141
142                             if (!type.endsWith(".sha1") && !type.endsWith(".md5")) {
143                                 if(artifactMatch != null && !artifactMatch.equals(artifactId)) {
144                                     continue;
145                                 }
146                                 if(typeMatch != null && !typeMatch.equals(type)) {
147                                     continue;
148                                 }
149                                 if(versionMatch != null && !versionMatch.equals(version)) {
150                                     continue;
151                                 }
152                                 artifacts.add(new Artifact(groupId,
153                                         artifactId,
154                                         version,
155                                         type));
156                             }
157                         }
158                     } else { // this is just part of the path to the artifact
159
String JavaDoc nextGroupId;
160                         if (groupId == null) {
161                             nextGroupId = artifactId;
162                         } else {
163                             nextGroupId = groupId + "." + artifactId;
164                         }
165
166                         artifacts.addAll(getArtifacts(nextGroupId, file, artifactMatch, typeMatch, versionMatch));
167                     }
168                 } else if (groupId != null) {
169                     String JavaDoc version = versionDir.getName();
170                     String JavaDoc fileHeader = artifactId + "-" + version + ".";
171
172                     String JavaDoc fileName = file.getName();
173                     if (fileName.startsWith(fileHeader)) {
174                         // type is everything after the file header
175
String JavaDoc type = fileName.substring(fileHeader.length());
176
177                         if (!type.endsWith(".sha1") && !type.endsWith(".md5")) {
178                             if(artifactMatch != null && !artifactMatch.equals(artifactId)) {
179                                 continue;
180                             }
181                             if(typeMatch != null && !typeMatch.equals(type)) {
182                                 continue;
183                             }
184                             if(versionMatch != null && !versionMatch.equals(version)) {
185                                 continue;
186                             }
187                             artifacts.add(new Artifact(groupId,
188                                     artifactId,
189                                     version,
190                                     type
191                             ));
192                         }
193                     }
194                 }
195             }
196         }
197         return artifacts;
198     }
199
200
201     public static final GBeanInfo GBEAN_INFO;
202
203     static {
204         GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(Maven2Repository.class, "Repository");
205         infoFactory.addAttribute("root", URI JavaDoc.class, true);
206         infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
207         infoFactory.addInterface(Maven2Repository.class);
208         infoFactory.setConstructor(new String JavaDoc[]{"root", "ServerInfo"});
209         GBEAN_INFO = infoFactory.getBeanInfo();
210     }
211
212     public static GBeanInfo getGBeanInfo() {
213         return GBEAN_INFO;
214     }
215 }
216
Popular Tags