|
7 | 7 | * |
8 | 8 | * SPDX-License-Identifier: EPL-2.0 |
9 | 9 | ********************************************************************************/ |
| 10 | + |
10 | 11 | package org.eclipse.m2e.jdt; |
11 | 12 |
|
12 | 13 | import java.io.File; |
|
17 | 18 | import java.util.Optional; |
18 | 19 | import java.util.stream.Stream; |
19 | 20 |
|
20 | | -import org.apache.maven.cli.MavenCli; |
21 | | -import org.apache.maven.toolchain.io.DefaultToolchainsReader; |
22 | | -import org.apache.maven.toolchain.io.ToolchainsReader; |
23 | | -import org.apache.maven.toolchain.java.JavaToolchainImpl; |
24 | | -import org.apache.maven.toolchain.model.PersistedToolchains; |
25 | | -import org.apache.maven.toolchain.model.ToolchainModel; |
26 | | -import org.codehaus.plexus.util.xml.Xpp3Dom; |
27 | 21 | import org.eclipse.core.runtime.IProgressMonitor; |
28 | 22 | import org.eclipse.core.runtime.IStatus; |
29 | 23 | import org.eclipse.core.runtime.Status; |
|
35 | 29 | import org.eclipse.jdt.launching.JavaRuntime; |
36 | 30 | import org.eclipse.jdt.launching.VMStandin; |
37 | 31 |
|
| 32 | +import org.codehaus.plexus.util.xml.Xpp3Dom; |
| 33 | + |
| 34 | +import org.apache.maven.toolchain.io.DefaultToolchainsReader; |
| 35 | +import org.apache.maven.toolchain.io.ToolchainsReader; |
| 36 | +import org.apache.maven.toolchain.java.JavaToolchainImpl; |
| 37 | +import org.apache.maven.toolchain.model.PersistedToolchains; |
| 38 | +import org.apache.maven.toolchain.model.ToolchainModel; |
| 39 | + |
| 40 | +import org.eclipse.m2e.core.internal.embedder.MavenCLICompat; |
| 41 | + |
| 42 | + |
38 | 43 | public class LookupJDKToolchainsJob extends Job { |
39 | 44 |
|
40 | | - private final IVMInstallType standardType; |
| 45 | + private final IVMInstallType standardType; |
41 | 46 |
|
42 | | - public LookupJDKToolchainsJob() { |
43 | | - super(LookupJDKToolchainsJob.class.getSimpleName()); |
44 | | - this.standardType = JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE); |
45 | | - } |
| 47 | + public LookupJDKToolchainsJob() { |
| 48 | + super(LookupJDKToolchainsJob.class.getSimpleName()); |
| 49 | + this.standardType = JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE); |
| 50 | + } |
46 | 51 |
|
47 | | - @Override |
48 | | - protected IStatus run(IProgressMonitor monitor) { |
49 | | - List<File> toolchainFiles = List.of(MavenCli.DEFAULT_GLOBAL_TOOLCHAINS_FILE, MavenCli.DEFAULT_USER_TOOLCHAINS_FILE); |
50 | | - ToolchainsReader reader = new DefaultToolchainsReader(); |
51 | | - for (File toolchainsFile : toolchainFiles) { |
52 | | - if (toolchainsFile.isFile() && toolchainsFile.canRead()) { |
53 | | - try { |
54 | | - PersistedToolchains toolchains = reader.read(toolchainsFile, null); |
55 | | - for (ToolchainModel toolchain : toolchains.getToolchains()) { |
56 | | - if (monitor.isCanceled()) { |
57 | | - return Status.CANCEL_STATUS; |
58 | | - } |
59 | | - addToolchain(toolchain); |
60 | | - } |
61 | | - } catch(Exception e) { |
62 | | - return Status.error(e.getMessage(), e); |
63 | | - } |
64 | | - } |
65 | | - } |
66 | | - return Status.OK_STATUS; |
67 | | - } |
| 52 | + @Override |
| 53 | + protected IStatus run(IProgressMonitor monitor) { |
| 54 | + List<File> toolchainFiles = List.of(MavenCLICompat.DEFAULT_GLOBAL_TOOLCHAINS_FILE, |
| 55 | + MavenCLICompat.DEFAULT_USER_TOOLCHAINS_FILE); |
| 56 | + ToolchainsReader reader = new DefaultToolchainsReader(); |
| 57 | + for(File toolchainsFile : toolchainFiles) { |
| 58 | + if(toolchainsFile.isFile() && toolchainsFile.canRead()) { |
| 59 | + try { |
| 60 | + PersistedToolchains toolchains = reader.read(toolchainsFile, null); |
| 61 | + for(ToolchainModel toolchain : toolchains.getToolchains()) { |
| 62 | + if(monitor.isCanceled()) { |
| 63 | + return Status.CANCEL_STATUS; |
| 64 | + } |
| 65 | + addToolchain(toolchain); |
| 66 | + } |
| 67 | + } catch(Exception e) { |
| 68 | + return Status.error(e.getMessage(), e); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + return Status.OK_STATUS; |
| 73 | + } |
68 | 74 |
|
69 | | - private Optional<File> getVMInstallation(ToolchainModel toolchain) { |
70 | | - return Optional.ofNullable(toolchain) |
71 | | - .filter(t -> "jdk".equals(t.getType())) |
72 | | - .map(ToolchainModel::getConfiguration) |
73 | | - .filter(Xpp3Dom.class::isInstance) |
74 | | - .map(Xpp3Dom.class::cast) |
75 | | - .map(dom -> dom.getChild(JavaToolchainImpl.KEY_JAVAHOME)) |
76 | | - .map(Xpp3Dom::getValue) |
77 | | - .map(File::new) |
78 | | - .filter(File::isDirectory); |
79 | | - } |
| 75 | + private Optional<File> getVMInstallation(ToolchainModel toolchain) { |
| 76 | + return Optional.ofNullable(toolchain).filter(t -> "jdk".equals(t.getType())).map(ToolchainModel::getConfiguration) |
| 77 | + .filter(Xpp3Dom.class::isInstance).map(Xpp3Dom.class::cast) |
| 78 | + .map(dom -> dom.getChild(JavaToolchainImpl.KEY_JAVAHOME)).map(Xpp3Dom::getValue).map(File::new) |
| 79 | + .filter(File::isDirectory); |
| 80 | + } |
80 | 81 |
|
81 | | - private void addToolchain(ToolchainModel toolchain) { |
82 | | - getVMInstallation(toolchain) |
83 | | - .filter(f -> standardType.validateInstallLocation(f).isOK()) |
84 | | - .ifPresent(candidate -> { |
85 | | - if (Arrays.stream(standardType.getVMInstalls()) // |
86 | | - .map(IVMInstall::getInstallLocation) // |
87 | | - .filter(Objects::nonNull) |
88 | | - .noneMatch(install -> isSameCanonicalFile(candidate, install))) { |
89 | | - VMStandin workingCopy = new VMStandin(standardType, candidate.getAbsolutePath()); |
90 | | - workingCopy.setInstallLocation(candidate); |
91 | | - String name = candidate.getName(); |
92 | | - int i = 1; |
93 | | - while (isDuplicateName(name)) { |
94 | | - name = candidate.getName() + '(' + i++ + ')'; |
95 | | - } |
96 | | - workingCopy.setName(name); |
97 | | - IVMInstall newVM = workingCopy.convertToRealVM(); |
98 | | - // next lines workaround https://github.com/eclipse-jdt/eclipse.jdt.debug/issues/248 |
99 | | - if (!(newVM instanceof IVMInstall2 newVM2 && newVM2.getJavaVersion() != null)) { |
100 | | - standardType.disposeVMInstall(newVM.getId()); |
101 | | - } |
102 | | - } |
103 | | - }); |
104 | | - } |
| 82 | + private void addToolchain(ToolchainModel toolchain) { |
| 83 | + getVMInstallation(toolchain).filter(f -> standardType.validateInstallLocation(f).isOK()).ifPresent(candidate -> { |
| 84 | + if(Arrays.stream(standardType.getVMInstalls()) // |
| 85 | + .map(IVMInstall::getInstallLocation) // |
| 86 | + .filter(Objects::nonNull).noneMatch(install -> isSameCanonicalFile(candidate, install))) { |
| 87 | + VMStandin workingCopy = new VMStandin(standardType, candidate.getAbsolutePath()); |
| 88 | + workingCopy.setInstallLocation(candidate); |
| 89 | + String name = candidate.getName(); |
| 90 | + int i = 1; |
| 91 | + while(isDuplicateName(name)) { |
| 92 | + name = candidate.getName() + '(' + i++ + ')'; |
| 93 | + } |
| 94 | + workingCopy.setName(name); |
| 95 | + IVMInstall newVM = workingCopy.convertToRealVM(); |
| 96 | + // next lines workaround https://github.com/eclipse-jdt/eclipse.jdt.debug/issues/248 |
| 97 | + if(!(newVM instanceof IVMInstall2 newVM2 && newVM2.getJavaVersion() != null)) { |
| 98 | + standardType.disposeVMInstall(newVM.getId()); |
| 99 | + } |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
105 | 103 |
|
106 | | - private static boolean isDuplicateName(String name) { |
107 | | - return Stream.of(JavaRuntime.getVMInstallTypes()) // |
108 | | - .flatMap(vmType -> Arrays.stream(vmType.getVMInstalls())) // |
109 | | - .map(IVMInstall::getName) // |
110 | | - .anyMatch(name::equals); |
111 | | - } |
| 104 | + private static boolean isDuplicateName(String name) { |
| 105 | + return Stream.of(JavaRuntime.getVMInstallTypes()) // |
| 106 | + .flatMap(vmType -> Arrays.stream(vmType.getVMInstalls())) // |
| 107 | + .map(IVMInstall::getName) // |
| 108 | + .anyMatch(name::equals); |
| 109 | + } |
112 | 110 |
|
113 | | - private static boolean isSameCanonicalFile(File f1, File f2) { |
114 | | - try { |
115 | | - return Objects.equals(f1.getCanonicalFile(), f2.getCanonicalFile()); |
116 | | - } catch (IOException ex) { |
117 | | - return false; |
118 | | - } |
119 | | - } |
| 111 | + private static boolean isSameCanonicalFile(File f1, File f2) { |
| 112 | + try { |
| 113 | + return Objects.equals(f1.getCanonicalFile(), f2.getCanonicalFile()); |
| 114 | + } catch(IOException ex) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + } |
120 | 118 | } |
0 commit comments