Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 23f48af65408782efd1cd06efc9fb164da9fb675 (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
/*******************************************************************************
 * Copyright (c) 2008, 2009 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 ext.framework.b;

import java.util.Dictionary;
import java.util.HashMap;
import org.osgi.framework.Bundle;
import org.osgi.service.condpermadmin.Condition;
import org.osgi.service.condpermadmin.ConditionInfo;

public class TestCondition implements Condition {

	private final String id;
	private final boolean mutable;
	private boolean curMutable;
	private final boolean postponed;
	private boolean curPostponed;
	private final boolean satisfied;
	private boolean curSatisfied;
	private final Bundle bundle;

	private static final HashMap conditionIDs = new HashMap();

	private TestCondition(String id, boolean mutable, boolean postponed, boolean satisfied, Bundle bundle) {
		this.id = id;
		this.mutable = this.curMutable = mutable;
		this.postponed = this.curPostponed = postponed;
		this.satisfied = this.curSatisfied = satisfied;
		this.bundle = bundle;
	}

	static public Condition getCondition(final Bundle bundle, ConditionInfo info) {
		if (!TestCondition.class.getName().equals(info.getType()))
			throw new IllegalArgumentException("ConditionInfo must be of type \"" + TestCondition.class.getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
		String[] args = info.getArgs();
		if (args.length != 4)
			throw new IllegalArgumentException("Illegal number of args: " + args.length); //$NON-NLS-1$
		String identity = args[0] + '_' + bundle.getBundleId();
		boolean mut = Boolean.valueOf(args[1]).booleanValue();
		boolean post = Boolean.valueOf(args[2]).booleanValue();
		boolean sat = Boolean.valueOf(args[3]).booleanValue();
		synchronized (conditionIDs) {
			TestCondition condition = (TestCondition) conditionIDs.get(identity);
			if (condition == null) {
				condition = new TestCondition(identity, mut, post, sat, bundle);
				conditionIDs.put(identity, condition);
			}
			return condition;
		}
	}

	static public TestCondition getTestCondition(String id) {
		synchronized (conditionIDs) {
			return (TestCondition) conditionIDs.get(id);
		}
	}

	static public void clearConditions() {
		synchronized (conditionIDs) {
			conditionIDs.clear();
		}
	}

	public boolean isMutable() {
		return curMutable;
	}

	public boolean isPostponed() {
		return curPostponed;
	}

	public boolean isSatisfied() {
		return curSatisfied;
	}

	public boolean isSatisfied(Condition[] conditions, Dictionary context) {
		if (!isPostponed())
			throw new IllegalStateException("Should not call isSatisfied(Condition[] conditions, Dictionary context)"); //$NON-NLS-1$
		for (int i = 0; i < conditions.length; i++) {
			Boolean isSatisfied = (Boolean) context.get(conditions[i]);
			if (isSatisfied == null) {
				isSatisfied = Boolean.valueOf(conditions[i].isSatisfied());
				context.put(conditions[i], isSatisfied);
			}
			if (!isSatisfied.booleanValue())
				return false;
		}
		return true;
	}

	public int hashCode() {
		return id.hashCode();
	}

	public boolean equals(Object o) {
		if (!(o instanceof TestCondition))
			return false;
		TestCondition otherCondition = (TestCondition) o;
		return id.equals(otherCondition.id) && postponed == otherCondition.postponed && satisfied == otherCondition.satisfied && mutable == otherCondition.mutable && bundle == otherCondition.bundle;
	}

	public void setMutable(boolean mutable) {
		this.curMutable = mutable;
	}

	public void setPostponed(boolean isPostponed) {
		this.curPostponed = isPostponed;
	}

	public void setSatisfied(boolean isSatisfied) {
		this.curSatisfied = isSatisfied;
	}

	public Bundle getBundle() {
		return bundle;
	}

	public String toString() {
		return id + '-' + postponed + '-' + mutable + '-' + satisfied;
	}
}

Back to the top