Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / client / RestPropertiesLoader.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
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 package org.onap.so.client;
22
23 import java.util.Iterator;
24 import java.util.ServiceLoader;
25
26 public class RestPropertiesLoader {
27
28     /* required to make ServiceLoader thread safe */
29     private static final ThreadLocal<ServiceLoader<RestProperties>> services =
30             new ThreadLocal<ServiceLoader<RestProperties>>() {
31                 @Override
32                 protected ServiceLoader<RestProperties> initialValue() {
33                     return ServiceLoader.load(RestProperties.class);
34                 }
35             };
36
37     private RestPropertiesLoader() {}
38
39     private static class Helper {
40         private static final RestPropertiesLoader INSTANCE = new RestPropertiesLoader();
41     }
42
43     public static RestPropertiesLoader getInstance() {
44         return Helper.INSTANCE;
45     }
46
47     public <T> T getNewImpl(Class<? extends RestProperties> clazz) {
48         return this.getImpl(clazz, true);
49     }
50
51     public <T> T getImpl(Class<? extends RestProperties> clazz) {
52         return this.getImpl(clazz, false);
53     }
54
55     private <T> T getImpl(Class<? extends RestProperties> clazz, boolean forceNewInstance) {
56         T result = null;
57         ServiceLoader<RestProperties> loader = this.services.get();
58         Iterator<RestProperties> propertyImpls = loader.iterator();
59         RestProperties item;
60         while (propertyImpls.hasNext()) {
61             item = propertyImpls.next();
62             if (clazz.isAssignableFrom(item.getClass())) {
63                 try {
64                     if (forceNewInstance) {
65                         result = (T) item.getClass().newInstance();
66                     } else {
67                         result = (T) item;
68                     }
69                 } catch (InstantiationException | IllegalAccessException e) {
70                     /*
71                      * all spi implementations must provide a public no argument constructor
72                      */
73
74                 }
75                 // break;
76             }
77         }
78
79         return result;
80     }
81 }