Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5181955e02d32d70a9b458cdb7e4086e3e88c814 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*******************************************************************************
 * Copyright (c) 2009, 2017 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
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.core;

import java.net.URI;
import java.util.*;
import org.eclipse.equinox.p2.core.IAgentLocation;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.core.spi.IAgentService;
import org.eclipse.equinox.p2.core.spi.IAgentServiceFactory;
import org.osgi.framework.*;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

/**
 * Represents a p2 agent instance.
 */
public class ProvisioningAgent implements IProvisioningAgent, ServiceTrackerCustomizer<IAgentServiceFactory, Object> {

	private final Map<String, Object> agentServices = Collections.synchronizedMap(new HashMap<String, Object>());
	private BundleContext context;
	private volatile boolean stopped = false;
	private ServiceRegistration<IProvisioningAgent> reg;
	private final Map<ServiceReference<IAgentServiceFactory>, ServiceTracker<IAgentServiceFactory, Object>> trackers = Collections.synchronizedMap(new HashMap<ServiceReference<IAgentServiceFactory>, ServiceTracker<IAgentServiceFactory, Object>>());

	/**
	 * Instantiates a provisioning agent.
	 */
	public ProvisioningAgent() {
		super();
		registerService(IProvisioningAgent.INSTALLER_AGENT, this);
		registerService(IProvisioningAgent.INSTALLER_PROFILEID, "_SELF_"); //$NON-NLS-1$
	}

	@Override
	public Object getService(String serviceName) {
		//synchronize so concurrent gets always obtain the same service
		synchronized (agentServices) {
			checkRunning();
			Object service = agentServices.get(serviceName);
			if (service != null)
				return service;
			//attempt to get factory service from service registry
			Collection<ServiceReference<IAgentServiceFactory>> refs;
			try {
				refs = context.getServiceReferences(IAgentServiceFactory.class, "(" + IAgentServiceFactory.PROP_CREATED_SERVICE_NAME + '=' + serviceName + ')'); //$NON-NLS-1$
			} catch (InvalidSyntaxException e) {
				e.printStackTrace();
				return null;
			}
			if (refs == null || refs.isEmpty())
				return null;
			ServiceReference<IAgentServiceFactory> firstRef = Collections.max(refs);
			//track the factory so that we can automatically remove the service when the factory goes away
			ServiceTracker<IAgentServiceFactory, Object> tracker = new ServiceTracker<>(context, firstRef, this);
			tracker.open();
			IAgentServiceFactory factory = (IAgentServiceFactory) tracker.getService();
			if (factory == null) {
				tracker.close();
				return null;
			}
			service = factory.createService(this);
			if (service == null) {
				tracker.close();
				return null;
			}
			registerService(serviceName, service);
			trackers.put(firstRef, tracker);
			return service;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getService(Class<T> key) {
		return (T) getService(key.getSimpleName());
	}

	private void checkRunning() {
		if (stopped)
			throw new IllegalStateException("Attempt to access stopped agent: " + this); //$NON-NLS-1$
	}

	@Override
	public void registerService(String serviceName, Object service) {
		checkRunning();
		agentServices.put(serviceName, service);
		if (service instanceof IAgentService)
			((IAgentService) service).start();
	}

	public void setBundleContext(BundleContext context) {
		this.context = context;
	}

	public void setLocation(URI location) {
		//treat a null location as using the currently running platform
		IAgentLocation agentLocation = null;
		if (location == null) {
			ServiceReference<IAgentLocation> ref = context.getServiceReference(IAgentLocation.class);
			if (ref != null) {
				agentLocation = context.getService(ref);
				context.ungetService(ref);
			}
		} else {
			agentLocation = new AgentLocation(location);
		}
		registerService(IAgentLocation.SERVICE_NAME, agentLocation);
	}

	@Override
	public void unregisterService(String serviceName, Object service) {
		synchronized (agentServices) {
			if (stopped)
				return;
			if (agentServices.get(serviceName) == service)
				agentServices.remove(serviceName);
		}
		if (service instanceof IAgentService)
			((IAgentService) service).stop();
	}

	@Override
	public void stop() {
		List<Object> toStop;
		synchronized (agentServices) {
			toStop = new ArrayList<>(agentServices.values());
		}
		//give services a chance to do their own shutdown
		for (Object service : toStop) {
			if (service instanceof IAgentService)
				if (service != this)
					((IAgentService) service).stop();
		}
		stopped = true;
		//close all service trackers
		synchronized (trackers) {
			for (ServiceTracker<IAgentServiceFactory, Object> t : trackers.values())
				t.close();
			trackers.clear();
		}
		if (reg != null) {
			reg.unregister();
			reg = null;
		}
	}

	public void setServiceRegistration(ServiceRegistration<IProvisioningAgent> reg) {
		this.reg = reg;
	}

	@Override
	public Object addingService(ServiceReference<IAgentServiceFactory> reference) {
		if (stopped)
			return null;
		return context.getService(reference);
	}

	@Override
	public void modifiedService(ServiceReference<IAgentServiceFactory> reference, Object service) {
		//nothing to do
	}

	@Override
	public void removedService(ServiceReference<IAgentServiceFactory> reference, Object factoryService) {
		if (stopped)
			return;
		String serviceName = (String) reference.getProperty(IAgentServiceFactory.PROP_CREATED_SERVICE_NAME);
		if (serviceName == null)
			return;
		Object registered = agentServices.get(serviceName);
		if (registered == null)
			return;
		if (FrameworkUtil.getBundle(registered.getClass()) == FrameworkUtil.getBundle(factoryService.getClass())) {
			//the service we are holding is going away
			unregisterService(serviceName, registered);
			ServiceTracker<IAgentServiceFactory, Object> toRemove = trackers.remove(reference);
			if (toRemove != null)
				toRemove.close();
		}
	}

}

Back to the top