-
Notifications
You must be signed in to change notification settings - Fork 706
/
ModuleManagerImpl.java
179 lines (151 loc) · 6.1 KB
/
ModuleManagerImpl.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
*
* * Copyright 1999-2018 Alibaba Group Holding Ltd.
* *
* * Licensed 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 com.alipay.jarslink.api.impl;
import com.alipay.jarslink.api.Module;
import com.alipay.jarslink.api.ModuleManager;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.instanceOf;
import static com.google.common.collect.Iterables.filter;
/**
* 模块管理,包含获取模块,执行模块里的方法
*
* @author tengfei.fangtf
* @version $Id: ModuleManagerImpl.java, v 0.1 Mar 20, 2017 4:04:32 PM tengfei.fangtf Exp $
*/
public class ModuleManagerImpl implements ModuleManager, DisposableBean {
private static final Logger LOGGER = LoggerFactory
.getLogger(ModuleManagerImpl.class);
/**
* 已注册的所有模块,key:moduleName upperCase
*/
private final ConcurrentHashMap<String, RuntimeModule> allModules = new ConcurrentHashMap();
private RuntimeModule getRuntimeModule(String name) {
RuntimeModule runtimeModule = allModules.get(name.toUpperCase());
return runtimeModule != null ? runtimeModule : new RuntimeModule();
}
@Override
public List<Module> getModules() {
List<Module> modules = Lists.newArrayList();
for (String name : allModules.keySet()) {
RuntimeModule runtimeModule = getRuntimeModule((String) name);
for (String version : runtimeModule.getModules().keySet()) {
modules.add(runtimeModule.getModules().get(version));
}
}
return ImmutableList
.copyOf(filter(modules, instanceOf(SpringModule.class)));
}
@Override
public Module find(String name) {
checkNotNull(name, "module name is null");
String defaultVersion = getDefaultVersion(name);
checkNotNull(defaultVersion, "module default version is null");
return find(name, defaultVersion);
}
private String getDefaultVersion(String name) {return getRuntimeModule((String) name).getDefaultVersion();}
@Override
public Module find(String name, String version) {
checkNotNull(name, "module name is null");
checkNotNull(version, "module version is null");
return getRuntimeModule((String) name).getModule(version);
}
@Override
public void activeVersion(String name, String version) {
checkNotNull(name, "module name is null");
checkNotNull(version, "module version is null");
getRuntimeModule((String) name).setDefaultVersion(version);
}
@Override
public String getActiveVersion(String name) {
checkNotNull(name, "module name is null");
return getDefaultVersion(name);
}
@Override
public Module register(Module module) {
checkNotNull(module, "module is null");
String name = module.getName();
String version = module.getVersion();
if (LOGGER.isInfoEnabled()) {
LOGGER.info("register Module: {}-{}", name, version);
}
//same module and same version can not register
Module registeredModule = getRuntimeModule(name).getModule(version);
if (registeredModule != null) {
return null;
}
RuntimeModule runtimeModule = getRuntimeModule(name);
Module oldModule = null;
//module frist register
if (runtimeModule.getModules().isEmpty()) {
runtimeModule = new RuntimeModule().withName(name).withDefaultVersion(version).addModule(module);
allModules.put(name.toUpperCase(), runtimeModule);
} else {
//the same module to register again
oldModule = runtimeModule.getDefaultModule();
runtimeModule.addModule(module).setDefaultVersion(version);
// remove module old version
if (oldModule != null && module.getModuleConfig().isNeedUnloadOldVersion() && !runtimeModule.getModules().isEmpty()) {
runtimeModule.getModules().remove(oldModule.getVersion());
}
}
return oldModule;
}
@Override
public Module remove(String name) {
checkNotNull(name, "module name is null");
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Remove Module: {}", name);
}
return remove(name, getRuntimeModule((String) name).getDefaultVersion());
}
@Override
public Module remove(String name, String version) {
checkNotNull(name, "module name is null");
checkNotNull(version, "module version is null");
return getRuntimeModule((String) name).getModules().remove(version);
}
@Override
public void destroy() throws Exception {
for (Module each : getModules()) {
try {
each.destroy();
} catch (Exception e) {
LOGGER.error("Failed to destroy module: " + each.getName(), e);
}
}
allModules.clear();
}
@Override
public Map<String, String> getErrorModuleContext() {
Map<String, String> result = Maps.newHashMap();
for (String name : allModules.keySet()) {
RuntimeModule runtimeModule = getRuntimeModule((String) name);
result.put(name, runtimeModule.getErrorContext());
}
return result;
}
}