forked from eclipse-tycho/tycho
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
P2ApplicationLauncher.java
170 lines (137 loc) · 6.37 KB
/
P2ApplicationLauncher.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
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
163
164
165
166
167
168
169
170
/*******************************************************************************
* Copyright (c) 2008, 2011 Sonatype Inc. 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:
* Sonatype Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.sisu.equinox.launching.internal;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.cli.Commandline;
import org.eclipse.sisu.equinox.embedder.EquinoxRuntimeLocator;
import org.eclipse.sisu.equinox.embedder.EquinoxRuntimeLocator.EquinoxRuntimeDescription;
import org.eclipse.sisu.equinox.launching.BundleStartLevel;
import org.eclipse.sisu.equinox.launching.DefaultEquinoxInstallationDescription;
import org.eclipse.sisu.equinox.launching.EquinoxInstallation;
import org.eclipse.sisu.equinox.launching.EquinoxInstallationDescription;
import org.eclipse.sisu.equinox.launching.EquinoxInstallationFactory;
import org.eclipse.sisu.equinox.launching.EquinoxLauncher;
import org.eclipse.tycho.ArtifactKey;
import org.eclipse.tycho.PackagingType;
import org.eclipse.tycho.core.TychoProject;
import org.eclipse.tycho.core.osgitools.OsgiBundleProject;
/**
* Convenience wrapper around {@link Commandline} to run Eclipse applications from tycho-p2-runtime
*
* @author igor
*/
@Component(role = P2ApplicationLauncher.class, instantiationStrategy = "per-lookup")
public class P2ApplicationLauncher {
@Requirement
private Logger logger;
@Requirement
private EquinoxInstallationFactory installationFactory;
@Requirement
private EquinoxLauncher launcher;
@Requirement
private EquinoxRuntimeLocator runtimeLocator;
@Requirement(role = TychoProject.class, hint = PackagingType.TYPE_ECLIPSE_PLUGIN)
private OsgiBundleProject osgiBundle;
private File workingDirectory;
private String applicationName;
private final List<String> vmargs = new ArrayList<>();
private final List<String> args = new ArrayList<>();
public void setWorkingDirectory(File workingDirectory) {
this.workingDirectory = workingDirectory;
}
public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}
public void addArguments(String... args) {
this.args.addAll(Arrays.asList(args));
}
public void addVMArguments(String... vmargs) {
this.vmargs.addAll(Arrays.asList(vmargs));
}
public int execute(int forkedProcessTimeoutInSeconds) {
try {
File installationFolder = newTemporaryFolder();
try {
final EquinoxInstallationDescription description = new DefaultEquinoxInstallationDescription();
runtimeLocator.locateRuntime(new EquinoxRuntimeDescription() {
@Override
public void addPlatformProperty(String property, String value) {
description.addPlatformProperty(property, value);
}
@Override
public void addInstallation(File location) {
for (File file : new File(location, "plugins").listFiles()) {
P2ApplicationLauncher.this.addBundle(description, file);
}
}
@Override
public void addExtraSystemPackage(String systemPackages) {
}
@Override
public void addBundle(File location) {
P2ApplicationLauncher.this.addBundle(description, location);
}
@Override
public void addBundleStartLevel(String id, int level, boolean autostart) {
description.addBundleStartLevel(new BundleStartLevel(id, level, autostart));
}
}, true);
EquinoxInstallation installation = installationFactory.createInstallation(description,
installationFolder);
EquinoxLaunchConfiguration launchConfiguration = new EquinoxLaunchConfiguration(installation);
launchConfiguration.setWorkingDirectory(workingDirectory);
launchConfiguration.addProgramArguments("-configuration", installation.getConfigurationLocation()
.getAbsolutePath());
// logging
if (logger.isDebugEnabled()) {
launchConfiguration.addProgramArguments("-debug", "-consoleLog");
launchConfiguration.addProgramArguments("-console");
}
// application and application arguments
launchConfiguration.addProgramArguments("-nosplash", "-application", applicationName);
launchConfiguration.addProgramArguments(args.toArray(new String[args.size()]));
return launcher.execute(launchConfiguration, forkedProcessTimeoutInSeconds);
} finally {
try {
FileUtils.deleteDirectory(installationFolder);
} catch (IOException e) {
// this may happen if child process did not close all file handles
logger.warn("Failed to delete temp folder " + installationFolder);
}
}
} catch (Exception e) {
// TODO better exception?
throw new RuntimeException(e);
}
}
private void addBundle(EquinoxInstallationDescription description, File file) {
ArtifactKey key = osgiBundle.readArtifactKey(file);
if (key != null) {
description.addBundle(key, file);
}
}
private File newTemporaryFolder() throws IOException {
File tmp = File.createTempFile("tycho-p2-runtime", ".tmp");
tmp.delete();
tmp.mkdir(); // anyone got any better idea?
return tmp;
}
}