Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9d66c8de909fc6fe35c76960c5d8e6969d55f2ef (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
/*******************************************************************************
 * Copyright (c) 2012, 2016 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.osgi.container;

import java.util.ArrayList;
import java.util.List;
import org.osgi.framework.Bundle;
import org.osgi.framework.wiring.BundleRevision;
import org.osgi.framework.wiring.BundleRevisions;

/**
 * An implementation of {@link BundleRevisions} which represent a 
 * {@link Module} installed in a {@link ModuleContainer container}.
 * The ModuleRevisions provides a bridge between the revisions, the 
 * module and the container they are associated with.  The 
 * ModuleRevisions holds the information about the installation of
 * a module in a container such as the module id and location.
 * @since 3.10
 */
public final class ModuleRevisions implements BundleRevisions {
	private final Object monitor = new Object();
	private final Module module;
	private final ModuleContainer container;
	/* @GuardedBy("monitor") */
	private final List<ModuleRevision> revisions = new ArrayList<>(1);
	/* @GuardedBy("monitor") */
	private boolean uninstalled = false;
	/* @GuardedBy("monitor") */
	private ModuleRevision uninstalledCurrent;

	ModuleRevisions(Module module, ModuleContainer container) {
		this.module = module;
		this.container = container;
	}

	public Module getModule() {
		return module;
	}

	ModuleContainer getContainer() {
		return container;
	}

	@Override
	public Bundle getBundle() {
		return module.getBundle();
	}

	@Override
	public List<BundleRevision> getRevisions() {
		synchronized (monitor) {
			return new ArrayList<BundleRevision>(revisions);
		}
	}

	/**
	 * Same as {@link ModuleRevisions#getRevisions()} except it
	 * returns a list of {@link ModuleRevision}.
	 * @return the list of module revisions
	 */
	public List<ModuleRevision> getModuleRevisions() {
		synchronized (monitor) {
			return new ArrayList<>(revisions);
		}
	}

	/**
	 * Returns the current {@link ModuleRevision revision} associated with this revisions.
	 * 
	 * @return the current {@link ModuleRevision revision} associated with this revisions
	 *     or {@code null} if the current revision does not exist.
	 */
	ModuleRevision getCurrentRevision() {
		synchronized (monitor) {
			if (uninstalled) {
				return uninstalledCurrent;
			}
			if (revisions.isEmpty()) {
				return null;
			}
			return revisions.get(0);
		}
	}

	ModuleRevision addRevision(ModuleRevision revision) {
		synchronized (monitor) {
			revisions.add(0, revision);
		}
		return revision;
	}

	boolean removeRevision(ModuleRevision revision) {
		try {
			synchronized (monitor) {
				return revisions.remove(revision);
			}
		} finally {
			module.cleanup(revision);
		}
	}

	boolean isUninstalled() {
		synchronized (monitor) {
			return uninstalled;
		}
	}

	void uninstall() {
		synchronized (monitor) {
			uninstalled = true;
			// save off the current revision
			if (revisions.isEmpty()) {
				throw new IllegalStateException("Revisions is empty on uninstall!"); //$NON-NLS-1$
			}
			uninstalledCurrent = revisions.get(0);
		}
	}

	public String toString() {
		return "moduleID=" + module.getId(); //$NON-NLS-1$
	}
}

Back to the top