KickJava   Java API By Example, From Geeks To Geeks.

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


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

40 public class Maven1Repository extends AbstractRepository implements WritableListableRepository {
41     public Maven1Repository(URI JavaDoc root, ServerInfo serverInfo) {
42         super(root, serverInfo);
43     }
44
45     public Maven1Repository(File JavaDoc rootFile) {
46         super(rootFile);
47     }
48
49     public File JavaDoc getLocation(Artifact artifact) {
50         File JavaDoc path = new File JavaDoc(rootFile, artifact.getGroupId());
51         path = new File JavaDoc(path, artifact.getType() + "s");
52         String JavaDoc ext = artifact.getType();
53         if(ext.equals("ejb")) {
54             ext = "jar";
55         }
56         path = new File JavaDoc(path, artifact.getArtifactId() + "-" + artifact.getVersion() + "." + ext);
57
58         return path;
59     }
60
61     public SortedSet JavaDoc list(Artifact query) {
62         SortedSet JavaDoc artifacts = new TreeSet JavaDoc();
63         if(query.getGroupId() != null && query.getArtifactId() != null && query.getType() != null) {
64
65             File JavaDoc path = new File JavaDoc(rootFile, query.getGroupId());
66             path = new File JavaDoc(path, query.getType() + "s");
67
68             File JavaDoc[] files = path.listFiles();
69             if (files != null) {
70                 for (int i = 0; i < files.length; i++) {
71                     File JavaDoc file = files[i];
72                     String JavaDoc fileName = file.getName();
73                     if (fileName.startsWith(query.getArtifactId() + "-") && fileName.endsWith("." + query.getType())) {
74                         String JavaDoc version = fileName.substring(query.getArtifactId().length() + 1);
75                         version = version.substring(0, version.length() - 1 - query.getType().length());
76                         if(query.getVersion() != null && !query.getVersion().toString().equals(version)) {
77                             continue;
78                         }
79                         artifacts.add(new Artifact(query.getGroupId(), query.getArtifactId(), version, query.getType()));
80                     }
81                 }
82             }
83         } else {
84             // todo: not very efficient
85
SortedSet JavaDoc set = list();
86             String JavaDoc targetGroup = query.getGroupId();
87             String JavaDoc targetArtifact = query.getArtifactId();
88             Version targetVersion = query.getVersion();
89             String JavaDoc targetType = query.getType();
90             for (Iterator JavaDoc it = set.iterator(); it.hasNext();) {
91                 Artifact candidate = (Artifact) it.next();
92                 if(targetGroup != null && !targetGroup.equals(candidate.getGroupId())) {
93                     continue;
94                 }
95                 if(targetArtifact != null && !targetArtifact.equals(candidate.getArtifactId())) {
96                     continue;
97                 }
98                 if(targetType != null && !targetType.equals(candidate.getType())) {
99                     continue;
100                 }
101                 if(targetVersion != null && !targetVersion.equals(candidate.getVersion())) {
102                     continue;
103                 }
104                 artifacts.add(candidate);
105             }
106         }
107         return artifacts;
108     }
109
110     //thanks to Brett Porter for this regex lifted from a maven1-2 porting tool
111
private static final Pattern JavaDoc MAVEN_1_PATTERN = Pattern.compile("(.+)/(.+)s/(.+)-([0-9].+)\\.([^0-9]+)");
112
113     public SortedSet JavaDoc list() {
114         SortedSet JavaDoc artifacts = new TreeSet JavaDoc();
115         String JavaDoc[] names = getFiles(rootFile, "");
116         Matcher JavaDoc matcher = MAVEN_1_PATTERN.matcher("");
117         for (int i = 0; i < names.length; i++) {
118             matcher.reset(names[i]);
119             if (matcher.matches()) {
120                 String JavaDoc groupId = matcher.group(1);
121                 String JavaDoc artifactId = matcher.group(3);
122                 String JavaDoc version = matcher.group(4);
123                 String JavaDoc type = matcher.group(2);
124                 if(groupId.indexOf('/') > -1 || artifactId.indexOf('/') > -1 || type.indexOf('/') > -1 ||
125                     version.indexOf('/') > -1) {
126                     log.warn("could not resolve URI for malformed repository entry: " + names[i] +
127                     " - the filename should look like: <groupId>/<type>s/<artifactId>-<version>.<type> "+
128                     "Perhaps you put in a file without a version number in the name?");
129                 } else {
130                     artifacts.add(new Artifact(groupId, artifactId, version, type));
131                 }
132             } else {
133                 log.warn("could not resolve URI for malformed repository entry: " + names[i] +
134                 " - the filename should look like: <groupId>/<type>s/<artifactId>-<version>.<type> "+
135                 "Perhaps you put in a file without a version number in the name?");
136             }
137
138         }
139         return artifacts;
140     }
141
142     public String JavaDoc[] getFiles(File JavaDoc base, String JavaDoc prefix) {
143         if (!base.canRead() || !base.isDirectory()) {
144             throw new IllegalArgumentException JavaDoc(base.getAbsolutePath());
145         }
146         List JavaDoc list = new ArrayList JavaDoc();
147         File JavaDoc[] hits = base.listFiles();
148         for (int i = 0; i < hits.length; i++) {
149             File JavaDoc hit = hits[i];
150             if (hit.canRead()) {
151                 if (hit.isDirectory()) {
152                     list.addAll(Arrays.asList(getFiles(hit, prefix.equals("") ? hit.getName() : prefix + "/" + hit.getName())));
153                 } else {
154                     list.add(prefix.equals("") ? hit.getName() : prefix + "/" + hit.getName());
155                 }
156             }
157         }
158         return (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
159     }
160
161     public static final GBeanInfo GBEAN_INFO;
162
163     static {
164         GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(Maven1Repository.class, "Repository");
165
166         infoFactory.addAttribute("root", URI JavaDoc.class, true);
167
168         infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
169
170         infoFactory.addInterface(Maven1Repository.class);
171
172         infoFactory.setConstructor(new String JavaDoc[]{"root", "ServerInfo"});
173
174         GBEAN_INFO = infoFactory.getBeanInfo();
175     }
176
177     public static GBeanInfo getGBeanInfo() {
178         return GBEAN_INFO;
179     }
180 }
181
Popular Tags