/
EclipseWorkspaceManager.java
92 lines (81 loc) · 3.3 KB
/
EclipseWorkspaceManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*******************************************************************************
* Copyright (c) 2023 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.osgi.framework;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.Mojo;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
/**
* The {@link EclipseWorkspaceManager} manages dedicated workspaces on a per thread basis using a
* key object
*/
@Component(role = EclipseWorkspaceManager.class)
public class EclipseWorkspaceManager implements Disposable {
private final Map<Thread, Map<Object, EclipseWorkspace<?>>> cache = new WeakHashMap<>();
private final List<EclipseWorkspace<?>> toclean = new ArrayList<>();
@Requirement
private Logger logger;
/**
* @param key
* the key to use
* @return a workspace directory that can be used by the current thread.
*/
@SuppressWarnings("unchecked")
public <T> EclipseWorkspace<T> getWorkspace(T key) {
Thread currentThread = Thread.currentThread();
synchronized (cache) {
return (EclipseWorkspace<T>) cache.computeIfAbsent(currentThread, t -> new ConcurrentHashMap<>())
.computeIfAbsent(key, x -> {
try {
EclipseWorkspace<T> workspace = new EclipseWorkspace<>(
Files.createTempDirectory("eclipseWorkspace"), key, currentThread);
toclean.add(workspace);
return workspace;
} catch (IOException e) {
throw new IllegalStateException("can't create a temporary directory for the workspace!", e);
}
});
}
}
@Override
public void dispose() {
cache.clear();
for (EclipseWorkspace<?> workspace : toclean) {
FileUtils.deleteQuietly(workspace.getWorkDir().toFile());
}
}
/**
* Get a workspace that is unique for the given uri, current thread and mojo and therefore safe
* to be used in a maven multithread execution
*
* @param uri
* @param mojo
* @return an {@link EclipseWorkspace}
*/
public EclipseWorkspace<?> getWorkspace(URI uri, Mojo mojo) {
return getWorkspace(new MojoKey(uri.normalize(), mojo.getClass().getName()));
}
private static final record MojoKey(URI uri, String mojoClassName) {
//a key that uses the mojo class and a URI
}
}