bdced2d08ab4b27a77c91128d6a94d8fe08304a3
[dmaap/kafka11aaf.git] / src / main / java / org / onap / dmaap / commonauth / kafka / base / authorization / AuthorizationProviderFactory.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  *  Modification copyright (C) 2021 Nordix Foundation.
7  *  ================================================================================
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *        http://www.apache.org/licenses/LICENSE-2.0
12 *  
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *  ============LICENSE_END=========================================================
19  *  
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.commonauth.kafka.base.authorization;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.ServiceLoader;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class AuthorizationProviderFactory {
31         private static final Logger logger = LoggerFactory.getLogger(AuthorizationProviderFactory.class);
32         private static final Map<String, AuthorizationProvider> AUTHORIZATION_PROVIDER_MAP = new HashMap<>();
33         private static final AuthorizationProviderFactory AUTHORIZATION_PROVIDER_FACTORY = new AuthorizationProviderFactory();
34
35         private AuthorizationProviderFactory() {
36                 try {
37                         ServiceLoader<AuthorizationProvider> serviceLoader = ServiceLoader.load(AuthorizationProvider.class);
38                         for (AuthorizationProvider authzProvider : serviceLoader) {
39                                 AUTHORIZATION_PROVIDER_MAP.put(authzProvider.getId(), authzProvider);
40
41                         }
42                 } catch (Exception ee) {
43                         logger.error(ee.getMessage(), ee);
44                         System.exit(0);
45                 }
46         }
47
48         public static AuthorizationProviderFactory getProviderFactory() {
49                 return AUTHORIZATION_PROVIDER_FACTORY;
50         }
51
52         public AuthorizationProvider getProvider() {
53                 return AUTHORIZATION_PROVIDER_MAP.get(System.getProperty("kafka.authorization.provider", "CADI_AAF_PROVIDER"));
54         }
55 }