1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.kernel.bundleinfo;
23
24 import static org.opencastproject.kernel.bundleinfo.BundleInfoImpl.bundleInfo;
25 import static org.opencastproject.kernel.bundleinfo.BundleInfos.getBuildNumber;
26 import static org.opencastproject.util.OsgiUtil.getContextProperty;
27 import static org.opencastproject.util.data.Option.none;
28 import static org.opencastproject.util.data.Option.option;
29 import static org.opencastproject.util.data.Option.some;
30
31 import org.opencastproject.systems.OpencastConstants;
32 import org.opencastproject.util.UrlSupport;
33 import org.opencastproject.util.data.Option;
34 import org.opencastproject.util.data.functions.Strings;
35
36 import org.osgi.framework.Bundle;
37 import org.osgi.framework.BundleEvent;
38 import org.osgi.framework.BundleListener;
39 import org.osgi.service.component.ComponentContext;
40 import org.osgi.service.component.annotations.Activate;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.Deactivate;
43 import org.osgi.service.component.annotations.Reference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47
48
49
50 @Component(
51 immediate = true
52 )
53 public class BundleInfoLogger implements BundleListener {
54
55 private static final Logger logger = LoggerFactory.getLogger(BundleInfoLogger.class);
56
57
58
59
60
61
62
63 private Option<BundleInfoDb> db;
64 private String host;
65
66
67 @Reference(unbind = "unsetDb")
68 public void setDb(BundleInfoDb db) {
69 this.db = some(db);
70 }
71
72
73 public void unsetDb(BundleInfoDb db) {
74 this.db = none();
75 }
76
77
78 @Activate
79 public void activate(ComponentContext cc) {
80 host = option(getContextProperty(cc, OpencastConstants.SERVER_URL_PROPERTY)).bind(Strings.trimToNone).getOrElse(
81 UrlSupport.DEFAULT_BASE_URL);
82 for (BundleInfoDb a : db)
83 a.clear(host);
84 cc.getBundleContext().addBundleListener(this);
85 for (Bundle b : cc.getBundleContext().getBundles()) {
86 logBundle(b);
87 }
88 }
89
90
91 @Deactivate
92 public void deactivate() {
93 for (BundleInfoDb a : db) {
94 logger.info("Clearing versions");
95 a.clear(host);
96 }
97 }
98
99 @Override
100 public void bundleChanged(BundleEvent event) {
101 switch (event.getType()) {
102 case BundleEvent.INSTALLED:
103 logBundle(event.getBundle());
104 break;
105 case BundleEvent.STOPPED:
106 case BundleEvent.UNINSTALLED:
107 for (BundleInfoDb a : db)
108 a.delete(host, event.getBundle().getBundleId());
109 break;
110 default:
111
112 }
113 }
114
115 private void logBundle(final Bundle bundle) {
116 final BundleInfo info = bundleInfo(host, bundle.getSymbolicName(), bundle.getBundleId(), bundle.getVersion()
117 .toString(), getBuildNumber(bundle));
118 final String log = String.format("Bundle %s, id %d, version %s, build number %s", info.getBundleSymbolicName(),
119 info.getBundleId(), info.getBundleVersion(), info.getBuildNumber().getOrElse("n/a"));
120 logger.info(log);
121 for (BundleInfoDb a : db)
122 a.store(info);
123 }
124 }