1d65c3bd925bb43549baa4bce68a9fc84d0c4407
[controller.git] / opendaylight / md-sal / sal-dom-config / src / main / java / org / opendaylight / controller / config / yang / config / legacy_entity_ownership_service_provider / LegacyEntityOwnershipServiceProviderModule.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.controller.config.yang.config.legacy_entity_ownership_service_provider;
9
10 import com.google.common.reflect.AbstractInvocationHandler;
11 import com.google.common.reflect.Reflection;
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.Method;
14 import org.opendaylight.controller.config.api.DependencyResolver;
15 import org.opendaylight.controller.config.api.ModuleIdentifier;
16 import org.opendaylight.controller.config.api.osgi.WaitingServiceTracker;
17 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
18 import org.osgi.framework.BundleContext;
19
20 /**
21  * Deprecated.
22  *
23  * @deprecated Replaced by blueprint wiring
24  */
25 @Deprecated
26 public class LegacyEntityOwnershipServiceProviderModule extends AbstractLegacyEntityOwnershipServiceProviderModule {
27     private BundleContext bundleContext;
28
29     public LegacyEntityOwnershipServiceProviderModule(ModuleIdentifier identifier,
30             DependencyResolver dependencyResolver) {
31         super(identifier, dependencyResolver);
32     }
33
34     public LegacyEntityOwnershipServiceProviderModule(ModuleIdentifier identifier,
35             DependencyResolver dependencyResolver,
36             LegacyEntityOwnershipServiceProviderModule oldModule, java.lang.AutoCloseable oldInstance) {
37         super(identifier, dependencyResolver, oldModule, oldInstance);
38     }
39
40     @Override
41     public void customValidation() {
42         // add custom validation form module attributes here.
43     }
44
45     @Override
46     public AutoCloseable createInstance() {
47         final WaitingServiceTracker<EntityOwnershipService> tracker = WaitingServiceTracker.create(
48                 EntityOwnershipService.class, bundleContext);
49         final EntityOwnershipService service = tracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);
50
51         return Reflection.newProxy(AutoCloseableEntityOwnershipService.class, new AbstractInvocationHandler() {
52             @Override
53             @SuppressWarnings("checkstyle:AvoidHidingCauseException")
54             protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
55                 if (method.getName().equals("close")) {
56                     tracker.close();
57                     return null;
58                 } else {
59                     try {
60                         return method.invoke(service, args);
61                     } catch (InvocationTargetException e) {
62                         // https://bugs.opendaylight.org/show_bug.cgi?id=6564
63                         // http://stackoverflow.com/a/10719613/421602
64                         // https://amitstechblog.wordpress.com/2011/07/24/java-proxies-and-undeclaredthrowableexception/
65                         throw e.getCause();
66                     }
67                 }
68             }
69         });
70     }
71
72     public void setBundleContext(final BundleContext bundleContext) {
73         this.bundleContext = bundleContext;
74     }
75
76     private interface AutoCloseableEntityOwnershipService extends EntityOwnershipService, AutoCloseable {
77     }
78
79 }

©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.