Package: SpringUtils

SpringUtils

nameinstructionbranchcomplexitylinemethod
SpringUtils()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getBundleBlueprintHeader(Dictionary)
M: 6 C: 16
73%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 4
100%
M: 0 C: 1
100%
getSpringContextConfigurations(Bundle)
M: 26 C: 28
52%
M: 6 C: 4
40%
M: 4 C: 2
33%
M: 2 C: 8
80%
M: 0 C: 1
100%
getSpringContextHeader(Dictionary)
M: 6 C: 16
73%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 4
100%
M: 0 C: 1
100%
getSpringContextHeaderLocations(Dictionary)
M: 14 C: 77
85%
M: 4 C: 12
75%
M: 4 C: 5
56%
M: 2 C: 16
89%
M: 0 C: 1
100%
isArrayEmpty(Object[])
M: 6 C: 9
60%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 1
100%
M: 0 C: 1
100%
isSpringDMPoweredBundle(Bundle)
M: 6 C: 10
63%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1:
2: package org.eclipse.virgo.nano.core.internal;
3:
4: import java.net.URL;
5: import java.util.Dictionary;
6: import java.util.Enumeration;
7:
8: import org.eclipse.virgo.util.common.StringUtils;
9: import org.osgi.framework.Bundle;
10:
11: /**
12: * Utilities class for covering various Spring features like Spring Context discovery.
13: *
14: * <strong>Concurrent Semantics</strong><br />
15: * Thread-safe.
16: *
17: */
18: public class SpringUtils {
19:
20: private static final String SPRING_DM_CONTEXT_DIR = "/META-INF/spring/";
21:
22: private static final String BLUEPRINT_CONTEXT_DIR = "/OSGI-INF/blueprint/";
23:
24: private static final String CONTEXT_FILES = "*.xml";
25:
26: private static final String BUNDLE_URL_PREFIX = "osgibundle:";
27:
28: private static final String SPRING_CONTEXT_HEADER = "Spring-Context";
29:
30: private static final String BUNDLE_BLUEPRINT_HEADER = "Bundle-Blueprint";
31:
32: private static final String DIRECTIVE_SEPARATOR = ";";
33:
34: private static final String CONTEXT_LOCATION_SEPARATOR = ",";
35:
36: private static final String CONFIG_WILDCARD = "*";
37:
38: /** Default configuration locations */
39: private static final String SPRING_DM_DEFAULT_CONFIG = BUNDLE_URL_PREFIX + SPRING_DM_CONTEXT_DIR + CONTEXT_FILES;
40: private static final String BLUEPRINT_DEFAULT_CONFIG = BUNDLE_URL_PREFIX + BLUEPRINT_CONTEXT_DIR + CONTEXT_FILES;
41:
42: private static String[] getSpringContextConfigurations(Bundle bundle) {
43: String[] locations = getSpringContextHeaderLocations(bundle.getHeaders());
44:
45: // if no location is specified in the header, try the defaults
46:• if (isArrayEmpty(locations)) {
47: // check the default locations if the manifest doesn't provide any info
48: Enumeration<URL> defaultConfig = bundle.findEntries(SPRING_DM_CONTEXT_DIR, CONTEXT_FILES, false);
49:• if (defaultConfig != null && defaultConfig.hasMoreElements()) {
50: return new String[] { SPRING_DM_DEFAULT_CONFIG };
51: } else {
52: defaultConfig = bundle.findEntries(BLUEPRINT_CONTEXT_DIR, CONTEXT_FILES, false);
53:• if (defaultConfig != null && defaultConfig.hasMoreElements()) {
54: return new String[] { BLUEPRINT_DEFAULT_CONFIG };
55: } else {
56: return new String[0];
57: }
58: }
59: } else {
60: return locations;
61: }
62: }
63:
64: /**
65: * Returns the location headers (if any) specified by the Spring-Context header (if available). The returned Strings
66: * can be sent to a {@link org.springframework.core.io.ResourceLoader} for loading the configurations.
67: *
68: * @param headers bundle headers
69: * @return array of locations specified (if any)
70: */
71: static String[] getSpringContextHeaderLocations(Dictionary<String, String> headers) {
72: String header = getSpringContextHeader(headers);
73: String[] ctxEntries;
74:• if (StringUtils.hasText(header) && !(';' == header.charAt(0))) {
75: // get the config locations
76: String locations = StringUtils.tokenizeToStringArray(header, DIRECTIVE_SEPARATOR)[0];
77: // parse it into individual token
78: ctxEntries = StringUtils.tokenizeToStringArray(locations, CONTEXT_LOCATION_SEPARATOR);
79: // replace * with a 'digestable' location
80:• for (int i = 0; i < ctxEntries.length; i++) {
81:• if (CONFIG_WILDCARD.equals(ctxEntries[i]))
82: ctxEntries[i] = SPRING_DM_DEFAULT_CONFIG;
83: }
84: } else {
85: header = getBundleBlueprintHeader(headers);
86:• if (StringUtils.hasText(header) && !(';' == header.charAt(0))) {
87: // get the config locations
88: String locations = StringUtils.tokenizeToStringArray(header, DIRECTIVE_SEPARATOR)[0];
89: // parse it into individual token
90: ctxEntries = StringUtils.tokenizeToStringArray(locations, CONTEXT_LOCATION_SEPARATOR);
91: // replace * with a 'digestable' location
92:• for (int i = 0; i < ctxEntries.length; i++) {
93:• if (CONFIG_WILDCARD.equals(ctxEntries[i]))
94: ctxEntries[i] = BLUEPRINT_DEFAULT_CONFIG;
95: }
96: } else {
97: ctxEntries = new String[0];
98: }
99: }
100: return ctxEntries;
101: }
102:
103: static boolean isArrayEmpty(Object[] array) {
104:• return (array == null || array.length == 0);
105: }
106:
107: static String getSpringContextHeader(Dictionary headers) {
108: Object header = null;
109:• if (headers != null) {
110: header = headers.get(SPRING_CONTEXT_HEADER);
111: }
112:• return (header != null ? header.toString().trim() : null);
113: }
114:
115: static String getBundleBlueprintHeader(Dictionary headers) {
116: Object header = null;
117:• if (headers != null) {
118: header = headers.get(BUNDLE_BLUEPRINT_HEADER);
119: }
120:• return (header != null ? header.toString().trim() : null);
121: }
122:
123: /**
124: * Queries whether the supplied {@link Bundle} is Spring-DM powered.
125: *
126: * @param bundle the <code>Bundle</code>.
127: * @return <code>true</code> if the <code>Bundle</code> is Spring-DM powered, otherwise <code>false</code>.
128: */
129: public static boolean isSpringDMPoweredBundle(Bundle bundle) {
130: String[] configurations = getSpringContextConfigurations(bundle);
131:• return !isArrayEmpty(configurations);
132: }
133: }