1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package de.smartics.properties.resource.filesystem.heap; |
17 |
|
|
18 |
|
import java.io.File; |
19 |
|
import java.io.IOException; |
20 |
|
import java.net.MalformedURLException; |
21 |
|
import java.net.URL; |
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Iterator; |
24 |
|
import java.util.List; |
25 |
|
|
26 |
|
import org.apache.commons.io.FileUtils; |
27 |
|
import org.slf4j.Logger; |
28 |
|
import org.slf4j.LoggerFactory; |
29 |
|
|
30 |
|
import de.smartics.properties.resource.domain.ArtifactId; |
31 |
|
import de.smartics.properties.resource.domain.ArtifactRef; |
32 |
|
import de.smartics.properties.resource.domain.ClassPathEnvironment; |
33 |
|
import de.smartics.properties.resource.heap.HeapException; |
34 |
|
import de.smartics.properties.resource.heap.ResourceHeap; |
35 |
|
import de.smartics.util.lang.Arg; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
7 |
public final class FileSystemResourceHeap implements ResourceHeap |
41 |
|
{ |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
1 |
private static final Logger LOG = LoggerFactory |
50 |
|
.getLogger(FileSystemResourceHeap.class); |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
7 |
private final List<File> rootFolders = new ArrayList<File>(); |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
7 |
private ArtifactId defaultArtifactId = new ArtifactId.Builder() |
63 |
|
.withGroupId("unknown-group").withName("unknown") |
64 |
|
.withVersion("unknown-version").withArchiveType("properties").build(); |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
public ArtifactId getDefaultArtifactId() |
84 |
|
{ |
85 |
0 |
return defaultArtifactId; |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
public void setDefaultArtifactId(final ArtifactId defaultArtifactId) |
95 |
|
{ |
96 |
0 |
this.defaultArtifactId = defaultArtifactId; |
97 |
0 |
} |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
public void addRootFolder(final File rootFolder) throws NullPointerException, |
110 |
|
IllegalArgumentException |
111 |
|
{ |
112 |
3 |
Arg.checkNotNull("rootFolder", rootFolder); |
113 |
|
|
114 |
3 |
if (!rootFolder.isDirectory()) |
115 |
|
{ |
116 |
0 |
throw new IllegalArgumentException(String.format("'%s' is not a folder.", |
117 |
|
rootFolder.getAbsolutePath())); |
118 |
|
} |
119 |
|
|
120 |
3 |
rootFolders.add(rootFolder); |
121 |
3 |
} |
122 |
|
|
123 |
|
@Override |
124 |
|
@SuppressWarnings("unchecked") |
125 |
|
public ClassPathEnvironment resolve() throws HeapException |
126 |
|
{ |
127 |
4 |
final ClassPathEnvironment env = new ClassPathEnvironment(); |
128 |
4 |
final String[] extensions = |
129 |
|
new String[] { "jar", "ear", "war", "sar", "properties" }; |
130 |
|
|
131 |
4 |
for (final File rootFolder : rootFolders) |
132 |
|
{ |
133 |
3 |
boolean containsPropertiesFile = false; |
134 |
3 |
for (final Iterator<File> files = |
135 |
5 |
FileUtils.iterateFiles(rootFolder, extensions, true); files.hasNext();) |
136 |
|
{ |
137 |
2 |
final File file = files.next(); |
138 |
|
|
139 |
2 |
if (!isPropertiesFile(file)) |
140 |
|
{ |
141 |
1 |
final ArtifactRef archiveArtifactRef = createArchiveArtifactRef(file); |
142 |
1 |
if (archiveArtifactRef != null) |
143 |
|
{ |
144 |
1 |
env.addArchiveArtifactRef(archiveArtifactRef); |
145 |
|
} |
146 |
1 |
} |
147 |
1 |
else if (!containsPropertiesFile) |
148 |
|
{ |
149 |
1 |
containsPropertiesFile = true; |
150 |
1 |
final ArtifactRef rootRef = createFolderRef(rootFolder); |
151 |
1 |
env.addAnyArtifactRef(rootRef); |
152 |
|
} |
153 |
2 |
} |
154 |
3 |
} |
155 |
|
|
156 |
4 |
return env; |
157 |
|
} |
158 |
|
|
159 |
|
private ArtifactRef createFolderRef(final File file) |
160 |
|
{ |
161 |
|
try |
162 |
|
{ |
163 |
1 |
return new ArtifactRef(defaultArtifactId, file.toURI().toURL()); |
164 |
|
} |
165 |
0 |
catch (final MalformedURLException e) |
166 |
|
{ |
167 |
0 |
LOG.debug("Cannot transform file '{}' to an URL: " |
168 |
|
+ e.getLocalizedMessage()); |
169 |
0 |
return null; |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
|
private boolean isPropertiesFile(final File file) |
174 |
|
{ |
175 |
2 |
final String name = file.getName(); |
176 |
2 |
final boolean isPropertiesFile = name.endsWith(".properties"); |
177 |
2 |
return isPropertiesFile; |
178 |
|
} |
179 |
|
|
180 |
|
private static ArtifactRef createArchiveArtifactRef(final File archiveFile) |
181 |
|
throws HeapException |
182 |
|
{ |
183 |
|
try |
184 |
|
{ |
185 |
1 |
final ArtifactId id = readArtifactId(archiveFile); |
186 |
1 |
final URL url = archiveFile.toURI().toURL(); |
187 |
1 |
if (id != null) |
188 |
|
{ |
189 |
1 |
final ArtifactRef ref = new ArtifactRef(id, url); |
190 |
1 |
return ref; |
191 |
|
} |
192 |
|
} |
193 |
0 |
catch (final MalformedURLException e) |
194 |
|
{ |
195 |
0 |
LOG.debug("Cannot transform file '{}' to an URL: " |
196 |
|
+ e.getLocalizedMessage()); |
197 |
|
} |
198 |
0 |
catch (final IOException e) |
199 |
|
{ |
200 |
0 |
throw new HeapException(new FileMessageBean( |
201 |
|
FileSystemResourceCode.FAILED_TO_READ_ARCHIVE_FILE, e, archiveFile)); |
202 |
0 |
} |
203 |
0 |
return null; |
204 |
|
} |
205 |
|
|
206 |
|
private static ArtifactId readArtifactId(final File archiveFile) |
207 |
|
throws NullPointerException, IOException |
208 |
|
{ |
209 |
1 |
final ArtifactIdGrabber grabber = ArtifactIdGrabber.createLazy(archiveFile); |
210 |
1 |
if (grabber.isDefinitionArchive()) |
211 |
|
{ |
212 |
1 |
final ArtifactId id = grabber.getArtifactId(); |
213 |
1 |
return id; |
214 |
|
} |
215 |
0 |
return null; |
216 |
|
} |
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
} |