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