BUG-1381: make users of JavassistUtils.getLock synchronized properly
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / ExtensibleBundleTracker.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.manager.impl.osgi;
9
10 import org.osgi.framework.Bundle;
11 import org.osgi.framework.BundleContext;
12 import org.osgi.framework.BundleEvent;
13 import org.osgi.util.tracker.BundleTracker;
14 import org.osgi.util.tracker.BundleTrackerCustomizer;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  *
20  * Extensible bundle tracker. Takes several BundleTrackerCustomizers and propagates bundle events to all of them.
21  * Primary customizer
22  *
23  * @param <T>
24  */
25 public final class ExtensibleBundleTracker<T> extends BundleTracker<T> {
26
27     private final BundleTrackerCustomizer<T> primaryTracker;
28     private final BundleTrackerCustomizer<?>[] additionalTrackers;
29
30     private static final Logger logger = LoggerFactory.getLogger(ExtensibleBundleTracker.class);
31
32     public ExtensibleBundleTracker(BundleContext context, BundleTrackerCustomizer<T> primaryBundleTrackerCustomizer,
33                                    BundleTrackerCustomizer<?>... additionalBundleTrackerCustomizers) {
34         this(context, Bundle.ACTIVE, primaryBundleTrackerCustomizer, additionalBundleTrackerCustomizers);
35     }
36
37     public ExtensibleBundleTracker(BundleContext context, int bundleState,
38                                    BundleTrackerCustomizer<T> primaryBundleTrackerCustomizer,
39                                    BundleTrackerCustomizer<?>... additionalBundleTrackerCustomizers) {
40         super(context, bundleState, null);
41         this.primaryTracker = primaryBundleTrackerCustomizer;
42         this.additionalTrackers = additionalBundleTrackerCustomizers;
43         logger.trace("Registered as extender with context {} and bundle state {}", context, bundleState);
44     }
45
46     @Override
47     public T addingBundle(final Bundle bundle, final BundleEvent event) {
48         T primaryTrackerRetVal = primaryTracker.addingBundle(bundle, event);
49
50         forEachAdditionalBundle(new BundleStrategy() {
51             @Override
52             public void execute(BundleTrackerCustomizer<?> tracker) {
53                 tracker.addingBundle(bundle, event);
54             }
55         });
56
57         return primaryTrackerRetVal;
58     }
59
60     @Override
61     public void modifiedBundle(final Bundle bundle, final BundleEvent event, final T object) {
62         primaryTracker.modifiedBundle(bundle, event, object);
63
64         forEachAdditionalBundle(new BundleStrategy() {
65             @Override
66             public void execute(BundleTrackerCustomizer<?> tracker) {
67                 tracker.modifiedBundle(bundle, event, null);
68             }
69         });
70
71     }
72
73     @Override
74     public void removedBundle(final Bundle bundle, final BundleEvent event, final T object) {
75         primaryTracker.removedBundle(bundle, event, object);
76
77         forEachAdditionalBundle(new BundleStrategy() {
78             @Override
79             public void execute(BundleTrackerCustomizer<?> tracker) {
80                 tracker.removedBundle(bundle, event, null);
81             }
82         });
83     }
84
85     private void forEachAdditionalBundle(BundleStrategy lambda) {
86         for (BundleTrackerCustomizer<?> trac : additionalTrackers) {
87             lambda.execute(trac);
88         }
89     }
90
91     private static interface BundleStrategy {
92         void execute(BundleTrackerCustomizer<?> tracker);
93     }
94
95 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.