2aa74758d47c6fd2c7cc6b1fa095695ea8e91ded
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / dependencyresolver / DestroyedModule.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.dependencyresolver;
9
10 import org.opendaylight.controller.config.api.ModuleIdentifier;
11 import org.opendaylight.controller.config.manager.impl.jmx.ModuleJMXRegistrator;
12 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager.OsgiRegistration;
13 import org.opendaylight.yangtools.concepts.Identifiable;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * Transfer object representing already committed module that needs to be
19  * destroyed. Implements comparable in order to preserve order in which modules
20  * were created. Module instances should be closed in order defined by the
21  * compareTo method.
22  */
23 public class DestroyedModule implements AutoCloseable,
24         Comparable<DestroyedModule>, Identifiable<ModuleIdentifier> {
25     private static final Logger logger = LoggerFactory
26             .getLogger(DestroyedModule.class);
27
28     private final ModuleIdentifier identifier;
29     private final AutoCloseable instance;
30     private final ModuleJMXRegistrator oldJMXRegistrator;
31     private final OsgiRegistration osgiRegistration;
32     private final int orderingIdx;
33
34     public DestroyedModule(ModuleIdentifier identifier, AutoCloseable instance,
35             ModuleJMXRegistrator oldJMXRegistrator,
36             OsgiRegistration osgiRegistration, int orderingIdx) {
37         this.identifier = identifier;
38         this.instance = instance;
39         this.oldJMXRegistrator = oldJMXRegistrator;
40         this.osgiRegistration = osgiRegistration;
41         this.orderingIdx = orderingIdx;
42     }
43
44     @Override
45     public void close() {
46         logger.trace("Destroying {}", identifier);
47         try {
48             instance.close();
49         } catch (Exception e) {
50             logger.error("Error while closing instance of {}", identifier, e);
51         }
52         try {
53             oldJMXRegistrator.close();
54         } catch (Exception e) {
55             logger.error("Error while closing jmx registrator of {}", identifier, e);
56         }
57         try {
58             osgiRegistration.close();
59         } catch (Exception e) {
60             logger.error("Error while closing osgi registration of {}", identifier, e);
61         }
62     }
63
64     @Override
65     public int compareTo(DestroyedModule o) {
66         return Integer.compare(orderingIdx, o.orderingIdx);
67     }
68
69     @Override
70     public ModuleIdentifier getIdentifier() {
71         return identifier;
72     }
73 }