Skip to main content
summaryrefslogtreecommitdiffstats
blob: c39e314d7da85afda742ca5bed3552013517c2b2 (plain) (blame)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*******************************************************************************
 * Copyright (c) 2017 Red Hat Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat - Initial Contribution
 *******************************************************************************/
package org.eclipse.linuxtools.jdt.docker.launcher;

import java.io.File;
import java.net.URL;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.IVMInstallType;
import org.eclipse.jdt.launching.IVMRunner;
import org.eclipse.jdt.launching.LibraryLocation;
import org.eclipse.linuxtools.docker.core.DockerConnectionManager;
import org.eclipse.linuxtools.docker.core.IDockerImage;
import org.eclipse.linuxtools.internal.docker.core.DockerConnection;

public class ContainerVMInstall implements IVMInstall {

	private ILaunchConfiguration config;
	private String name;
	private File installLocation;
	private IDockerImage image;
	private int port;

	public ContainerVMInstall (ILaunchConfiguration cfg, IDockerImage img, int port) {
		this.config = cfg;
		this.image = img;
		this.port = port;
	}

	@Override
	public IVMRunner getVMRunner(String mode) {
		// We hard-coded ContainerVMRunner
		return null;
	}

	@Override
	public String getId() {
		return image.id();
	}

	@Override
	public String getName() {
		if (name == null) {
			DockerConnection conn = getConnection();
			ImageQuery q = new ImageQuery(conn, image.id());
			name = q.getDefaultJVMName();
			q.destroy();
		}
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public File getInstallLocation() {
		if (installLocation == null) {
			DockerConnection conn = getConnection();
			ImageQuery q = new ImageQuery(conn, image.id());
			installLocation = q.getDefaultJVMInstallLocation();
			q.destroy();
		}
		return installLocation;
	}

	@Override
	public void setInstallLocation(File installLocation) {
		this.installLocation = installLocation;
	}

	@Override
	public IVMInstallType getVMInstallType() {
		return null;
	}

	@Override
	public LibraryLocation[] getLibraryLocations() {
		return null;
	}

	@Override
	public void setLibraryLocations(LibraryLocation[] locations) {
	}

	@Override
	public void setJavadocLocation(URL url) {
	}

	@Override
	public URL getJavadocLocation() {
		return null;
	}

	@Override
	public String[] getVMArguments() {
		return null;
	}

	@Override
	public void setVMArguments(String[] vmArgs) {
	}

	// org.eclipse.jdt.internal.launching.StandardVM#getJavaExecutable()
	public File getJavaExecutable() {
		File installLocation = getInstallLocation();
        if (installLocation != null) {
            return findJavaExecutable(installLocation);
        }
        return null;
	}

	public int getPort() {
		return port;
	}

	// org.eclipse.jdt.internal.launching.StandardVMType#findJavaExecutable(File)
	public File findJavaExecutable(File vmInstallLocation) {
		final String JRE = "jre"; //$NON-NLS-1$
		final String[] fgCandidateJavaFiles = {"javaw", "javaw.exe", "java", "java.exe", "j9w", "j9w.exe", "j9", "j9.exe"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
		final String[] fgCandidateJavaLocations = {"bin" + UnixFile.separatorChar, JRE + UnixFile.separatorChar + "bin" + UnixFile.separatorChar}; //$NON-NLS-1$ //$NON-NLS-2$

		DockerConnection conn = getConnection();
		ImageQuery q = new ImageQuery(conn, image.id());

		// Try each candidate in order.  The first one found wins.  Thus, the order
		// of fgCandidateJavaLocations and fgCandidateJavaFiles is significant.
		for (int i = 0; i < fgCandidateJavaFiles.length; i++) {
			for (int j = 0; j < fgCandidateJavaLocations.length; j++) {
				File javaFile = new UnixFile(vmInstallLocation, fgCandidateJavaLocations[j] + fgCandidateJavaFiles[i]);
				if (q.isFile(javaFile)) {
					q.destroy();
					return javaFile;
				}
			}
		}
		q.destroy();
		return null;
	}

	public DockerConnection getConnection () {
		String connectionURI;
		try {
			connectionURI = config.getAttribute(JavaLaunchConfigurationConstants.CONNECTION_URI, (String) null);
			return (DockerConnection) DockerConnectionManager.getInstance().getConnectionByUri(connectionURI);
		} catch (CoreException e) {
		}
		return null;
	}

}

Back to the top