-
Notifications
You must be signed in to change notification settings - Fork 32
/
OsgiFramework.kt
465 lines (384 loc) · 16.1 KB
/
OsgiFramework.kt
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package com.cognifide.gradle.aem.common.instance.service.osgi
import com.cognifide.gradle.aem.common.bundle.BundleFile
import com.cognifide.gradle.aem.common.instance.InstanceService
import com.cognifide.gradle.aem.common.instance.InstanceSync
import com.cognifide.gradle.common.CommonException
import com.cognifide.gradle.common.build.Retry
import com.cognifide.gradle.common.http.ResponseException
import com.cognifide.gradle.common.utils.Formats
import org.apache.http.HttpStatus
import java.io.File
/**
* Controls OSGi framework using Apache Felix Web Console endpoints.
*
* @see <https://felix.apache.org/documentation/subprojects/apache-felix-web-console.html>
*/
@Suppress("TooManyFunctions")
class OsgiFramework(sync: InstanceSync) : InstanceService(sync) {
// ----- Bundles -----
/**
* Get all OSGi bundles.
*/
val bundles: List<Bundle> get() = determineBundleState().bundles
/**
* Determine all OSGi bundle states.
*/
fun determineBundleState(): BundleState {
logger.debug("Asking for OSGi bundles on $instance")
return try {
sync.http.get(BUNDLES_LIST_JSON) { asObjectFromJson(it, BundleState::class.java) }
} catch (e: CommonException) {
logger.debug("Cannot request OSGi bundles state on $instance", e)
BundleState.unknown(e)
}.apply {
this.instance = [email protected]
this.bundles.forEach { it.instance = instance }
}
}
/**
* Find OSGi bundle by symbolic name.
*/
fun findBundle(symbolicName: String): Bundle? = determineBundleState().bundles.find {
symbolicName.equals(it.symbolicName, ignoreCase = true)
}
/**
* Get OSGi bundle by symbolic name. Fail if not found.
*/
fun getBundle(symbolicName: String): Bundle = findBundle(symbolicName)
?: throw OsgiException("OSGi bundle '$symbolicName' cannot be found on $instance.")
/**
* Start OSGi bundle. Does nothing if already started.
*/
fun startBundle(symbolicName: String) {
val bundle = getBundle(symbolicName)
if (bundle.stable) {
logger.info("Not starting already started OSGi $bundle on $instance.")
return
}
logger.info("Starting OSGi $bundle on $instance.")
sync.http.post("$BUNDLES_PATH/${bundle.id}", mapOf("action" to "start"))
}
fun startBundle(bundle: Bundle) = startBundle(bundle.symbolicName)
/**
* Stop OSGi bundle. Does nothing if already stopped.
*/
fun stopBundle(symbolicName: String) {
val bundle = getBundle(symbolicName)
if (!bundle.stable) {
logger.info("Not stopping already stopped OSGi $bundle on $instance.")
return
}
logger.info("Stopping OSGi $bundle on $instance.")
sync.http.post("$BUNDLES_PATH/${bundle.id}", mapOf("action" to "stop"))
}
fun stopBundle(bundle: Bundle) = stopBundle(bundle.symbolicName)
fun <T> toggleBundle(symbolicName: String, action: () -> T): T = try {
stopBundle(symbolicName)
action()
} finally {
startBundle(symbolicName)
}
/**
* Stop then start again OSGi bundle. Works correctly even it is already stopped.
*/
fun restartBundle(symbolicName: String) {
stopBundle(symbolicName)
startBundle(symbolicName)
}
fun restartBundle(bundle: Bundle) = restartBundle(bundle.symbolicName)
/**
* Refresh OSGi bundle by symbolic name.
*/
fun refreshBundle(symbolicName: String) {
val bundle = getBundle(symbolicName)
logger.info("Refreshing OSGi $bundle on $instance.")
sync.http.post("$BUNDLES_PATH/${bundle.symbolicName}", mapOf("action" to "refresh"))
}
fun refreshBundle(bundle: Bundle) = refreshBundle(bundle.symbolicName)
/**
* Update OSGi bundle by symbolic name.
*/
fun updateBundle(symbolicName: String) {
val bundle = getBundle(symbolicName)
logger.info("Updating OSGi $bundle on $instance.")
sync.http.post("$BUNDLES_PATH/${bundle.symbolicName}", mapOf("action" to "update"))
}
fun updateBundle(bundle: Bundle) = updateBundle(bundle.symbolicName)
/**
* Install OSGi bundle JAR.
*/
fun installBundle(
bundle: File,
start: Boolean = true,
startLevel: Int = 20,
refreshPackages: Boolean = true,
retry: Retry = common.retry()
) {
logger.info("Installing OSGi bundle '$bundle' on $instance.")
retry.withCountdown<Unit, CommonException>("install bundle '${bundle.name}' on '${instance.name}'") {
sync.http.postMultipart(
BUNDLES_PATH,
mapOf(
"action" to "install",
"bundlefile" to bundle,
"bundlestart" to "start".takeIf { start },
"bundlestartlevel" to startLevel,
"refreshPackages" to "refresh".takeIf { refreshPackages }
)
) { checkStatus(it, HttpStatus.SC_MOVED_TEMPORARILY) }
}
}
/**
* Uninstall OSGi bundle JAR.
*/
fun uninstallBundle(bundle: File) {
val bundleFile = BundleFile(bundle)
logger.info("Uninstalling OSGi $bundleFile on $instance.")
uninstallBundleInternal(bundleFile.symbolicName)
}
/**
* Uninstall OSGi bundle by symbolic name.
*/
fun uninstallBundle(symbolicName: String) {
logger.info("Uninstalling OSGi bundle '$symbolicName' on $instance.")
uninstallBundleInternal(symbolicName)
}
fun uninstallBundle(bundle: Bundle) = uninstallBundle(bundle.symbolicName)
private fun uninstallBundleInternal(symbolicName: String) {
sync.http.post("$BUNDLES_PATH/${getBundle(symbolicName).id}", mapOf("action" to "uninstall"))
}
// ----- Components -----
/**
* Get all OSGi components.
*/
val components: List<Component> get() = determineComponentState().components
/**
* Determine OSGi components state.
*/
fun determineComponentState(): ComponentState {
logger.debug("Asking for OSGi components on $instance")
return try {
sync.http.get(COMPONENTS_LIST_JSON) { asObjectFromJson(it, ComponentState::class.java) }
} catch (e: CommonException) {
logger.debug("Cannot determine OSGi components state on $instance. Cause: ${e.message}", e)
ComponentState.unknown()
}.apply {
this.instance = [email protected]
this.components.forEach { it.instance = instance }
}
}
/**
* Find OSGi component by PID.
*/
fun findComponent(pid: String): Component? = determineComponentState().components.find {
pid.equals(it.uid, ignoreCase = true)
}
/**
* Get OSGi component by PID. Fail if not found.
*/
fun getComponent(pid: String): Component = findComponent(pid)
?: throw OsgiException("OSGi component '$pid' cannot be found on $instance.")
/**
* Enable OSGi component. Does nothing if already enabled.
*/
fun enableComponent(pid: String) {
val component = getComponent(pid)
if (component.id.isNotBlank()) {
logger.info("Not enabling already enabled OSGi $component on $instance.")
return
}
logger.info("Enabling OSGi $component on $instance.")
sync.http.post("$COMPONENTS_PATH/${component.uid}", mapOf("action" to "enable"))
}
fun enableComponent(component: Component) = enableComponent(component.uid)
/**
* Disable OSGi component. Does nothing if already disabled.
*/
fun disableComponent(pid: String) {
val component = getComponent(pid)
if (component.id.isBlank()) {
logger.info("Not disabling already disabled OSGi $component on $instance.")
return
}
logger.info("Disabling OSGi $component on $instance.")
sync.http.post("$COMPONENTS_PATH/${component.id}", mapOf("action" to "disable"))
}
fun disableComponent(component: Component) = disableComponent(component.uid)
/**
* Disable then enable again OSGi component. Works correctly even it is already disabled.
*/
fun restartComponent(pid: String) {
disableComponent(pid)
enableComponent(pid)
}
fun restartComponent(component: Component) = restartComponent(component.uid)
fun <T> toggleComponent(pid: String, action: () -> T): T = try {
disableComponent(pid)
action()
} finally {
enableComponent(pid)
}
// ----- Configurations -----
/**
* Set properties for existing OSGi configuration.
*/
fun configure(pid: String, propertyName: String, propertyValue: Any?) = configure(pid, mapOf(propertyName to propertyValue))
/**
* Set properties for existing OSGi configuration.
*/
fun configure(pid: String, properties: Map<String, Any?>) = updateConfiguration(pid, properties)
/**
* Get all OSGi configurations.
*/
val configurations: Sequence<Configuration>
get() = determineConfigurationState().pids.asSequence()
.mapNotNull { findConfiguration(it.id) }
/**
* Determine all OSGi configuration PIDs.
*/
fun determineConfigurationState(): ConfigurationState {
logger.debug("Asking for OSGi configurations on $instance")
return try {
sync.http.get(CONFIGURATION_PATH) { response ->
val html = asString(response)
val configJson = CONFIGURATIONS_REGEX.find(html)?.groups?.get(1)?.value
?: throw ResponseException("OSGi configuration cannot be found in console response of $instance.")
Formats.toObjectFromJson<ConfigurationState>(configJson)
}
} catch (e: CommonException) {
logger.debug("Cannot determine OSGi configuration state on $instance. Cause: ${e.message}", e)
ConfigurationState.unknown()
}.apply {
this.instance = [email protected]
this.pids.forEach { it.instance = instance }
}
}
/**
* Find OSGi configuration by PID.
* Also ensures that for given configuration there is metatype service available (to reduce typo errors).
*/
fun findConfiguration(pid: String, metatypeChecking: Boolean = true): Configuration? = try {
sync.http.get("$CONFIGURATION_PATH/$pid?post=true") { response ->
asObjectFromJson(response, Configuration::class.java).takeIf { !metatypeChecking || !it.metatypeAbsence }
}
} catch (e: CommonException) {
throw OsgiException("Cannot read OSGi configuration for PID '$pid' on $instance. Cause: ${e.message}", e)
}?.apply {
this.instance = [email protected]
}
/**
* Get OSGi configuration by PID. Fail if not found.
*/
fun getConfiguration(pid: String): Configuration = findConfiguration(pid)
?: throw OsgiException("OSGi configuration for PID '$pid' cannot be found on $instance")
/**
* Get all OSGi configurations for specified factory PID.
*/
fun getConfigurations(fpid: String): Sequence<Configuration> = determineConfigurationState().pids.asSequence()
.filter { fpid == it.fpid }
.mapNotNull { findConfiguration(it.id) }
/**
* Set properties for existing OSGi configuration.
*/
fun updateConfiguration(pid: String, properties: Map<String, Any?>) = try {
logger.info("Updating OSGi configuration for PID '$pid' on $instance using properties: $properties")
val config = getConfiguration(pid)
val props = configurationProperties(config, properties)
sync.http.post("$CONFIGURATION_PATH/$pid", props) { checkStatus(it, HttpStatus.SC_MOVED_TEMPORARILY) }
} catch (e: CommonException) {
throw OsgiException("OSGi configuration for PID '$pid' cannot be updated on $instance. Cause: ${e.message}", e)
}
/**
* Set properties for existing OSGi configuration.
*/
fun updateConfiguration(pid: String, service: String, properties: Map<String, Any>) = updateConfiguration("$pid~$service", properties)
/**
* Set properties for existing OSGi configuration or create new.
*/
fun saveConfiguration(pid: String, properties: Map<String, Any?>) = try {
logger.info("Saving OSGi configuration for PID '$pid' on $instance using properties: $properties")
val config = findConfiguration(pid, false)!! // endpoint always return data even for non-existing PID
val props = configurationProperties(config, properties)
sync.http.post("$CONFIGURATION_PATH/$pid", props) { checkStatus(it, HttpStatus.SC_MOVED_TEMPORARILY) }
} catch (e: CommonException) {
throw OsgiException("OSGi configuration for PID '$pid' cannot be saved on $instance. Cause: ${e.message}", e)
}
private fun configurationProperties(existingConfig: Configuration, properties: Map<String, Any?>): Map<String, Any?> {
val valueProperties = existingConfig.properties + properties
return valueProperties + mapOf(
"apply" to true,
"action" to "ajaxConfigManager",
"\$location" to existingConfig.bundleLocation,
"propertylist" to valueProperties.keys.joinToString(",")
)
}
/**
* Set properties for existing OSGi configuration or create new.
*/
fun saveConfiguration(pid: String, service: String, properties: Map<String, Any>) = saveConfiguration("$pid~$service", properties)
/**
* Delete existing OSGi configuration.
*/
fun deleteConfiguration(pid: String) = try {
logger.info("Deleting OSGi configuration for PID '$pid' on $instance")
val properties = mapOf(
"apply" to 1,
"delete" to 1
)
sync.http.post("$CONFIGURATION_PATH/$pid", properties)
} catch (e: CommonException) {
throw OsgiException("OSGi configuration for PID '$pid' cannot be deleted on $instance. Cause: ${e.message}", e)
}
fun deleteConfiguration(configuration: Configuration) = deleteConfiguration(configuration.pid)
/**
* Delete existing OSGi configuration.
*/
fun deleteConfiguration(pid: String, service: String) = deleteConfiguration("$pid~$service")
// ----- Events -----
/**
* Get OSGi events for current moment.
*/
val events: List<Event> get() = determineEventState().events
/**
* Determine OSGi events for current moment.
*/
fun determineEventState(): EventState {
logger.debug("Asking for OSGi events on $instance")
return try {
sync.http.get(EVENTS_LIST_JSON) { asObjectFromJson(it, EventState::class.java) }
} catch (e: CommonException) {
logger.debug("Cannot determine OSGi events state on $instance. Cause: ${e.message}", e)
EventState.unknown()
}.apply {
this.instance = [email protected]
this.events.forEach { it.instance = instance }
}
}
// ----- Framework -----
/**
* Restart OSGi framework (Apache Felix)
*/
fun restart() = shutdown("Restart")
/**
* Stop OSGi framework (Apache Felix)
*
* Warning! After executing instance will be no longer available.
*/
fun stop() = shutdown("Stop")
private fun shutdown(type: String) = try {
logger.info("Triggering OSGi framework shutdown of type '$type' on $instance.")
sync.http.postUrlencoded(VMSTAT_PATH, mapOf("shutdown_type" to type))
} catch (e: CommonException) {
throw OsgiException("Cannot trigger shutdown of $instance. Cause: ${e.message}", e)
}
companion object {
const val BUNDLES_PATH = "/system/console/bundles"
const val BUNDLES_LIST_JSON = "$BUNDLES_PATH.json"
const val COMPONENTS_PATH = "/system/console/components"
const val COMPONENTS_LIST_JSON = "$COMPONENTS_PATH.json"
const val EVENTS_LIST_JSON = "/system/console/events.json"
const val VMSTAT_PATH = "/system/console/vmstat"
const val CONFIGURATION_PATH = "/system/console/configMgr"
val CONFIGURATIONS_REGEX = Regex("configData = (.*);")
}
}