72094daa296c24fa449f738b4e2c194747728597
[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 / DOMClusterSingletonServiceProviderModule.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.dom.api.DOMClusterSingletonServiceProvider;
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 DOMClusterSingletonServiceProviderModule extends AbstractDOMClusterSingletonServiceProviderModule {
25     private BundleContext bundleContext;
26
27     public DOMClusterSingletonServiceProviderModule(ModuleIdentifier identifier, DependencyResolver dependencyResolver) {
28         super(identifier, dependencyResolver);
29     }
30
31     public DOMClusterSingletonServiceProviderModule(ModuleIdentifier identifier, DependencyResolver dependencyResolver,
32             DOMClusterSingletonServiceProviderModule oldModule, java.lang.AutoCloseable oldInstance) {
33         super(identifier, dependencyResolver, oldModule, oldInstance);
34     }
35
36
37     @Override
38     public AutoCloseable createInstance() {
39         final WaitingServiceTracker<DOMClusterSingletonServiceProvider> tracker =
40                 WaitingServiceTracker.create(DOMClusterSingletonServiceProvider.class, bundleContext);
41         final DOMClusterSingletonServiceProvider 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, new AbstractInvocationHandler() {
46             @Override
47             protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
48                 if (method.getName().equals("close")) {
49                     tracker.close();
50                     return null;
51                 } else {
52                     return method.invoke(service, args);
53                 }
54             }
55         });
56
57     }
58
59     void setBundleContext(BundleContext bundleContext) {
60         this.bundleContext = bundleContext;
61     }
62
63     private static interface AutoCloseableDOMClusterSingletonServiceProvider extends DOMClusterSingletonServiceProvider, AutoCloseable {
64     }
65 }