2 * Copyright 2017 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openo.baseservice.i18n;
18 import org.reflections.Reflections;
19 import org.reflections.scanners.ResourcesScanner;
20 import org.reflections.util.ClasspathHelper;
21 import org.reflections.util.ConfigurationBuilder;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Iterator;
29 import java.util.regex.Pattern;
31 final class JsonResourceScanner {
32 private static final org.slf4j.Logger LOG =
33 org.slf4j.LoggerFactory.getLogger(JsonResourceScanner.class);
34 private static final Pattern PATTERN_OF_I18N_FILE_NAME =
35 Pattern.compile(".*?\\-i18n\\-.*?\\.json");
36 private static final Pattern PATTERN_OF_ERROR_CODE_FILE_NAME =
37 Pattern.compile(".*?\\-errorcode\\-.*?\\.json");
39 private static Collection<String> findFromClassPath(Pattern pattern) {
40 ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
41 Collection<URL> urls = ClasspathHelper.forJavaClassPath();
43 for (Iterator<URL> iter = urls.iterator(); iter.hasNext();) {
44 URL url = iter.next();
45 boolean exist = false;
47 exist = new File(url.getFile()).exists();
49 LOG.info("class path url ignored for not exists: " + url.toString());
51 } catch (Exception e) {
52 LOG.info("class path url ignored for exception: " + url.toString(), e);
59 for (URL url : urls) {
60 LOG.info("class path url:" + url.toString());
62 configurationBuilder.addUrls(urls);
63 configurationBuilder.setScanners(new ResourcesScanner());
64 configurationBuilder.useParallelExecutor();
65 Reflections reflections = new Reflections(configurationBuilder);
66 Set<String> results = reflections.getResources(pattern);
67 if (results == null) {
68 return Collections.emptySet();
74 static Collection<String> findI18nPaths() {
75 return findFromClassPath(PATTERN_OF_I18N_FILE_NAME);
78 static Collection<String> findErrorCodePaths() {
79 return findFromClassPath(PATTERN_OF_ERROR_CODE_FILE_NAME);