001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.kernel.repository;
017:
018: import java.io.File;
019: import java.io.FilenameFilter;
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.SortedSet;
023: import java.util.TreeSet;
024:
025: /**
026: * @version $Rev: 476049 $ $Date: 2006-11-17 15:35:17 +1100 (Fri, 17 Nov 2006) $
027: */
028: public class Maven2Repository extends AbstractRepository implements
029: WritableListableRepository {
030:
031: public Maven2Repository(File rootFile) {
032: super (rootFile);
033: }
034:
035: public File getLocation(Artifact artifact) {
036: if (!artifact.isResolved()) {
037: throw new IllegalArgumentException("Artifact " + artifact
038: + " is not fully resolved");
039: }
040: File path = new File(rootFile, artifact.getGroupId().replace(
041: '.', File.separatorChar));
042: path = new File(path, artifact.getArtifactId());
043: path = new File(path, artifact.getVersion().toString());
044: path = new File(path, artifact.getArtifactId() + "-"
045: + artifact.getVersion() + "." + artifact.getType());
046:
047: return path;
048: }
049:
050: public SortedSet list() {
051: return listInternal(null, null, null);
052: }
053:
054: public SortedSet list(Artifact query) {
055: if (query.getGroupId() != null) { // todo: see if more logic can be shared with the other case
056: File path = new File(rootFile, query.getGroupId().replace(
057: '.', File.separatorChar));
058: path = new File(path, query.getArtifactId());
059: if (!path.canRead() || !path.isDirectory()) {
060: return new TreeSet();
061: }
062:
063: SortedSet artifacts = new TreeSet();
064:
065: File[] versionDirs = path.listFiles();
066: for (int i = 0; i < versionDirs.length; i++) {
067: File versionDir = versionDirs[i];
068: if (versionDir.canRead() && versionDir.isDirectory()) {
069: String version = versionDir.getName();
070: if (query.getVersion() != null
071: && !query.getVersion().toString().equals(
072: version)) {
073: continue;
074: }
075: // Assumes that artifactId is set
076: final String filePrefix = query.getArtifactId()
077: + "-" + version + ".";
078: File[] list = versionDir
079: .listFiles(new FilenameFilter() {
080: public boolean accept(File dir,
081: String name) {
082: return name.startsWith(filePrefix);
083: }
084: });
085: for (int j = 0; j < list.length; j++) {
086: File file = list[j];
087: String end = file.getName().substring(
088: filePrefix.length());
089: if (query.getType() != null
090: && !query.getType().equals(end)) {
091: continue;
092: }
093: if (end.indexOf('.') < 0) {
094: artifacts.add(new Artifact(query
095: .getGroupId(), query
096: .getArtifactId(), version, end));
097: }
098: }
099: }
100: }
101: return artifacts;
102: } else {
103: return listInternal(query.getArtifactId(), query.getType(),
104: query.getVersion() == null ? null : query
105: .getVersion().toString());
106: }
107: }
108:
109: private SortedSet listInternal(String artifactMatch,
110: String typeMatch, String versionMatch) {
111: SortedSet artifacts = new TreeSet();
112: File[] groupIds = rootFile.listFiles();
113: for (int i = 0; i < groupIds.length; i++) {
114: File groupId = groupIds[i];
115: if (groupId.canRead() && groupId.isDirectory()) {
116: File[] versionDirs = groupId.listFiles();
117: for (int j = 0; j < versionDirs.length; j++) {
118: File versionDir = versionDirs[j];
119: if (versionDir.canRead()
120: && versionDir.isDirectory()) {
121: artifacts
122: .addAll(getArtifacts(null, versionDir,
123: artifactMatch, typeMatch,
124: versionMatch));
125: }
126: }
127: }
128: }
129: return artifacts;
130: }
131:
132: private List getArtifacts(String groupId, File versionDir,
133: String artifactMatch, String typeMatch, String versionMatch) {
134: // org/apache/xbean/xbean-classpath/2.2-SNAPSHOT/xbean-classpath-2.2-SNAPSHOT.jar
135: List artifacts = new ArrayList();
136: String artifactId = versionDir.getParentFile().getName();
137:
138: File[] files = versionDir.listFiles();
139: for (int i = 0; i < files.length; i++) {
140: File file = files[i];
141: if (file.canRead()) {
142: if (file.isDirectory()) {
143: File test = new File(file, "META-INF");
144: if (test.exists() && test.isDirectory()
145: && test.canRead() && groupId != null) {
146: String version = versionDir.getName();
147: String fileHeader = artifactId + "-" + version
148: + ".";
149:
150: String fileName = file.getName();
151: if (fileName.startsWith(fileHeader)) {
152: // type is everything after the file header
153: String type = fileName.substring(fileHeader
154: .length());
155:
156: if (!type.endsWith(".sha1")
157: && !type.endsWith(".md5")) {
158: if (artifactMatch != null
159: && !artifactMatch
160: .equals(artifactId)) {
161: continue;
162: }
163: if (typeMatch != null
164: && !typeMatch.equals(type)) {
165: continue;
166: }
167: if (versionMatch != null
168: && !versionMatch
169: .equals(version)) {
170: continue;
171: }
172: artifacts.add(new Artifact(groupId,
173: artifactId, version, type));
174: }
175: }
176: } else { // this is just part of the path to the artifact
177: String nextGroupId;
178: if (groupId == null) {
179: nextGroupId = artifactId;
180: } else {
181: nextGroupId = groupId + "." + artifactId;
182: }
183:
184: artifacts.addAll(getArtifacts(nextGroupId,
185: file, artifactMatch, typeMatch,
186: versionMatch));
187: }
188: } else if (groupId != null) {
189: String version = versionDir.getName();
190: String fileHeader = artifactId + "-" + version
191: + ".";
192:
193: String fileName = file.getName();
194: if (fileName.startsWith(fileHeader)) {
195: // type is everything after the file header
196: String type = fileName.substring(fileHeader
197: .length());
198:
199: if (!type.endsWith(".sha1")
200: && !type.endsWith(".md5")) {
201: if (artifactMatch != null
202: && !artifactMatch
203: .equals(artifactId)) {
204: continue;
205: }
206: if (typeMatch != null
207: && !typeMatch.equals(type)) {
208: continue;
209: }
210: if (versionMatch != null
211: && !versionMatch.equals(version)) {
212: continue;
213: }
214: artifacts.add(new Artifact(groupId,
215: artifactId, version, type));
216: }
217: }
218: }
219: }
220: }
221: return artifacts;
222: }
223:
224: }
|