Package: StandardContextSelectorDelegate

StandardContextSelectorDelegate

nameinstructionbranchcomplexitylinemethod
StandardContextSelectorDelegate(CallingBundleResolver, ConfigurationLocator, Bundle, LoggerContextConfigurer)
M: 0 C: 30
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
bundleChanged(BundleEvent)
M: 19 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
configureDefaultContext(LoggerContext)
M: 34 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
createConfiguredContext(LoggingConfiguration)
M: 4 C: 16
80%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 1 C: 5
83%
M: 0 C: 1
100%
detachLoggerContext(String)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getContextNames()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getLoggerContext()
M: 0 C: 83
100%
M: 2 C: 10
83%
M: 2 C: 5
71%
M: 0 C: 19
100%
M: 0 C: 1
100%
getLoggerContext(String)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
locateConfiguration(Bundle)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.medic.log.impl.logback;
13:
14: import java.util.ArrayList;
15: import java.util.HashMap;
16: import java.util.List;
17: import java.util.Map;
18: import java.util.WeakHashMap;
19:
20: import org.eclipse.virgo.medic.log.LoggingConfiguration;
21: import org.eclipse.virgo.medic.log.impl.CallingBundleResolver;
22: import org.eclipse.virgo.medic.log.impl.config.ConfigurationLocator;
23: import org.osgi.framework.Bundle;
24: import org.osgi.framework.BundleEvent;
25: import org.osgi.framework.BundleListener;
26:
27: import ch.qos.logback.classic.LoggerContext;
28:
29:
30: public class StandardContextSelectorDelegate implements ContextSelectorDelegate, BundleListener {
31:
32: private final CallingBundleResolver loggingCallerLocator;
33:
34: private final Map<String, LoggerContext> loggerContexts = new HashMap<String, LoggerContext>();
35:
36: private final Map<Bundle, LoggingConfiguration> loggingConfigurations = new WeakHashMap<Bundle, LoggingConfiguration>();
37:
38: private final Object monitor = new Object();
39:
40: private final ConfigurationLocator configurationLocator;
41:
42: private final LoggerContextConfigurer configurer;
43:
44: private final Bundle medicBundle;
45:
46: public StandardContextSelectorDelegate(CallingBundleResolver loggingCallerLocator, ConfigurationLocator configurationLocator, Bundle medicBundle,
47: LoggerContextConfigurer configurer) {
48: this.loggingCallerLocator = loggingCallerLocator;
49: this.configurationLocator = configurationLocator;
50: this.medicBundle = medicBundle;
51: this.configurer = configurer;
52: }
53:
54: public LoggerContext detachLoggerContext(String name) {
55: synchronized (monitor) {
56: return this.loggerContexts.remove(name);
57: }
58: }
59:
60: public List<String> getContextNames() {
61: synchronized (this.monitor) {
62: List<String> names = new ArrayList<String>();
63: names.addAll(this.loggerContexts.keySet());
64: return names;
65: }
66: }
67:
68: public LoggerContext getLoggerContext() {
69: Bundle callingBundle = this.loggingCallerLocator.getCallingBundle();
70:
71: LoggingConfiguration loggingConfiguration = null;
72:
73:• if (callingBundle != null) {
74:         synchronized(this.monitor) {
75:                 loggingConfiguration = this.loggingConfigurations.get(callingBundle);
76:         }
77: }
78:
79:• if (loggingConfiguration == null) {
80:         loggingConfiguration = locateConfiguration(callingBundle);
81:•         if (loggingConfiguration != null && callingBundle != null) {
82:                  synchronized(this.monitor) {
83:                          this.loggingConfigurations.put(callingBundle, loggingConfiguration);        
84:                  }
85:         }
86: }
87:
88:• if (loggingConfiguration != null) {
89: synchronized (this.monitor) {
90: LoggerContext existingContext = this.loggerContexts.get(loggingConfiguration.getName());
91:• if (existingContext != null) {
92: return existingContext;
93: } else {
94: LoggerContext configuredContext = createConfiguredContext(loggingConfiguration);
95: this.loggerContexts.put(loggingConfiguration.getName(), configuredContext);
96: return configuredContext;
97: }
98: }
99: }
100:
101: return null;
102: }
103:
104: private LoggingConfiguration locateConfiguration(Bundle callingBundle) {
105: return this.configurationLocator.locateConfiguration(callingBundle);
106: }
107:
108: private LoggerContext createConfiguredContext(LoggingConfiguration configuration) {
109: LoggerContext loggerContext = new LoggerContext();
110: loggerContext.setName(configuration.getName());
111:
112: try {
113: this.configurer.applyConfiguration(configuration, loggerContext);
114: } catch (LoggerContextConfigurationFailedException lccfe) {
115: lccfe.printStackTrace(System.err);
116: }
117:
118: return loggerContext;
119: }
120:
121: public LoggerContext getLoggerContext(String name) {
122: synchronized (monitor) {
123: return this.loggerContexts.get(name);
124: }
125: }
126:
127: public void configureDefaultContext(LoggerContext defaultContext) {
128: LoggingConfiguration configuration = locateConfiguration(this.medicBundle);
129:
130:• if (configuration != null) {
131: defaultContext.reset();
132: try {
133: this.configurer.applyConfiguration(configuration, defaultContext);
134:
135: // Remember the default context.
136: synchronized (this.monitor) {
137: this.loggerContexts.put(configuration.getName(), defaultContext);
138: }
139: } catch (LoggerContextConfigurationFailedException lccfe) {
140: lccfe.printStackTrace(System.err);
141: }
142: }
143: }
144:
145:         public void bundleChanged(BundleEvent event) {
146:•                if (BundleEvent.UPDATED == event.getType()) {
147:                         synchronized (this.monitor) {
148:                                 this.loggingConfigurations.remove(event.getBundle());
149:                         }
150:                 }
151:         }
152: }