Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/repoweb/model/file/FileRepository.java


1   /*
2    * REPOWEB, repository manager.
3    *
4    * Terms of license - http://opensource.org/licenses/apachepl.php
5    */
6   package org.repoweb.model.file;
7   import java.io.File;
8   import java.io.IOException;
9   import java.util.ArrayList;
10  import java.util.Iterator;
11  import java.util.List;
12  import java.util.jar.JarFile;
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  import org.repoweb.model.Artifact;
16  import org.repoweb.model.ArtifactList;
17  import org.repoweb.model.BadRepositoryLocationException;
18  import org.repoweb.model.Group;
19  import org.repoweb.model.GroupList;
20  import org.repoweb.model.Repository;
21  import org.repoweb.model.UnknownGroupIdException;
22  /**
23   * Represente un repository maven accede directement par des fichiers .
24   */
25  public class FileRepository implements Repository {
26      private static final Log LOG = LogFactory.getLog(FileRepository.class);
27      private final File _root;
28  
29      public FileRepository(String root) throws BadRepositoryLocationException {
30          this(new File(root));
31      }
32  
33  
34      public FileRepository(File root) throws BadRepositoryLocationException {
35          _root = root;
36          if (!_root.exists()) {
37              throw new BadRepositoryLocationException();
38          }
39      }
40  
41      public String toString() {
42          return "FileRepository(" + _root.toString() + ")";
43      }
44  
45  
46      public Group findGroup(String groupId) throws UnknownGroupIdException {
47          File groupFolder = new File(_root, groupId);
48          if (!groupFolder.exists()) {
49              throw new UnknownGroupIdException(groupId);
50          }
51  
52          List artifacts = loadArtifactFrom(groupFolder, null);
53  
54          return new FGroup(groupId,
55              new FArtifactList((Artifact[])artifacts.toArray(new Artifact[] {})));
56      }
57  
58  
59      public GroupList findGroupsByName(String pattern) {
60          return buildGroups(_root.listFiles(new PatternFilter(pattern)));
61      }
62  
63  
64      public ArtifactList findArtifactByName(String pattern) {
65          List result = loadAllArtifact(null);
66  
67          String lcPattern = pattern.toLowerCase();
68          for (Iterator iter = result.iterator(); iter.hasNext();) {
69              Artifact artifact = (Artifact)iter.next();
70              if (artifact.getId().toLowerCase().indexOf(lcPattern) == -1) {
71                  iter.remove();
72              }
73          }
74  
75          return new FArtifactList((Artifact[])result.toArray(new Artifact[] {}));
76      }
77  
78  
79      public ArtifactList findArtifactByClass(String fullClassName) {
80          List result = loadAllArtifact("jars");
81  
82          String classFile = toClassFile(fullClassName);
83  
84          for (Iterator iter = result.iterator(); iter.hasNext();) {
85              Artifact artifact = (Artifact)iter.next();
86              if (!containsFile(artifact, classFile)) {
87                  iter.remove();
88              }
89          }
90  
91          return new FArtifactList((Artifact[])result.toArray(new Artifact[] {}));
92      }
93  
94  
95      public GroupList findAllGroups() {
96          return buildGroups(_root.listFiles());
97      }
98  
99  
100     private List loadArtifactFrom(File groupFolder, String type) {
101         List artifacts = new ArrayList();
102         File[] typesFolder = groupFolder.listFiles(new TypeFolderFilter());
103         for (int i = 0; i < typesFolder.length; i++) {
104             if (type == null || type.equals(typesFolder[i].getName())) {
105                 addArtifact(typesFolder[i], artifacts);
106             }
107         }
108         return artifacts;
109     }
110 
111 
112     private GroupList buildGroups(File[] grpFolders) {
113         long start = System.currentTimeMillis();
114 
115         Group[] groups = new Group[grpFolders.length];
116 
117         for (int i = 0; i < grpFolders.length; i++) {
118             groups[i] = new FGroup(grpFolders[i].getName());
119         }
120 
121         long end = System.currentTimeMillis();
122 
123         LOG.debug("***********************************************");
124         LOG.debug("scanne de " + groups.length + " groupes");
125         LOG.debug("delay = " + (end - start) + " ms");
126         LOG.debug("***********************************************");
127 
128         return new FGroupList(groups);
129     }
130 
131 
132     private void addArtifact(File typeFolder, List artifactList) {
133         File[] artifactFile = typeFolder.listFiles(new FileFilterArtifact());
134         for (int i = 0; i < artifactFile.length; i++) {
135             File artifact = artifactFile[i];
136             try {
137                 artifactList.add(new FArtifact(artifact));
138             }
139             catch (BadArtifactFileException e) {
140                 LOG.error("Bad artifact file detected " + artifact, e);
141             }
142         }
143     }
144 
145 
146     private List loadAllArtifact(String type) {
147         List result = new ArrayList();
148 
149         File[] folders = _root.listFiles();
150 
151         for (int i = 0; i < folders.length; i++) {
152             File folder = folders[i];
153             result.addAll(loadArtifactFrom(folder, type));
154         }
155         return result;
156     }
157 
158 
159     private boolean containsFile(Artifact artifact, String fileName) {
160         try {
161             final File artifactFile =
162                 new File(_root, artifact.getGroupId() + "/jars/" + artifact.getFileName());
163             final JarFile jarFile = new JarFile(artifactFile);
164             try {
165                 return jarFile.getEntry(fileName) != null;
166             }
167             finally {
168                 jarFile.close();
169             }
170         }
171         catch (IOException e) {
172             return false;
173         }
174     }
175 
176 
177     private static String toClassFile(String fullClassName) {
178         return fullClassName.replace('.', '/') + ".class";
179     }
180 }