Add international basic service module
[vfc/nfvo/wfengine.git] / baseservice-i18n / src / main / java / org / openo / baseservice / i18n / JsonResourceScanner.java
1 /**
2  * Copyright 2017 ZTE Corporation.
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 package org.openo.baseservice.i18n;
17
18 import org.reflections.Reflections;
19 import org.reflections.scanners.ResourcesScanner;
20 import org.reflections.util.ClasspathHelper;
21 import org.reflections.util.ConfigurationBuilder;
22
23 import java.io.File;
24 import java.net.URL;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.Set;
29 import java.util.regex.Pattern;
30
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");
38
39   private static Collection<String> findFromClassPath(Pattern pattern) {
40     ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
41     Collection<URL> urls = ClasspathHelper.forJavaClassPath();
42
43     for (Iterator<URL> iter = urls.iterator(); iter.hasNext();) {
44       URL url = iter.next();
45       boolean exist = false;
46       try {
47         exist = new File(url.getFile()).exists();
48         if (!exist) {
49           LOG.info("class path url ignored for not exists: " + url.toString());
50         }
51       } catch (Exception e) {
52         LOG.info("class path url ignored for exception: " + url.toString(), e);
53         exist = false;
54       }
55       if (!exist) {
56         iter.remove();
57       }
58     }
59     for (URL url : urls) {
60       LOG.info("class path url:" + url.toString());
61     }
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();
69     } else {
70       return results;
71     }
72   }
73
74   static Collection<String> findI18nPaths() {
75     return findFromClassPath(PATTERN_OF_I18N_FILE_NAME);
76   }
77
78   static Collection<String> findErrorCodePaths() {
79     return findFromClassPath(PATTERN_OF_ERROR_CODE_FILE_NAME);
80   }
81 }