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     */
017    package org.apache.geronimo.kernel.repository;
018    
019    import java.io.File;
020    import java.util.ArrayList;
021    import java.util.Arrays;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.SortedSet;
025    import java.util.TreeSet;
026    import java.util.regex.Matcher;
027    import java.util.regex.Pattern;
028    
029    /**
030     * @version $Rev: 476049 $ $Date: 2006-11-17 15:35:17 +1100 (Fri, 17 Nov 2006) $
031     */
032    public class Maven1Repository extends AbstractRepository implements WritableListableRepository {
033    
034        public Maven1Repository(File rootFile) {
035            super(rootFile);
036        }
037    
038        public File getLocation(Artifact artifact) {
039            File path = new File(rootFile, artifact.getGroupId());
040            path = new File(path, artifact.getType() + "s");
041            String ext = artifact.getType();
042            if(ext.equals("ejb")) {
043                ext = "jar";
044            }
045            path = new File(path, artifact.getArtifactId() + "-" + artifact.getVersion() + "." + ext);
046    
047            return path;
048        }
049    
050        public SortedSet list(Artifact query) {
051            SortedSet artifacts = new TreeSet();
052            if(query.getGroupId() != null && query.getArtifactId() != null && query.getType() != null) {
053    
054                File path = new File(rootFile, query.getGroupId());
055                path = new File(path, query.getType() + "s");
056    
057                File[] files = path.listFiles();
058                if (files != null) {
059                    for (int i = 0; i < files.length; i++) {
060                        File file = files[i];
061                        String fileName = file.getName();
062                        if (fileName.startsWith(query.getArtifactId() + "-") && fileName.endsWith("." + query.getType())) {
063                            String version = fileName.substring(query.getArtifactId().length() + 1);
064                            version = version.substring(0, version.length() - 1 - query.getType().length());
065                            if(query.getVersion() != null && !query.getVersion().toString().equals(version)) {
066                                continue;
067                            }
068                            artifacts.add(new Artifact(query.getGroupId(), query.getArtifactId(), version, query.getType()));
069                        }
070                    }
071                }
072            } else {
073                // todo: not very efficient
074                SortedSet set = list();
075                String targetGroup = query.getGroupId();
076                String targetArtifact = query.getArtifactId();
077                Version targetVersion = query.getVersion();
078                String targetType = query.getType();
079                for (Iterator it = set.iterator(); it.hasNext();) {
080                    Artifact candidate = (Artifact) it.next();
081                    if(targetGroup != null && !targetGroup.equals(candidate.getGroupId())) {
082                        continue;
083                    }
084                    if(targetArtifact != null && !targetArtifact.equals(candidate.getArtifactId())) {
085                        continue;
086                    }
087                    if(targetType != null && !targetType.equals(candidate.getType())) {
088                        continue;
089                    }
090                    if(targetVersion != null && !targetVersion.equals(candidate.getVersion())) {
091                        continue;
092                    }
093                    artifacts.add(candidate);
094                }
095            }
096            return artifacts;
097        }
098    
099        //thanks to Brett Porter for this regex lifted from a maven1-2 porting tool
100        private static final Pattern MAVEN_1_PATTERN = Pattern.compile("(.+)/(.+)s/(.+)-([0-9].+)\\.([^0-9]+)");
101    
102        public SortedSet list() {
103            SortedSet artifacts = new TreeSet();
104            String[] names = getFiles(rootFile, "");
105            Matcher matcher = MAVEN_1_PATTERN.matcher("");
106            for (int i = 0; i < names.length; i++) {
107                matcher.reset(names[i]);
108                if (matcher.matches()) {
109                    String groupId = matcher.group(1);
110                    String artifactId = matcher.group(3);
111                    String version = matcher.group(4);
112                    String type = matcher.group(2);
113                    if(groupId.indexOf('/') > -1 || artifactId.indexOf('/') > -1 || type.indexOf('/') > -1 ||
114                        version.indexOf('/') > -1) {
115                        log.warn("could not resolve URI for malformed repository entry: " + names[i] +
116                        " - the filename should look like: <groupId>/<type>s/<artifactId>-<version>.<type>   "+
117                        "Perhaps you put in a file without a version number in the name?");
118                    } else {
119                        artifacts.add(new Artifact(groupId, artifactId, version, type));
120                    }
121                } else {
122                    log.warn("could not resolve URI for malformed repository entry: " + names[i] +
123                    " - the filename should look like: <groupId>/<type>s/<artifactId>-<version>.<type>   "+
124                    "Perhaps you put in a file without a version number in the name?");
125                }
126    
127            }
128            return artifacts;
129        }
130    
131        public String[] getFiles(File base, String prefix) {
132            if (!base.canRead() || !base.isDirectory()) {
133                throw new IllegalArgumentException(base.getAbsolutePath());
134            }
135            List list = new ArrayList();
136            File[] hits = base.listFiles();
137            for (int i = 0; i < hits.length; i++) {
138                File hit = hits[i];
139                if (hit.canRead()) {
140                    if (hit.isDirectory()) {
141                        list.addAll(Arrays.asList(getFiles(hit, prefix.equals("") ? hit.getName() : prefix + "/" + hit.getName())));
142                    } else {
143                        list.add(prefix.equals("") ? hit.getName() : prefix + "/" + hit.getName());
144                    }
145                }
146            }
147            return (String[]) list.toArray(new String[list.size()]);
148        }
149    
150    }