|
18 | 18 |
|
19 | 19 | import io.mybatis.config.Config;
|
20 | 20 | import io.mybatis.config.ConfigHelper;
|
| 21 | +import org.springframework.core.io.Resource; |
| 22 | +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
| 23 | +import org.springframework.core.io.support.ResourcePatternResolver; |
21 | 24 |
|
22 | 25 | import java.io.*;
|
23 | 26 | import java.net.JarURLConnection;
|
| 27 | +import java.net.URI; |
24 | 28 | import java.net.URL;
|
| 29 | +import java.nio.file.Path; |
| 30 | +import java.nio.file.Paths; |
25 | 31 | import java.util.*;
|
26 | 32 | import java.util.function.Function;
|
27 | 33 | import java.util.jar.JarEntry;
|
@@ -230,6 +236,42 @@ private Properties chooseFromFile(File file, String version) throws IOException
|
230 | 236 | });
|
231 | 237 | }
|
232 | 238 |
|
| 239 | + /** |
| 240 | + * 选择版本 |
| 241 | + */ |
| 242 | + private Properties chooseFromResource(String version) throws IOException { |
| 243 | + |
| 244 | + Map<String, URI> fileMap = new HashMap<>(); |
| 245 | + |
| 246 | + ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); |
| 247 | + Resource[] resources = resolver.getResources("classpath*:io/mybatis/provider/config/*.properties"); |
| 248 | + for (Resource resource : resources) { |
| 249 | + URI uri = resource.getURI(); |
| 250 | + |
| 251 | + String urlString = uri.toString(); |
| 252 | + if (urlString.endsWith(FILE_TYPE)) { |
| 253 | + Path path = Paths.get(uri); |
| 254 | + // 获取文件名 |
| 255 | + Path fileName = path.getFileName(); |
| 256 | + fileMap.put(fileName.toString(), uri); |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + List<ConfigVersion> versions = sortVersions(new ArrayList<>(fileMap.keySet())); |
| 261 | + ConfigVersion chooseVersion = chooseVersion(versions, version); |
| 262 | + |
| 263 | + return build(versions, chooseVersion, configVersion -> { |
| 264 | + URI url = fileMap.get(chooseVersion.getFileName()); |
| 265 | + InputStream inputStream = null; |
| 266 | + try { |
| 267 | + inputStream = url.toURL().openStream(); |
| 268 | + } catch (IOException e) { |
| 269 | + return null; |
| 270 | + } |
| 271 | + return inputStream; |
| 272 | + }); |
| 273 | + } |
| 274 | + |
233 | 275 | /**
|
234 | 276 | * 获取版本配置
|
235 | 277 | */
|
@@ -263,6 +305,12 @@ protected Properties buildVersionProperties() {
|
263 | 305 | } catch (IOException e) {
|
264 | 306 | throw new RuntimeException(e);
|
265 | 307 | }
|
| 308 | + } else if (resource.getProtocol().equals("resource")) { |
| 309 | + try { |
| 310 | + return chooseFromResource(version); |
| 311 | + } catch (Exception e) { |
| 312 | + throw new RuntimeException(e); |
| 313 | + } |
266 | 314 | }
|
267 | 315 | return null;
|
268 | 316 | }
|
|
0 commit comments