1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.springframework.osgi.extender.support;
21
22 import java.util.Dictionary;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.osgi.framework.Bundle;
27 import org.springframework.osgi.extender.support.internal.ConfigUtils;
28 import org.springframework.osgi.extender.support.scanning.ConfigurationScanner;
29 import org.springframework.osgi.extender.support.scanning.DefaultConfigurationScanner;
30 import org.springframework.osgi.util.OsgiStringUtils;
31 import org.springframework.util.Assert;
32 import org.springframework.util.ObjectUtils;
33
34 /**
35 * Configuration class for Spring-DM application contexts.
36 *
37 * Determines the configuration information available in a bundle for
38 * constructing an application context. Reads all the Spring-DM options present
39 * in the bundle header.
40 *
41 * @author Adrian Colyer
42 * @author Costin Leau
43 */
44 public class ApplicationContextConfiguration {
45
46 /** logger */
47 private static final Log log = LogFactory.getLog(ApplicationContextConfiguration.class);
48
49 private final Bundle bundle;
50
51 private final ConfigurationScanner configurationScanner;
52
53 private final boolean asyncCreation;
54
55 private final String[] configurationLocations;
56
57 private final boolean isSpringPoweredBundle;
58
59 private final boolean publishContextAsService;
60
61 private final boolean waitForDeps;
62
63 private final String toString;
64
65 private final long timeout;
66
67
68 /**
69 * Constructs a new <code>ApplicationContextConfiguration</code> instance
70 * from the given bundle. Uses the {@link DefaultConfigurationScanner}
71 * internally for discovering Spring-powered bundles.
72 *
73 * @param bundle bundle for which the application context configuration is
74 * created
75 */
76 public ApplicationContextConfiguration(Bundle bundle) {
77 this(bundle, new DefaultConfigurationScanner());
78 }
79
80 public ApplicationContextConfiguration(Bundle bundle, ConfigurationScanner configurationScanner) {
81 Assert.notNull(bundle);
82 Assert.notNull(configurationScanner);
83 this.bundle = bundle;
84 this.configurationScanner = configurationScanner;
85
86 Dictionary headers = this.bundle.getHeaders();
87
88 String[] configs = this.configurationScanner.getConfigurations(bundle);
89
90 this.isSpringPoweredBundle = !ObjectUtils.isEmpty(configs);
91 this.configurationLocations = configs;
92 long option = ConfigUtils.getTimeOut(headers);
93
94 this.timeout = (option >= 0 ? option * 1000 : option);
95 this.publishContextAsService = ConfigUtils.getPublishContext(headers);
96 this.asyncCreation = ConfigUtils.getCreateAsync(headers);
97 this.waitForDeps = ConfigUtils.getWaitForDependencies(headers);
98
99
100 StringBuffer buf = new StringBuffer();
101 buf.append("AppCtxCfg [Bundle=");
102 buf.append(OsgiStringUtils.nullSafeSymbolicName(bundle));
103 buf.append("]isSpringBundle=");
104 buf.append(isSpringPoweredBundle);
105 buf.append("|async=");
106 buf.append(asyncCreation);
107 buf.append("|wait-for-deps=");
108 buf.append(waitForDeps);
109 buf.append("|publishCtx=");
110 buf.append(publishContextAsService);
111 buf.append("|timeout=");
112 buf.append(timeout / 1000);
113 buf.append("s");
114 toString = buf.toString();
115 if (log.isTraceEnabled()) {
116 log.trace("Configuration: " + toString);
117 }
118 }
119
120 /**
121 * Indicates if the given bundle is "Spring-Powered" or not.
122 *
123 * True if this bundle has at least one defined application context
124 * configuration file.
125 *
126 * <p/> A bundle is "Spring-Powered" if it has at least one configuration
127 * resource.
128 */
129 public boolean isSpringPoweredBundle() {
130 return this.isSpringPoweredBundle;
131 }
132
133 /**
134 * Returns the timeout (in milliseconds) an application context needs to
135 * wait for mandatory dependent services.
136 */
137 public long getTimeout() {
138 return this.timeout;
139 }
140
141 /**
142 * Indicates if an application context needs to be created asynchronously or
143 * not.
144 *
145 * Should the application context wait for all non-optional service
146 * references to be satisfied before starting?
147 */
148 public boolean isCreateAsynchronously() {
149 return this.asyncCreation;
150 }
151
152 /**
153 * Indicates if the application context needs to be published as a service
154 * or not.
155 *
156 * @return Returns the publishContextAsService.
157 */
158 public boolean isPublishContextAsService() {
159 return publishContextAsService;
160 }
161
162 /**
163 * Indicates if the configuration must wait for dependencies.
164 *
165 * @return true if the configuration indicates that dependencies should be
166 * waited for.
167 */
168 public boolean isWaitForDependencies() {
169 return waitForDeps;
170 }
171
172 /**
173 * Returns the locations of the configuration resources used to build the
174 * application context (as Spring resource paths).
175 *
176 * @return configuration paths
177 */
178 public String[] getConfigurationLocations() {
179 return this.configurationLocations;
180 }
181
182 public String toString() {
183 return toString;
184 }
185 }