Skip to main content
summaryrefslogtreecommitdiffstats
blob: ad94733ed2c1b73f5ae07acaca6b7309f7ea1d03 (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
/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.artifact.repository;

import java.util.HashMap;
import java.util.Map;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
import org.osgi.framework.*;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

public class Activator implements BundleActivator, ServiceTrackerCustomizer {
	public static final String ID = "org.eclipse.equinox.p2.artifact.repository"; //$NON-NLS-1$
	public static final String REPO_PROVIDER_XPT = ID + '.' + "artifactRepositories"; //$NON-NLS-1$

	private static BundleContext context;
	private ServiceRegistration repositoryManagerRegistration;
	private static final Map<ArtifactRepositoryManager, IProvisioningAgent> createdManagers = new HashMap<ArtifactRepositoryManager, IProvisioningAgent>();
	private ServiceTracker agentTracker;

	public static BundleContext getContext() {
		return Activator.context;
	}

	/**
	 * Remember an artifact repository manager so we can shut it down when the bundle stops
	 */
	static void addManager(ArtifactRepositoryManager manager, IProvisioningAgent agent) {
		synchronized (createdManagers) {
			createdManagers.put(manager, agent);
		}
	}

	public void start(BundleContext aContext) throws Exception {
		Activator.context = aContext;
		// need to track agent so we can register global artifact repository manager
		String filter = "(&(objectClass=" + IProvisioningAgent.SERVICE_NAME + ")(agent.current=true))"; //$NON-NLS-1$ //$NON-NLS-2$
		agentTracker = new ServiceTracker(context, aContext.createFilter(filter), this);
		agentTracker.open();
	}

	public void stop(BundleContext aContext) throws Exception {
		Activator.context = null;
		if (repositoryManagerRegistration != null)
			repositoryManagerRegistration.unregister();
		repositoryManagerRegistration = null;
		synchronized (createdManagers) {
			for (ArtifactRepositoryManager manager : createdManagers.keySet()) {
				manager.shutdown();
				IProvisioningAgent agent = createdManagers.get(manager);
				agent.unregisterService(IArtifactRepositoryManager.SERVICE_NAME, manager);
			}
			createdManagers.clear();
		}
		agentTracker.close();
	}

	public Object addingService(ServiceReference reference) {
		//when someone registers the agent service, register a repository manager service
		IProvisioningAgent agent = (IProvisioningAgent) context.getService(reference);
		repositoryManagerRegistration = context.registerService(IArtifactRepositoryManager.SERVICE_NAME, agent.getService(IArtifactRepositoryManager.SERVICE_NAME), null);
		return agent;
	}

	public void modifiedService(ServiceReference reference, Object service) {
		// ignored
	}

	public void removedService(ServiceReference reference, Object service) {
		//the agent is going away so withdraw our service
		if (repositoryManagerRegistration != null) {
			repositoryManagerRegistration.unregister();
			repositoryManagerRegistration = null;
		}
	}
}

Back to the top