1 /*******************************************************************************
2  * Copyright (c) 2007, 2017 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.equinox.internal.frameworkadmin.equinox;
15 
16 import java.io.File;
17 import java.io.IOException;
18 import java.util.LinkedList;
19 import java.util.List;
20 import org.eclipse.equinox.internal.frameworkadmin.utils.SimpleBundlesState;
21 import org.eclipse.equinox.internal.frameworkadmin.utils.Utils;
22 import org.eclipse.equinox.internal.provisional.frameworkadmin.*;
23 import org.osgi.service.log.LogService;
24 
25 public class EclipseLauncherImpl {
getStringOfCmd(String[] cmdarray)26 	static String getStringOfCmd(String[] cmdarray) {
27 		StringBuilder sb = new StringBuilder();
28 		for (String cmd : cmdarray) {
29 			sb.append(cmd);
30 			sb.append(" "); //$NON-NLS-1$
31 		}
32 		return sb.toString();
33 	}
34 
35 	EquinoxFwAdminImpl fwAdmin = null;
36 
EclipseLauncherImpl(EquinoxFwAdminImpl fwAdmin)37 	EclipseLauncherImpl(EquinoxFwAdminImpl fwAdmin) {
38 		this.fwAdmin = fwAdmin;
39 	}
40 
launch(Manipulator manipulator, File cwd)41 	public Process launch(Manipulator manipulator, File cwd)
42 			throws IllegalArgumentException, IOException, FrameworkAdminRuntimeException {
43 		SimpleBundlesState.checkAvailability(fwAdmin);
44 		Log.log(LogService.LOG_DEBUG, this, "launch(Manipulator , File )", ""); //$NON-NLS-1$ //$NON-NLS-2$
45 		LauncherData launcherData = manipulator.getLauncherData();
46 		if (launcherData.getLauncher() == null)
47 			return launchInMemory(manipulator, cwd);
48 		return launchByLauncher(manipulator, cwd);
49 	}
50 
launchByLauncher(Manipulator manipulator, File cwd)51 	private Process launchByLauncher(Manipulator manipulator, File cwd) throws IOException {
52 		LauncherData launcherData = manipulator.getLauncherData();
53 
54 		if (launcherData.getLauncher() == null)
55 			throw new IllegalStateException(Messages.exception_launcherLocationNotSet);
56 		String[] cmdarray = new String[] { launcherData.getLauncher().getAbsolutePath() };
57 		if (cwd == null)
58 			cwd = launcherData.getLauncher().getParentFile();
59 		Process process = Runtime.getRuntime().exec(cmdarray, null, cwd);
60 		Log.log(LogService.LOG_DEBUG, "\t" + getStringOfCmd(cmdarray)); //$NON-NLS-1$
61 		return process;
62 	}
63 
launchInMemory(Manipulator manipulator, File cwd)64 	private Process launchInMemory(Manipulator manipulator, File cwd) throws IOException {
65 		LauncherData launcherData = manipulator.getLauncherData();
66 		Utils.checkAbsoluteFile(launcherData.getFwJar(), "fwJar"); //$NON-NLS-1$
67 		Utils.checkAbsoluteDir(cwd, "cwd"); //$NON-NLS-1$
68 
69 		List<String> cmdList = new LinkedList<>();
70 		if (launcherData.getJvm() != null)
71 			cmdList.add(launcherData.getJvm().getAbsolutePath());
72 		else
73 			cmdList.add("java"); //$NON-NLS-1$
74 
75 		if (launcherData.getJvmArgs() != null)
76 			for (int i = 0; i < launcherData.getJvmArgs().length; i++)
77 				cmdList.add(launcherData.getJvmArgs()[i]);
78 
79 		cmdList.add("-jar"); //$NON-NLS-1$
80 		cmdList.add(Utils.getRelativePath(launcherData.getFwJar(), cwd));
81 
82 		EquinoxManipulatorImpl.checkConsistencyOfFwConfigLocAndFwPersistentDataLoc(launcherData);
83 		cmdList.add(EquinoxConstants.OPTION_CONFIGURATION);
84 		cmdList.add(Utils.getRelativePath(launcherData.getFwPersistentDataLocation(), cwd));
85 
86 		if (launcherData.isClean())
87 			cmdList.add(EquinoxConstants.OPTION_CLEAN);
88 
89 		String[] cmdarray = new String[cmdList.size()];
90 		cmdList.toArray(cmdarray);
91 		Log.log(LogService.LOG_DEBUG, "In CWD = " + cwd + "\n\t" + getStringOfCmd(cmdarray)); //$NON-NLS-1$ //$NON-NLS-2$
92 		Process process = Runtime.getRuntime().exec(cmdarray, null, cwd);
93 		return process;
94 	}
95 }
96