Skip to content

Commit f666121

Browse files
SheliakLyrljacomet
authored andcommitted
Added support for Zinc 1.2.x
Signed-off-by: Sheliak Lyr <[email protected]>
1 parent c73bbb7 commit f666121

File tree

2 files changed

+148
-95
lines changed

2 files changed

+148
-95
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.gradle.api.internal.tasks.scala;
18+
19+
import sbt.internal.inc.classpath.AbstractClassLoaderCache;
20+
import sbt.io.IO;
21+
import scala.Function0;
22+
import scala.collection.JavaConverters;
23+
24+
import java.io.File;
25+
import java.io.IOException;
26+
import java.net.URL;
27+
import java.net.URLClassLoader;
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
import java.util.Objects;
31+
import java.util.Set;
32+
import java.util.stream.Collectors;
33+
34+
/**
35+
* This class implements AbstractClassLoaderCache in a way that allows safe
36+
* resource release when entries are evicted.
37+
*
38+
* Cache is based on file timestamps, because this method is used in sbt implementation.
39+
*/
40+
class TimeCheckingClassLoaderCache implements AbstractClassLoaderCache {
41+
private final URLClassLoader commonParent;
42+
private final GuavaBackedClassLoaderCache<Set<TimestampedFile>> cache;
43+
44+
static class TimestampedFile {
45+
private final File file;
46+
private final long timestamp;
47+
48+
public TimestampedFile(File file) {
49+
this.file = file;
50+
this.timestamp = IO.getModifiedTimeOrZero(file);
51+
}
52+
53+
@Override
54+
public boolean equals(Object o) {
55+
if (this == o) {
56+
return true;
57+
}
58+
if (o == null || getClass() != o.getClass()) {
59+
return false;
60+
}
61+
TimestampedFile that = (TimestampedFile) o;
62+
return timestamp == that.timestamp &&
63+
Objects.equals(file, that.file);
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return Objects.hash(file, timestamp);
69+
}
70+
}
71+
72+
public TimeCheckingClassLoaderCache(int maxSize) {
73+
commonParent = new URLClassLoader(new URL[0]);
74+
cache = new GuavaBackedClassLoaderCache<>(maxSize);
75+
}
76+
77+
@Override
78+
public ClassLoader commonParent() {
79+
return commonParent;
80+
}
81+
82+
@Override
83+
public ClassLoader apply(scala.collection.immutable.List<File> files) {
84+
try {
85+
List<File> jFiles = JavaConverters.seqAsJavaList(files);
86+
return cache.get(getTimestampedFiles(jFiles), () -> {
87+
ArrayList<URL> urls = new ArrayList<>(jFiles.size());
88+
for (File f : jFiles) {
89+
urls.add(f.toURI().toURL());
90+
}
91+
return new URLClassLoader(urls.toArray(new URL[0]), commonParent);
92+
});
93+
} catch (Exception e) {
94+
throw new RuntimeException(e);
95+
}
96+
}
97+
98+
@Override
99+
public ClassLoader cachedCustomClassloader(scala.collection.immutable.List<File> files, Function0<ClassLoader> mkLoader) {
100+
try {
101+
return cache.get(getTimestampedFiles(JavaConverters.seqAsJavaList(files)), () -> mkLoader.apply());
102+
} catch (Exception e) {
103+
throw new RuntimeException(e);
104+
}
105+
}
106+
107+
private Set<TimestampedFile> getTimestampedFiles(List<File> fs) {
108+
return fs.stream().map(TimestampedFile::new).collect(Collectors.toSet());
109+
}
110+
111+
@Override
112+
public void close() throws IOException {
113+
cache.clear();
114+
commonParent.close();
115+
}
116+
}