Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 46a2ece047b8c9b93dd4c5b332ab1f74a22e3ff7 (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
/*******************************************************************************
 * Copyright (c) 2013 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.osgi.internal.log;

import java.util.*;
import org.eclipse.osgi.internal.log.ExtendedLogServiceFactory.EquinoxLoggerContext;
import org.osgi.framework.Bundle;
import org.osgi.framework.Version;
import org.osgi.service.log.admin.LoggerContext;

public class LoggerContextTargetMap {
	private final Map<Bundle, ExtendedLogServiceImpl> logServices = new HashMap<>();
	private final Map<String, EquinoxLoggerContext> loggerContexts = new HashMap<>();
	private final Map<Bundle, List<String>> targetToQualifiedNames = new HashMap<>();
	private final Map<String, Collection<Bundle>> qualifiedNameToTargets = new HashMap<>();

	List<String> add(Bundle b) {
		String bsn = b.getSymbolicName();
		if (bsn == null) {
			bsn = ""; //$NON-NLS-1$
		}
		Version v = b.getVersion();
		String version = v == null ? "" : v.toString(); //$NON-NLS-1$
		String location = ExtendedLogServiceFactory.secureAction.getLocation(b);

		List<String> result = new ArrayList<>(3);

		StringBuilder sb = new StringBuilder(bsn);
		getTargetsInternal(bsn).add(b);

		sb.append('|').append(version);
		String bsnVersion = sb.toString();
		getTargetsInternal(bsnVersion).add(b);

		sb.append('|').append(location);
		String bsnVersionLocation = sb.toString();
		getTargetsInternal(bsnVersionLocation).add(b);

		result.add(bsnVersionLocation);
		result.add(bsnVersion);
		result.add(bsn);

		List<String> unmodifiable = Collections.unmodifiableList(result);
		targetToQualifiedNames.put(b, unmodifiable);
		return unmodifiable;
	}

	void remove(Bundle b) {
		List<String> qualifiedNames = targetToQualifiedNames.remove(b);
		if (qualifiedNames != null) {
			for (String qualifiedName : qualifiedNames) {
				Collection<Bundle> targets = qualifiedNameToTargets.get(qualifiedName);
				if (targets != null) {
					targets.remove(b);
					if (targets.isEmpty()) {
						qualifiedNameToTargets.remove(qualifiedName);
					}
				}
			}
		}
		logServices.remove(b);
	}

	private Collection<Bundle> getTargetsInternal(String pid) {
		Collection<Bundle> targets = qualifiedNameToTargets.get(pid);
		if (targets == null) {
			targets = new ArrayList<>(1);
			qualifiedNameToTargets.put(pid, targets);
		}
		return targets;
	}

	ExtendedLogServiceImpl getLogService(Bundle bundle, ExtendedLogServiceFactory factory) {
		ExtendedLogServiceImpl logService = logServices.get(bundle);
		if (logService == null) {
			// add bundle to target maps before constructing
			add(bundle);
			logService = new ExtendedLogServiceImpl(factory, bundle);
			if (bundle != null && bundle.getState() != Bundle.UNINSTALLED)
				logServices.put(bundle, logService);
		}
		return logService;
	}

	void replaceSystemBundleLogService(Bundle previousBundle, Bundle currentBundle) {
		ExtendedLogServiceImpl existing = logServices.get(previousBundle);
		if (existing != null) {
			remove(previousBundle);
			add(currentBundle);
			logServices.put(currentBundle, existing);
			existing.applyLogLevels(getEffectiveLoggerContext(currentBundle));
		}
	}

	void clear() {
		logServices.clear();
		qualifiedNameToTargets.clear();
		targetToQualifiedNames.clear();
		loggerContexts.clear();
	}

	LoggerContext createLoggerContext(String name, ExtendedLogServiceFactory factory) {
		EquinoxLoggerContext loggerContext = loggerContexts.get(name);
		if (loggerContext == null) {
			loggerContext = factory.createEquinoxLoggerContext(name);
			loggerContexts.put(name, loggerContext);
		}
		return loggerContext;
	}

	EquinoxLoggerContext getRootLoggerContext() {
		return loggerContexts.get(null);
	}

	void applyLogLevels(EquinoxLoggerContext loggerContext) {
		Collection<Bundle> matching;
		boolean isRoot = loggerContext.getName() == null;
		if (isRoot) {
			// root applies to all loggers
			matching = logServices.keySet();
		} else {
			matching = qualifiedNameToTargets.get(loggerContext.getName());
		}
		for (Bundle bundle : matching) {
			ExtendedLogServiceImpl logService = logServices.get(bundle);
			if (logService != null) {
				// Always apply the effective log context.
				// This may be more costly but it is more simple than checking
				// if the changed context overrides the existing settings
				logService.applyLogLevels(getEffectiveLoggerContext(bundle));
			}
		}
	}

	EquinoxLoggerContext getEffectiveLoggerContext(Bundle bundle) {
		List<String> qualifiedNames = targetToQualifiedNames.get(bundle);
		if (qualifiedNames != null) {
			for (String qualifiedName : qualifiedNames) {
				EquinoxLoggerContext loggerContext = loggerContexts.get(qualifiedName);
				if (loggerContext != null && !loggerContext.isEmpty()) {
					return loggerContext;
				}
			}
		}
		return getRootLoggerContext();
	}
}

Back to the top