Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c7e342f28b1f163af68adc1a3fb201ab83e54c78 (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
/*******************************************************************************
 * Copyright (c) 2007, 2012 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *     Pascal Rapicault - Support for bundled macosx http://bugs.eclipse.org/57349
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.touchpoint.eclipse;

import java.io.IOException;
import org.eclipse.equinox.frameworkadmin.BundleInfo;
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
import org.eclipse.equinox.internal.provisional.frameworkadmin.*;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.engine.IProfile;
import org.osgi.framework.*;
import org.osgi.util.tracker.ServiceTracker;

public class LazyManipulator implements Manipulator {

	private final static String FILTER_OBJECTCLASS = "(" + Constants.OBJECTCLASS + '=' + FrameworkAdmin.class.getName() //$NON-NLS-1$
			+ ')';
	private final static String filterFwName = "(" + FrameworkAdmin.SERVICE_PROP_KEY_FW_NAME + "=Equinox)"; //$NON-NLS-1$ //$NON-NLS-2$
	private final static String filterLauncherName = "(" + FrameworkAdmin.SERVICE_PROP_KEY_LAUNCHER_NAME //$NON-NLS-1$
			+ "=Eclipse.exe)"; //$NON-NLS-1$
	private final static String filterFwAdmin = "(&" + FILTER_OBJECTCLASS + filterFwName + filterLauncherName + ')'; //$NON-NLS-1$ ;

	private Manipulator manipulator;
	private final IProfile profile;
	private final IProvisioningAgent agent;

	public LazyManipulator(IProvisioningAgent agent, IProfile profile) {
		this.profile = profile;
		this.agent = agent;
	}

	private void loadDelegate() {
		if (manipulator != null)
			return;

		manipulator = getFrameworkManipulator();
		if (manipulator == null)
			throw new IllegalStateException(Messages.failed_acquire_framework_manipulator);

		LauncherData launcherData = manipulator.getLauncherData();
		launcherData.setFwConfigLocation(Util.getConfigurationFolder(profile));
		launcherData.setLauncher(Util.getLauncherPath(profile));
		launcherData.setLauncherConfigLocation(Util.getLauncherConfigLocation(profile));
		launcherData.setOS(Util.getOSFromProfile(profile));
		launcherData.setHome(Util.getInstallFolder(profile));

		try {
			manipulator.load();
		} catch (IllegalStateException e) {
			// if fwJar is not included, this exception will be thrown. But ignore it.
			LogHelper.log(Util.createError(Messages.error_loading_manipulator, e));
			throw new IllegalStateException(Messages.error_loading_manipulator);
		} catch (FrameworkAdminRuntimeException e) {
			LogHelper.log(Util.createError(Messages.error_loading_manipulator, e));
		} catch (IOException e) {
			LogHelper.log(Util.createError(Messages.error_loading_manipulator, e));
			throw new IllegalStateException(Messages.error_loading_manipulator);
		}
		// TODO These values should be inserted by a configuration unit (bug 204124)
		manipulator.getConfigData().setProperty("eclipse.p2.profile", profile.getProfileId()); //$NON-NLS-1$
		manipulator.getConfigData().setProperty("eclipse.p2.data.area", //$NON-NLS-1$
				Util.getAgentLocation(agent).getRootLocation().toString());
	}

	public static FrameworkAdmin getFrameworkAdmin() {
		ServiceTracker<FrameworkAdmin, FrameworkAdmin> fwAdminTracker = null;
		try {
			Filter filter = Activator.getContext().createFilter(filterFwAdmin);
			fwAdminTracker = new ServiceTracker<>(Activator.getContext(), filter, null);
			fwAdminTracker.open();
			FrameworkAdmin fwAdmin = fwAdminTracker.getService();
			return fwAdmin;
		} catch (InvalidSyntaxException e) {
			// Can't happen we are writing the filter ourselves
			return null;
		} finally {
			if (fwAdminTracker != null)
				fwAdminTracker.close();
		}
	}

	private static Manipulator getFrameworkManipulator() {
		FrameworkAdmin fwAdmin = getFrameworkAdmin();
		if (fwAdmin != null)
			return fwAdmin.getManipulator();
		return null;
	}

	@Override
	public void save(boolean backup) throws IOException, FrameworkAdminRuntimeException {
		if (manipulator != null)
			manipulator.save(backup);
	}

	// DELEGATE METHODS

	@Override
	public BundlesState getBundlesState() throws FrameworkAdminRuntimeException {
		loadDelegate();
		return manipulator.getBundlesState();
	}

	@Override
	public ConfigData getConfigData() throws FrameworkAdminRuntimeException {
		loadDelegate();
		return manipulator.getConfigData();
	}

	@Override
	public BundleInfo[] getExpectedState() throws IllegalStateException, IOException, FrameworkAdminRuntimeException {
		loadDelegate();
		return manipulator.getExpectedState();
	}

	@Override
	public LauncherData getLauncherData() throws FrameworkAdminRuntimeException {
		loadDelegate();
		return manipulator.getLauncherData();
	}

	@Override
	public long getTimeStamp() {
		loadDelegate();
		return manipulator.getTimeStamp();
	}

	@Override
	public void initialize() {
		loadDelegate();
		manipulator.initialize();
	}

	@Override
	public void load() throws IllegalStateException, FrameworkAdminRuntimeException {
		loadDelegate();
	}

	@Override
	public void setConfigData(ConfigData configData) {
		loadDelegate();
		manipulator.setConfigData(configData);
	}

	@Override
	public void setLauncherData(LauncherData launcherData) {
		loadDelegate();
		manipulator.setLauncherData(launcherData);
	}
}

Back to the top