daeb0a59a4c5e2c7854ebe752cd19a3b8803d954
[controller.git] / opendaylight / md-sal / sal-dom-config / src / main / java / org / opendaylight / yang / gen / v1 / urn / opendaylight / params / xml / ns / yang / md / sal / config / impl / cluster / singleton / service / rev160718 / ClusterSingletonServiceProviderModule.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.md.sal.config.impl.cluster.singleton.service.rev160718;
9
10 import com.google.common.reflect.AbstractInvocationHandler;
11 import com.google.common.reflect.Reflection;
12 import java.lang.reflect.Method;
13 import org.opendaylight.controller.config.api.DependencyResolver;
14 import org.opendaylight.controller.config.api.ModuleIdentifier;
15 import org.opendaylight.controller.config.api.osgi.WaitingServiceTracker;
16 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
17 import org.osgi.framework.BundleContext;
18
19 /**
20  * @deprecated Replaced by blueprint wiring but remains for backwards compatibility until downstream users
21  *             of the provided config system service are converted to blueprint.
22  */
23 @Deprecated
24 public class ClusterSingletonServiceProviderModule extends AbstractClusterSingletonServiceProviderModule {
25     private BundleContext bundleContext;
26
27     public ClusterSingletonServiceProviderModule(final ModuleIdentifier identifier, final DependencyResolver dependencyResolver) {
28         super(identifier, dependencyResolver);
29     }
30
31     public ClusterSingletonServiceProviderModule(final ModuleIdentifier identifier, final DependencyResolver dependencyResolver,
32             final ClusterSingletonServiceProviderModule oldModule, final java.lang.AutoCloseable oldInstance) {
33         super(identifier, dependencyResolver, oldModule, oldInstance);
34     }
35
36
37     @Override
38     public AutoCloseable createInstance() {
39         final WaitingServiceTracker<ClusterSingletonServiceProvider> tracker = WaitingServiceTracker
40                 .create(ClusterSingletonServiceProvider.class, bundleContext);
41         final ClusterSingletonServiceProvider service = tracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);
42
43         // Create a proxy to override close to close the ServiceTracker. The actual DOMClusterSingletonServiceProvider
44         // instance will be closed via blueprint.
45         return Reflection.newProxy(AutoCloseableDOMClusterSingletonServiceProvider.class,
46                 new AbstractInvocationHandler() {
47                     @Override
48                     protected Object handleInvocation(final Object proxy, final Method method, final Object[] args)
49                             throws Throwable {
50                         if (method.getName().equals("close")) {
51                             tracker.close();
52                             return null;
53                         } else {
54                             return method.invoke(service, args);
55                         }
56                     }
57                 });
58     }
59
60     void setBundleContext(final BundleContext bundleContext) {
61         this.bundleContext = bundleContext;
62     }
63
64     private static interface AutoCloseableDOMClusterSingletonServiceProvider
65             extends ClusterSingletonServiceProvider, AutoCloseable {
66     }
67 }