b16f3944e9fa8ae9a42262f3dbae944227cc6bbc
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
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.openecomp.sdc.logging.api;
18
19 import org.openecomp.sdc.logging.provider.LoggerCreationService;
20 import org.openecomp.sdc.logging.provider.LoggingContextService;
21 import org.openecomp.sdc.logging.provider.LoggingServiceProvider;
22
23 import java.util.Iterator;
24 import java.util.Optional;
25 import java.util.ServiceLoader;
26
27 /**
28  * <p>Binds to a concrete implementation of logging services.</p>
29  *
30  * <p>In order to use the factory, a particular (e.g. framework-specific) implementation of a service must be
31  * configured as described in
32  * <a href="http://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html">java.util.ServiceLoader</a>).</p>
33  *
34  * @author evitaliy
35  * @since 13/09/2016.
36  *
37  * @see ServiceLoader
38  */
39
40 // No advanced logging can be used here because we don't know
41 // which underlying implementation will be used
42 @SuppressWarnings({"UseOfSystemOutOrSystemErr", "squid:S106"})
43 class ServiceBinder {
44
45     private static final LoggingServiceProvider PROVIDER = lookupProvider();
46
47     private ServiceBinder () {
48         // prevent instantiation
49     }
50
51     private static LoggingServiceProvider lookupProvider() {
52
53         ServiceLoader<LoggingServiceProvider> loader = ServiceLoader.load(LoggingServiceProvider.class);
54         Iterator<LoggingServiceProvider> iterator = loader.iterator();
55
56         if (!iterator.hasNext()) {
57             System.err.printf("[ERROR] No provider configured for logging services %s. " +
58                             "Default implementation will be used.\n",
59                     LoggingServiceProvider.class.getName());
60             return null;
61         }
62
63         try {
64
65             LoggingServiceProvider provider = iterator.next();
66             if (!iterator.hasNext()) {
67                 return provider;
68             }
69
70             Logger logger = provider.getLogger(ServiceBinder.class);
71             if (logger.isWarnEnabled()) {
72                 logger.warn("More than one provider for logging services {} found",
73                         LoggingServiceProvider.class.getName());
74             }
75
76             return provider;
77
78         } catch (Exception e) {
79             // don't fail if the provider cannot be instantiated
80             e.printStackTrace(System.err);
81             return null;
82         }
83     }
84
85     static Optional<LoggingContextService> getContextServiceBinding() {
86         return Optional.ofNullable(PROVIDER);
87     }
88
89     static Optional<LoggerCreationService> getCreationServiceBinding() {
90         return Optional.ofNullable(PROVIDER);
91     }
92 }
93