Update gvnfm-driver .gitreview file
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / main / java / org / openo / nfvo / jujuvnfmadapter / common / SpringContextHolder.java
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openo.nfvo.jujuvnfmadapter.common;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.springframework.context.ApplicationContext;
22 import org.springframework.context.ApplicationContextAware;
23
24 /**
25  * 
26  * Spring context holder class.<br>
27  * <p>
28  * </p>
29  * 
30  * @author
31  * @version     NFVO 0.5  Sep 12, 2016
32  */
33 public class SpringContextHolder implements ApplicationContextAware {
34
35     private static final Logger LOG = LoggerFactory.getLogger(SpringContextHolder.class);
36
37     private static ApplicationContext appContext;
38
39     /**
40      * application context<br/>
41      * 
42      * @param applicationContext
43      */
44     @Override
45     public void setApplicationContext(ApplicationContext applicationContext) {
46         setAppContext(applicationContext);
47     }
48
49     /**
50      * <br>
51      * 
52      * @return applicationContext
53      */
54     public static ApplicationContext getApplicationContext() {
55         checkApplicationContext();
56         return appContext;
57     }
58
59     /**
60      * 
61      * Get spring bean.<br>
62      * 
63      * @param name
64      * @return
65      * @since  NFVO 0.5
66      */
67     @SuppressWarnings("unchecked")
68     public static <T> T getSpringBean(String name) {
69         checkApplicationContext();
70         return (T)appContext.getBean(name);
71     }
72
73     /**
74      * 
75      * Get spring bean.<br>
76      * 
77      * @param requiredType
78      * @return
79      * @since  NFVO 0.5
80      */
81     @SuppressWarnings("unchecked")
82     public static <T> T getSpringBean(Class<T> requiredType) {
83         checkApplicationContext();
84         return (T)appContext.getBeansOfType(requiredType);
85     }
86
87     private static void checkApplicationContext() {
88         if(appContext == null) {
89             LOG.error("spring appContext do not insert.");
90             throw new IllegalStateException("spring appContext is null.");
91         }
92     }
93
94     /**
95      * 
96      * Clean application context.<br>
97      * 
98      * @since  NFVO 0.5
99      */
100     public static void cleanApplicationContext() {
101         appContext = null;
102     }
103
104     private static void setAppContext(ApplicationContext applicationContext) {
105         SpringContextHolder.appContext = applicationContext;
106     }
107 }