1 17 package org.apache.geronimo.system.repository; 18 19 import java.io.File ; 20 import java.net.URI ; 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.List ; 24 import java.util.SortedSet ; 25 import java.util.TreeSet ; 26 import java.util.Iterator ; 27 import java.util.regex.Matcher ; 28 import java.util.regex.Pattern ; 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 40 public class Maven1Repository extends AbstractRepository implements WritableListableRepository { 41 public Maven1Repository(URI root, ServerInfo serverInfo) { 42 super(root, serverInfo); 43 } 44 45 public Maven1Repository(File rootFile) { 46 super(rootFile); 47 } 48 49 public File getLocation(Artifact artifact) { 50 File path = new File (rootFile, artifact.getGroupId()); 51 path = new File (path, artifact.getType() + "s"); 52 String ext = artifact.getType(); 53 if(ext.equals("ejb")) { 54 ext = "jar"; 55 } 56 path = new File (path, artifact.getArtifactId() + "-" + artifact.getVersion() + "." + ext); 57 58 return path; 59 } 60 61 public SortedSet list(Artifact query) { 62 SortedSet artifacts = new TreeSet (); 63 if(query.getGroupId() != null && query.getArtifactId() != null && query.getType() != null) { 64 65 File path = new File (rootFile, query.getGroupId()); 66 path = new File (path, query.getType() + "s"); 67 68 File [] files = path.listFiles(); 69 if (files != null) { 70 for (int i = 0; i < files.length; i++) { 71 File file = files[i]; 72 String fileName = file.getName(); 73 if (fileName.startsWith(query.getArtifactId() + "-") && fileName.endsWith("." + query.getType())) { 74 String 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 SortedSet set = list(); 86 String targetGroup = query.getGroupId(); 87 String targetArtifact = query.getArtifactId(); 88 Version targetVersion = query.getVersion(); 89 String targetType = query.getType(); 90 for (Iterator 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 private static final Pattern MAVEN_1_PATTERN = Pattern.compile("(.+)/(.+)s/(.+)-([0-9].+)\\.([^0-9]+)"); 112 113 public SortedSet list() { 114 SortedSet artifacts = new TreeSet (); 115 String [] names = getFiles(rootFile, ""); 116 Matcher 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 groupId = matcher.group(1); 121 String artifactId = matcher.group(3); 122 String version = matcher.group(4); 123 String 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 [] getFiles(File base, String prefix) { 143 if (!base.canRead() || !base.isDirectory()) { 144 throw new IllegalArgumentException (base.getAbsolutePath()); 145 } 146 List list = new ArrayList (); 147 File [] hits = base.listFiles(); 148 for (int i = 0; i < hits.length; i++) { 149 File 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 []) list.toArray(new String [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 .class, true); 167 168 infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean"); 169 170 infoFactory.addInterface(Maven1Repository.class); 171 172 infoFactory.setConstructor(new String []{"root", "ServerInfo"}); 173 174 GBEAN_INFO = infoFactory.getBeanInfo(); 175 } 176 177 public static GBeanInfo getGBeanInfo() { 178 return GBEAN_INFO; 179 } 180 } 181 | Popular Tags |