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.system.repository;
18
19 import java.io.File;
20 import java.io.FilenameFilter;
21 import java.net.URI;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.SortedSet;
25 import java.util.TreeSet;
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: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
35 */
36 public class Maven2Repository extends AbstractRepository implements WritableListableRepository {
37 public Maven2Repository(URI root, ServerInfo serverInfo) {
38 super(root, serverInfo);
39 }
40
41 public Maven2Repository(File rootFile) {
42 super(rootFile);
43 }
44
45 public File getLocation(Artifact artifact) {
46 if(!artifact.isResolved()) {
47 throw new IllegalArgumentException("Artifact "+artifact+" is not fully resolved");
48 }
49 File path = new File(rootFile, artifact.getGroupId().replace('.', File.separatorChar));
50 path = new File(path, artifact.getArtifactId());
51 path = new File(path, artifact.getVersion().toString());
52 path = new File(path, artifact.getArtifactId() + "-" + artifact.getVersion() + "." + artifact.getType());
53
54 return path;
55 }
56
57 public SortedSet list() {
58 return listInternal(null, null, null);
59 }
60
61 public SortedSet list(Artifact query) {
62 if(query.getGroupId() != null) {
63 File path = new File(rootFile, query.getGroupId().replace('.', File.separatorChar));
64 path = new File(path, query.getArtifactId());
65 if(!path.canRead() || !path.isDirectory()) {
66 return new TreeSet();
67 }
68
69 SortedSet artifacts = new TreeSet();
70
71 File[] versionDirs = path.listFiles();
72 for (int i = 0; i < versionDirs.length; i++) {
73 File versionDir = versionDirs[i];
74 if (versionDir.canRead() && versionDir.isDirectory()) {
75 String version = versionDir.getName();
76 if(query.getVersion() != null && !query.getVersion().toString().equals(version)) {
77 continue;
78 }
79
80 final String filePrefix = query.getArtifactId() + "-" + version + ".";
81 File[] list = versionDir.listFiles(new FilenameFilter() {
82 public boolean accept(File dir, String name) {
83 return name.startsWith(filePrefix);
84 }
85 });
86 for (int j = 0; j < list.length; j++) {
87 File file = list[j];
88 String 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 listInternal(String artifactMatch, String typeMatch, String versionMatch) {
105 SortedSet artifacts = new TreeSet();
106 File[] groupIds = rootFile.listFiles();
107 for (int i = 0; i < groupIds.length; i++) {
108 File groupId = groupIds[i];
109 if (groupId.canRead() && groupId.isDirectory()) {
110 File[] versionDirs = groupId.listFiles();
111 for (int j = 0; j < versionDirs.length; j++) {
112 File 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 getArtifacts(String groupId, File versionDir, String artifactMatch, String typeMatch, String versionMatch) {
123
124 List artifacts = new ArrayList();
125 String artifactId = versionDir.getParentFile().getName();
126
127 File[] files = versionDir.listFiles();
128 for (int i = 0; i < files.length; i++) {
129 File file = files[i];
130 if (file.canRead()) {
131 if (file.isDirectory()) {
132 File test = new File(file, "META-INF");
133 if(test.exists() && test.isDirectory() && test.canRead() && groupId != null) {
134 String version = versionDir.getName();
135 String fileHeader = artifactId + "-" + version + ".";
136
137 String fileName = file.getName();
138 if (fileName.startsWith(fileHeader)) {
139
140 String 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 {
159 String 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 version = versionDir.getName();
170 String fileHeader = artifactId + "-" + version + ".";
171
172 String fileName = file.getName();
173 if (fileName.startsWith(fileHeader)) {
174
175 String 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.class, true);
206 infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
207 infoFactory.addInterface(Maven2Repository.class);
208 infoFactory.setConstructor(new String[]{"root", "ServerInfo"});
209 GBEAN_INFO = infoFactory.getBeanInfo();
210 }
211
212 public static GBeanInfo getGBeanInfo() {
213 return GBEAN_INFO;
214 }
215 }