This repository has been archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
ServiceContextManager.java
154 lines (139 loc) · 5.88 KB
/
ServiceContextManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tamaya.spi;
import java.util.Map;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.tamaya.ConfigException;
/**
* This singleton provides access to the services available in the current {@link ServiceContext}. The
* behaviour can be adapted, by calling {@link ServiceContextManager#set(ServiceContext)} before accessing any
* services.
*/
public final class ServiceContextManager {
/** The logger used. */
private static final Logger LOG = Logger.getLogger(ServiceContextManager.class.getName());
/**
* The ServiceProvider used.
*/
private static final Map<ClassLoader, ServiceContext> SERVICE_CONTEXTS = new ConcurrentHashMap<>();
/**
* Private singletons constructor.
*/
private ServiceContextManager() {
}
/**
* Load the {@link ServiceContext} to be used.
*
* @return {@link ServiceContext} to be used for loading the services.
*/
private static ServiceContext loadDefaultServiceProvider(ClassLoader classLoader) {
ServiceContext highestServiceContext = null;
try {
int highestOrdinal = 0;
for (ServiceContext serviceContext : ServiceLoader.load(ServiceContext.class, classLoader)) {
if (highestServiceContext == null
|| serviceContext.ordinal() > highestOrdinal) {
highestServiceContext = serviceContext;
highestOrdinal = serviceContext.ordinal();
}
}
} catch (Exception e) {
throw new ConfigException("ServiceContext not loadable", e);
}
if (highestServiceContext==null){
throw new ConfigException("No ServiceContext found");
}
highestServiceContext.init(classLoader);
LOG.info("Using Service Context of type: " + highestServiceContext.getClass().getName());
return highestServiceContext;
}
/**
* Replace the current {@link ServiceContext} in use.
*
* @param serviceContext the new {@link ServiceContext}, not {@code null}.
* @return the currently used context after setting it.
*/
public static ServiceContext set(ServiceContext serviceContext) {
Objects.requireNonNull(serviceContext);
return setWithClassLoader(serviceContext, serviceContext.getClassLoader());
}
/**
* Replace the current {@link ServiceContext} for the ServiceContextManager's classloader in use.
*
* @param serviceContext the new {@link ServiceContext}, not {@code null}.
* @return the currently used context after setting it.
*/
public static ServiceContext setToStaticClassLoader(ServiceContext serviceContext) {
Objects.requireNonNull(serviceContext);
return setWithClassLoader(serviceContext, ServiceContextManager.class.getClassLoader());
}
private static ServiceContext setWithClassLoader(ServiceContext serviceContext, ClassLoader cl) {
Objects.requireNonNull(serviceContext);
Objects.requireNonNull(cl);
ServiceContext previousContext;
synchronized (ServiceContextManager.class) {
previousContext = ServiceContextManager.SERVICE_CONTEXTS
.put(cl, serviceContext);
}
if(previousContext!=null) {
LOG.log(Level.WARNING, "Replaced ServiceProvider " +
previousContext.getClass().getName() +
" with: " + serviceContext.getClass().getName() + " for classloader: " +
serviceContext.getClassLoader());
}else{
LOG.log(Level.INFO, "Using ServiceProvider: " + serviceContext.getClass().getName() +
" for classloader: " + serviceContext.getClassLoader());
}
return serviceContext;
}
/**
* Ge {@link ServiceContext}. If necessary the {@link ServiceContext} will be laziliy loaded.
*
* @param classLoader the classloader to be used, not null.
* @return the {@link ServiceContext} used.
*/
public static ServiceContext getServiceContext(ClassLoader classLoader) {
Objects.requireNonNull(classLoader, "Classloader required.");
return SERVICE_CONTEXTS.computeIfAbsent(classLoader, ServiceContextManager::loadDefaultServiceProvider);
}
/**
* Ge {@link ServiceContext}. If necessary the {@link ServiceContext} will be laziliy loaded.
*
* @return the {@link ServiceContext} used.
*/
public static ServiceContext getServiceContext() {
return getServiceContext(getDefaultClassLoader());
}
/**
* Evaluate the default classloader: This return the current thread context classloader, or this
* class's classloader as fallback.
* @return the classloder, not null.
*/
public static ClassLoader getDefaultClassLoader() {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if(cl==null) {
cl = ServiceContextManager.class.getClassLoader();
}
return cl;
}
}