1515 ******************************************************************************/
1616package in .bytehue .messaging .mqtt5 .provider .command ;
1717
18- import static in .bytehue .messaging .mqtt5 .provider .command .MqttCommand .PID ;
1918import static org .osgi .framework .namespace .PackageNamespace .PACKAGE_NAMESPACE ;
19+ import static org .osgi .service .condition .Condition .CONDITION_ID ;
20+ import static org .osgi .service .condition .Condition .INSTANCE ;
2021
21- import java .io .IOException ;
22- import java .util .Hashtable ;
22+ import java .util .HashMap ;
23+ import java .util .Map ;
24+ import java .util .concurrent .atomic .AtomicReference ;
2325
2426import org .osgi .framework .BundleContext ;
27+ import org .osgi .framework .FrameworkUtil ;
28+ import org .osgi .framework .ServiceRegistration ;
2529import org .osgi .framework .wiring .BundleWiring ;
26- import org .osgi .service .cm .Configuration ;
27- import org .osgi .service .cm .ConfigurationAdmin ;
2830import org .osgi .service .component .annotations .Activate ;
2931import org .osgi .service .component .annotations .Component ;
3032import org .osgi .service .component .annotations .Deactivate ;
31- import org .osgi .service .component . annotations . Reference ;
33+ import org .osgi .service .condition . Condition ;
3234
3335@ Component
3436public final class GogoCommandActivator {
3537
38+ private static final String CONDITION_VALUE = "gogo-available" ;
3639 private static final String GOGO_PACKAGE = "org.apache.felix.service.command" ;
3740
3841 private final BundleContext bundleContext ;
39- private final ConfigurationAdmin configAdmin ;
42+ private final AtomicReference < ServiceRegistration < Condition >> ref = new AtomicReference <>() ;
4043
4144 @ Activate
42- public GogoCommandActivator (
43- // @formatter:off
44- final BundleContext bundleContext ,
45- @ Reference
46- final ConfigurationAdmin configAdmin
47- // @formatter:on
48- ) {
49- this .configAdmin = configAdmin ;
45+ public GogoCommandActivator (final BundleContext bundleContext ) {
5046 this .bundleContext = bundleContext ;
51-
5247 if (isGogoPackageImported ()) {
53- createGogoCommandConfig ();
48+ registerCondition ();
5449 }
5550 }
5651
@@ -66,23 +61,23 @@ private boolean isGogoPackageImported() {
6661 // @formatter:on
6762 }
6863
69- private void createGogoCommandConfig () {
70- try {
71- final Configuration configuration = configAdmin . getConfiguration ( PID , "?" );
72- configuration . updateIfDifferent ( new Hashtable <>());
73- } catch ( final IOException e ) {
74- throw new RuntimeException ( e );
75- }
64+ private void registerCondition () {
65+ final Map < String , Object > properties = new HashMap <>();
66+ properties . put ( CONDITION_ID , CONDITION_VALUE );
67+
68+ final ServiceRegistration < Condition > reg = bundleContext . registerService ( Condition . class , INSTANCE ,
69+ FrameworkUtil . asDictionary ( properties ) );
70+ ref . set ( reg );
7671 }
7772
7873 @ Deactivate
79- private void deleteGogoCommandConfig () {
80- try {
81- final Configuration configuration = configAdmin . getConfiguration ( PID , "?" );
82- configuration . delete ();
83- } catch ( final IOException e ) {
84- throw new RuntimeException ( e ) ;
85- }
74+ private void deregisterCondition () {
75+ ref . getAndUpdate ( reg -> {
76+ if ( reg != null ) {
77+ reg . unregister ();
78+ }
79+ return null ;
80+ });
8681 }
8782
8883}
0 commit comments